资源简介
自己准备实现一个web服务器学习IOCP而封装的一个框架,能适应高并发(默认我设置是64个)连入及多client同时在线传输数据(最大测试到30000).包括TCP与UDP两种协议的IOCP封装实现。比较好的解决了掉线,死机,超时等socket关闭问题及WAIT_TIME问题。同时用较好方式实现了AcceptContext,ReceiveContext,Sendcontext的管理(自己写了个static类),重用各种context,避免频繁的new与delete操作.系统容量会随接入用户自动提升.框架清楚,接口已经做好,写web服务器是很容易的事情了。
代码片段和文件信息
#include “StdAfx.h“
#include “.\acceptcontext.h“
#include “baseDefine.h“
bool CAcceptContext::m_bInitialized = false;
CRITICAL_SECTION CAcceptContext::m_struCriSec = {0};
CContextStack* CAcceptContext::m_pAcceptContextStack = NULL;
CContextStack* CAcceptContext::m_pAcceptContextManageStack = NULL;
CAcceptContext::CAcceptContext(int opModeSOCKET listenSocketSOCKET clientSocket)
{
m_iOperateMode = opMode;
m_struListenSocket = listenSocket;
m_hSocket = clientSocket;
ZeroMemory(&m_struOperateOlsizeof(WSAOVERLAPPED));
ZeroMemory(m_ucAddressbufACCEPT_ADDRESS_LENGTH*2);
}
CAcceptContext::~CAcceptContext(void)
{
m_iOperateMode = SC_WAIT_ACCEPT;
m_struListenSocket = NULL;
// closesocket(m_hSocket);
m_hSocket = NULL;
ZeroMemory(&m_struOperateOlsizeof(WSAOVERLAPPED));
ZeroMemory(m_ucAddressbufACCEPT_ADDRESS_LENGTH*2);
}
void CAcceptContext::SetAcceptParameters(SOCKET listenSocketSOCKET clientSocket)
{
m_struListenSocket = listenSocket;
m_hSocket = clientSocket;
}
void CAcceptContext::ResetContext()
{
//m_iOperateMode = SC_WAIT_ACCEPT;
m_struListenSocket = NULL;
m_hSocket = NULL;
ZeroMemory(&m_struOperateOlsizeof(WSAOVERLAPPED));
ZeroMemory(m_ucAddressbufACCEPT_ADDRESS_LENGTH*2);
}
void CAcceptContext::InitContextPool(long poolSize/* = DEFAULT_ACCEPT_CONTEXT_POOL*/)
{
if(m_bInitialized)
return ;
m_pAcceptContextStack = new CContextStack();
m_pAcceptContextManageStack = new CContextStack();
if(m_pAcceptContextStack == NULL)
return ;
InitializeCriticalSection(&m_struCriSec);
CAcceptContext* pContext = NULL;
for(int i=0;i {
pContext = new CAcceptContext(SC_WAIT_ACCEPTNULLNULL);
if(pContext)
{
m_pAcceptContextStack->Push(pContext);//链接对象入栈
m_pAcceptContextManageStack->Push(pContext);
pContext->m_iContextIndex = m_pAcceptContextManageStack->Size();
}
}
m_bInitialized = true;
}
CAcceptContext* CAcceptContext::GetContext()
{
if(!m_bInitialized)
return 0;
CAcceptContext* pContext = NULL;
EnterCriticalSection(&m_struCriSec);
if(!m_pAcceptContextStack->IsEmpty())
{
pContext = (CAcceptContext*)m_pAcceptContextStack->Pop();
}
else
{
pContext = new CAcceptContext(SC_WAIT_ACCEPTNULLNULL);
pContext->m_iContextIndex = m_pAcceptContextManageStack->Size();
m_pAcceptContextManageStack->Push(pContext);
}
LeaveCriticalSection(&m_struCriSec);
//此处有可能会出现线程同步问题
return pContext;
}
void CAcceptContext::ReleaseContext()
{
if(this)
{
EnterCriticalSection(&m_struCriSec);
m_pAcceptContextStack->Push(this);
LeaveCriticalSection(&m_struCriSec);
}
}
//销毁整个链接池context
void CAcceptContext::DestroyContextPool()
{
CAcceptContext* pContext = NULL;
int size = m_pAcceptContextStack->Size();
for(int i=0;i {
pContext = (CAcceptContext*)m_pAcceptContextManageStack->Pop();
if(pContext)
{
delete pContext;
pContext = NULL;
}
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 3915 2009-03-06 01:16 IOCP_Server\IOCP_Server\AcceptContext.cpp
文件 1225 2009-03-05 14:41 IOCP_Server\IOCP_Server\AcceptContext.h
文件 1088 2009-03-07 11:30 IOCP_Server\IOCP_Server\ba
文件 1181 2009-03-05 14:41 IOCP_Server\IOCP_Server\ContextStack.cpp
文件 544 2009-03-07 10:37 IOCP_Server\IOCP_Server\ContextStack.h
文件 17540 2009-02-25 17:00 IOCP_Server\IOCP_Server\IOCP_Server.aps
文件 684 2009-03-10 16:05 IOCP_Server\IOCP_Server\IOCP_Server.cpp
文件 1282 2009-02-25 17:00 IOCP_Server\IOCP_Server\IOCP_Server.rc
文件 5450 2009-03-03 09:34 IOCP_Server\IOCP_Server\IOCP_Server.vcproj
文件 277 2009-03-10 10:32 IOCP_Server\IOCP_Server\IOCP_Server.vcproj.vspscc
文件 409 2009-03-10 17:30 IOCP_Server\IOCP_Server\MemoryBlock.cpp
文件 437 2009-03-10 17:31 IOCP_Server\IOCP_Server\MemoryBlock.h
文件 177 2009-03-10 10:32 IOCP_Server\IOCP_Server\mssccprj.scc
文件 398 2009-03-05 14:41 IOCP_Server\IOCP_Server\OperateContext.cpp
文件 792 2009-03-05 14:41 IOCP_Server\IOCP_Server\OperateContext.h
文件 1015 2009-02-18 22:18 IOCP_Server\IOCP_Server\ReadMe.txt
文件 390 2009-02-25 17:00 IOCP_Server\IOCP_Server\resource.h
文件 215 2009-02-18 22:18 IOCP_Server\IOCP_Server\stdafx.cpp
文件 195 2009-02-18 22:18 IOCP_Server\IOCP_Server\stdafx.h
文件 4303 2009-03-10 17:30 IOCP_Server\IOCP_Server\TcpReceiveContext.cpp
文件 1264 2009-03-06 15:24 IOCP_Server\IOCP_Server\TcpReceiveContext.h
文件 4148 2009-03-11 14:27 IOCP_Server\IOCP_Server\TcpSendContext.cpp
文件 1248 2009-03-11 14:21 IOCP_Server\IOCP_Server\TcpSendContext.h
文件 28228 2009-03-11 14:58 IOCP_Server\IOCP_Server\TcpServer.cpp
文件 2716 2009-03-10 14:28 IOCP_Server\IOCP_Server\TcpServer.h
文件 347 2009-03-07 10:51 IOCP_Server\IOCP_Server\TcpSrvException.cpp
文件 645 2009-03-07 10:50 IOCP_Server\IOCP_Server\TcpSrvException.h
文件 4441 2009-03-07 14:45 IOCP_Server\IOCP_Server\UdpContext.cpp
文件 1198 2009-03-09 01:03 IOCP_Server\IOCP_Server\UdpContext.h
文件 15861 2009-03-10 10:32 IOCP_Server\IOCP_Server\UdpServer.cpp
............此处省略15个文件信息
- 上一篇:两轮移动式倒立摆两轮自平衡小车
- 下一篇:03_UVM-Harness.pdf
评论
共有 条评论