|
void
CAceClientHandler::handle_read_stream (const ACE_Asynch_Read_Stream::Result &result)
{
{
ACE_GUARD (ACE_SYNCH_MUTEX, monitor, this->lock_);
CAceCCharacter * pCharacter = m_pCharacter;
int nSuccess(0), nBytes_transferred(0);
{
ACE_Message_Block & mb = result.message_block ();
nSuccess = result.success();
nBytes_transferred = result.bytes_transferred();
if ( nSuccess && (nBytes_transferred != 0) )
{
if ( !m_pRecvHeader )
{
m_pRecvHeader = new char[PACK_DATA_SIZE * 2];
}
int nThisLen = mb.length();
if ( pCharacter && pCharacter->IsAddHeader() )
{
// 这里假设每次收到一个包,不会收到半包。(不知道服务器发多少字节,没有办法呀!)
WORD wThisMsgSize = sizeof(SMG_PK_HEADER) + (WORD)nThisLen;
wThisMsgSize = MIN(wThisMsgSize, MAX_SMG_PK_SIZE);
((SMG_PK_HEADER *)m_pRecvHeader)->m_wtype1 = SMG_PK_TYPE_GENERAL;
((SMG_PK_HEADER *)m_pRecvHeader)->m_wtype2 = wThisMsgSize;
((SMG_PK_HEADER *)m_pRecvHeader)->m_nPackSize = wThisMsgSize;
((SMG_PK_HEADER *)m_pRecvHeader)->m_nID = -1;
((SMG_PK_HEADER *)m_pRecvHeader)->m_nConnectionID = -1;
((SMG_PK_HEADER *)m_pRecvHeader)->m_nGlobalID = -1;
((SMG_PK_HEADER *)m_pRecvHeader)->m_aPara[0] = SMG_STOC_OTHER_NO_HEADER_RETURN;
memcpy( SMG_PK_DATA(m_pRecvHeader), mb.rd_ptr(), nThisLen );
m_nRecvHeaderLen = MAX_SMG_PK_SIZE; // 唉,开始胡乱搞了。
}
else
{
if ( m_nRecvHeaderLen + nThisLen > MAX_SMG_PK_SIZE * 2 )
{
OutputError( SMG_E_NULL, __FILE__, __LINE__, "怎么这么长!<%d>", m_nRecvHeaderLen + nThisLen );
nSuccess = 0; // 断开吧。
}
else
{
memcpy( m_pRecvHeader + m_nRecvHeaderLen, mb.rd_ptr(), nThisLen );
m_nRecvHeaderLen += nThisLen;
}
}
}
else
{
m_nRecvHeaderLen = 0;
}
mb.release ();
}
if ( nSuccess && nBytes_transferred != 0 )
{
if ( pCharacter )
{
if ( m_nRecvHeaderLen >= MAX_SMG_PK_SIZE )
{
pCharacter->AddRecvPack( m_pRecvHeader );
// 往前移动一个包
memcpy( m_pRecvHeader, m_pRecvHeader + MAX_SMG_PK_SIZE, m_nRecvHeaderLen - MAX_SMG_PK_SIZE );
m_nRecvHeaderLen -= MAX_SMG_PK_SIZE;
}
this->initiate_read_stream ();
return;
}
else
{
SMG_ASSERT(FALSE);
OutputError( SMG_E_NULL, __FILE__, __LINE__, "pCharacter 为空!" );
}
}
else
{
//TRACE("出错了,要关闭。\r\n");
}
}
//delete this;
SetInvalid();//这里就断开了!!!!是合理的么?如果不断开,有什么办法补救???另外为什么这里断开的很频繁??是跟网络状况有关么???
}
代码是别人写的,我自己对ACE也只是一知半解,勉强能看懂而已,希望各位大哥帮个忙,给些提示吧!很急的! |
|