[Gcblue-commits] gcb_wx/src/network tcConnectionData.cpp,1.9,1.10 tcControlMessageHandler.cpp,1.7,1.
Status: Alpha
Brought to you by:
ddcforge
|
From: Dewitt C. <ddc...@us...> - 2005-04-08 01:54:22
|
Update of /cvsroot/gcblue/gcb_wx/src/network In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv7286/src/network Modified Files: tcConnectionData.cpp tcControlMessageHandler.cpp tcMultiplayerInterface.cpp tcNetworkInterface.cpp Log Message: multiplayer work Index: tcConnectionData.cpp =================================================================== RCS file: /cvsroot/gcblue/gcb_wx/src/network/tcConnectionData.cpp,v retrieving revision 1.9 retrieving revision 1.10 diff -C2 -d -r1.9 -r1.10 *** tcConnectionData.cpp 2 Nov 2004 04:23:56 -0000 1.9 --- tcConnectionData.cpp 8 Apr 2005 01:54:12 -0000 1.10 *************** *** 39,42 **** --- 39,52 ---- tcNetworkInterface* tcConnectionData::networkInterface = NULL; + const wxIPV4address& tcConnectionData::GetPeerAddress() const + { + return UDPaddress; + } + + float tcConnectionData::GetPingTime() const + { + return pingTime_s; + } + /** * @return total number of bytes read from this connection *************** *** 48,51 **** --- 58,74 ---- /** + * @return number of bytes read from this connection during last sec + */ + unsigned int tcConnectionData::GetReadCountSec() const + { + return readCount_sec; + } + + wxSocketBase* tcConnectionData::GetSocket() + { + return socket; + } + + /** * @return total number of bytes written to this connection */ *************** *** 56,59 **** --- 79,90 ---- /** + * @return number of bytes written to this connection during last sec + */ + unsigned int tcConnectionData::GetWriteCountSec() const + { + return writeCount_sec; + } + + /** * Attempts to read next whole message. If message is incomplete, the partial * message is returned to the socket read buffer. This method does not block. *************** *** 79,82 **** --- 110,115 ---- } + unsigned t0 = tcTime::Get()->GetUpdated30HzCount(); + // Read message socket->Read(tempMessage.data.buffer + tempMessage.bufferIdx, *************** *** 84,87 **** --- 117,123 ---- tempMessage.bufferIdx += socket->LastCount(); + unsigned t1 = tcTime::Get()->GetUpdated30HzCount(); + fprintf(stdout, "***** TCP READ time %d\n", t1-t0); + if (tempMessage.bufferIdx >= tempMessage.data.header.messageSize) { *************** *** 192,195 **** --- 228,248 ---- } + void tcConnectionData::SetPingTime(float ping_s) + { + pingTime_s = ping_s; + } + + void tcConnectionData::SetSocket(wxSocketBase* sock) + { + socket = sock; + wxASSERT(socket); + + + socket->GetPeer(UDPaddress); + UDPaddress.Service(tcNetworkInterface::UDP_PORT); + + peerName = UDPaddress.Hostname(); + } + /** * *************** *** 201,204 **** --- 254,265 ---- //ReadNextMessage(); WriteQueuedMessages(); + + if (tcTime::Get()->Get30HzCount() % 30 == 0) + { + readCount_sec = readCount - lastReadCount; + writeCount_sec = writeCount - lastWriteCount; + lastReadCount = readCount; + lastWriteCount = writeCount; + } } *************** *** 214,219 **** WriteUDP(); - - } --- 275,278 ---- *************** *** 223,227 **** void tcConnectionData::WriteTCP() { ! if (writeQueueTCP.empty()) return; int bufferId = writeQueueTCP.front(); --- 282,286 ---- void tcConnectionData::WriteTCP() { ! if (writeQueueTCP.empty()) return; int bufferId = writeQueueTCP.front(); *************** *** 235,238 **** --- 294,300 ---- return; } + + unsigned t0 = tcTime::Get()->GetUpdated30HzCount(); + //socket->SetFlags(wxSOCKET_WAITALL); *************** *** 264,267 **** --- 326,332 ---- } + unsigned t1 = tcTime::Get()->GetUpdated30HzCount(); + + fprintf(stdout, "***** TCP send time %d\n", t1-t0); //socket->SetFlags(wxSOCKET_NOWAIT); *************** *** 285,294 **** } ! wxIPV4address addr; ! socket->GetPeer(addr); ! addr.Service(tcNetworkInterface::UDP_PORT); ! ! wxString peerName = addr.Hostname(); ! wxDatagramSocket *datagramSock = networkInterface->GetDatagramSocket(); if (datagramSock == NULL) --- 350,354 ---- } ! wxDatagramSocket *datagramSock = networkInterface->GetDatagramSocket(); if (datagramSock == NULL) *************** *** 297,301 **** return; } ! datagramSock->SendTo(addr, message->data.buffer, message->data.header.messageSize); if (datagramSock->Error()) { --- 357,361 ---- return; } ! datagramSock->SendTo(UDPaddress, message->data.buffer, message->data.header.messageSize); if (datagramSock->Error()) { *************** *** 318,327 **** */ tcConnectionData::tcConnectionData() { ! socket = NULL; ! idString = "Err"; ! id = -1; ! readCount = 0; ! writeCount = 0; } --- 378,394 ---- */ tcConnectionData::tcConnectionData() + : + socket(0), + idString("Err"), + id(-1), + readCount(0), + writeCount(0), + lastReadCount(0), + lastWriteCount(0), + readCount_sec(0), + writeCount_sec(0), + pingTime_s(0) { ! } Index: tcControlMessageHandler.cpp =================================================================== RCS file: /cvsroot/gcblue/gcb_wx/src/network/tcControlMessageHandler.cpp,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** tcControlMessageHandler.cpp 2 Nov 2004 04:23:56 -0000 1.7 --- tcControlMessageHandler.cpp 8 Apr 2005 01:54:12 -0000 1.8 *************** *** 32,35 **** --- 32,36 ---- #include "common/tcStream.h" #include "tcSimState.h" + #include "tcTime.h" #include "wxcommands.h" *************** *** 70,73 **** --- 71,75 ---- return; } + break; case CM_ALLIANCE: { *************** *** 80,83 **** --- 82,109 ---- } break; + case CM_PING: + { + tcStream stream; + stream << messageCode; + + unsigned int t = tcTime::Get()->GetUpdated30HzCount(); + stream << t; + + messageSize = stream.size(); + stream.read((char*)data, messageSize); + } + break; + case CM_PONG: + { + tcStream stream; + stream << messageCode; + + unsigned int t = (unsigned int)param; + stream << t; + + messageSize = stream.size(); + stream.read((char*)data, messageSize); + } + break; default: { *************** *** 92,101 **** 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; --- 118,121 ---- *************** *** 112,116 **** stream >> messageCode; ! //memcpy(&messageCode, data, sizeof(messageCode)); switch (messageCode) --- 132,147 ---- stream >> messageCode; ! ! // if server, check to ensure control message is valid ! if (isServer) ! { ! if (!((messageCode == CM_PING) || (messageCode == CM_PONG))) ! { ! fprintf(stderr, "Warning - illegal control message received by server (%d)\n", ! messageCode); ! return; ! } ! } ! switch (messageCode) *************** *** 146,149 **** --- 177,202 ---- } break; + case CM_PING: + { + unsigned int t0; + stream >> t0; + + tcMultiplayerInterface::Get()-> + SendControlMessageUDP(connectionId, CM_PONG, int(t0)); + } + break; + case CM_PONG: + { + unsigned int t0; + stream >> t0; + + + unsigned int t = tcTime::Get()->GetUpdated30HzCount(); + unsigned int pingTime = t - t0; + float ping_s = (1.0f/30.0f) * float(pingTime); + tcMultiplayerInterface::Get()-> + SetPingTime(connectionId, ping_s); + } + break; default: { Index: tcMultiplayerInterface.cpp =================================================================== RCS file: /cvsroot/gcblue/gcb_wx/src/network/tcMultiplayerInterface.cpp,v retrieving revision 1.20 retrieving revision 1.21 diff -C2 -d -r1.20 -r1.21 *** tcMultiplayerInterface.cpp 6 Apr 2005 02:19:49 -0000 1.20 --- tcMultiplayerInterface.cpp 8 Apr 2005 01:54:12 -0000 1.21 *************** *** 88,91 **** --- 88,92 ---- } + /** * Updates lastUpdate time. *************** *** 541,545 **** /** ! * Send control message to destination */ void tcMultiplayerInterface::SendControlMessage(int destination, int messageCode, int param) --- 542,546 ---- /** ! * Send control message to destination using TCP */ void tcMultiplayerInterface::SendControlMessage(int destination, int messageCode, int param) *************** *** 554,557 **** --- 555,571 ---- } + /** + * Send control message to destination using UDP + */ + void tcMultiplayerInterface::SendControlMessageUDP(int destination, int messageCode, int param) + { + unsigned char data[64]; + unsigned messageSize; + + tcControlMessageHandler::CreateControlMessage(messageCode, messageSize, data, param); + networkInterface->SendMessage(destination, MSG_CONTROL, + messageSize , data, + tcNetworkInterface::UDP); + } void tcMultiplayerInterface::SendTestUDP(int destination, const std::string& message) *************** *** 604,611 **** * Sets identification name string for player using this interface */ ! void tcMultiplayerInterface::SetName(const std::string& name) ! { ! myName = name; ! } /** --- 618,633 ---- * Sets identification name string for player using this interface */ ! void tcMultiplayerInterface::SetName(const std::string& name) ! { ! myName = name; ! } ! ! /** ! * Sets ping time associated with connection ! */ ! void tcMultiplayerInterface::SetPingTime(int connectionId, float ping_s) ! { ! networkInterface->SetPingTime(connectionId, ping_s); ! } /** *************** *** 616,621 **** --- 638,653 ---- void tcMultiplayerInterface::Update() { + unsigned startCount = tcTime::Get()->GetUpdated30HzCount(); + networkInterface->Update(); + unsigned endCount = tcTime::Get()->GetUpdated30HzCount(); + + unsigned elapsed = endCount - startCount; + if ((elapsed > updateCount) || ((endCount >> 3) % 5 == 0)) + { + updateCount = elapsed; + } + // process receive messages ProcessReceiveMessages(); *************** *** 643,646 **** --- 675,681 ---- UpdateTime(); } + + UpdatePing(); + } *************** *** 901,904 **** --- 936,960 ---- } + + /** + * + */ + void tcMultiplayerInterface::UpdatePing() + { + static unsigned lastUpdate = 0; + unsigned currentTime = tcTime::Get()->Get30HzCount(); + if (currentTime - lastUpdate < 150) return; + + lastUpdate = currentTime; + + unsigned nConn = networkInterface->GetNumConnections(); + for (unsigned n=0;n<nConn;n++) + { + int connId = networkInterface->GetConnectionId(n); + + SendControlMessageUDP(connId, tcControlMessageHandler::CM_PING, 0); + } + } + /** * *************** *** 920,923 **** --- 976,980 ---- { tcPlayerStatus stat; + stat.ping_s = 0; stat.timestamp = tcTime::Get()->Get30HzCount(); stat.name = "anonymous"; *************** *** 1044,1047 **** --- 1101,1106 ---- errorPlayerStatus.name = "error"; errorPlayerStatus.timestamp = 0; + + updateCount = 0; } Index: tcNetworkInterface.cpp =================================================================== RCS file: /cvsroot/gcblue/gcb_wx/src/network/tcNetworkInterface.cpp,v retrieving revision 1.14 retrieving revision 1.15 diff -C2 -d -r1.14 -r1.15 *** tcNetworkInterface.cpp 2 Nov 2004 04:23:56 -0000 1.14 --- tcNetworkInterface.cpp 8 Apr 2005 01:54:12 -0000 1.15 *************** *** 45,49 **** tcConnectionData cdata; ! cdata.socket = socket; wxIPV4address addr; --- 45,49 ---- tcConnectionData cdata; ! cdata.SetSocket(socket); wxIPV4address addr; *************** *** 91,97 **** for(size_t n=0;n<nClients;n++) { ! if(connectionData[n].socket) { ! connectionData[n].socket->Destroy(); } else --- 91,97 ---- for(size_t n=0;n<nClients;n++) { ! if (connectionData[n].GetSocket()) { ! connectionData[n].GetSocket()->Destroy(); } else *************** *** 177,185 **** float dt_sec = (1.0f/30.0f)*(float)(timeCount - connData.timestamp); ! unsigned long bytesIn = connData.GetReadCount(); ! unsigned long bytesOut = connData.GetWriteCount(); ! wxString status = wxString::Format("%s (%.0f) %07d %07d", ! connData.idString.c_str(), dt_sec, bytesIn, bytesOut); return status.c_str(); } --- 177,187 ---- float dt_sec = (1.0f/30.0f)*(float)(timeCount - connData.timestamp); ! ! unsigned int bytesIn = connData.GetReadCountSec(); ! unsigned int bytesOut = connData.GetWriteCountSec(); ! float ping_ms = 1000.0f * connData.GetPingTime(); ! wxString status = wxString::Format("%s (%.0f) %04d %04d %.0f", ! connData.idString.c_str(), dt_sec, bytesIn, bytesOut, ping_ms); return status.c_str(); } *************** *** 482,487 **** for(int n=nConnections-1;n>=0;n--) { ! wxASSERT(connectionData[n].socket); ! if (!connectionData[n].socket->IsConnected()) { connectionData.erase(connectionData.begin()+n); --- 484,489 ---- for(int n=nConnections-1;n>=0;n--) { ! wxASSERT(connectionData[n].GetSocket()); ! if (!connectionData[n].GetSocket()->IsConnected()) { connectionData.erase(connectionData.begin()+n); *************** *** 650,653 **** --- 652,668 ---- } + /** + * Sets ping time associated with connection + */ + void tcNetworkInterface::SetPingTime(int connectionId, float ping_s) + { + int connectionIdx = LookupConnectionIndex(connectionId); + + if (tcConnectionData* conn = GetConnection(connectionIdx)) + { + conn->SetPingTime(ping_s); + } + } + /** *************** *** 742,750 **** for(size_t n=0;n<nConnections;n++) { - wxIPV4address peerAddr; - connectionLookup[connectionData[n].id] = (int)n; ! ! connectionData[n].socket->GetPeer(peerAddr); std::string hostName = peerAddr.IPAddress().c_str(); peerMap[hostName] = (int)n; --- 757,763 ---- for(size_t n=0;n<nConnections;n++) { connectionLookup[connectionData[n].id] = (int)n; ! ! wxIPV4address peerAddr = connectionData[n].GetPeerAddress(); std::string hostName = peerAddr.IPAddress().c_str(); peerMap[hostName] = (int)n; *************** *** 754,762 **** --- 767,781 ---- void tcNetworkInterface::UpdateServer() { + //unsigned t0 = tcTime::Get()->GetUpdated30HzCount(); + RemoveDeadConnections(); + //unsigned t1 = tcTime::Get()->GetUpdated30HzCount(); + // check for new connections wxSocketBase *socket = serverSock->Accept(FALSE); + //unsigned t2 = tcTime::Get()->GetUpdated30HzCount(); + if (socket) { *************** *** 764,768 **** --- 783,793 ---- } + //unsigned t3 = tcTime::Get()->GetUpdated30HzCount(); + UpdateConnections(); + + //unsigned t4 = tcTime::Get()->GetUpdated30HzCount(); + + } |