📄 cmoverlappedsendbuffer.cpp
字号:
#include "CMOverlappedSendBuffer.h"
#include <assert.h>
#include <MatrixCore/Network/CMIocpMudule.h>
#include <MatrixCore/Pattern/CMSingleton.h>
using namespace MatrixCore::Pattern;
using namespace MatrixCore::Network;
CMSendBuffer::CMSendBuffer()
{
m_pSendMemPool = new CMMemPooler<CMOVERLAPPED>(30000);
}
CMSendBuffer::~CMSendBuffer()
{
delete m_pSendMemPool;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//
//
//
//-------------------------------------------------------------------------------------------------------
// Name :: BOOL SendMsg(SOCKET s,char *pMsg,int iSize)
// Create Date :: 2004/ 1/30
// Description ::
// param ::
// Return Value :: void 鸥涝
// Bug Report :: 傈眉 季靛 何盒 父甸绢具 茄促.
//-------------------------------------------------------------------------------------------------------
BOOL CMSendBuffer::SendMsg(SOCKET s,char *pMsg,int iSize,DWORD dwSockID,CMOVERLAPPED* pSendOV)
{
int ret;
assert(pMsg != NULL);
DWORD dwSendBytes=0;
DWORD dwFlags=0;
CMOVERLAPPED *pOV;
char *pBuff = NULL;
if(dwSockID != NULL && dwSockID != pSendOV->dwRandomID)
{
//荤侩磊绰 辆丰 登菌栏唱 皋技瘤 钮俊 巢酒乐绰 惑怕捞绊 促弗荤侩磊啊 立加窍看促..
//蝶扼辑 捞皋技瘤绰 皋技瘤 钮俊辑 昏力 登菌绢具 且皋技瘤促.
//苞楷 捞繁版快啊 乐阑鳖? 抛胶飘 夸噶
CMSingleton<CMDebug>::getInstance()->output("%s(%d)弊成 俊矾(荤侩磊(%d) 立加辆丰揪 皋技瘤啊 促 昏力 登瘤 臼疽促.)\n",__FILE__,__LINE__,s);
return FALSE;
}
// CMIocpMudule::GetInstance()->m_pIOAccepter->m_pClientOverlapped;
//坷滚乏 皋葛府 劝寸
pOV = m_pSendMemPool->Alloc();
if(pOV == NULL)
{
//坷滚乏 皋葛府甫 棱绰单 角菩窍看促.
CMSingleton<CMDebug>::getInstance()->output("%s(%d)农府萍拿 俊矾(send 皋葛府 何练)\n",__FILE__,__LINE__);
return FALSE;
}
pOV->s = s;
pOV->eState = eSEND;
pOV->SendIoContext.pThisPoint = m_pSendMemPool;
pOV->SendIoContext.iSendCnt = NULL; // 1俺楼府 皋技瘤.
pBuff = new char[iSize+ 1];
if(pBuff == NULL)
{
//皋葛府 棱绰单 角菩 窍看促.
CMSingleton<CMDebug>::getInstance()->output("%s(%d)农府萍拿 俊矾(send1 皋葛府 何练)\n",__FILE__,__LINE__);
return FALSE;
}
memcpy(pBuff,pMsg,iSize);
CMPacket pPacket;
unsigned short sId;
unsigned short sGameType;
unsigned short sDataSize = 0;
unsigned short sReplayID = 0;
pPacket.GetHeaderInfo(pBuff,&sId,&sGameType,&sDataSize,&sReplayID);
pOV->sendBufferDescriptor.buf = pBuff;
pOV->sendBufferDescriptor.len = iSize;
if(WSASend(s, &(pOV->sendBufferDescriptor), 1, &dwSendBytes, dwFlags, pOV, NULL) == SOCKET_ERROR)
{
ret = WSAGetLastError();
if(ret != ERROR_IO_PENDING)
{
if( ret == 10053 || ret == 10054)
{
//皋葛府 钱秦力.. 肯丰风凭俊 救甸绢 柯促.
if(pOV->sendBufferDescriptor.buf != NULL)
delete pOV->sendBufferDescriptor.buf;
m_pSendMemPool->Free(pOV);
return FALSE;
}
else
{
if(ret != 10038)
{
//WSASend Error!!!!
CMSingleton<CMDebug>::getInstance()->output("%s(%d)WSASend() Error!!!!!!! (%d)\n",__FILE__,__LINE__,ret);
//皋葛府 钱秦力.. 肯丰风凭俊 救甸绢 柯促.
if(pOV->sendBufferDescriptor.buf != NULL)
delete pOV->sendBufferDescriptor.buf;
m_pSendMemPool->Free(pOV);
return -1;
}
}
}
}
return TRUE;
}
//-------------------------------------------------------------------------------------------------------
// Name :: BOOL SendMsg(SOCKET s,char *pMsg,int iSize)
// Create Date :: 2004/ 1/30
// Description ::
// param ::
// Return Value :: void 鸥涝
// Bug Report :: 傈眉 季靛 何盒 父甸绢具 茄促.
//-------------------------------------------------------------------------------------------------------
BOOL CMSendBuffer::SendAllMsg(SOCKET *s,char *pMsg,int iMsgSize,int iSockCnt ,SOCKET sSocket )
{
int ret;
int iCnt;
assert(pMsg != NULL);
DWORD dwSendBytes=0;
DWORD dwFlags=0;
CMOVERLAPPED *pOV;
char *pBuff = NULL;
volatile long *ipSockCnt;
CMPacket pPacket;
unsigned short sId;
unsigned short sGameType;
unsigned short sDataSize = 0;
unsigned short sReplayID = 0;
// 焊尘 荤侩磊啊 绝促. 俊矾绰 酒聪促.
if (iSockCnt <= 0)
return TRUE;
pBuff = new char[iMsgSize+ 1];
if(pBuff == NULL)
{
//皋葛府 棱绰单 角菩 窍看促.
CMSingleton<CMDebug>::getInstance()->output("%s(%d)农府萍拿 俊矾(send1 皋葛府 何练)\n",__FILE__,__LINE__);
return FALSE;
}
// 单捞鸥甫 滚欺俊 墨乔 茄促.
memcpy(pBuff,pMsg,iMsgSize);
pPacket.GetHeaderInfo(pBuff,&sId,&sGameType,&sDataSize,&sReplayID);
ipSockCnt = new volatile long;
*ipSockCnt = iSockCnt;
// 瘤沥等 家南篮 救焊郴扁困茄 风凭
if (sSocket == INVALID_SOCKET)
{
for(int i = 0; i < iSockCnt ; i++)
{
//坷滚乏 皋葛府 劝寸
pOV = m_pSendMemPool->Alloc();
if(pOV == NULL)
{
//坷滚乏 皋葛府甫 棱绰单 角菩窍看促.
CMSingleton<CMDebug>::getInstance()->output("%s(%d)农府萍拿 俊矾(send 皋葛府 何练)\n",__FILE__,__LINE__);
return FALSE;
}
//? 抛胶飘吝
pOV->iTestDebug = 1;
pOV->eState = eSEND;
pOV->SendIoContext.pThisPoint = m_pSendMemPool;
pOV->SendIoContext.iSendCnt = ipSockCnt;
pOV->sendBufferDescriptor.buf = pBuff;
pOV->sendBufferDescriptor.len = iMsgSize;
pOV->s = s[i];
if(WSASend(s[i], &(pOV->sendBufferDescriptor), 1, &dwSendBytes, dwFlags, pOV, NULL) == SOCKET_ERROR)
{
ret = WSAGetLastError();
if(ret != ERROR_IO_PENDING)
{
if( ret == 10053 || ret == 10054)
{
//皋葛府 钱秦力.. 肯丰风凭俊 救甸绢 柯促.
pOV->iTestDebug1 = ret;
DeleteSendMemBuf2(*pOV);
//InterlockedDecrement(pOV->SendIoContext.iSendCnt);
//if( 0 >= *pOV->SendIoContext.iSendCnt)
//{
// if(pOV->sendBufferDescriptor.buf != NULL)
// {
// delete pOV->SendIoContext.iSendCnt;
// delete pOV->sendBufferDescriptor.buf;
// }
//}
//m_pSendMemPool->Free(pOV);
// 俊矾啊 唱歹扼档 促澜 荤侩磊甫 焊郴具 茄促.
//return FALSE;
}
else
{
if(ret != 10038)
{
//WSASend Error!!!!
CMSingleton<CMDebug>::getInstance()->output("%s(%d)WSASend() Error!!!!!!! (%d)\n",__FILE__,__LINE__,ret);
//皋葛府 钱秦力.. 肯丰风凭俊 救甸绢 柯促.
pOV->iTestDebug1 = ret;
DeleteSendMemBuf2(*pOV);
//m_pSendMemPool->Free(pOV);
//if( 0 >= *pOV->SendIoContext.iSendCnt)
//{
// if(pOV->sendBufferDescriptor.buf != NULL)
// {
// delete pOV->SendIoContext.iSendCnt;
// delete pOV->sendBufferDescriptor.buf;
// }
//}
// 俊矾啊 唱歹扼档 促澜 荤侩磊甫 焊郴具 茄促.
// return -1;
}
}
}
}
}
}
else
{
for(int i = 0; i < iSockCnt ; i++)
{
// 瘤沥等 家南篮 救焊辰促.
if(s[i] == sSocket)
{
iCnt = InterlockedDecrement(ipSockCnt);
if( 0 >= iCnt)
{
if(pBuff != NULL)
{
delete ipSockCnt;
delete pBuff;
}
// 咯扁 甸绢坷搁 歹捞惑 贸府且 鞘夸啊 绝促.
//return FALSE;
}
continue;
}
//坷滚乏 皋葛府 劝寸
pOV = m_pSendMemPool->Alloc();
//? 抛胶飘吝
pOV->iTestDebug = 2;
if(pOV == NULL)
{
//坷滚乏 皋葛府甫 棱绰单 角菩窍看促.
CMSingleton<CMDebug>::getInstance()->output("%s(%d)农府萍拿 俊矾(send 皋葛府 何练)\n",__FILE__,__LINE__);
return FALSE;
}
pOV->eState = eSEND;
pOV->SendIoContext.pThisPoint = m_pSendMemPool;
pOV->SendIoContext.iSendCnt = ipSockCnt;
pOV->sendBufferDescriptor.buf = pBuff;
pOV->sendBufferDescriptor.len = iMsgSize;
pOV->s = s[i];
if(WSASend(s[i], &(pOV->sendBufferDescriptor), 1, &dwSendBytes, dwFlags, pOV, NULL) == SOCKET_ERROR)
{
ret = WSAGetLastError();
if(ret != ERROR_IO_PENDING)
{
if( ret == 10053 || ret == 10054)
{
//皋葛府 钱秦力.. 肯丰风凭俊 救甸绢 柯促.
pOV->iTestDebug1 = ret;
DeleteSendMemBuf2(*pOV);
//InterlockedDecrement(pOV->SendIoContext.iSendCnt);
//if( 0 >= *pOV->SendIoContext.iSendCnt)
//{
// if(pOV->sendBufferDescriptor.buf != NULL)
// {
// delete pOV->SendIoContext.iSendCnt;
// delete pOV->sendBufferDescriptor.buf;
// pOV->SendIoContext.iSendCnt = NULL;
// pOV->sendBufferDescriptor.buf = NULL;
// }
// m_pSendMemPool->Free(pOV);
//}
// 俊矾啊 唱歹扼档 促澜 荤侩磊甫 焊郴具 茄促.
//return FALSE;
}
else
{
if(ret != 10038)
{
//WSASend Error!!!!
CMSingleton<CMDebug>::getInstance()->output("%s(%d)WSASend() Error!!!!!!! (%d)\n",__FILE__,__LINE__,ret);
//皋葛府 钱秦力.. 肯丰风凭俊 救甸绢 柯促.
pOV->iTestDebug1 = ret;
DeleteSendMemBuf2(*pOV);
//if( 0 >= *pOV->SendIoContext.iSendCnt)
//{
// if(pOV->sendBufferDescriptor.buf != NULL)
// {
// delete pOV->SendIoContext.iSendCnt;
// delete pOV->sendBufferDescriptor.buf;
// pOV->SendIoContext.iSendCnt = NULL;
// pOV->sendBufferDescriptor.buf = NULL;
// }
//}
//m_pSendMemPool->Free(pOV);
// 俊矾啊 唱歹扼档 促澜 荤侩磊甫 焊郴具 茄促.
// return -1;
}
}
}
}
}
}
return TRUE;
}
//-------------------------------------------------------------------------------------------------------
// Name :: BOOL DeleteSendMemBuf(CMOVERLAPPED* pSendOV)
// Create Date :: 2004/ 7/30
// Description ::
// param ::
// Return Value :: void 鸥涝
// Bug Report ::
//-------------------------------------------------------------------------------------------------------
BOOL CMSendBuffer::DeleteSendMemBuf(CMOVERLAPPED &SendOV)
{
if(SendOV.eState == eSEND)
{
if(SendOV.SendIoContext.iSendCnt == NULL)
{
if(SendOV.sendBufferDescriptor.buf != NULL)
{
delete SendOV.sendBufferDescriptor.buf;
SendOV.sendBufferDescriptor.buf = NULL;
}
}
else
{
InterlockedDecrement(SendOV.SendIoContext.iSendCnt);
if( 0 >= *SendOV.SendIoContext.iSendCnt)
{
if(SendOV.sendBufferDescriptor.buf != NULL)
{
delete SendOV.SendIoContext.iSendCnt;
delete SendOV.sendBufferDescriptor.buf;
SendOV.SendIoContext.iSendCnt = NULL;
SendOV.sendBufferDescriptor.buf = NULL;
}
}
}
}
return TRUE;
}
//-------------------------------------------------------------------------------------------------------
// Name :: BOOL DeleteSendMemBuf2(CMOVERLAPPED* pSendOV)
// Create Date :: 2004/ 7/30
// Description ::
// param ::
// Return Value :: void 鸥涝
// Bug Report ::
//-------------------------------------------------------------------------------------------------------
BOOL CMSendBuffer::DeleteSendMemBuf2(CMOVERLAPPED &SendOV)
{
int iCnt;
iCnt = InterlockedDecrement(SendOV.SendIoContext.iSendCnt);
if( 0 >= iCnt)
{
if(SendOV.sendBufferDescriptor.buf != NULL)
{
delete SendOV.SendIoContext.iSendCnt;
delete SendOV.sendBufferDescriptor.buf;
SendOV.SendIoContext.iSendCnt = NULL;
SendOV.sendBufferDescriptor.buf = NULL;
}
}
m_pSendMemPool->Free(&SendOV);
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -