[Gcblue-commits] gcb_wx/src/network tcConnectionData.cpp,1.1,1.2 tcMessage.cpp,1.1,1.2 tcMultiplayer
Status: Alpha
Brought to you by:
ddcforge
|
From: <ddc...@us...> - 2004-03-02 03:03:43
|
Update of /cvsroot/gcblue/gcb_wx/src/network In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv13312/src/network Modified Files: tcConnectionData.cpp tcMessage.cpp tcMultiplayerInterface.cpp tcNetworkInterface.cpp Log Message: Index: tcConnectionData.cpp =================================================================== RCS file: /cvsroot/gcblue/gcb_wx/src/network/tcConnectionData.cpp,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** tcConnectionData.cpp 29 Feb 2004 22:51:36 -0000 1.1 --- tcConnectionData.cpp 2 Mar 2004 02:52:14 -0000 1.2 *************** *** 39,47 **** * 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. ! * Uses tempMessage for temporary storage. If return message is to be stored, ! * the message must be COPIED. Do not store the pointer. ! * @return pointer to new message if entire message is ready, otherwise NULL */ ! tcMessage* tcConnectionData::ReadNextMessage() { wxASSERT(socket); --- 39,46 ---- * 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. ! * Uses tempMessage for temporary storage. When the complete message is collected ! * the idx is stored in the appropriate receive queue. */ ! void tcConnectionData::ReadNextMessage() { wxASSERT(socket); *************** *** 68,72 **** } ! if (tempMessage.id == -1) return NULL; // Read message --- 67,75 ---- } ! if (tempMessage.id == -1) ! { ! fprintf(stdout, " Read partial message: %d bytes of data\n", tempMessage.bufferIdx); ! return; ! } // Read message *************** *** 77,103 **** if (tempMessage.bufferIdx >= tempMessage.messageSize) { - tempMessage.bufferIdx = 0; // reset bufferIdx } else { ! fprintf(stdout, "Read %d bytes of data\n", tempMessage.bufferIdx); ! return NULL; } - //socket->SetFlags(wxSOCKET_NOWAIT); - tempMessage.buffer[256] = 0; std::string testString; testString = (char*)tempMessage.buffer; ! fprintf(stdout, "Received message, size: %d, id:%d, data:%s\n", ! tempMessage.messageSize, tempMessage.id, testString.c_str()); ! wxMessageBox(testString.c_str(),"Message",wxOK); ! tempMessage.Reset(); - return &tempMessage; } --- 80,117 ---- if (tempMessage.bufferIdx >= tempMessage.messageSize) { } else { ! fprintf(stdout, " Read partial message: %d bytes of data\n", tempMessage.bufferIdx); ! return; } std::string testString; testString = (char*)tempMessage.buffer; ! /* ! tempMessage.buffer[256] = 0; wxMessageBox(testString.c_str(),"Message",wxOK); ! */ ! ! int bufferIdx = networkInterface->CheckoutMessage(); ! tcMessage *message = networkInterface->GetMessage(bufferIdx); ! if (message == NULL) ! { ! std::cerr << "Error - Message buffer full, receive message lost" << std::endl; ! tempMessage.Reset(); ! return; ! } ! ! *message = tempMessage; ! readQueueTCP.push(bufferIdx); ! ! fprintf(stdout, " Received message, size: %d, id:%d, t:%d data:%s\n", ! tempMessage.messageSize, tempMessage.id, tcTime::Get30HzCount(), testString.c_str()); ! tempMessage.Reset(); } *************** *** 137,141 **** return; } ! // socket->SetFlags(wxSOCKET_WAITALL); const unsigned idx1 = sizeof(message->messageSize); --- 151,155 ---- return; } ! //socket->SetFlags(wxSOCKET_WAITALL); const unsigned idx1 = sizeof(message->messageSize); *************** *** 179,182 **** --- 193,198 ---- if (message->bufferIdx >= (message->messageSize + idx2)) { + fprintf(stdout,"Sent message, size:%d, id:%d, t:%d\n", + message->messageSize, message->id, tcTime::Get30HzCount()); writeQueueTCP.pop(); networkInterface->ReturnMessage(bufferId); *************** *** 187,191 **** wxSocketError err = socket->LastError(); } ! // socket->SetFlags(wxSOCKET_NOWAIT); } --- 203,209 ---- wxSocketError err = socket->LastError(); } ! ! ! //socket->SetFlags(wxSOCKET_NOWAIT); } Index: tcMessage.cpp =================================================================== RCS file: /cvsroot/gcblue/gcb_wx/src/network/tcMessage.cpp,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** tcMessage.cpp 29 Feb 2004 22:51:36 -0000 1.1 --- tcMessage.cpp 2 Mar 2004 02:52:15 -0000 1.2 *************** *** 67,69 **** --- 67,86 ---- } + /** + * Assignment operator + */ + const tcMessage& tcMessage::operator=(const tcMessage& rhs) + { + id = rhs.id;; + timestamp = rhs.timestamp; + sourceId = rhs.sourceId; + bufferIdx = rhs.bufferIdx; + messageSize = rhs.messageSize; + wxASSERT(messageSize <= BUFFER_SIZE); + if (messageSize > BUFFER_SIZE) messageSize = BUFFER_SIZE; + memcpy(buffer, rhs.buffer, messageSize); + + return *this; + } + END_NAMESPACE \ No newline at end of file Index: tcMultiplayerInterface.cpp =================================================================== RCS file: /cvsroot/gcblue/gcb_wx/src/network/tcMultiplayerInterface.cpp,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** tcMultiplayerInterface.cpp 29 Feb 2004 22:51:36 -0000 1.1 --- tcMultiplayerInterface.cpp 2 Mar 2004 02:52:15 -0000 1.2 *************** *** 28,31 **** --- 28,32 ---- #include "network/tcNetworkInterface.h" #include "network/tcMultiplayerInterface.h" + #include <iostream> BEGIN_NAMESPACE(network) *************** *** 40,43 **** --- 41,52 ---- } + std::string tcMultiplayerInterface::GetChatText() + { + if (chatText.empty()) return "ERROR"; + std::string text = chatText.front(); + chatText.pop(); + return text; + } + int tcMultiplayerInterface::GetConnectionId(unsigned connectionIdx) { *************** *** 55,58 **** --- 64,72 ---- } + bool tcMultiplayerInterface::IsChatTextAvail() + { + return !chatText.empty(); + } + void tcMultiplayerInterface::MakeClient() { *************** *** 70,78 **** } /** * Sends a test message of text to destination */ ! void tcMultiplayerInterface::SendTestMessage(int destination, std::string message) { char buff[256]; --- 84,141 ---- } + /** + * Process single receive message for connection associated with connectionId + */ + void tcMultiplayerInterface::ProcessMessage(int connectionId, int messageId, + unsigned messageSize, 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: + std::cerr << "Error - tcMultiplayerInterface::ProcessMessage unrecognized message" + << std::endl; + break; + } + } + + /** + * + */ + void tcMultiplayerInterface::ProcessReceiveMessages() + { + int messageId; + unsigned messageSize; + + unsigned nConn = networkInterface->GetNumConnections(); + for (unsigned n=0;n<nConn;n++) + { + int connId = networkInterface->GetConnectionId(n); + unsigned char *data = networkInterface->ReceiveMessage(connId, messageId, + messageSize, tcNetworkInterface::TCP); + if (data != NULL) + { + ProcessMessage(connId, messageId, messageSize, data); + } + } + } + /** * Sends a test message of text to destination */ ! void tcMultiplayerInterface::SendChatText(int destination, std::string message) { char buff[256]; *************** *** 82,86 **** strncpy(buff, message.c_str(), messageLength); buff[messageLength] = 0; ! networkInterface->SendMessage(destination, 1, (unsigned)(messageLength+1), (unsigned char*)buff); } --- 145,149 ---- strncpy(buff, message.c_str(), messageLength); buff[messageLength] = 0; ! networkInterface->SendMessage(destination, MSG_CHATTEXT, (unsigned)(messageLength+1), (unsigned char*)buff); } *************** *** 93,96 **** --- 156,162 ---- { networkInterface->Update(); + + // process receive messages + ProcessReceiveMessages(); } Index: tcNetworkInterface.cpp =================================================================== RCS file: /cvsroot/gcblue/gcb_wx/src/network/tcNetworkInterface.cpp,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** tcNetworkInterface.cpp 29 Feb 2004 22:51:36 -0000 1.4 --- tcNetworkInterface.cpp 2 Mar 2004 02:52:15 -0000 1.5 *************** *** 231,234 **** --- 231,242 ---- /** + * @return true if this interface is acting as a server + */ + bool tcNetworkInterface::IsServer() + { + return isServer; + } + + /** * Looks up connection index in connectionData vector based on connection id * @return connection index for connection id or -1 if not found or invalid *************** *** 443,453 **** } ! unsigned char* tcNetworkInterface::ReceiveMessage(int connectionId, int& messageId, unsigned& messageSize, int protocol) { if (protocol != TCP) return NULL; // only TCP supported right now int connectionIdx = LookupConnectionIndex(connectionId); ! return NULL; } --- 451,483 ---- } ! /** ! * @return pointer to message data, data should be immediately used or copied to store ! */ unsigned char* tcNetworkInterface::ReceiveMessage(int connectionId, int& messageId, unsigned& messageSize, int protocol) { if (protocol != TCP) return NULL; // only TCP supported right now + int connectionIdx = LookupConnectionIndex(connectionId); ! tcConnectionData *conn = GetConnection(connectionIdx); ! if (conn == NULL) return NULL; ! ! // get next message in queue ! if (conn->readQueueTCP.empty()) return NULL; // no messages ! int bufferIdx = conn->readQueueTCP.front(); ! conn->readQueueTCP.pop(); ! ! tcMessage *message = GetMessage(bufferIdx); ! if (message == NULL) ! { ! std::cerr << "Error - Bad index passed to GetMessage" << std::endl; ! return NULL; ! } ! ! messageId = message->id; ! messageSize = message->messageSize; ! ! ReturnMessage(bufferIdx); // dangerous, fix this ! return message->buffer; } |