diff options
Diffstat (limited to 'src/net')
-rw-r--r-- | src/net/network.cpp | 50 | ||||
-rw-r--r-- | src/net/network.h | 67 |
2 files changed, 78 insertions, 39 deletions
diff --git a/src/net/network.cpp b/src/net/network.cpp index 8da0033d..c90e0201 100644 --- a/src/net/network.cpp +++ b/src/net/network.cpp @@ -28,6 +28,8 @@ #include "../log.h" +#include <sstream> + /** Warning: buffers and other variables are shared, so there can be only one connection active at a time */ @@ -126,8 +128,7 @@ bool Network::connect(const std::string &address, short port) if (address.empty()) { - logger->log("Empty address given to Network::connect()!"); - mState = NET_ERROR; + setError("Empty address given to Network::connect()!"); return false; } @@ -145,8 +146,7 @@ bool Network::connect(const std::string &address, short port) mWorkerThread = SDL_CreateThread(networkThread, this); if (!mWorkerThread) { - logger->log("Unable to create network worker thread"); - mState = NET_ERROR; + setError("Unable to create network worker thread"); return false; } @@ -172,12 +172,9 @@ void Network::disconnect() void Network::registerHandler(MessageHandler *handler) { - const Uint16 *i = handler->handledMessages; - - while(*i) + for (const Uint16 *i = handler->handledMessages; *i; i++) { mMessageHandlers[*i] = handler; - i++; } handler->setNetwork(this); @@ -232,8 +229,8 @@ void Network::flush() ret = SDLNet_TCP_Send(mSocket, mOutBuffer, mOutSize); if (ret < (int)mOutSize) { - logger->log("Error in SDLNet_TCP_Send(): %s", SDLNet_GetError()); - mState = NET_ERROR; + setError("Error in SDLNet_TCP_Send(): " + + std::string(SDLNet_GetError())); } mOutSize = 0; SDL_mutexV(mMutex); @@ -314,8 +311,9 @@ bool Network::realConnect() if (SDLNet_ResolveHost(&ipAddress, mAddress.c_str(), mPort) == -1) { - logger->log("Error in SDLNet_ResolveHost(): %s", SDLNet_GetError()); - mState = NET_ERROR; + std::string error = "Unable to resolve host \"" + mAddress + "\""; + setError(error); + logger->log("SDLNet_ResolveHost: %s", error.c_str()); return false; } @@ -325,7 +323,7 @@ bool Network::realConnect() if (!mSocket) { logger->log("Error in SDLNet_TCP_Open(): %s", SDLNet_GetError()); - mState = NET_ERROR; + setError(SDLNet_GetError()); return false; } @@ -343,15 +341,15 @@ void Network::receive() if (!(set = SDLNet_AllocSocketSet(1))) { - logger->log("Error in SDLNet_AllocSocketSet(): %s", SDLNet_GetError()); - mState = NET_ERROR; + setError("Error in SDLNet_AllocSocketSet(): " + + std::string(SDLNet_GetError())); return; } if (SDLNet_TCP_AddSocket(set, mSocket) == -1) { - logger->log("Error in SDLNet_AddSocket(): %s", SDLNet_GetError()); - mState = NET_ERROR; + setError("Error in SDLNet_AddSocket(): " + + std::string(SDLNet_GetError())); } while (mState == CONNECTED) @@ -381,8 +379,8 @@ void Network::receive() } else if (ret < 0) { - logger->log("Error in SDLNet_TCP_Recv(): %s", SDLNet_GetError()); - mState = NET_ERROR; + setError("Error in SDLNet_TCP_Recv(): " + + std::string(SDLNet_GetError())); } else { mInSize += ret; @@ -407,8 +405,10 @@ void Network::receive() default: // more than one socket is ready.. // this should not happen since we only listen once socket. - logger->log("Error in SDLNet_TCP_Recv(), %d sockets are ready : %s", numReady, SDLNet_GetError()); - mState = NET_ERROR; + std::stringstream errorStream; + errorStream << "Error in SDLNet_TCP_Recv(), " << numReady + << " sockets are ready: " << SDLNet_GetError(); + setError(errorStream.str()); break; } } @@ -434,6 +434,14 @@ char *iptostring(int address) return asciiIP; } +void +Network::setError(const std::string& error) +{ + logger->log("Network error: %s", error.c_str()); + mError = error; + mState = NET_ERROR; +} + Uint16 Network::readWord(int pos) { #if SDL_BYTEORDER == SDL_BIG_ENDIAN diff --git a/src/net/network.h b/src/net/network.h index 13929ec1..d6fec761 100644 --- a/src/net/network.h +++ b/src/net/network.h @@ -41,29 +41,52 @@ class Network friend class MessageOut; Network(); + ~Network(); - bool connect(const std::string &address, short port); - void disconnect(); + bool + connect(const std::string &address, short port); + + void + disconnect(); + + void + registerHandler(MessageHandler *handler); + + void + unregisterHandler(MessageHandler *handler); + + void + clearHandlers(); + + int + getState() const { return mState; } - void registerHandler(MessageHandler *handler); - void unregisterHandler(MessageHandler *handler); - void clearHandlers(); + const std::string& + getError() const { return mError; } - int getState() const { return mState; } - bool isConnected() const { return mState == CONNECTED; } + bool + isConnected() const { return mState == CONNECTED; } - int getInSize() const { return mInSize; } + int + getInSize() const { return mInSize; } - void skip(int len); + void + skip(int len); - bool messageReady(); - MessageIn getNextMessage(); + bool + messageReady(); - void dispatchMessages(); - void flush(); + MessageIn + getNextMessage(); - // ERROR replaced by NET_ERROR because already defined in Windows + void + dispatchMessages(); + + void + flush(); + + // ERROR replaced by NET_ERROR because already defined in Windows enum { IDLE, CONNECTED, @@ -73,7 +96,17 @@ class Network }; protected: - Uint16 readWord(int pos); + void + setError(const std::string& error); + + Uint16 + readWord(int pos); + + bool + realConnect(); + + void + receive(); TCPsocket mSocket; @@ -86,6 +119,7 @@ class Network unsigned int mToSkip; int mState; + std::string mError; SDL_Thread *mWorkerThread; SDL_mutex *mMutex; @@ -93,9 +127,6 @@ class Network typedef std::map<Uint16, MessageHandler*> MessageHandlers; typedef MessageHandlers::iterator MessageHandlerIterator; MessageHandlers mMessageHandlers; - - bool realConnect(); - void receive(); }; /** Convert an address from int format to string */ |