bool GameServer::SendPacket(ClientConnection* pConn, BasePacket* p, bool bDontCheckSessionId /*= false*/){ if( !pConn ) return false; ASSERT(p); Session* pSess = pConn->GetSession(false); if( pSess ){ p->m_nSessionId = pSess->GetSessionId(); // Session Id } else{ if( !bDontCheckSessionId ) return false; p->m_nSessionId = 0; // Session Id } // m_lockSendPacket.Lock(); // Lock pConn->SetSendingStatus(true); // Set connection sending status bool bRet = false; m_serializeMem.ZeroUsedMemory(); p->m_nPacketSize = 0xFFFF; if( p->Serialize(&m_serializeMem, &m_arrVariableOffset) ){ // Update packet size value in buffer.{{ void* lpBuffer = m_serializeMem.GetBufferPtr(); int nOffsetPacketSize = (int)m_arrVariableOffset.GetData(0); *(int*)(&((BYTE*)lpBuffer)[nOffsetPacketSize]) = m_serializeMem.GetUsedBufferSize(); // }} bRet = pConn->Send(&m_serializeMem); } pConn->SetSendingStatus(false); // m_lockSendPacket.Unlock(); // Unlock return bRet; }
/** Creates a new session */ SocketDwarf::Server::Session * SocketDwarf::Server::DwarfServer::CreateSession (struct mg_connection * clientConnection) { const struct mg_request_info * requestInfo = mg_get_request_info(clientConnection); std::map<std::string, std::string> headerValues (ExtractHeaderValues(requestInfo)); std::map<std::string, std::string>::const_iterator citWebSocketProtocol = headerValues.find("Sec-WebSocket-Protocol"); if (citWebSocketProtocol == headerValues.end()) { throw std::runtime_error("Sec-WebSocket-Protocol header not defined"); } Session * session = new Session (clientConnection, citWebSocketProtocol->second); sessions.insert(sessions.begin(), std::make_pair(session->GetSessionId(), std::unique_ptr<Session>(session))); session->Start(); return session; }
void GameServer::OnHandShake(HandShakePacket* p, void* pParam){ ClientConnection* pConn = (ClientConnection*)pParam; if( p->m_nProtocolVer != PROTOCOL_VER ){ p->m_nErrorCode = ES_ERROR_PROTOCOL_VERSION_MISMATCH; p->m_nSessionId = 0; p->m_sError = _T(""); SendPacket(pConn, p, true); // Reply packet. return; } Session* pSess = CreateOrRestoreSession(pConn, p->m_nSessionId); if( !pSess ) return; p->m_nSessionId = pSess->GetSessionId(); p->m_sError = _T(""); p->m_nErrorCode = 0; // Success SendPacket(pConn, p, true); // Reply packet. SafePointer::Release(pSess); // Release session. }