summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCedric Borgese <cedric.borgese@gmail.com>2005-09-27 13:31:15 +0000
committerCedric Borgese <cedric.borgese@gmail.com>2005-09-27 13:31:15 +0000
commit8225942307f8290e5941cdeaff5c7b212f978390 (patch)
treea60d0efb5eaeec89a002d8a58ee485de190e8615
parentd56c142c43939657df13f3b7246defe8f181306e (diff)
downloadmana-8225942307f8290e5941cdeaff5c7b212f978390.tar.gz
mana-8225942307f8290e5941cdeaff5c7b212f978390.tar.bz2
mana-8225942307f8290e5941cdeaff5c7b212f978390.tar.xz
mana-8225942307f8290e5941cdeaff5c7b212f978390.zip
Dont crash if there is an unknown error at login.
Remove a bug that do x86_64 arch fail to connect to server : long is 8 bytes on that arch, use int for 4 bytes integer.
-rw-r--r--src/gui/login.cpp5
-rw-r--r--src/net/messageout.cpp15
-rw-r--r--src/net/messageout.h2
-rw-r--r--src/net/network.cpp35
4 files changed, 38 insertions, 19 deletions
diff --git a/src/gui/login.cpp b/src/gui/login.cpp
index 83a20f8b..b4d431d2 100644
--- a/src/gui/login.cpp
+++ b/src/gui/login.cpp
@@ -346,6 +346,11 @@ int attemptLogin(const std::string& user, const std::string& pass)
// Receive reply
MessageIn msg = get_next_message();
+ if (state == ERROR_STATE)
+ {
+ close_session();
+ return LOGIN_UNKNOWN_ERROR;
+ }
// Login ok
if (msg.getId() == 0x0069)
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);
}