From 4f948c82ea95c6b9fac307f455fe91851e27017a Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Tue, 8 Oct 2013 23:09:49 +0300 Subject: split network mutexe into read and write mutexes. --- src/net/ea/network.cpp | 26 +++++++++++++++----------- src/net/ea/network.h | 3 ++- src/net/eathena/network.cpp | 8 ++++---- src/net/tmwa/network.cpp | 8 ++++---- 4 files changed, 25 insertions(+), 20 deletions(-) diff --git a/src/net/ea/network.cpp b/src/net/ea/network.cpp index 87ab01722..ab76fd220 100644 --- a/src/net/ea/network.cpp +++ b/src/net/ea/network.cpp @@ -61,7 +61,8 @@ Network::Network() : mState(IDLE), mError(), mWorkerThread(nullptr), - mMutex(SDL_CreateMutex()), + mMutexIn(SDL_CreateMutex()), + mMutexOut(SDL_CreateMutex()), mSleep(config.getIntValue("networksleep")) { TcpNet::init(); @@ -72,8 +73,10 @@ Network::~Network() if (mState != IDLE && mState != NET_ERROR) disconnect(); - SDL_DestroyMutex(mMutex); - mMutex = nullptr; + SDL_DestroyMutex(mMutexIn); + mMutexIn = nullptr; + SDL_DestroyMutex(mMutexOut); + mMutexOut = nullptr; delete []mInBuffer; delete []mOutBuffer; @@ -146,25 +149,26 @@ void Network::flush() if (!mOutSize || mState != CONNECTED) return; - SDL_mutexP(mMutex); + SDL_mutexP(mMutexOut); const int ret = TcpNet::send(mSocket, mOutBuffer, mOutSize); DEBUGLOG(std::string("Send ").append(toString(mOutSize)).append(" bytes")); if (ret < static_cast(mOutSize)) { + SDL_mutexV(mMutexOut); setError("Error in TcpNet::send(): " + std::string(TcpNet::getError())); } mOutSize = 0; - SDL_mutexV(mMutex); + SDL_mutexV(mMutexOut); } void Network::skip(const int len) { - SDL_mutexP(mMutex); + SDL_mutexP(mMutexIn); mToSkip += len; if (!mInSize) { - SDL_mutexV(mMutex); + SDL_mutexV(mMutexIn); return; } @@ -179,7 +183,7 @@ void Network::skip(const int len) mToSkip -= mInSize; mInSize = 0; } - SDL_mutexV(mMutex); + SDL_mutexV(mMutexIn); } bool Network::realConnect() @@ -252,10 +256,10 @@ void Network::receive() case 1: { // Receive data from the socket - SDL_mutexP(mMutex); + SDL_mutexP(mMutexIn); if (mInSize > BUFFER_LIMIT) { - SDL_mutexV(mMutex); + SDL_mutexV(mMutexIn); SDL_Delay(100); continue; } @@ -294,7 +298,7 @@ void Network::receive() } } } - SDL_mutexV(mMutex); + SDL_mutexV(mMutexIn); break; } diff --git a/src/net/ea/network.h b/src/net/ea/network.h index 81a95566b..9298f021f 100644 --- a/src/net/ea/network.h +++ b/src/net/ea/network.h @@ -104,7 +104,8 @@ class Network std::string mError; SDL_Thread *mWorkerThread; - SDL_mutex *mMutex; + SDL_mutex *mMutexIn; + SDL_mutex *mMutexOut; int mSleep; }; diff --git a/src/net/eathena/network.cpp b/src/net/eathena/network.cpp index 4786d9ee7..481a945ba 100644 --- a/src/net/eathena/network.cpp +++ b/src/net/eathena/network.cpp @@ -162,7 +162,7 @@ void Network::dispatchMessages() { while (messageReady()) { - SDL_mutexP(mMutex); + SDL_mutexP(mMutexIn); const int msgId = readWord(0); int len = -1; if (msgId == SMSG_SERVER_VERSION_RESPONSE) @@ -183,7 +183,7 @@ void Network::dispatchMessages() len = readWord(2); MessageIn msg(mInBuffer, len); - SDL_mutexV(mMutex); + SDL_mutexV(mMutexIn); if (len == 0) { @@ -210,7 +210,7 @@ bool Network::messageReady() { int len = -1; - SDL_mutexP(mMutex); + SDL_mutexP(mMutexIn); if (mInSize >= 2) { const int msgId = readWord(0); @@ -226,7 +226,7 @@ bool Network::messageReady() } const bool ret = (mInSize >= static_cast(len)); - SDL_mutexV(mMutex); + SDL_mutexV(mMutexIn); return ret; } diff --git a/src/net/tmwa/network.cpp b/src/net/tmwa/network.cpp index 175713d49..9c1508ccb 100644 --- a/src/net/tmwa/network.cpp +++ b/src/net/tmwa/network.cpp @@ -141,7 +141,7 @@ void Network::dispatchMessages() BLOCK_START("Network::dispatchMessages 1") while (messageReady()) { - SDL_mutexP(mMutex); + SDL_mutexP(mMutexIn); BLOCK_START("Network::dispatchMessages 2") const int msgId = readWord(0); int len; @@ -156,7 +156,7 @@ void Network::dispatchMessages() len = readWord(2); MessageIn msg(mInBuffer, len); - SDL_mutexV(mMutex); + SDL_mutexV(mMutexIn); BLOCK_END("Network::dispatchMessages 2") BLOCK_START("Network::dispatchMessages 3") @@ -187,7 +187,7 @@ bool Network::messageReady() { int len = -1; - SDL_mutexP(mMutex); + SDL_mutexP(mMutexIn); if (mInSize >= 2) { const int msgId = readWord(0); @@ -210,7 +210,7 @@ bool Network::messageReady() } const bool ret = (mInSize >= static_cast(len)); - SDL_mutexV(mMutex); + SDL_mutexV(mMutexIn); return ret; } -- cgit v1.2.3-70-g09d2