diff options
Diffstat (limited to 'src/net')
-rw-r--r-- | src/net/messageout.cpp | 15 | ||||
-rw-r--r-- | src/net/messageout.h | 2 | ||||
-rw-r--r-- | src/net/network.cpp | 35 |
3 files changed, 33 insertions, 19 deletions
diff --git a/src/net/messageout.cpp b/src/net/messageout.cpp index 00a37b67..713d49be 100644 --- a/src/net/messageout.cpp +++ b/src/net/messageout.cpp @@ -77,16 +77,19 @@ void MessageOut::writeShort(short value) out_size += sizeof(short); } -void MessageOut::writeLong(long value) +/* + writeLong should use an int because long is 8 bytes in x86_64 arch ! +*/ +void MessageOut::writeLong(int value) { - expand(mPos + sizeof(long)); + expand(mPos + sizeof(int)); #if SDL_BYTEORDER == SDL_BIG_ENDIAN - (*(long *)(mData + mPos)) = SDL_Swap32(value); + (*(int *)(mData + mPos)) = SDL_Swap32(value); #else - (*(long *)(mData + mPos)) = value; + (*(int *)(mData + mPos)) = value; #endif - mPos += sizeof(long); - out_size += sizeof(long); + mPos += sizeof(int); + out_size += sizeof(int); } void MessageOut::writeString(const std::string &string, int length) diff --git a/src/net/messageout.h b/src/net/messageout.h index 1e6d75ff..04a5ba91 100644 --- a/src/net/messageout.h +++ b/src/net/messageout.h @@ -50,7 +50,7 @@ class MessageOut void writeByte(char value); /**< Writes a byte. */ void writeShort(short value); /**< Writes a short. */ - void writeLong(long value); /**< Writes a long. */ + void writeLong(int value); /**< Writes a long. */ /** * Writes a string. If a fixed length is not given (-1), it is stored diff --git a/src/net/network.cpp b/src/net/network.cpp index 6f852529..6e3b6c6c 100644 --- a/src/net/network.cpp +++ b/src/net/network.cpp @@ -204,13 +204,31 @@ void close_session() void flush() { + // Send all available data, waits if not all data can be sent immediately + if (out_size > 0) + { + int ret = SDLNet_TCP_Send(sock, (char*)out, out_size); + if (ret < (int)out_size) + { + logger->log("Error in SDLNet_TCP_Send(): %s", SDLNet_GetError()); + errorMessage = "You got disconnected from server"; + state = ERROR_STATE; + return; + } + out_size -= ret; + } + int numReady = SDLNet_CheckSockets(set, 0); if (numReady == -1) { logger->log("Error: SDLNet_CheckSockets"); return; } - else if (numReady) + else if (numReady == 0) // any socket ready + { + return; + } + else if (numReady == 1) // one socket is ready { // Receive data from the socket int ret = SDLNet_TCP_Recv(sock, in + in_size, buffer_size - in_size); @@ -225,19 +243,12 @@ void flush() in_size += ret; } } - - // Send all available data, waits if not all data can be sent immediately - if (out_size > 0) + else // more than one socket is ready.. this should not happen since we only listen once socket. { - int ret = SDLNet_TCP_Send(sock, (char*)out, out_size); - if (ret < (int)out_size) - { - logger->log("Error in SDLNet_TCP_Send(): %s", SDLNet_GetError()); + logger->log("Error in SDLNet_TCP_Recv(), %d sockets are ready : %s", numReady, SDLNet_GetError()); errorMessage = "You got disconnected from server"; state = ERROR_STATE; return; - } - out_size -= ret; } } @@ -254,7 +265,7 @@ MessageIn get_next_message() { // At least 2 bytes should be received for the message ID - while (in_size < 2) flush(); + while (in_size < 2 && state != ERROR_STATE) flush(); int length = packet_lengths[readWord(0)]; @@ -270,7 +281,7 @@ get_next_message() #endif // Make sure the whole packet is received - while (in_size < (unsigned int)length) flush(); + while (in_size < static_cast<unsigned int>(length) && state != ERROR_STATE) flush(); return MessageIn(in, length); } |