[Gcblue-commits] gcb_wx/src/network tcMultiplayerInterface.cpp,1.11,1.12 tcUpdateMessageHandler.cpp,
Status: Alpha
Brought to you by:
ddcforge
|
From: Dewitt C. <ddc...@us...> - 2004-05-01 21:49:15
|
Update of /cvsroot/gcblue/gcb_wx/src/network In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv6949/src/network Modified Files: tcMultiplayerInterface.cpp tcUpdateMessageHandler.cpp Log Message: multiplayer Index: tcUpdateMessageHandler.cpp =================================================================== RCS file: /cvsroot/gcblue/gcb_wx/src/network/tcUpdateMessageHandler.cpp,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** tcUpdateMessageHandler.cpp 27 Apr 2004 21:45:14 -0000 1.4 --- tcUpdateMessageHandler.cpp 1 May 2004 21:49:06 -0000 1.5 *************** *** 33,47 **** #include "wxcommands.h" #include "common/tcStream.h" BEGIN_NAMESPACE(network) /** * Saves data to create obj to stream. */ ! void tcUpdateMessageHandler::AddCreate(tcGameObject* obj, tcStream& stream) { stream << obj->mnID; stream << obj->mnDBKey; ! stream << obj->mnAlliance; } --- 33,79 ---- #include "wxcommands.h" #include "common/tcStream.h" + #include "common/tcObjStream.h" BEGIN_NAMESPACE(network) /** + * stream must have SetAck(true) called first + */ + void tcUpdateMessageHandler::AddCommandAck(tcGameObject* obj, tcCommandStream& stream) + { + wxASSERT(stream.GetAck()); + + stream << obj->mnID; // write id + *obj >> stream; + + } + + /** + * + */ + void tcUpdateMessageHandler::AddCommandUpdate(tcGameObject* obj, tcCommandStream& stream) + { + wxASSERT(!stream.GetAck()); + + stream << obj->mnID; // write id + *obj >> stream; + } + + /** * Saves data to create obj to stream. */ ! void tcUpdateMessageHandler::AddCreate(tcGameObject* obj, tcCreateStream& stream) { stream << obj->mnID; stream << obj->mnDBKey; ! *obj >> stream; ! } ! ! /** ! * ! */ ! void tcUpdateMessageHandler::AddCreateRequest(long id, tcStream& stream) ! { ! stream << id; } *************** *** 59,63 **** * @see tcUpdateMessageHandler::AddCreate */ ! void tcUpdateMessageHandler::AddUpdate(tcGameObject* obj, tcStream& stream) { stream << obj->mnID; // write id --- 91,95 ---- * @see tcUpdateMessageHandler::AddCreate */ ! void tcUpdateMessageHandler::AddUpdate(tcGameObject* obj, tcUpdateStream& stream) { stream << obj->mnID; // write id *************** *** 78,85 **** --- 110,123 ---- case CREATE: break; + case CREATE_REQUEST: + break; case UPDATE: // update messages start with time and time acceleration info tcSimState::Get()->SaveTimeToStream(stream); break; + case COMMAND_UPDATE: + break; + case COMMAND_ACK: + break; case DESTROY: break; *************** *** 95,108 **** 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); ! tcStream stream((const char*)data, messageSize); --- 133,140 ---- void tcUpdateMessageHandler::Handle(int connectionId, unsigned messageSize, const unsigned char *data) { tcSimState* simState = tcSimState::Get(); wxASSERT(simState); ! // end up creating stream twice. could split to use multiple handlers to avoid this tcStream stream((const char*)data, messageSize); *************** *** 113,123 **** { case CREATE: ! HandleCreate(stream); break; case UPDATE: ! HandleUpdate(stream); break; case DESTROY: ! HandleDestroy(stream); break; default: --- 145,181 ---- { case CREATE: ! { ! tcCreateStream stream((const char*)data, messageSize); ! stream >> messageType; ! HandleCreate(stream); ! } break; case UPDATE: ! { ! tcUpdateStream stream((const char*)data, messageSize); ! stream >> messageType; ! HandleUpdate(stream); ! } ! break; ! case COMMAND_UPDATE: ! { ! tcCommandStream stream((const char*)data, messageSize); ! stream >> messageType; ! HandleCommandUpdate(stream, connectionId); ! } ! break; ! case COMMAND_ACK: ! { ! tcCommandStream stream((const char*)data, messageSize); ! stream >> messageType; ! HandleCommandAck(stream); ! } break; case DESTROY: ! { ! tcStream stream((const char*)data, messageSize); ! stream >> messageType; ! HandleDestroy(stream); ! } break; default: *************** *** 129,135 **** /** * Handle CREATE update message */ ! void tcUpdateMessageHandler::HandleCreate(tcStream& stream) { tcSimState* simState = tcSimState::Get(); --- 187,282 ---- /** + * Handle COMMAND_ACK + */ + void tcUpdateMessageHandler::HandleCommandAck(tcCommandStream& stream) + { + stream.SetAck(true); + + tcSimState* simState = tcSimState::Get(); + wxASSERT(simState); + + fprintf(stdout, "Upding obj cmd acks, time %.1f: ", simState->GetTime()); + + long id; + + while ((stream >> id).eof() == false) + { + // lookup obj + tcGameObject* obj = simState->GetObject(id); + + // update obj if it exists, otherwise create object + if (obj) + { + *obj << stream; + fprintf(stdout, "%d ", id); + } + else + { + fprintf(stderr, "Error - HandleCommandAck - obj %d not found\n", id); + fprintf(stdout, "\n"); + return; + } + } + fprintf(stdout, "\n"); + + } + + /** + * Handle COMMAND_UPDATE + */ + void tcUpdateMessageHandler::HandleCommandUpdate(tcCommandStream& stream, int connectionId) + { + tcSimState* simState = tcSimState::Get(); + wxASSERT(simState); + + + // Init ack messag + tcCommandStream commandStream; + tcUpdateMessageHandler::InitializeMessage(tcUpdateMessageHandler::COMMAND_ACK, commandStream); + commandStream.SetAck(true); + + unsigned nAcks = 0; + + fprintf(stdout, "Updating obj cmds, time %.1f: ", simState->GetTime()); + + long id; + + while ((stream >> id).eof() == false) + { + // lookup obj + tcGameObject* obj = simState->GetObject(id); + + // update obj if it exists, otherwise create object + if (obj) + { + *obj << stream; + + tcUpdateMessageHandler::AddCommandAck(obj, commandStream); + nAcks++; + + fprintf(stdout, "%d ", id); + } + else + { + fprintf(stderr, "Error - HandleCommandUpdate - obj %d not found\n", id); + fprintf(stdout, "\n"); + return; + } + } + fprintf(stdout, "\n"); + + if (nAcks) + { + tcMultiplayerInterface::Get()->SendUpdateMessage(connectionId, commandStream); + #ifdef _DEBUG + fprintf(stdout, "Sent obj command update ack, time: %.1f\n", simState->GetTime()); + #endif + } + } + + /** * Handle CREATE update message */ ! void tcUpdateMessageHandler::HandleCreate(tcCreateStream& stream) { tcSimState* simState = tcSimState::Get(); *************** *** 141,150 **** long id; long databaseId; - unsigned char alliance; while ((stream >> id).eof() == false) { stream >> databaseId; - stream >> alliance; // lookup obj --- 288,295 ---- *************** *** 163,169 **** { obj = simState->CreateGameObject(dbObj); ! obj->mnAlliance = alliance; simState->AddPlatformWithKey(obj, id); ! fprintf(stdout, "%d (%d)", id, alliance); } else --- 308,314 ---- { obj = simState->CreateGameObject(dbObj); ! *obj << stream; simState->AddPlatformWithKey(obj, id); ! fprintf(stdout, "%d (%d)", id, obj->mnAlliance); } else *************** *** 179,183 **** /** ! * Handle DESTROY update message */ void tcUpdateMessageHandler::HandleDestroy(tcStream& stream) --- 324,335 ---- /** ! * Handle CREATE_REQUEST message (server only) ! */ ! void tcUpdateMessageHandler::HandleCreateRequest(tcStream& stream) ! { ! } ! ! /** ! * Handle DESTROY update message (client only) */ void tcUpdateMessageHandler::HandleDestroy(tcStream& stream) *************** *** 212,218 **** /** ! * Handle UPDATE update message */ ! void tcUpdateMessageHandler::HandleUpdate(tcStream& stream) { tcSimState* simState = tcSimState::Get(); --- 364,370 ---- /** ! * Handle UPDATE update message (client only) */ ! void tcUpdateMessageHandler::HandleUpdate(tcUpdateStream& stream) { tcSimState* simState = tcSimState::Get(); *************** *** 248,251 **** --- 400,405 ---- + + tcUpdateMessageHandler::tcUpdateMessageHandler() { Index: tcMultiplayerInterface.cpp =================================================================== RCS file: /cvsroot/gcblue/gcb_wx/src/network/tcMultiplayerInterface.cpp,v retrieving revision 1.11 retrieving revision 1.12 diff -C2 -d -r1.11 -r1.12 *** tcMultiplayerInterface.cpp 27 Apr 2004 21:45:14 -0000 1.11 --- tcMultiplayerInterface.cpp 1 May 2004 21:49:06 -0000 1.12 *************** *** 32,35 **** --- 32,36 ---- #include "network/tcUpdateMessageHandler.h" #include "common/tcStream.h" + #include "common/tcObjStream.h" #include <iostream> #include <queue> *************** *** 228,231 **** --- 229,234 ---- if (IsServer()) { // server-specific handlers + // server needs update msg handler to handle command updates from client + AddMessageHandler(MSG_UPDATE, new tcUpdateMessageHandler()); } else *************** *** 638,641 **** --- 641,700 ---- } + void tcMultiplayerInterface::UpdateObjectCommands(unsigned connIdx, bool updateUnack, bool clearNewCmdFlag) + { + int connId = GetConnectionId(connIdx); + + tcPlayerStatus& pstatus = GetPlayerStatus(connId); + + tcSimState* simState = tcSimState::Get(); + wxASSERT(simState); + double t = simState->GetTime(); + + // iterate through all game objects and build command update stream + tcCommandStream commandStream; + tcUpdateMessageHandler::InitializeMessage(tcUpdateMessageHandler::COMMAND_UPDATE, commandStream); + + tcGameObjIterator iter; + unsigned updateCount = 0; + for (iter.First();iter.NotDone();iter.Next()) + { + tcGameObject* obj = iter.Get(); + if ((pstatus.alliance == obj->mnAlliance) || !IsServer()) + { + if (obj->HasNewCommand() || (updateUnack && obj->HasUnacknowledgedCommand())) + { + tcUpdateMessageHandler::AddCommandUpdate(obj, commandStream); + if (clearNewCmdFlag) obj->ClearNewCommand(); + updateCount++; + #ifdef _DEBUG + fprintf(stdout, "%d ", obj->mnID); + #endif + } + } + + // create new message if updateCount gets too large + if (updateCount >= 4) + { + SendUpdateMessage(connId, commandStream); + #ifdef _DEBUG + fprintf(stdout, "Sent obj command update, time: %.1f\n", t); + #endif + commandStream.clear(); + tcUpdateMessageHandler::InitializeMessage(tcUpdateMessageHandler::COMMAND_UPDATE, commandStream); + updateCount = 0; + } + } + + if (updateCount) + { + SendUpdateMessage(connId, commandStream); + #ifdef _DEBUG + fprintf(stdout, "Sent obj command update, time: %.1f\n", t); + #endif + } + } + + + /** * Server only -- send updates for existing objects to client *************** *** 653,658 **** // iterate through all game objects and build update stream ! tcStream stream; ! tcUpdateMessageHandler::InitializeMessage(tcUpdateMessageHandler::UPDATE, stream); tcGameObjIterator iter; --- 712,717 ---- // iterate through all game objects and build update stream ! tcUpdateStream updateStream; ! tcUpdateMessageHandler::InitializeMessage(tcUpdateMessageHandler::UPDATE, updateStream); tcGameObjIterator iter; *************** *** 666,670 **** if (pstatus.GetLastUpdate(obj->mnID, lastUpdate)) { ! tcUpdateMessageHandler::AddUpdate(obj, stream); pstatus.SetUpdate(obj->mnID, t); updateCount++; --- 725,730 ---- if (pstatus.GetLastUpdate(obj->mnID, lastUpdate)) { ! tcUpdateMessageHandler::AddUpdate(obj, updateStream); ! pstatus.SetUpdate(obj->mnID, t); updateCount++; *************** *** 675,684 **** if (updateCount >= 4) { ! SendUpdateMessage(connId, stream); #ifdef _DEBUG fprintf(stdout, "Sent obj state update, time: %.1f\n", t); #endif ! stream.clear(); ! tcUpdateMessageHandler::InitializeMessage(tcUpdateMessageHandler::UPDATE, stream); updateCount = 0; } --- 735,744 ---- if (updateCount >= 4) { ! SendUpdateMessage(connId, updateStream); #ifdef _DEBUG fprintf(stdout, "Sent obj state update, time: %.1f\n", t); #endif ! updateStream.clear(); ! tcUpdateMessageHandler::InitializeMessage(tcUpdateMessageHandler::UPDATE, updateStream); updateCount = 0; } *************** *** 687,691 **** if (updateCount) { ! SendUpdateMessage(connId, stream); #ifdef _DEBUG fprintf(stdout, "Sent obj state update, time: %.1f\n", t); --- 747,751 ---- if (updateCount) { ! SendUpdateMessage(connId, updateStream); #ifdef _DEBUG fprintf(stdout, "Sent obj state update, time: %.1f\n", t); *************** *** 705,709 **** // iterate through all game objects and build update stream ! tcStream stream; tcUpdateMessageHandler::InitializeMessage(tcUpdateMessageHandler::CREATE, stream); --- 765,769 ---- // iterate through all game objects and build update stream ! tcCreateStream stream; tcUpdateMessageHandler::InitializeMessage(tcUpdateMessageHandler::CREATE, stream); *************** *** 747,752 **** static long lastAccel = 0; // state of tcSimState::timeAcceleration at last update - if (!IsServer()) return; - tcSimState* simState = tcSimState::Get(); wxASSERT(simState); --- 807,810 ---- *************** *** 755,774 **** double t = simState->GetTime(); // update if enough time elapsed OR if timeAcceleration state has changed ! if ((t <= lastUpdate + 2.0) && (accel == lastAccel)) return; lastUpdate = t; lastAccel = accel; - // iterate through all game objects and build update stream - tcStream stream; - - unsigned nConnections = GetNumConnections(); - for (unsigned n=0;n<nConnections;n++) { ! UpdateDestroyedObjects(n); ! UpdateNewObjects(n); ! UpdateExistingObjects(n); } --- 813,843 ---- double t = simState->GetTime(); + unsigned nConnections = GetNumConnections(); + // update if enough time elapsed OR if timeAcceleration state has changed ! if ((t <= lastUpdate + 2.0) && (accel == lastAccel)) ! { ! // always do new cmd update ! for (unsigned n=0;n<nConnections;n++) ! { ! bool clearNewCmdFlag = (n == nConnections - 1); ! UpdateObjectCommands(n, false, clearNewCmdFlag); ! } ! return; ! } lastUpdate = t; lastAccel = accel; for (unsigned n=0;n<nConnections;n++) { ! if (IsServer()) ! { ! UpdateDestroyedObjects(n); ! UpdateNewObjects(n); ! UpdateExistingObjects(n); ! } ! bool clearNewCmdFlag = (n == nConnections - 1); ! UpdateObjectCommands(n, true, clearNewCmdFlag); } *************** *** 849,853 **** tcMultiplayerInterface::tcMultiplayerInterface() ! : tcpChat(true), myName("Somebody"), evtHandler(0) { networkInterface = new tcNetworkInterface(); --- 918,922 ---- tcMultiplayerInterface::tcMultiplayerInterface() ! : tcpChat(true), myName("Server"), evtHandler(0) { networkInterface = new tcNetworkInterface(); |