程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> UDP的IOCP方式

UDP的IOCP方式

編輯:.NET實例教程
class CIOCPServer
{
 CString m_sConnectString;


public:
 CDatabase m_Database;
 /*讀起用戶信息到內存*/
 CIOCPServer();
 virtual ~CIOCPServer();

 //2005.6.2
 BOOL StartServer();
 BOOL StopServer();


 BOOL InitSocket(SOCKET_STYPE stype,int port, int num = -1);

 //綁定在端口上工作線程
 static DWord WINAPI CompletionWorkerThread( void * lpParam);

private:
 BOOL ProcessLoadMsg(LPCLIENTCONTEXT lpContext);

private:
 //根據消息overlapped的類型,處理消息
 BOOL ProcessIOMessage(SOCKET_STYPE stype,IOType opCode, LPCLIENTCONTEXT lpContext , DWord dwiOSize);

 //在端口上產生線程,並創建完成端口
 void CreateWorkerThread();
 //關閉完成端口
 void CloseCompletionPort();
 //分配連接overlappedplus
 LPOVERLAPPEDPLUS AllocateOverlappedPlus(IOType ioType);
 //分配連接進入的客戶的相關信息
 LPCLIENTCONTEXT AllocateContext();
 //釋放overlappedplus
 void FreeOverlappedPlus(LPOVERLAPPEDPLUS lpOlp);
private:

 HANDLE m_hIocp; //完成端口句柄
 DWord m_dwThreads; //線程數
};

CPP方式:
#include "IOCPServer.h"

#include "common.h"

CIOCPServer::CIOCPServer()
{

 //socket初始化
 WSADATA wsd;
 WORD wVersionRequested = MAKEWord(2, 2);
 int nResult = WSAStartup(wVersionRequested, &wsd);
 if (nResult == SOCKET_ERROR)
 {
 WSACleanup();
 }

 if (LOBYTE(wsd.wVersion) != 2 || HIBYTE(wsd.wVersion) != 2)
 {
 WSACleanup();
 }


}

CIOCPServer::~CIOCPServer()
{
 WSACleanup();
}


//分配連接overlappedplus
LPOVERLAPPEDPLUS CIOCPServer::AllocateOverlappedPlus(IOType ioType)
{
 OVERLAPPEDPLUS* pOlp = NULL;

 pOlp = new OVERLAPPEDPLUS;
 ZeroMemory(pOlp, sizeof(OVERLAPPEDPLUS));
 pOlp->opCode = ioType;

 return pOlp;

}

//分配連接進入的客戶的相關信息
LPCLIENTCONTEXT CIOCPServer::AllocateContext()
{
 LPCLIENTCONTEXT lpContext = NULL;
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved