IOCPServer.h
上传用户:hkcoast
上传日期:2007-01-12
资源大小:979k
文件大小:5k
源码类别:

手机短信编程

开发平台:

Visual C++

  1. // IOCPServer.h: interface for the CIOCPServer class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #if !defined(AFX_IOCPSERVER_H__75B80E90_FD25_4FFB_B273_0090AA43BBDF__INCLUDED_)
  5. #define AFX_IOCPSERVER_H__75B80E90_FD25_4FFB_B273_0090AA43BBDF__INCLUDED_
  6. #if _MSC_VER > 1000
  7. #pragma once
  8. #endif // _MSC_VER > 1000
  9. #include <winsock2.h>
  10. #pragma comment(lib,"ws2_32.lib")
  11. #include "Buffer.h"
  12. #include "CpuUsage.h"
  13. #include <process.h>
  14. #include <afxtempl.h>
  15. class CSmppLibTest;
  16. ////////////////////////////////////////////////////////////////////
  17. #define NC_CLIENT_CONNECT 0x0001
  18. #define NC_CLIENT_DISCONNECT 0x0002
  19. #define NC_TRANSMIT 0x0003
  20. #define NC_RECEIVE 0x0004
  21. class CLock
  22. {
  23. public:
  24. CLock(CRITICAL_SECTION& cs, const CString& strFunc)
  25. {
  26. m_strFunc = strFunc;
  27. m_pcs = &cs;
  28. Lock();
  29. }
  30. ~CLock()
  31. {
  32. Unlock();
  33. }
  34. void Unlock()
  35. {
  36. LeaveCriticalSection(m_pcs);
  37. TRACE(_T("LC %d %sn") , GetCurrentThreadId() , m_strFunc);
  38. }
  39. void Lock()
  40. {
  41. TRACE(_T("EC %d %sn") , GetCurrentThreadId(), m_strFunc);
  42. EnterCriticalSection(m_pcs);
  43. }
  44. protected:
  45. CRITICAL_SECTION* m_pcs;
  46. CString m_strFunc;
  47. };
  48. enum IOType 
  49. {
  50. IOInitialize,
  51. IORead,
  52. IOWrite,
  53. IOIdle
  54. };
  55. class OVERLAPPEDPLUS 
  56. {
  57. public:
  58. OVERLAPPED m_ol;
  59. IOType m_ioType;
  60. OVERLAPPEDPLUS(IOType ioType) {
  61. ZeroMemory(this, sizeof(OVERLAPPEDPLUS));
  62. m_ioType = ioType;
  63. }
  64. };
  65. struct ClientContext
  66. {
  67.     SOCKET m_Socket;
  68. // Store buffers
  69. CBuffer m_ReadBuffer;
  70. CBuffer m_WriteBuffer;
  71. // Input Elements for Winsock
  72. WSABUF m_wsaInBuffer;
  73. BYTE m_byInBuffer[8192];    
  74. // Output elements for Winsock
  75. WSABUF m_wsaOutBuffer;
  76. // HANDLE m_hWriteComplete;
  77. // Message counts... purely for example purposes
  78. LONG m_nMsgIn;
  79. LONG m_nMsgOut;
  80. // ClientContext* m_pWriteContext;
  81. // ClientContext* m_pReadContext;
  82. };
  83. template<>
  84. inline UINT AFXAPI HashKey(CString & strGuid)
  85. {
  86.   return HashKey( (LPCTSTR) strGuid);         
  87. }
  88. #include "Mapper.h"
  89. typedef void (CALLBACK* NOTIFYPROC)(LPVOID, ClientContext*, UINT nCode);
  90. typedef CMap<CString, CString&, ClientContext*, ClientContext* > ContextList;
  91. typedef CList<ClientContext*, ClientContext* > FreeContextList;
  92. class CMainFrame;
  93. class CIOCPServer
  94. {
  95. public:
  96. void DisconnectAll();
  97. CIOCPServer();
  98. virtual ~CIOCPServer();
  99. CSmppLibTest *m_pSmppLibTest;
  100. // void SetEchoMode();
  101. // bool GetEchoMode();
  102. // NOTIFYPROC m_pNotifyProc;
  103. // CMainFrame* m_pFrame;
  104. bool Initialize(int nConnections, int nPort);
  105. static unsigned __stdcall ListenThreadProc(LPVOID lpVoid);
  106. static unsigned __stdcall  ThreadPoolFunc (LPVOID WorkContext);
  107. static CRITICAL_SECTION m_cs;
  108. void Send(const CString& strClient, CString strData);
  109. void Send(ClientContext* pContext, PBYTE pbyData, const long nSize);
  110. void Shutdown();
  111. void ResetConnection(ClientContext* pContext);
  112. LONG m_nCurrentThreads;
  113. LONG m_nBusyThreads;
  114. protected:
  115. void InitializeClientRead(ClientContext* pContext);
  116. BOOL AssociateSocketWithCompletionPort(SOCKET device, HANDLE hCompletionPort, DWORD dwCompletionKey);
  117. void RemoveStaleClient(ClientContext* pContext, BOOL bGraceful);
  118. ClientContext* FindClient(const CString& strLogonID);
  119. void MoveToFreePool(CString& strKey);
  120. ClientContext*  AllocateContext();
  121. LONG m_nWorkerCnt;
  122. bool m_bInit;
  123. bool m_bDisconnectAll;
  124. void CloseCompletionPort();
  125. void OnAccept();
  126. bool InitializeIOCP(void);
  127. void Stop();
  128. ContextList m_listContexts;
  129. FreeContextList m_listFreePool;
  130. WSAEVENT m_hEvent;
  131. SOCKET m_socListen;    
  132.     HANDLE m_hKillEvent;
  133. HANDLE m_hThread;
  134. HANDLE m_hCompletionPort;
  135. bool m_bTimeToKill;
  136. // CCpuUsage m_cpu;
  137. // bool m_bEcho;
  138. // Thread Pool Tunables
  139. LONG m_nThreadPoolMin;
  140. LONG m_nThreadPoolMax;
  141. LONG m_nCPULoThreshold;
  142. LONG m_nCPUHiThreshold;
  143. CString GetHostName(SOCKET socket);
  144. void CreateStream(ClientContext* pContext);
  145. BEGIN_IO_MSG_MAP()
  146. IO_MESSAGE_HANDLER(IORead, OnClientReading)
  147. IO_MESSAGE_HANDLER(IOWrite, OnClientWriting)
  148. IO_MESSAGE_HANDLER(IOInitialize, OnClientInitializing)
  149. END_IO_MSG_MAP()
  150. bool OnClientInitializing (ClientContext* pContext, DWORD dwSize = 0);
  151. bool OnClientReading (ClientContext* pContext, DWORD dwSize = 0);
  152. bool OnClientWriting (ClientContext* pContext, DWORD dwSize = 0);
  153. };
  154. #endif // !defined(AFX_IOCPSERVER_H__75B80E90_FD25_4FFB_B273_0090AA43BBDF__INCLUDED_)