[Gcblue-commits] gcb_wx/src/network tcControlMessageHandler.cpp,NONE,1.1 tcMessageHandler.cpp,NONE,1
Status: Alpha
Brought to you by:
ddcforge
Update of /cvsroot/gcblue/gcb_wx/src/network In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv25434/src/network Modified Files: tcMultiplayerInterface.cpp tcNetworkInterface.cpp Added Files: tcControlMessageHandler.cpp tcMessageHandler.cpp tcTextMessageHandler.cpp Removed Files: tcChatMessageHandler.cpp Log Message: --- NEW FILE: tcMessageHandler.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 tcMessageHandler.cpp */ #include "network/tcMessageHandler.h" BEGIN_NAMESPACE(network) bool tcMessageHandler::isServer = false; void tcMessageHandler::SetAsServer() { isServer = true; } void tcMessageHandler::SetAsClient() { isServer = false; } END_NAMESPACE --- tcChatMessageHandler.cpp DELETED --- --- NEW FILE: tcControlMessageHandler.cpp --- /** * @file tcControlMessageHandler.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 */ #include "stdwx.h" // precompiled header file #ifndef WX_PRECOMP #include "wx/wx.h" #endif #include "wx/string.h" #include "network/tcControlMessageHandler.h" #include "network/tcMultiplayerInterface.h" BEGIN_NAMESPACE(network) void tcControlMessageHandler::CreateControlMessage(int messageCode, unsigned& messageSize, unsigned char *data) { switch (messageCode) { case CM_BEEP: case CM_STARTGAME: { messageSize = sizeof(messageCode); memcpy(data, &messageCode, messageSize); return; } break; default: { fprintf(stderr, "tcControlMessageHandler::CreateControlMessage - unrecognized message code\n"); break; } } } void tcControlMessageHandler::Handle(int connectionId, unsigned messageSize, const unsigned char *data) { // server ignores control messages if (isServer) { fprintf(stderr, "Warning - control message received by server\n"); return; } int messageCode; if (messageSize <= sizeof(messageCode)) { fprintf(stderr, "Warning - Empty or short control message received by server\n"); return; } memcpy(&messageCode, data, sizeof(messageCode)); wxMessageBox(wxString::Format("Received control message %d", messageCode), "Msg received", wxOK); } tcControlMessageHandler::tcControlMessageHandler() { } tcControlMessageHandler::~tcControlMessageHandler() { } END_NAMESPACE --- NEW FILE: tcTextMessageHandler.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 tcTextMessageHandler.cpp */ #include "network/tcTextMessageHandler.h" #include "network/tcMultiplayerInterface.h" BEGIN_NAMESPACE(network) /** * Creates text message. * @param maxSize maximum size of string to send not including terminating null */ void tcTextMessageHandler::CreateMessage(unsigned& messageSize, unsigned char *data, std::string text, unsigned maxSize) { messageSize = (unsigned int)text.length(); if (messageSize > maxSize) messageSize = maxSize; strncpy((char*)data, text.c_str(), messageSize); data[messageSize++] = 0; // append terminating null } void tcTextMessageHandler::Handle(int connectionId, unsigned messageSize, const unsigned char *data) { std::string text((char*)data); tcMultiplayerInterface* multiplayerInterface = tcMultiplayerInterface::Get(); if (multiplayerInterface->IsServer()) { // create string with name prepended to text to identify source tcPlayerStatus pstatus = multiplayerInterface->GetPlayerStatus(connectionId); std::string namedText = std::string("<") + pstatus.name + std::string("> ") + text; // check if this is a text command if (multiplayerInterface->IsCommand(text)) { multiplayerInterface->ProcessCommandClient(connectionId, text); } else { // 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, namedText); } } text = namedText; } chatText.push(text); } tcTextMessageHandler::tcTextMessageHandler(std::queue<std::string>& _chatText) : chatText(_chatText) { } tcTextMessageHandler::~tcTextMessageHandler() { } END_NAMESPACE Index: tcMultiplayerInterface.cpp =================================================================== RCS file: /cvsroot/gcblue/gcb_wx/src/network/tcMultiplayerInterface.cpp,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** tcMultiplayerInterface.cpp 5 Apr 2004 02:24:29 -0000 1.4 --- tcMultiplayerInterface.cpp 7 Apr 2004 00:41:08 -0000 1.5 *************** *** 1,6 **** ! /** @file tcNetworkInterface.cpp */ ! /** tcMultiplayerInterface.cpp */ ! /* ! ** Copyright (C) 2003 Dewitt "Cole" Colclough (de...@tw...) ** All rights reserved. --- 1,5 ---- ! /** @file tcMultiplayerInterface.cpp ! ** ! ** Copyright (C) 2003-2004 Dewitt "Cole" Colclough (de...@tw...) ** All rights reserved. *************** *** 29,33 **** #include "network/tcMultiplayerInterface.h" #include "network/tcMessageHandler.h" ! #include "network/tcChatMessageHandler.h" #include <iostream> #include <queue> --- 28,33 ---- #include "network/tcMultiplayerInterface.h" #include "network/tcMessageHandler.h" ! #include "network/tcTextMessageHandler.h" ! #include "network/tcControlMessageHandler.h" #include <iostream> #include <queue> *************** *** 72,75 **** --- 72,90 ---- /** + * Broadcast control message to all connected clients + */ + void tcMultiplayerInterface::BroadcastControlMessage(int messageCode) + { + unsigned nConnections = GetNumConnections(); + + for (unsigned n=0;n<nConnections;n++) + { + int connId = GetConnectionId(n); + SendControlMessage(connId, messageCode); + } + + } + + /** * Clears messageMap, deleting all message handlers */ *************** *** 114,117 **** --- 129,140 ---- } + /** + * @return identification name string for player using this interface + */ + std::string tcMultiplayerInterface::GetName() const + { + return myName; + } + unsigned tcMultiplayerInterface::GetNumConnections() { *************** *** 119,122 **** --- 142,186 ---- } + /** + * @return tcPlayerStatus object associated with connectionId + * @see tcPlayerStatus + */ + tcPlayerStatus& tcMultiplayerInterface::GetPlayerStatus(int connectionId) + { + std::map<int,tcPlayerStatus>::iterator mapIter; + + mapIter = playerInfo.find(connectionId); + if (mapIter == playerInfo.end()) + { + fprintf(stderr, "Error - tcMultiplayerInterface::GetPlayerStatus - " + "conn id: %d not found\n", connectionId); + return errorPlayerStatus; + } + + return mapIter->second; + } + + /** + * Clear message map and (re)initialize based on + * multiplayer mode. + */ + void tcMultiplayerInterface::InitMessageHandlers() + { + ClearMessageMap(); + + // common handlers + // register chat text message handler + AddMessageHandler(MSG_CHATTEXT, new tcTextMessageHandler(chatText)); + AddMessageHandler(MSG_CONTROL, new tcControlMessageHandler()); + + if (IsServer()) + { // server-specific handlers + } + else + { // client-specific handlers + + } + } + bool tcMultiplayerInterface::IsChatTextAvail() { *************** *** 124,127 **** --- 188,202 ---- } + /** + * Tests if text is a command. Commands start with a + * forward slash '/' + * @return true if text is a (server) command + */ + bool tcMultiplayerInterface::IsCommand(std::string text) + { + wxString candidate(text.c_str()); + return ((candidate.find('/') == 0)); + } + bool tcMultiplayerInterface::IsNewPlayer(int id) { *************** *** 150,153 **** --- 225,230 ---- { networkInterface->MakeClient(); + tcMessageHandler::SetAsClient(); + InitMessageHandlers(); } *************** *** 155,158 **** --- 232,237 ---- { networkInterface->MakeServer(); + tcMessageHandler::SetAsServer(); + InitMessageHandlers(); } *************** *** 178,181 **** --- 257,339 ---- } + + + /** + * Process client command + * text commands start with a forward slash '/' + * @return string to send back to client + */ + void tcMultiplayerInterface::ProcessCommandClient(int connectionId, std::string text) + { + wxString candidate(text.c_str()); + + SendChatText(connectionId, text); + + wxString commandLine = candidate.AfterFirst('/'); + wxString command = commandLine.BeforeFirst(' '); + wxString args = commandLine.AfterFirst(' '); + + /* replace this with a std::map<std::string, handle> registry system + ** when it outgrows this switch */ + if (command == "test") + { + chatText.push(std::string("*** test command from client ***")); + SendChatText(connectionId, "*** test command acknowledged ***"); + } + else if (command == "help") + { + SendChatText(connectionId, "*** Help ***"); + SendChatText(connectionId, " /help - print command list"); + SendChatText(connectionId, " /test - dummy test command"); + } + else if (command == "name") + { + wxString updatedName = args; + tcPlayerStatus& pstatus = GetPlayerStatus(connectionId); + pstatus.name = updatedName.c_str(); + SendChatText(connectionId, "*** set name acknowledged ***"); + } + else + { + SendChatText(connectionId, "*** unrecognized command ***"); + } + } + + /** + * Process server command + * text commands start with a forward slash '/' + */ + void tcMultiplayerInterface::ProcessCommandServer(std::string text) + { + wxASSERT(IsServer()); + wxString candidate(text.c_str()); + + wxString command = candidate.AfterFirst('/'); + /* replace this with a std::map<std::string, handle> registry system + ** when it outgrows this switch */ + if (command == "startgame") + { + chatText.push(std::string("*** sending STARTGAME ***")); + BroadcastControlMessage(tcControlMessageHandler::CM_STARTGAME); + } + else if (command == "test") + { + chatText.push(std::string("*** test command ***")); + } + else if (command == "help") + { + chatText.push(std::string("*** Help ***")); + chatText.push(std::string(" /help - print command list")); + chatText.push(std::string(" /startgame - starts the game")); + chatText.push(std::string(" /test - dummy test command")); + } + else + { + chatText.push(std::string("*** unrecognized command ***")); + tcSound::Get()->PlayEffect("intercom"); + } + + } + /** * Process single receive message for connection associated with connectionId *************** *** 255,272 **** { char buff[256]; int protocol = tcpChat ? tcNetworkInterface::TCP : tcNetworkInterface::UDP; ! size_t messageLength = message.length(); ! if (messageLength > 255) messageLength = 255; ! strncpy(buff, message.c_str(), messageLength); ! buff[messageLength] = 0; ! networkInterface->SendMessage(destination, MSG_CHATTEXT, ! (unsigned)(messageLength+1), (unsigned char*)buff, ! protocol); ! /** networkInterface->SendMessage(destination, MSG_CHATTEXT, ! (unsigned)(messageLength+1), (unsigned char*)buff); ! */ } --- 413,441 ---- { char buff[256]; + unsigned messageLength; int protocol = tcpChat ? tcNetworkInterface::TCP : tcNetworkInterface::UDP; ! tcTextMessageHandler::CreateMessage(messageLength, (unsigned char*)buff, message, 255); ! networkInterface->SendMessage(destination, MSG_CHATTEXT, ! messageLength, (unsigned char*)buff, protocol); ! } ! ! /** ! * Send control message to destination ! */ ! void tcMultiplayerInterface::SendControlMessage(int destination, int messageCode) ! { ! unsigned char data[64]; ! unsigned messageSize; ! ! tcControlMessageHandler::CreateControlMessage(tcControlMessageHandler::CM_STARTGAME, ! messageSize, ! data); ! networkInterface->SendMessage(destination, MSG_CONTROL, ! (messageSize+1), data, ! tcNetworkInterface::TCP); ! } *************** *** 297,300 **** --- 466,477 ---- /** + * Sets identification name string for player using this interface + */ + void tcMultiplayerInterface::SetName(const std::string &name) + { + myName = name; + } + + /** * This must be called regularly to perform network functions. * (avoids need for multithreadeding) *************** *** 331,334 **** --- 508,512 ---- tcPlayerStatus stat; stat.timestamp = tcTime::Get30HzCount(); + stat.name = "anonymous"; playerInfo[connId] = stat; tcSound::Get()->PlayEffect("intercom"); *************** *** 367,377 **** tcMultiplayerInterface::tcMultiplayerInterface() ! : tcpChat(true) { networkInterface = new tcNetworkInterface(); wxASSERT(networkInterface); ! // register chat text message handler ! AddMessageHandler(MSG_CHATTEXT, new tcChatMessageHandler(chatText)); } --- 545,555 ---- tcMultiplayerInterface::tcMultiplayerInterface() ! : tcpChat(true), myName("Somebody") { networkInterface = new tcNetworkInterface(); wxASSERT(networkInterface); ! errorPlayerStatus.name = "error"; ! errorPlayerStatus.timestamp = 0; } Index: tcNetworkInterface.cpp =================================================================== RCS file: /cvsroot/gcblue/gcb_wx/src/network/tcNetworkInterface.cpp,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** tcNetworkInterface.cpp 4 Apr 2004 21:00:43 -0000 1.7 --- tcNetworkInterface.cpp 7 Apr 2004 00:41:08 -0000 1.8 *************** *** 51,55 **** else { ! cdata.idString = wxString::Format("%s:%d",addr.Hostname().c_str(), addr.Service()); } --- 51,56 ---- else { ! // cdata.idString = wxString::Format("%s:%d",addr.Hostname().c_str(), addr.Service()); ! cdata.idString = wxString::Format("%s",addr.IPAddress().c_str()); } *************** *** 400,403 **** --- 401,408 ---- } + /** + * Opens connection with server specified by hostName. + * @param hostName server address--can be a host name or an IP-style address in dot notation (a.b.c.d) + */ void tcNetworkInterface::OpenConnection(wxString hostName) { *************** *** 685,691 **** { connectState = NOT_CONNECTED; ! fprintf(stdout, "Connection refused %s : %d\n", ! hostAddress.Hostname().c_str(), hostAddress.Service()); ! std::cerr << "Error - Connection refused." << std::endl; return; } --- 690,696 ---- { connectState = NOT_CONNECTED; ! ! fprintf(stderr, "Error - Connection with %s refused.\n", ! hostAddress.IPAddress()); return; } *************** *** 720,724 **** connectionData[n].socket->GetPeer(peerAddr); ! std::string hostName = peerAddr.Hostname().c_str(); peerMap[hostName] = (int)n; } --- 725,730 ---- connectionData[n].socket->GetPeer(peerAddr); ! //std::string hostName = peerAddr.Hostname().c_str(); ! std::string hostName = peerAddr.IPAddress().c_str(); peerMap[hostName] = (int)n; } |