diff --git a/src/net/common/senderthread.cpp b/src/net/common/senderthread.cpp index f0e15cb1..f3b60b04 100644 --- a/src/net/common/senderthread.cpp +++ b/src/net/common/senderthread.cpp @@ -22,10 +22,12 @@ #include #include +#include #include using namespace std; +#define SEND_TIMEOUT_MSEC 2000 SenderThread::SenderThread(SenderCallback &cb) : m_tmpOutBufSize(0), m_callback(cb) @@ -100,11 +102,26 @@ SenderThread::InternalStore(SendDataDeque &sendQueue, unsigned maxQueueSize, boo // TODO: Throw exception if failed. } +void +SenderThread::RemoveCurSendData() +{ + m_tmpOutBufSize = 0; + m_curSession.reset(); + // TODO use callback to remove session. +} + void SenderThread::Main() { + boost::timers::portable::microsec_timer sendTimer(boost::posix_time::time_duration(0, 0, 0), boost::timers::portable::microsec_timer::manual_start); + while (!ShouldTerminate()) { + if (sendTimer.is_running() && sendTimer.elapsed().total_milliseconds() > SEND_TIMEOUT_MSEC) + { + RemoveCurSendData(); + sendTimer.reset(); + } // Send remaining bytes of output buffer OR // copy ONE packet to output buffer. // For reasons of simplicity, only one packet is sent at a time. @@ -135,13 +152,15 @@ SenderThread::Main() if (tmpData.first.get()) { if (tmpData.second.get()) - m_curSession = tmpData.second; - - u_int16_t tmpLen = tmpData.first->GetLen(); - if (tmpLen <= MAX_PACKET_SIZE) { - m_tmpOutBufSize = tmpLen; - memcpy(m_tmpOutBuf, tmpData.first->GetRawData(), tmpLen); + u_int16_t tmpLen = tmpData.first->GetLen(); + if (tmpLen <= MAX_PACKET_SIZE) + { + m_curSession = tmpData.second; + m_tmpOutBufSize = tmpLen; + memcpy(m_tmpOutBuf, tmpData.first->GetRawData(), tmpLen); + sendTimer.restart(); + } } } } @@ -167,8 +186,7 @@ SenderThread::Main() // Ignore invalid or not connected sockets. if (errCode != SOCKET_ERR_NOTCONN && errCode != SOCKET_ERR_NOTSOCK) m_callback.SignalNetError(m_curSession->GetId(), ERR_SOCK_SELECT_FAILED, errCode); - m_tmpOutBufSize = 0; - m_curSession.reset(); + RemoveCurSendData(); } Msleep(SEND_TIMEOUT_MSEC); } @@ -187,8 +205,7 @@ SenderThread::Main() // Ignore invalid or not connected sockets. if (errCode != SOCKET_ERR_NOTCONN && errCode != SOCKET_ERR_NOTSOCK) m_callback.SignalNetError(m_curSession->GetId(), ERR_SOCK_SEND_FAILED, errCode); - m_tmpOutBufSize = 0; - m_curSession.reset(); + RemoveCurSendData(); } Msleep(SEND_TIMEOUT_MSEC); } @@ -201,6 +218,7 @@ SenderThread::Main() { m_tmpOutBufSize = 0; m_curSession.reset(); + sendTimer.reset(); } } } diff --git a/src/net/senderthread.h b/src/net/senderthread.h index 36ef247a..e8388d76 100644 --- a/src/net/senderthread.h +++ b/src/net/senderthread.h @@ -30,7 +30,7 @@ #include #include -#define SENDER_THREAD_TERMINATE_TIMEOUT 10000 +#define SENDER_THREAD_TERMINATE_TIMEOUT THREAD_WAIT_INFINITE #define SEND_TIMEOUT_MSEC 10 #define SEND_QUEUE_SIZE 1000 #define SEND_LOW_PRIO_QUEUE_SIZE 50000 @@ -57,6 +57,8 @@ protected: void InternalStore(SendDataDeque &sendQueue, unsigned maxQueueSize, boost::shared_ptr session, boost::shared_ptr packet); void InternalStore(SendDataDeque &sendQueue, unsigned maxQueueSize, boost::shared_ptr session, const NetPacketList &packetList); + void RemoveCurSendData(); + private: boost::shared_ptr m_curSession; diff --git a/src/net/servergamethread.h b/src/net/servergamethread.h index 7f51bc14..37de7932 100644 --- a/src/net/servergamethread.h +++ b/src/net/servergamethread.h @@ -27,7 +27,7 @@ #include -#define GAME_THREAD_TERMINATE_TIMEOUT 20000 +#define GAME_THREAD_TERMINATE_TIMEOUT THREAD_WAIT_INFINITE class SenderThread;