[Gcblue-commits] gcb_wx/src/network tcUpdateMessageHandler.cpp,NONE,1.1 tcMultiplayerInterface.cpp,1
Status: Alpha
Brought to you by:
ddcforge
|
From: Dewitt C. <ddc...@us...> - 2004-04-13 00:38:34
|
Update of /cvsroot/gcblue/gcb_wx/src/network In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv27636/src/network Modified Files: tcMultiplayerInterface.cpp Added Files: tcUpdateMessageHandler.cpp Log Message: Multiplayer related updates --- NEW FILE: tcUpdateMessageHandler.cpp --- /** * @file tcUpdateMessageHandler.cpp * * Copyright (C) 2004 Dewitt 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 "wx/event.h" #include "network/tcUpdateMessageHandler.h" #include "network/tcMultiplayerInterface.h" #include "tcSimState.h" #include "wxcommands.h" #include "common/tcStream.h" BEGIN_NAMESPACE(network) /** * Add update for game object to stream */ void tcUpdateMessageHandler::AddUpdate(tcGameObject* obj, tcStream& stream) { stream << obj->mnID; // write id stream << obj->mnDBKey; // write database key (to support object creation) // write obj state to stream *obj >> stream; } void tcUpdateMessageHandler::Handle(int connectionId, unsigned messageSize, const unsigned char *data) { // server ignores update messages if (isServer) { fprintf(stderr, "Warning - update message received by server\n"); return; } tcSimState* simState = tcSimState::Get(); wxASSERT(simState); tcDatabase* database = simState->mpDatabase; // convert database to singleton eventually tcStream stream((const char*)data, messageSize); fprintf(stdout, "Updating objs, time %.1f: ", simState->GetTime()); long id; long databaseId = -1; while ((stream >> id).eof() == false) { stream >> databaseId; // lookup obj tcGameObject* obj = simState->GetObject(id); // update obj if it exists, otherwise create object if (obj) { *obj << stream; fprintf(stdout, "%d ", id); } else { tcDatabaseObject* dbObj = database->GetObject(databaseId); if (dbObj) { obj = simState->CreateGameObject(dbObj); *obj << stream; simState->AddPlatformWithKey(obj, id); fprintf(stdout, "%d (NEW) ", id); } else { std::cerr << "Bad update, database entry not found\n"; return; } } } fprintf(stdout, "\n"); } tcUpdateMessageHandler::tcUpdateMessageHandler() { } tcUpdateMessageHandler::~tcUpdateMessageHandler() { } END_NAMESPACE Index: tcMultiplayerInterface.cpp =================================================================== RCS file: /cvsroot/gcblue/gcb_wx/src/network/tcMultiplayerInterface.cpp,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** tcMultiplayerInterface.cpp 9 Apr 2004 03:08:08 -0000 1.6 --- tcMultiplayerInterface.cpp 13 Apr 2004 00:24:39 -0000 1.7 *************** *** 30,37 **** --- 30,41 ---- #include "network/tcTextMessageHandler.h" #include "network/tcControlMessageHandler.h" + #include "network/tcUpdateMessageHandler.h" + #include "common/tcStream.h" #include <iostream> #include <queue> #include "tcSound.h" #include "tcTime.h" + #include "tcSimState.h" + #include "tcGameObjIterator.h" BEGIN_NAMESPACE(network) *************** *** 185,189 **** else { // client-specific handlers ! } } --- 189,193 ---- else { // client-specific handlers ! AddMessageHandler(MSG_UPDATE, new tcUpdateMessageHandler()); } } *************** *** 456,459 **** --- 460,476 ---- } + void tcMultiplayerInterface::SendUpdateMessage(int destination, tcStream& stream) + { + size_t streamSize = stream.size(); + char* buffer = new char[streamSize]; + stream.read(buffer, streamSize); + + networkInterface->SendMessage(destination, MSG_UPDATE, streamSize, (unsigned char*)buffer, + tcNetworkInterface::TCP); + + delete buffer; + + } + /** * 0 - UDP, otherwise - TCP *************** *** 498,504 **** --- 515,568 ---- ProcessReceiveMessages(); + // update player information UpdatePlayerInfo(); + + UpdateObjects(); } + /** + * Server only for now, send state updates on objects to clients + */ + void tcMultiplayerInterface::UpdateObjects() + { + static double lastUpdate = -1; + + if (!IsServer()) return; + + tcSimState* simState = tcSimState::Get(); + wxASSERT(simState); + + double t = simState->GetTime(); + if (t <= lastUpdate + 2.0) return; + + lastUpdate = t; + + // iterate through all game objects and build update stream + tcStream stream; + tcGameObjIterator iter; + int idx = 0; + for (iter.First();iter.NotDone();iter.Next()) + { + tcGameObject* obj = iter.Get(); + if (simState->mpUserInfo->IsOwnAlliance(obj->mnAlliance)) + { + tcUpdateMessageHandler::AddUpdate(obj, stream); + } + } + + // send update message to all clients + unsigned nConnections = GetNumConnections(); + + for (unsigned n=0;n<nConnections;n++) + { + int connId = GetConnectionId(n); + SendUpdateMessage(connId, stream); + } + if (nConnections) + { + fprintf(stdout, "Sent obj state update, time: %.1f\n", t); + } + + } /** |