Class Iocp // complete port function class IocpMemoryManager // This class provides IOCP memory management [cpp] # pragma once # include "IocpMemoryManager. h "# include <windows. h> # define MAXWORKERTHREADS 2 // iocp class encapsulation classCIocp {public: CIocp (void );~ CIocp (void); boolStartServer (intnPort = 4568, intnMaxConnections = 2000); // start service listening port, maximum number of connections boolReStartServer (); // restart the service boolStopServer (); // stop the service intGetCurrentConnection (); // get the current number of connections boolSendText (CIOCPContext * pContext, char * pszText, intnLen); // send data to the client char * GetCustomLastError (); // obtain the final error message protected: // event notification function virtualvoid OnConnectionEstablished (CIOCPContext * pContext, CIOCPBuffer * pBuffer) = 0; // Create a new connection virtualvoid OnConnectionClosing (CIOCPContext * pContext, CIOCPBuffer * pBuffer) = 0; // close virtualvoid OnConnectionError (CIOCPContext * pContext, CIOCPBuffer * pBuffer, intnError) = 0; // virtualvoid OnReadCompleted (CIOCPContext * pContext, CIOCPBuffer * pBuffer) = 0; // The read operation on a connection completes virtualvoid OnWriteCompleted (CIOCPContext * pContext, CIOCPBuffer * pBuffer) = 0; // The write operation on a connection is completed Private: IocpMemoryManager * m_pIocpMemoryManager; // This class provides IOCP memory management boolm_bServerStarted; // whether the service has started intm_nPort; // intm_nInitialAccepts; // initialize the number of accepted requests for delivery intm_nInitialReads; // initialize the number of received requests intm_nMaxAccepts; // The maximum number of accepted requests intm_nMaxSends; // The maximum number of sent requests for a single socket intm_nMaxListenCount; // maximum number of listeners intm_nMaxWorkerThreads; // maximum number of working threads char * m_pcaErrorMessage; // obtain the last error message with the error message // Event HANDLEm_hAcceptEvent; // used to deliver Accep T request token; LONGm_nRepostCount; // SOCKET information HANDLEm_hListenThread; // The listening thread HANDLEm_hCompletion; // The port handle SOCKET m_sListen; // The listening character handle LPFN_ACCEPTEX m_lpfnAcceptEx; // AcceptEx function address LPFN_GETACCEPTEXSOCKADDRS success; // GetAcceptExSockaddrs function address private: boolDoWSAStartup (); // initialize the socket library voidInitData (); // initialize the data voidInitEvent (); // initialize Event boolCreateIocpPort (); // create port v OidSetCustomLastError (char * error); // set the error boolPostAccept (CIOCPBuffer * pBuffer); // delivery acceptance I/O boolPostSend (CIOCPContext * pContext, CIOCPBuffer * pBuffer ); // delivery and sending I/O boolPostRecv (CIOCPContext * pContext, CIOCPBuffer * pBuffer); // delivery and receiving I/O voidHandleIO (DWORDdwKey, CIOCPBuffer * pBuffer, DWORDdwTrans, intnError ); // total request processing voidHandleAccept (CIOCPBuffer * pBuffer, DWORDdwTrans); // process the received request voidHandleRead (CIOCPCo Ntext * pContext, CIOCPBuffer * pBuffer, DWORDdwTrans); // process the Read Request voidHandleWrite (CIOCPContext * pContext, CIOCPBuffer * pBuffer, DWORDdwTrans ); // process write requests staticDWORD WINAPI _ ListenThreadProc (LPVOIDlpParam); // The listening thread staticDWORD WINAPI _ WorkerThreadProc (LPVOIDlpParam); // working thread}; [cpp] // IocpMemoryManager. h # pragma once # include <winsock2.h> # include <Mswsock. h> # include <list> # include <iostream> usingnamesp Ace std; // This class provides IOCP memory management # define BUFFER_SIZE 1024*4 // the buffer size of the I/O Request for a single transmission up to 4096 // This is per-I/O data. It contains the necessary information for processing I/O operations on the set of characters classCIOCPBuffer {public: CIOCPBuffer ();~ CIOCPBuffer (); boolisUsed; WSAOVERLAPPED ol; SOCKET sClient; // client-side character set char * buff received by AcceptEx; // buffer intnLen used by I/O operations; // buff buffer (used) ULONGnSequenceNumber; // intnOperation of the I/O serial number; // operation type # define OP_ACCEPT 1 # define OP_WRITE 2 # define OP_READ 3 }; // This is the per-Handle data. It contains the classCIOCPContext {public: CIOCPContext ();~ CIOCPContext (); boolisUsed; SOCKET s; // addr_in addrLocal; // The local connection address SOCKADDR_IN addrRemote; // The remote connection address boolbClosing; // whether intnOutstandingRecv is disabled for a set of knots; // The number of overlapping operations thrown on the set of knots intnOutstandingSend; ULONGnReadSequence; // specifies the next serial number ULONGnCurrentReadSequence to be received; // list of serial numbers to be read <CIOCPBuffer *> pOutOfOrderReadsList; // The records do not have read I/O CRITICAL_SECTION Lock completed in order; // protect this structure}; classIocpMemoryManager {publ Ic: IocpMemoryManager (void );~ IocpMemoryManager (void); staticIocpMemoryManager * GetInstence (); intm_nMaxFreeBuffers; // maximum idle Buffer buffers; // maximum number of idle Contexts intm_nMaxConnections; // maximum number of connections intGetContextListCount () {returnm_contextList.size ();} // boolInsertAPendingAccept (CIOCPBuffer * pBuffer); boolRemoveAPendingAccept (CIOCPBuffer * pBuffer); intGetPendingAcceptCount (); // buffer operation CIOCPBuffer * AllocateBuffer (intnLen = BUFFER_SIZE); // buffer application voidReleaseABuffer (CIOCPBuffer * pBuffer); // buffer releases CIOCPBuffer * GetANoUseBuffer (); // obtain an idle buffer intGetFreeBuffersCount (); // obtain the number of idle Buffer // Contexts operation CIOCPContext * AllocateContext (constSOCKET & sClient ); // Contexts apply for voidReleaseAContext (CIOCPContext * pBuffer); // Contexts release CIOCPContext * GetANoUseContexts (); // obtain an idle Contexts intGetFreeContextsCount (); //// obtain the number of idle Contexts voidCloseAConnection (CIOCPContext * pContext); voidCloseAllConnections (); // close all sockets but not FreeContexts (); voidFreeBuffers (); // clear all Buffers voidFreeContexts (); // clear all Contexts // obtain the CIOCPBuffer * GetNextReadBuffer (CIOCPContext * pContext, CIOCPBuffer * pBuffer) to be read ); // obtain the maximum number of working threads = number of CPUs * 2 {optimal} intGetMaxWorkerThreadCount (); voidHandlePendingAcceptTimeOut (); private: list <CIOCPBuffer *> m_bufferList; // unified management [buffer for all applications] list <CIOCPContext *> m_contextList; // unified management [context for all applications] list <CIOCPBuffer *> m_pPendingAcceptsList; CRITICAL_SECTION m_FreeBufferListLock; CRITICAL_SECTION m_FreeContextListLock; CRITICAL_SECTION m_PendingAcceptsLock; CRITICAL_SECTION m_ConnectionListLock ;};