diff options
author | Ben Longbons <b.r.longbons@gmail.com> | 2011-09-14 13:25:41 -0700 |
---|---|---|
committer | Ben Longbons <b.r.longbons@gmail.com> | 2011-09-15 14:20:36 -0700 |
commit | 0d6c98b33612b548b400e2bd396933936327f6de (patch) | |
tree | 64d6d2c90eb4c586ce921907c297e44c1346d9f2 | |
parent | 421cfc174dca220ad092759f013fa413efb07294 (diff) | |
download | mana-0d6c98b33612b548b400e2bd396933936327f6de.tar.gz mana-0d6c98b33612b548b400e2bd396933936327f6de.tar.bz2 mana-0d6c98b33612b548b400e2bd396933936327f6de.tar.xz mana-0d6c98b33612b548b400e2bd396933936327f6de.zip |
Instead of SDL_mutex, use Mutex wrapper and MutexLocker for safety
Reviewed-by: Thorbjørn Lindeijer <thorbjorn@lindeijer.nl>
-rw-r--r-- | src/net/tmwa/network.cpp | 31 | ||||
-rw-r--r-- | src/net/tmwa/network.h | 4 |
2 files changed, 12 insertions, 23 deletions
diff --git a/src/net/tmwa/network.cpp b/src/net/tmwa/network.cpp index 4d2073a0..44559170 100644 --- a/src/net/tmwa/network.cpp +++ b/src/net/tmwa/network.cpp @@ -111,7 +111,6 @@ Network::Network(): { SDLNet_Init(); - mMutex = SDL_CreateMutex(); mInstance = this; } @@ -122,7 +121,6 @@ Network::~Network() if (mState != IDLE && mState != NET_ERROR) disconnect(); - SDL_DestroyMutex(mMutex); mInstance = 0; delete[] mInBuffer; @@ -246,8 +244,7 @@ void Network::flush() int ret; - - SDL_mutexP(mMutex); + MutexLocker lock(&mMutex); ret = SDLNet_TCP_Send(mSocket, mOutBuffer, mOutSize); if (ret < (int)mOutSize) { @@ -255,18 +252,14 @@ void Network::flush() std::string(SDLNet_GetError())); } mOutSize = 0; - SDL_mutexV(mMutex); } void Network::skip(int len) { - SDL_mutexP(mMutex); + MutexLocker lock(&mMutex); mToSkip += len; if (!mInSize) - { - SDL_mutexV(mMutex); return; - } if (mInSize >= mToSkip) { @@ -279,14 +272,13 @@ void Network::skip(int len) mToSkip -= mInSize; mInSize = 0; } - SDL_mutexV(mMutex); } bool Network::messageReady() { int len = -1, msgId; - SDL_mutexP(mMutex); + MutexLocker lock(&mMutex); if (mInSize >= 2) { msgId = readWord(0); @@ -300,10 +292,7 @@ bool Network::messageReady() } - bool ret = (mInSize >= static_cast<unsigned int>(len)); - SDL_mutexV(mMutex); - - return ret; + return mInSize >= static_cast<unsigned int>(len); } MessageIn Network::getNextMessage() @@ -314,7 +303,7 @@ MessageIn Network::getNextMessage() break; } - SDL_mutexP(mMutex); + MutexLocker lock(&mMutex); int msgId = readWord(0); int len; if (msgId == SMSG_SERVER_VERSION_RESPONSE) @@ -329,10 +318,7 @@ MessageIn Network::getNextMessage() logger->log("Received packet 0x%x of length %d", msgId, len); #endif - MessageIn msg(mInBuffer, len); - SDL_mutexV(mMutex); - - return msg; + return MessageIn(mInBuffer, len); } bool Network::realConnect() @@ -399,8 +385,9 @@ void Network::receive() break; case 1: + { // Receive data from the socket - SDL_mutexP(mMutex); + MutexLocker lock(&mMutex); ret = SDLNet_TCP_Recv(mSocket, mInBuffer + mInSize, BUFFER_SIZE - mInSize); if (!ret) @@ -432,8 +419,8 @@ void Network::receive() } } } - SDL_mutexV(mMutex); break; + } default: // more than one socket is ready.. diff --git a/src/net/tmwa/network.h b/src/net/tmwa/network.h index 2c19c218..b82d07b3 100644 --- a/src/net/tmwa/network.h +++ b/src/net/tmwa/network.h @@ -22,6 +22,8 @@ #ifndef NET_TA_NETWORK_H #define NET_TA_NETWORK_H +#include "utils/mutex.h" + #include "net/serverinfo.h" #include "net/tmwa/messagehandler.h" @@ -116,7 +118,7 @@ class Network std::string mError; SDL_Thread *mWorkerThread; - SDL_mutex *mMutex; + Mutex mMutex; typedef std::map<Uint16, MessageHandler*> MessageHandlers; typedef MessageHandlers::iterator MessageHandlerIterator; |