资源简介
我们的一个项目的服务器端通信层,IOCP类是在此基础上写的,使用了近半年,并发量不大,1k上下,(不过曾做过压力测试,10k的并发量没有问题)还算比较稳定,基本上满足需求。
代码片段和文件信息
////////////////////////////////////////////////////////////////////////////////////////////////////
// Module Name:
// IOCPNetTest.cpp
// Author:
// Chun-Hyok Chong.
// Description:
// IOCPNet test code.
////////////////////////////////////////////////////////////////////////////////////////////////////
#include “stdafx.h“
#include “workframe.h“
#include “WorkframeReturnCode.h“
#include
#include
#include
#include
#include
#include
#include
#include “IOCPNet.h“
#define TEST_IP “127.0.0.1“ // change it!
#define TEST_PORT 20000
#define TEST_LOG_PATH “c:\\\\testlog\\log.txt“
#define WAIT_TIMEOUT_TEST 100
DWORD WINAPI LogicThread(void *pParam);
void MainLogic(OIOCPNet *pIOCPNet DWORD SocketUnique OBufferedSocket *pBuffSock BYTE *pReadData DWORD ReadSize);
DWORD g_dRunning;
int _tmain(int argc _TCHAR* argv[])
{
OErrLog EL;
OIOCPNet *pIOCPNet;
int key;
WSADATA WSAData;
HANDLE hThread;
WSAStartup(MAKEWORD(22) &WSAData);
EL.SetErrLogFile(TEST_LOG_PATH);
pIOCPNet = new OIOCPNet(&EL);
pIOCPNet->Start(TEST_IP TEST_PORT);
InterlockedExchange((long *)&g_dRunning 1);
hThread = CreateThread(0 0 LogicThread pIOCPNet 0 0);
if (0 == hThread)
{
printf(“CreateThread is failed. Error code = %d\n“ GetLastError());
goto ErrHand;
}
printf(“Server: Press any key to stop...\n“);
key = getch();
InterlockedExchange((long *)&g_dRunning 0);
WaitForSingleobject(hThread INFINITE);
ErrHand:
pIOCPNet->Stop();
delete pIOCPNet;
WSACleanup();
return 0;
} // _tmain()
DWORD WINAPI LogicThread(void *pParam)
{
int iRes;
OIOCPNet *pIOCPNet;
int EventType;
DWORD SocketUnique;
BYTE *pReadData;
DWORD ReadSize;
OBufferedSocket *pBuffSock;
OReadSlotElement *pSlot;
void *pCustData; // for formatting. no custom data is used in this test.
pIOCPNet = (OIOCPNet *)pParam;
while (1 == InterlockedExchange((long *)&g_dRunning g_dRunning))
{
iRes = pIOCPNet->GetSocketEventData(WAIT_TIMEOUT_TEST &EventType &SocketUnique &pReadData &ReadSize &pBuffSock &pSlot &pCustData);
if (RET_TIMEOUT == iRes)
{
continue;
}
else if (RET_FAIL == iRes)
{
return 0;
}
else if (RET_SOCKET_CLOSED == iRes)
{
// release pCustData.
continue;
}
// Process main logic.
MainLogic(pIOCPNet SocketUnique pBuffSock pReadData ReadSize);
pIOCPNet->ReleaseSocketEvent(pSlot);
}
return 0;
} // LogicThread()
void MainLogic(OIOCPNet *pIOCPNet DWORD SocketUnique OBufferedSocket *pBuffSock BYTE *pReadData DWORD ReadSize)
{
//printf(“pReadData = %s\n“ pReadData);
pIOCPNet->WriteData(SocketUnique pBuffSock pReadData ReadSize); // echo.
return;
} // MainLogic()
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 2877 2005-10-28 11:33 IOCPNetTest\IOCPNetTest\IOCPNetTest.cpp
文件 21 2005-08-24 22:46 IOCPNetTest\IOCPNetTest\stdafx.cpp
文件 101 2005-08-24 22:45 IOCPNetTest\IOCPNetTest\stdafx.h
文件 3856 2005-08-29 10:53 NetTestClient\NetTestClient\NetTestClient.cpp
文件 21 2005-08-24 22:48 NetTestClient\NetTestClient\stdafx.cpp
文件 153 2005-08-24 22:49 NetTestClient\NetTestClient\stdafx.h
文件 8038 2005-08-01 10:32 workfr
文件 1046 2005-07-27 17:00 workfr
文件 38280 2006-01-17 22:07 workfr
文件 8688 2006-01-17 22:06 workfr
文件 11298 2005-08-24 22:45 workfr
文件 6080 2005-09-10 02:59 workfr
文件 1443 2005-08-13 09:40 workfr
文件 1061 2005-08-01 10:32 workfr
文件 700 2005-07-27 17:00 workfr
文件 1227 2005-08-01 10:32 workfr
文件 903 2005-07-30 02:48 workfr
文件 2914 2005-08-29 10:43 workfr
文件 1397 2005-08-13 09:41 workfr
文件 9926 2005-08-24 22:49 workfr
文件 764 2005-08-24 22:49 workfr
文件 175 2005-08-23 14:10 workfr
文件 1230 2005-08-24 22:45 workfr
- 上一篇:基于单片机的水箱控制系统
- 下一篇:项目经理文档
评论
共有 条评论