diff options
author | Ben Longbons <b.r.longbons@gmail.com> | 2011-09-15 04:25:41 +0800 |
---|---|---|
committer | Yohann Ferreira <yohann.ferreira@orange.fr> | 2011-09-16 07:10:20 +0800 |
commit | 8e8fef21ee978fa2ab58b0639a351ba17c66d5d4 (patch) | |
tree | 36556df326cdc37f87b00b37feaa1cd277629ed7 /src/net/tmwa | |
parent | d4584d81599f90b54cbc0fdd1c55b8ab0d5f0d68 (diff) | |
download | mana-8e8fef21ee978fa2ab58b0639a351ba17c66d5d4.tar.gz mana-8e8fef21ee978fa2ab58b0639a351ba17c66d5d4.tar.bz2 mana-8e8fef21ee978fa2ab58b0639a351ba17c66d5d4.tar.xz mana-8e8fef21ee978fa2ab58b0639a351ba17c66d5d4.zip |
Instead of SDL_mutex, use Mutex wrapper and MutexLocker for safety
Reviewed-by: Thorbjørn Lindeijer <thorbjorn@lindeijer.nl>
Diffstat (limited to 'src/net/tmwa')
-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; |