⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 iocp_class.h

📁 一个完成端口的框架程序
💻 H
📖 第 1 页 / 共 2 页
字号:
private:
   //communications
   VTCCONN_CTX FConnectionList;
   //recycle for Connections
   VTCCONN_CTX FrecycleConnectionList;
   //Critical Section with communication list oprations
   CRITICAL_SECTION conn_CriticalSection;
   CRITICAL_SECTION conn_DUMP_CriticalSection;
   long BytesSentPerInterval;
   long BytesRecvPerInterval;
   long BytesSentSampSpeed;
   long BytesRecvSampSpeed;
   UINT FLocks;
   DWORD FWinver;
   UINT FMaxConnections;

/*For working Threads*/
//FExpectThreadCount:用户期望之最大同时运行之线程数量
//FThreadList:线程列表
//FThreadIDList:线程ID列表
//FrecycleThreadList:线程回收站
//thread_CriticalSection:线程/线程ID列表操作临界区
//thread_DUMP_CriticalSection:线程回收站操作临界区
//FActiveThreadCount:处于GetQueuedCompletionStatus阻塞状态下的线程数量
private:
   //the user expect number of threads
   UINT FExpectThreadCount; //预期工作线程数
   //Working threads
   VTCHandle FThreadList;   //总线程列表
   VTCThreadID FThreadIDList;//
   //Critical Section for working thread list oprations
   CRITICAL_SECTION thread_CriticalSection;
   //Number of GetQueuedCompletionStatus blocking threds
   long FActiveThreadCount; //当前工作线程数

   /*当一个连接在一段时间没有数据I/O时,则将其断开,以节省资源*/
   /*在设计客户端时,如果长时间没有操作则须发出心跳包,以未存活*/
   long FTimeOut;//连接超时时间

/******************************************************************/
/*           以上为类的数据结构,以下为类的内部方法               */
/******************************************************************/

/*For IOCP*/
//IOCPInitialize:用始初始化完成端口相关的前置参数
//~IOCPInitialize:用于销毁完成端口句柄等相关已分配资源
private:
   //Initialize I/O C. P. Handle
   bool __fastcall IOCPInitialize();
   //Destroy I/O C. P. Handle
   void __fastcall IOCPFinalize(void);

/*For Server Thread*/
//CreateListenSock:创建监听SOCKET
//CreateListenThread:创建监听(实为Accept)线程
//PauseListen:暂停监听(实为Accept)线程
//ResumeListen:恢复暂停的监听(实为Accept)线程
//DestroyListenThread:清理监听线程
//DestroyListenSock:清理监听SOCKET
private:
   //Create Listen Socket
   bool __fastcall CreateListenSock(void);
   //Create Listen Socket to do accept loop
   bool __fastcall CreateListenThread(void);
   //set the Listen thread to sleep,
   //but no affect other communications
   bool __fastcall PauseListen(void);
   //set the listen thread work on,
   //no effect with the listen thread wasn't sleeping
   bool __fastcall ResumeListen(void);
   //close the listen thread
   void __fastcall DestroyListenThread(void);
   //close the listen socket
   void __fastcall DestroyListenSock(void);

   /*此处定义ListenerThread为friend function是为了使其能方便地访问类内部变量*/
   ///////////////////////////////////////////////////////////////////////////
   //              详见Unit_ListenerThread.cpp                              //
   ///////////////////////////////////////////////////////////////////////////
   friend  unsigned long WINAPI ListenerThread(void *lpParamter);

/*For Purifier Thread*/
private:
   //Create the Purifier Thread loop
   bool __fastcall CreatePurifierTimer(void);
   //Terminate the Purifier Thread loop
   void __fastcall DestroyPurifierTimer(void);
   void __fastcall InternalOnTimer(void);
   friend  void CALLBACK PurifierTimerProc(UINT uTimerID,
                                           UINT uMsg,
                                           DWORD_PTR dwUser,
                                           DWORD_PTR dw1,
                                           DWORD_PTR dw2);
   friend unsigned WINAPI PurifierThread(LPVOID lpParam);
   
/*For Client(s) */
private:
   //accept a new communication
   bool __fastcall AddConnection(const SOCKET sockAccept,
                                 const int RemotePort,
                                 const char *RemoteAddress);
   //disconnect a communication
   bool __fastcall RemoveConnection(LPCONN_CTX Connection,
                                    VTCCONN_CTX ConnectionList);
   void __fastcall DumpConnection(LPCONN_CTX Connection);
   friend DWORD WINAPI WorkerThread(LPVOID lpParam);
   void __fastcall SetMaxConnections(const UINT Count);
   //disconnection all the communications
   void __fastcall CleanConnections(void);
   UINT __fastcall GetCount(void);
   UINT __fastcall GetValidCount(void);
   //return communication node
   LPCONN_CTX __fastcall GetConnection(const UINT Index);
   void __fastcall InternalFreeDataNode(LPIODATANode DataNode);
   void __fastcall InternalFreeIOData(LPIODATA Data);
   void __fastcall InternalAddDataNode(const LPCONN_CTX Connection,
                                       const LPIODATANode DataNode);

/*for Interface*/
private:
   void __fastcall InternalOnAccept(UINT RemotePort,
                                    char RemoteAddress[],
                                    bool &CanAccept);
   void __fastcall InternalOnConnect(LPCONN_CTX Connection);
   void __fastcall InternalOnBeforeDisconnect(LPCONN_CTX Connection);
   void __fastcall InternalOnDisconnect(LPCONN_CTX Connection);
   void __fastcall InternalOnReceiveData(LPCONN_CTX Connection,
                                         UINT Length,
                                         const char * const Buffer);

