Thread: [Gcblue-commits] gcb_wx/src/network tcChatMessageHandler.cpp,NONE,1.1 tcMultiplayerInterface.cpp,1.3
Status: Alpha
Brought to you by:
ddcforge
|
From: Dewitt C. <ddc...@us...> - 2004-04-05 02:37:03
|
Update of /cvsroot/gcblue/gcb_wx/src/network In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv4666/src/network Modified Files: tcMultiplayerInterface.cpp Added Files: tcChatMessageHandler.cpp Log Message: Index: tcMultiplayerInterface.cpp =================================================================== RCS file: /cvsroot/gcblue/gcb_wx/src/network/tcMultiplayerInterface.cpp,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** tcMultiplayerInterface.cpp 6 Mar 2004 20:52:29 -0000 1.3 --- tcMultiplayerInterface.cpp 5 Apr 2004 02:24:29 -0000 1.4 *************** *** 28,31 **** --- 28,33 ---- #include "network/tcNetworkInterface.h" #include "network/tcMultiplayerInterface.h" + #include "network/tcMessageHandler.h" + #include "network/tcChatMessageHandler.h" #include <iostream> #include <queue> *************** *** 38,47 **** * @return singleton instance */ ! tcMultiplayerInterface& tcMultiplayerInterface::Get() { static tcMultiplayerInterface instance; ! return instance; } std::string tcMultiplayerInterface::GetChatText() { --- 40,99 ---- * @return singleton instance */ ! tcMultiplayerInterface* tcMultiplayerInterface::Get() { static tcMultiplayerInterface instance; ! return &instance; } + /** + * Adds message handler for message with messageId. + * tcMessageHandler::Handle method will be called for any matching messages received + * Currently tcMultiplayerInterface deletes the message handlers--may want to change + * this such that creator deletes or use shared pointer. + */ + void tcMultiplayerInterface::AddMessageHandler(int messageId, tcMessageHandler* handler) + { + std::map<int, std::vector<tcMessageHandler*> >::iterator mapIter; + + mapIter = messageMap.find(messageId); + if (mapIter == messageMap.end()) + { + // create new map pair + std::vector<tcMessageHandler*> mm; + mm.push_back(handler); + messageMap[messageId] = mm; + } + else + { + // add handler to existing map pair + mapIter->second.push_back(handler); + } + } + + /** + * Clears messageMap, deleting all message handlers + */ + void tcMultiplayerInterface::ClearMessageMap() + { + std::map<int, std::vector<tcMessageHandler*> >::iterator mapIter; + + mapIter = messageMap.begin(); + + for (mapIter = messageMap.begin(); mapIter != messageMap.end(); + ++mapIter) + { + std::vector<tcMessageHandler*>& mm = mapIter->second; + size_t nHandlers = mm.size(); + for (size_t n = 0; n < nHandlers; n++) + { + wxASSERT(mm[n]); + delete mm[n]; + } + mm.clear(); + } + messageMap.clear(); + } + + std::string tcMultiplayerInterface::GetChatText() { *************** *** 110,133 **** } /** * Process single receive message for connection associated with connectionId */ ! void tcMultiplayerInterface::ProcessMessage(int connectionId, int messageId, unsigned messageSize, const unsigned char *data) { switch (messageId) { case MSG_CHATTEXT: ! chatText.push(std::string((char*)data)); ! if (networkInterface->IsServer()) ! { ! // broadcast chat text to all clients ! unsigned nConn = networkInterface->GetNumConnections(); ! for (unsigned n=0;n<nConn;n++) ! { ! int connId = networkInterface->GetConnectionId(n); ! SendChatText(connId, (char*)data); ! } ! } break; default: --- 162,214 ---- } + void tcMultiplayerInterface::ProcessChatMessage(int connectionId, + unsigned messageSize, const unsigned char *data) + { + chatText.push(std::string((char*)data)); + if (networkInterface->IsServer()) + { + // broadcast chat text to all clients + unsigned nConn = networkInterface->GetNumConnections(); + for (unsigned n=0;n<nConn;n++) + { + int connId = networkInterface->GetConnectionId(n); + SendChatText(connId, (char*)data); + } + } + } + /** * Process single receive message for connection associated with connectionId */ ! void tcMultiplayerInterface::ProcessMessage(int messageId, int connectionId, unsigned messageSize, const unsigned char *data) { + + std::map<int, std::vector<tcMessageHandler*> >::iterator mapIter; + + mapIter = messageMap.find(messageId); + if (mapIter == messageMap.end()) + { + fprintf(stderr, + "Warning - tcMultiplayerInterface::ProcessMessage unrecognized message ID (%d)\n", + messageId); + return; + } + + // call all registered message handlers for this message + std::vector<tcMessageHandler*>& mm = mapIter->second; + size_t nHandlers = mm.size(); + for (size_t n = 0; n < nHandlers; n++) + { + wxASSERT(mm[n]); + mm[n]->Handle(connectionId, messageSize, data); + } + + /* switch (messageId) { case MSG_CHATTEXT: ! ProcessChatMessage(connectionId, messageSize, data); ! break; default: *************** *** 136,139 **** --- 217,221 ---- break; } + */ } *************** *** 154,158 **** if (tcp_data != NULL) { ! ProcessMessage(connId, messageId, messageSize, tcp_data); } --- 236,240 ---- if (tcp_data != NULL) { ! ProcessMessage(messageId, connId, messageSize, tcp_data); } *************** *** 161,165 **** if (udp_data != NULL) { ! ProcessMessage(connId, messageId, messageSize, udp_data); } } --- 243,247 ---- if (udp_data != NULL) { ! ProcessMessage(messageId, connId, messageSize, udp_data); } } *************** *** 289,292 **** --- 371,377 ---- networkInterface = new tcNetworkInterface(); wxASSERT(networkInterface); + + // register chat text message handler + AddMessageHandler(MSG_CHATTEXT, new tcChatMessageHandler(chatText)); } *************** *** 301,304 **** --- 386,390 ---- tcMultiplayerInterface::~tcMultiplayerInterface() { + ClearMessageMap(); if (networkInterface) delete networkInterface; } --- NEW FILE: tcChatMessageHandler.cpp --- /** * Copyright (C) 2004 Dewitt "Cole" Colclough (de...@tw...) * All rights reserved. * * This file is part of the Global Conflict Blue (GCB) program. * GCB is free software; you can redistribute it and/or modify * it under the terms of version 2 of the GNU General Public License as * published by the Free Software Foundation. * * GCB is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with GCB; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /** @file tcChatMessageHandler.cpp */ #include "network/tcChatMessageHandler.h" #include "network/tcMultiplayerInterface.h" BEGIN_NAMESPACE(network) void tcChatMessageHandler::Handle(int connectionId, unsigned messageSize, const unsigned char *data) { chatText.push(std::string((char*)data)); tcMultiplayerInterface* multiplayerInterface = tcMultiplayerInterface::Get(); if (multiplayerInterface->IsServer()) { // broadcast chat text to all clients unsigned nConn = multiplayerInterface->GetNumConnections(); for (unsigned n=0;n<nConn;n++) { int connId = multiplayerInterface->GetConnectionId(n); multiplayerInterface->SendChatText(connId, (char*)data); } } } tcChatMessageHandler::tcChatMessageHandler(std::queue<std::string>& _chatText) : chatText(_chatText) { } tcChatMessageHandler::~tcChatMessageHandler() { } END_NAMESPACE |