void CTcpSocket::OnSend( const system::error_code &ec, size_t nByteTransferred ) { if (ec) { LOGError(ec.message().c_str()); DoClose(); return ; } m_wSendLength -= nByteTransferred; if (m_wSendLength > 0) { LOGError("理论来讲,这里是进不来的!"); // 理论来讲,这里是进不来的。 memcpy(m_szSendBuffer, m_szSendBuffer + nByteTransferred, m_wSendLength); // 发送数据 m_pSock->async_write_some(buffer(m_szSendBuffer, m_wSendLength), bind(&CTcpSocket::OnSend, this, placeholders::error, placeholders::bytes_transferred)); } else if (0 == m_wSendLength) { // 拷贝数据 std::list<MsgData>::iterator it = m_listMsgData.begin(); for (; it!=m_listMsgData.end(); ) { MsgData &msgData = (*it); if (MAX_SEND_BUFFER_LENGTH - m_wSendLength >= msgData.wLength) { USHORT *pPacketLength = (USHORT *)m_szSendBuffer; *pPacketLength = msgData.wLength; m_wSendLength += sizeof(USHORT); memcpy(m_szSendBuffer + m_wSendLength, msgData.szData, msgData.wLength); m_wSendLength += msgData.wLength; it = m_listMsgData.erase(it); } else { break; } } // 发送数据 m_pSock->async_write_some(buffer(m_szSendBuffer, m_wSendLength), bind(&CTcpSocket::OnSend, this, placeholders::error, placeholders::bytes_transferred)); } else { LOGError("异常错误!"); } }
bool CTcpSocket::DoSend( MsgData msgData ) { if (m_wSendLength > 0) { // 正在发送数据 m_listMsgData.push_back(msgData); return true; } else { // 拷贝数据并发送 if (msgData.wLength > MAX_SEND_BUFFER_LENGTH) { LOGError("发送数据长度越界。"); return false; } USHORT *pPacketLength = (USHORT *)m_szSendBuffer; *pPacketLength = msgData.wLength; m_wSendLength += sizeof(USHORT); memcpy(m_szSendBuffer + m_wSendLength, msgData.szData, msgData.wLength); m_wSendLength += msgData.wLength; // 发送数据 m_pSock->async_write_some(buffer(m_szSendBuffer, m_wSendLength), bind(&CTcpSocket::OnSend, this, placeholders::error, placeholders::bytes_transferred)); return true; } }
bool ICountry::SendMessage(unsigned short wProtocolId, google::protobuf::Message *pMessage) { if (nullptr == m_pClient) { LOGError("nullptr == m_pClient"); return false; } return m_pClient->SendMessage(wProtocolId, pMessage); }
bool ICountry::AddPrepareGObject(IGObject *pFightGObject) { if (nullptr == pFightGObject) { LOGError("nullptr == pFightGObject"); return false; } m_vtPrepareGObject.push_back(pFightGObject); return true; }
// 消息发送(广播) bool ICountry::SendGetGroundInfo(int nWGCount, int nHGCount, const MapGrid &mapGrid, const MapGObject &mapGObject) { gproto::MSG_G2C_GetGroundInfo msg; msg.set_wgcount(nWGCount); msg.set_hgcount(nHGCount); auto itGrid = mapGrid.begin(); for (; itGrid != mapGrid.end(); ++itGrid) { gproto::Info_Grid *pInfoGrid = msg.add_grids(); if (pInfoGrid) { pInfoGrid->set_sn(itGrid->second->GetSN()); pInfoGrid->set_x(itGrid->second->GetX()); pInfoGrid->set_y(itGrid->second->GetY()); } } auto itGObject = mapGObject.begin(); for ( ; itGObject != mapGObject.end(); ++itGObject) { gproto::Info_GObject *pInfoGObject = msg.add_gobjects(); if (pInfoGObject) { if (GObjectType_Still == itGObject->second->GetType()) { pInfoGObject->set_type(gproto::Info_GObject_EType_Still); } else if (GObjectType_Walkable == itGObject->second->GetType()) { pInfoGObject->set_type(gproto::Info_GObject_EType_Walkable); } else { pInfoGObject->set_type(gproto::Info_GObject_EType_None); LOGError("场景对象类型错误:" + static_cast<int>(itGObject->second->GetType())); } pInfoGObject->set_indexid(itGObject->second->GetIndexId()); pInfoGObject->set_sn(itGObject->second->GetSN()); pInfoGObject->set_x(itGObject->second->GetX()); pInfoGObject->set_y(itGObject->second->GetY()); pInfoGObject->set_hp(itGObject->second->GetHP()); pInfoGObject->set_maxhp(itGObject->second->GetMaxHP()); pInfoGObject->set_sp(itGObject->second->GetSP()); pInfoGObject->set_level(itGObject->second->GetLevel()); pInfoGObject->set_campid(itGObject->second->GetCampId()); } } return SendMessage(gproto::CSID_G2C_GetGroundInfo, &msg); }
ICountry * CCountryManager::CreateCountry() { ICountry *pNetCountry = new CCountry(); if (nullptr == pNetCountry) { LOGError("nullptr == pNetCountry"); return false; } m_mapCountry.insert(std::make_pair(pNetCountry->GetIndexId(), pNetCountry)); return pNetCountry; }
bool CTcpSocket::DoRecv() { char *pRecvBuffer = m_szRecvBuffer + m_wHaveRecvLength; USHORT wFreeRecvLength = MAX_RECV_BUFFER_LENGTH - m_wHaveRecvLength; if (0 == wFreeRecvLength) { LOGError("单个消息包过长,断开连接。"); // 单个消息包过长,断开连接 DoClose(); return false; } m_pSock->async_read_some(buffer(pRecvBuffer, wFreeRecvLength), bind(&CTcpSocket::OnRecv, this, placeholders::error, placeholders::bytes_transferred)); return true; }
bool CCountryManager::PushPrepareList(ICountry *pCountry) { if (nullptr == pCountry) { LOGError("nullptr == pCountry"); return false; } for (ICountry *pTmpCountry : m_listCountry) { if (pTmpCountry == pCountry) { // 已经存在 return false; } } m_listCountry.push_back(pCountry); return true; }
void CTcpSocket::OnRecv( const system::error_code& ec, size_t nByteTransferred ) { if (ec) { LOGError(ec.message().c_str()); DoClose(); return ; } m_wHaveRecvLength += nByteTransferred; // 黏包解析 USHORT wReadLength = 0; //解析长度修改都是完整消息包 while (true) { const char *pPacketHead = m_szRecvBuffer + wReadLength; USHORT wRemainLength = m_wHaveRecvLength - wReadLength; USHORT wReadPacketLength = ReadPacket(pPacketHead, wRemainLength); if (wReadPacketLength > 0) { wReadLength += wReadPacketLength; } else { if ((wRemainLength > 0) && (wRemainLength != m_wHaveRecvLength)) { memcpy(m_szRecvBuffer, m_szRecvBuffer+wReadLength, wRemainLength); } m_wHaveRecvLength = wRemainLength; break; } } DoRecv(); }
void ICountry::EnterGround() { if (0 == m_nPrepareBattleGroundIndexId) { SendEnterGround(false); return ; } if (nullptr != m_pBattleGround) { SendEnterGround(false); LOGDebug("Country[CountryIndexId:" + GetIndexId() + "]在场景中[battleGroundIndexId:" + m_pBattleGround->GetIndexId() + "]。"); return ; } IBattleGround *pBattleGround = CBattleGroundManager::getMe().FindBattleGround(m_nPrepareBattleGroundIndexId); if (nullptr == pBattleGround) { SendEnterGround(false); LOGError("BattleGround[" + m_nPrepareBattleGroundIndexId + "]不存在。"); return ; } if (pBattleGround->Enter(this)) { m_nPrepareBattleGroundIndexId = 0; SendEnterGround(true); } else { SendEnterGround(false); } }
void TextureManager::Tick() { // Initialize all FBOs { CPPListNode* pNextNode; for( CPPListNode* pNode = m_UninitializedFBOs.GetHead(); pNode != 0; pNode = pNextNode ) { pNextNode = pNode->GetNext(); FBODefinition* pFBODef = (FBODefinition*)pNode; if( pFBODef->m_FailedToInit ) continue; bool success = pFBODef->Create(); if( success ) { LOGInfo( LOGTag, "pFBODef->Create() succeeded\n" ); //g_pPanelMemory->AddTexture( pFBODef ); m_InitializedFBOs.MoveTail( pFBODef ); pFBODef->m_FullyLoaded = true; } else { pFBODef->m_FailedToInit = true; LOGError( LOGTag, "========================\n" ); LOGError( LOGTag, "pFBODef->Create() failed\n" ); LOGError( LOGTag, "========================\n" ); } } } //// debug: list all textures that need loading. //for( CPPListNode* pNode = m_TexturesStillLoading.GetHead(); pNode; pNode = pNode->GetNext() ) //{ // TextureDefinition* pTextureDef = (TextureDefinition*)pNode; // LOGInfo( LOGTag, "Still need to load: %s\n", pTextureDef->m_Filename ); //} int texturesloadedthistick = 0; CPPListNode* pNextNode; for( CPPListNode* pNode = m_TexturesStillLoading.GetHead(); pNode != 0; pNode = pNextNode ) { pNextNode = pNode->GetNext(); if( m_MaxTexturesToLoadInOneTick != -1 && texturesloadedthistick >= m_MaxTexturesToLoadInOneTick ) break; texturesloadedthistick++; TextureDefinition* pTextureDef = (TextureDefinition*)pNode; //LOGInfo( LOGTag, "Loading Texture: %s\n", pTextureDef->m_Filename ); // if we have an opengl texture, then nothing to do. this shouldn't happen, loaded textures should be in "m_LoadedTextures". MyAssert( pTextureDef->m_TextureID == 0 ); if( pTextureDef->m_TextureID != 0 ) { LOGInfo( LOGTag, "Loading Texture: Already had a texture id?!? pTextureDef->m_TextureID != 0\n" ); continue; } bool textureloaded = false; #if 0 //MYFW_ANDROID //LOGInfo( LOGTag, "Loading Texture: pTextureDef->m_pFile %d\n", pTextureDef->m_pFile ); if( pTextureDef->m_pFile == 0 ) { pTextureDef->m_pFile = RequestTexture( pTextureDef->m_Filename, pTextureDef ); textureloaded = true; } else { LOGInfo( LOGTag, "Loading Texture: calling Android_LoadTextureFromMemory\n" ); pTextureDef->m_TextureID = Android_LoadTextureFromMemory( pTextureDef ); textureloaded = true; } #else // if the file load hasn't started... start the file load. if( pTextureDef->m_pFile == 0 ) { //LOGInfo( LOGTag, "Loading Texture: RequestFile\n" ); pTextureDef->m_pFile = RequestFile( pTextureDef->m_Filename ); //LOGInfo( LOGTag, "Loading Texture: ~RequestFile\n" ); } else { // if the file is ready, create an opengl texture from it. if( pTextureDef->m_pFile->m_FileLoadStatus == FileLoadStatus_Success ) { //LOGInfo( LOGTag, "Loading Texture: pTextureDef->m_pFile->m_FileReady\n" ); pTextureDef->m_TextureID = CreateTextureFromBuffer( pTextureDef ); //LOGInfo( LOGTag, "Loading Texture: CreateTextureFromBuffer\n" ); if( pTextureDef->m_TextureID != 0 ) { //LOGInfo( LOGTag, "Loading Texture: textureloaded = true\n" ); textureloaded = true; } } if( pTextureDef->m_pFile->m_FileLoadStatus > FileLoadStatus_Success ) { LOGError( LOGTag, "File load failed %s\n", pTextureDef->m_Filename ); SAFE_RELEASE( pTextureDef ); } } #endif if( textureloaded ) { LOGInfo( LOGTag, "textureloaded %s\n", pTextureDef->m_Filename ); // by default, we don't free the texture from main ram, so if we free the opengl tex, we can "reload" quick. if( pTextureDef->QueryFreeWhenCreated() ) g_pFileManager->FreeFile( pTextureDef->m_pFile ); m_LoadedTextures.MoveTail( pTextureDef ); pTextureDef->m_FullyLoaded = true; #if MYFW_USING_WX g_pPanelMemory->AddTexture( pTextureDef, "Global", pTextureDef->m_Filename, TextureDefinition::StaticOnDrag ); #endif LOGInfo( LOGTag, "pTextureDef->m_FullyLoaded = true %s\n", pTextureDef->m_Filename ); } } }
void CCountryManager::OnMatched(ICountry *pCountryA, ICountry *pCountryB) { LOGDebug("两个国家(部队)匹配成功。"); if (nullptr == pCountryA || nullptr == pCountryB) { LOGError("nullptr == pCountryA || nullptr == pCountryB"); return ; } // 创建场景 CFrontBattleGround *pNewFrontBattleGround = CBattleGroundManager::getMe().CreateFrontBattleGround(); if (nullptr == pNewFrontBattleGround) { LOGError("nullptr == pNewBattleGround"); return ; } // 测试,给双方添加部队 { int nCampId = pCountryA->GetCampId(); VtGObject vtGObject; // 添加将领 CStillObject*pSirdar = new CStillObject(); if (pSirdar) { pSirdar->Init(4); pSirdar->SetLevel(1); pSirdar->SetCampId(nCampId); vtGObject.push_back(pSirdar); } // 添加英雄 CWalkableObject *pHeroA = new CWalkableObject(); if (pHeroA) { pHeroA->Init(2); pHeroA->SetLevel(1); pHeroA->SetCampId(nCampId); vtGObject.push_back(pHeroA); } CWalkableObject *pHeroB = new CWalkableObject(); if (pHeroB) { pHeroB->Init(3); pHeroB->SetLevel(1); pHeroB->SetCampId(nCampId); vtGObject.push_back(pHeroB); } // 添加小兵 CWalkableObject *pDogFaceA = new CWalkableObject(); if (pDogFaceA) { pDogFaceA->Init(1); pDogFaceA->SetLevel(1); pDogFaceA->SetCampId(nCampId); vtGObject.push_back(pDogFaceA); } CWalkableObject *pDogFaceB = new CWalkableObject(); if (pDogFaceB) { pDogFaceB->Init(1); pDogFaceB->SetLevel(1); pDogFaceB->SetCampId(nCampId); vtGObject.push_back(pDogFaceB); } pCountryA->ClearFightGObject(); pCountryA->AddPrepareGObject(vtGObject); } { int nCampId = pCountryB->GetCampId(); VtGObject vtGObject; // 添加将领 CStillObject*pSirdar = new CStillObject(); if (pSirdar) { pSirdar->Init(4); pSirdar->SetLevel(1); pSirdar->SetCampId(nCampId); vtGObject.push_back(pSirdar); } // 添加英雄 CWalkableObject *pHeroA = new CWalkableObject(); if (pHeroA) { pHeroA->Init(2); pHeroA->SetLevel(1); pHeroA->SetCampId(nCampId); vtGObject.push_back(pHeroA); } CWalkableObject *pHeroB = new CWalkableObject(); if (pHeroB) { pHeroB->Init(3); pHeroB->SetLevel(1); pHeroB->SetCampId(nCampId); vtGObject.push_back(pHeroB); } // 添加小兵 CWalkableObject *pDogFaceA = new CWalkableObject(); if (pDogFaceA) { pDogFaceA->Init(1); pDogFaceA->SetLevel(1); pDogFaceA->SetCampId(nCampId); vtGObject.push_back(pDogFaceA); } CWalkableObject *pDogFaceB = new CWalkableObject(); if (pDogFaceB) { pDogFaceB->Init(1); pDogFaceB->SetLevel(1); pDogFaceB->SetCampId(nCampId); vtGObject.push_back(pDogFaceB); } pCountryB->ClearFightGObject(); pCountryB->AddPrepareGObject(vtGObject); } // 准备场景 pCountryA->PrepareGround(pNewFrontBattleGround->GetIndexId()); pCountryB->PrepareGround(pNewFrontBattleGround->GetIndexId()); }