/*For working Threads*/
private:
   bool __fastcall AddThread(const UINT Count=1);
   //Create a new working thread
   bool __fastcall NewThread(void);
   //cleanup all the working threads
   void __fastcall CleanThreadList(void);
   //remove a thread handle from the working-threads vector
   // with valid checks
   void __fastcall DumpThread(const DWORD ThreadID,
                              const bool NotInCriticalSection=true);

/******************************************************************/
/*           以上为类的内部方法,以下为类的公用方法               */
/******************************************************************/

/*For Server Thread*/
//SetRunningState:用户接口,用于调整服务器的运行/服务状态
//SetServicePort:设置监听端口,注:只有当服务处于LS_CLOSED时有效
//SetBindAddress:设置绑定IP地址,注:只有当服务处于LS_CLOSED时有效
//GetBindAddress:取得绑定的IP地址
public:
   //tune the service state to vState Value
   bool __fastcall SetRunningState(const TEnumListenState vState);
   //Set Port value as vPort
   //if it is listening,no effect,
   //an false value will be returned
   bool __fastcall SetServicePort(const int vPort);
   //Set Bind Address Value,
   //any valid local address by default
   bool __fastcall SetBindAddress(const char vBindAddress[]);
   //Getting binding address
   unsigned int __fastcall GetBindAddress(char *vBuffer,
                                          const unsigned int BufferLength);

/*For Client(s) */
//DropConnection:断开一个用户连接

/*要对一个Connection并保存Connection的有效性,就必须要进行Lock*/
//Lock:进入临界区并增加Lock计数
//Unlock:退出临界区并减少Lock计数

//SendText:用户接口方法,用于发送一段文本信息
//SendBuffer:用户接口方法,用于发送一段缓冲区
//SendData:用户接口方法,用于发送个信息节点,注:内存请使用AllocEx分配
public:
   bool __fastcall DropConnection(LPCONN_CTX Connection);
   UINT __fastcall Lock();
   UINT __fastcall Unlock();
   //send text message to a communication node
   bool __fastcall SendText(const LPCONN_CTX Connection,
                            const char Message[]);
   //send binary message(s) to a communication node
   bool __fastcall SendBuffer(const LPCONN_CTX Connection,
                              const char *Buffer,
                              const UINT Length);
   //send Structured message(s) to a communication node
   bool __fastcall SendData(const LPCONN_CTX Connection,
                            const LPIODATANode DataNode);
/*For working Threads*/
//SetExpectThread:设置用户期望的同步运行工作线程数(参考值)
public:
   //set a user expect number of working thread
   //it must less then or equal to FThreadMax
   bool __fastcall SetExpectThread(const UINT Count);
   UINT  __fastcall GetThreadCount(void);

/*For Class*/
//TtcpIOCP:类的构造方法,用于对相关的数据进行初始化
//~TtcpIOCP:类的析构方法,用于清理分配下来的内存
public:
   __fastcall TtcpIOCP(void);/*constructor*/
   __fastcall ~TtcpIOCP(void);/*destructor*/

/******************************************************************/
/*           以上为类的公用方法,以下为类的接口属性               */
/******************************************************************/
#ifdef __DEBUG
/*For Class*/
//IsEchoServer:回显服务状态属性
public:
   __property bool IsEchoServer={read=FEchoServer,write=FEchoServer};
#endif

/*For IOCP*/
//IOCPInvalid:完成端口前置参数初始化状态
//Handle:完成端口句柄
public:
   __property bool IOCPInvalid={read=FIOCPInvalid};
   __property HANDLE Handle={read=FHandle};
   __property long TimeOut={read=FTimeOut,write=FTimeOut};   

/*For Server Thread*/
//Port:监听/预监听端口
//ListenSock:监听SOCKET
//RunningState:服务状态
//TuningListenState:预置服务状态
public:
   __property int Port={read=FPort};
   __property SOCKET ListenSock={read=FListenSock};
   __property TEnumListenState RunningState={read=FRunningState};
   __property TEnumListenState TuningListenState={read=FTuningListenState};

/*For Client(s) */
//Connections:当前连接数(包括回收站中的"垃圾"连接)
//ValidConnection:当前有效连接数
//Connection:用户连接(有效)
public:
   __property UINT Connections={read=GetCount};
   __property UINT ValidConnections={read=GetValidCount};
   __property LPCONN_CTX Connection[const UINT Index]={read=GetConnection};

/*For working Threads*/
//ThreadCount:有效工作线程数
//ExpectThreadCount:用户预同步运行工作线程数
//MaxConnections:最大同步服务连接数
//SendSpeedInBytes:单位时间(秒)发送速率(采样值)
//RecvSpeedInBytes:单位时间(秒)接收速率(采样值)
public:
   __property UINT LockCount={read=FLocks};
   __property DWORD WinVersion={read=FWinver};
   __property UINT ThreadCount={read=GetThreadCount};
   __property UINT ExpectThreadCount={read=FExpectThreadCount};
   __property UINT MaxConnections={read=FMaxConnections,write=SetMaxConnections};
   __property long SentSpeedInBytes={read=BytesSentSampSpeed};
   __property long RecvSpeedInBytes={read=BytesRecvSampSpeed};
};
#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -