New boost::asio based sender thread. Does not work on windows yet, because the completion routine is never called. No idea why not.
This commit is contained in:
+50
-103
@@ -26,16 +26,29 @@
|
||||
#include <cassert>
|
||||
|
||||
#include <boost/bind.hpp>
|
||||
#include <boost/asio.hpp>
|
||||
|
||||
using namespace std;
|
||||
using boost::asio::ip::tcp;
|
||||
|
||||
|
||||
#define SEND_ERROR_TIMEOUT_MSEC 20000
|
||||
#define SEND_TIMEOUT_MSEC 10
|
||||
#define SEND_QUEUE_SIZE 10000000
|
||||
#define SEND_LOG_INTERVAL_SEC 60
|
||||
|
||||
|
||||
|
||||
void
|
||||
SenderThread::SendDataManager::HandleWrite(const boost::system::error_code& error)
|
||||
{
|
||||
SetWriteInProgress(false);
|
||||
SetCompleted(true);
|
||||
}
|
||||
|
||||
|
||||
SenderThread::SenderThread(SenderCallback &cb)
|
||||
: m_bytesSent(0), m_callback(cb)
|
||||
: m_callback(cb)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -66,18 +79,12 @@ SenderThread::Send(boost::shared_ptr<SessionData> session, boost::shared_ptr<Net
|
||||
{
|
||||
if (packet.get() && session.get())
|
||||
{
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_sendQueueMutex);
|
||||
if (m_sendQueue.size() < SEND_QUEUE_SIZE)
|
||||
{
|
||||
m_sendQueue.push_back(packet);
|
||||
}
|
||||
}
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_sessionDataMutex);
|
||||
m_sessionSocket = session->GetSocket();
|
||||
m_sessionId = session->GetId();
|
||||
}
|
||||
boost::mutex::scoped_lock lock(m_sendQueueMapMutex);
|
||||
SendQueueMap::iterator pos = m_sendQueueMap.find(session->GetId());
|
||||
if (pos == m_sendQueueMap.end())
|
||||
pos = m_sendQueueMap.insert(SendQueueMap::value_type(session->GetId(), boost::shared_ptr<SendDataManager>(new SendDataManager(session, m_ioService)))).first;
|
||||
if (pos->second->list.size() < SEND_QUEUE_SIZE)
|
||||
pos->second->list.push_back(packet);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,22 +93,20 @@ SenderThread::Send(boost::shared_ptr<SessionData> session, const NetPacketList &
|
||||
{
|
||||
if (!packetList.empty() && session.get())
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_sendQueueMutex);
|
||||
if (m_sendQueue.size() + packetList.size() <= SEND_QUEUE_SIZE)
|
||||
boost::mutex::scoped_lock lock(m_sendQueueMapMutex);
|
||||
SendQueueMap::iterator pos = m_sendQueueMap.find(session->GetId());
|
||||
if (pos == m_sendQueueMap.end())
|
||||
pos = m_sendQueueMap.insert(SendQueueMap::value_type(session->GetId(), boost::shared_ptr<SendDataManager>(new SendDataManager(session, m_ioService)))).first;
|
||||
if (pos->second->list.size() + packetList.size() <= SEND_QUEUE_SIZE)
|
||||
{
|
||||
NetPacketList::const_iterator i = packetList.begin();
|
||||
NetPacketList::const_iterator end = packetList.end();
|
||||
while (i != end)
|
||||
{
|
||||
m_sendQueue.push_back(*i);
|
||||
pos->second->list.push_back(*i);
|
||||
++i;
|
||||
}
|
||||
}
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_sessionDataMutex);
|
||||
m_sessionSocket = session->GetSocket();
|
||||
m_sessionId = session->GetId();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -110,98 +115,40 @@ SenderThread::Main()
|
||||
{
|
||||
while (!ShouldTerminate())
|
||||
{
|
||||
if (!m_curPacket)
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_sendQueueMutex);
|
||||
if (!m_sendQueue.empty())
|
||||
boost::mutex::scoped_lock lock(m_sendQueueMapMutex);
|
||||
SendQueueMap::iterator i = m_sendQueueMap.begin();
|
||||
SendQueueMap::iterator end = m_sendQueueMap.end();
|
||||
while (i != end)
|
||||
{
|
||||
m_curPacket = m_sendQueue.front();
|
||||
m_sendQueue.pop_front();
|
||||
}
|
||||
}
|
||||
|
||||
if (m_curPacket)
|
||||
{
|
||||
const unsigned tmpLen = m_curPacket->GetLen();
|
||||
if (tmpLen > MAX_PACKET_SIZE)
|
||||
m_curPacket.reset(); // TODO log
|
||||
else
|
||||
{
|
||||
SOCKET tmpSocket;
|
||||
SendQueueMap::iterator next = i;
|
||||
++next;
|
||||
boost::shared_ptr<SendDataManager> tmpManager = i->second;
|
||||
if (tmpManager->list.empty())
|
||||
m_sendQueueMap.erase(i);
|
||||
else
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_sessionDataMutex);
|
||||
tmpSocket = m_sessionSocket;
|
||||
}
|
||||
|
||||
// send next chunk of data
|
||||
int bytesSent = send(tmpSocket, ((const char *)m_curPacket->GetRawData()) + m_bytesSent, tmpLen - m_bytesSent, SOCKET_SEND_FLAGS);
|
||||
|
||||
if (!IS_VALID_SEND(bytesSent))
|
||||
{
|
||||
// Never assume that this is a fatal error.
|
||||
int errCode = SOCKET_ERRNO();
|
||||
if (IS_SOCKET_ERR_WOULDBLOCK(errCode))
|
||||
if (!tmpManager->IsWriteInProgress())
|
||||
{
|
||||
fd_set writeSet;
|
||||
struct timeval timeout;
|
||||
|
||||
FD_ZERO(&writeSet);
|
||||
FD_SET(tmpSocket, &writeSet);
|
||||
|
||||
timeout.tv_sec = 0;
|
||||
timeout.tv_usec = SEND_TIMEOUT_MSEC * 1000;
|
||||
int selectResult = select(tmpSocket + 1, NULL, &writeSet, NULL, &timeout);
|
||||
if (!IS_VALID_SELECT(selectResult))
|
||||
if (tmpManager->IsCompleted())
|
||||
tmpManager->list.pop_front();
|
||||
else
|
||||
{
|
||||
// Never assume that this is a fatal error.
|
||||
int errCode = SOCKET_ERRNO();
|
||||
if (!IS_SOCKET_ERR_WOULDBLOCK(errCode))
|
||||
{
|
||||
// Skip this packet - this is bad, and is therefore reported.
|
||||
// Ignore invalid or not connected sockets.
|
||||
if (errCode != SOCKET_ERR_NOTCONN && errCode != SOCKET_ERR_NOTSOCK)
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_sessionDataMutex);
|
||||
m_callback.SignalNetError(m_sessionId, ERR_SOCK_SELECT_FAILED, errCode);
|
||||
}
|
||||
}
|
||||
Msleep(SEND_TIMEOUT_MSEC);
|
||||
boost::shared_ptr<NetPacket> tmpPacket = tmpManager->list.front();
|
||||
boost::asio::async_write(
|
||||
*tmpManager->socket,
|
||||
boost::asio::buffer(tmpPacket->GetRawData(),
|
||||
tmpPacket->GetLen()),
|
||||
boost::bind(&SendDataManager::HandleWrite, tmpManager,
|
||||
boost::asio::placeholders::error));
|
||||
tmpManager->SetWriteInProgress(true);
|
||||
}
|
||||
}
|
||||
else // other errors than would block
|
||||
{
|
||||
// Skip this packet - this is bad, and is therefore reported.
|
||||
// Ignore invalid or not connected sockets.
|
||||
if (errCode != SOCKET_ERR_NOTCONN && errCode != SOCKET_ERR_NOTSOCK)
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_sessionDataMutex);
|
||||
m_callback.SignalNetError(m_sessionId, ERR_SOCK_SEND_FAILED, errCode);
|
||||
}
|
||||
Msleep(SEND_TIMEOUT_MSEC);
|
||||
}
|
||||
}
|
||||
else if ((unsigned)bytesSent + m_bytesSent < tmpLen)
|
||||
{
|
||||
if (bytesSent)
|
||||
{
|
||||
// Send was partly successful.
|
||||
m_bytesSent += bytesSent;
|
||||
}
|
||||
else
|
||||
Msleep(SEND_TIMEOUT_MSEC);
|
||||
}
|
||||
else //if ((unsigned)bytesSent + m_bytesSent == tmpLen)
|
||||
{
|
||||
m_curPacket.reset();
|
||||
m_bytesSent = 0;
|
||||
}
|
||||
i = next;
|
||||
}
|
||||
}
|
||||
else
|
||||
Msleep(SEND_TIMEOUT_MSEC);
|
||||
}
|
||||
}
|
||||
boost::mutex::scoped_lock lock(m_sessionDataMutex);
|
||||
if (m_sessionSocket != INVALID_SOCKET)
|
||||
CLOSESOCKET(m_sessionSocket);
|
||||
}
|
||||
|
||||
|
||||
@@ -99,7 +99,12 @@ ServerAcceptThread::Listen()
|
||||
if (context.GetServerPort() < 1024)
|
||||
throw ServerException(__FILE__, __LINE__, ERR_SOCK_INVALID_PORT, 0);
|
||||
|
||||
#ifdef _WIN32
|
||||
context.SetSocket(WSASocket(context.GetAddrFamily(), SOCK_STREAM, context.GetProtocol(), 0, 0, WSA_FLAG_OVERLAPPED));
|
||||
#else
|
||||
context.SetSocket(socket(context.GetAddrFamily(), SOCK_STREAM, context.GetProtocol()));
|
||||
#endif
|
||||
|
||||
if (!IS_VALID_SOCKET(context.GetSocket()))
|
||||
throw ServerException(__FILE__, __LINE__, ERR_SOCK_CREATION_FAILED, SOCKET_ERRNO());
|
||||
|
||||
|
||||
@@ -69,9 +69,9 @@ public:
|
||||
// A serious send error should trigger a read error or a read
|
||||
// returning 0 afterwards, and we will handle this error.
|
||||
}
|
||||
virtual void SignalSessionTerminated(unsigned session)
|
||||
virtual void SignalSessionTerminated(unsigned /*session*/)
|
||||
{
|
||||
m_server.RemoveSender(session);
|
||||
// Nothing to do, since we only have one sender thread.
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -85,6 +85,7 @@ ServerLobbyThread::ServerLobbyThread(GuiInterface &gui, ConfigFile *playerConfig
|
||||
m_statDataChanged(false), m_startTime(boost::posix_time::second_clock::local_time())
|
||||
{
|
||||
m_senderCallback.reset(new ServerSenderCallback(*this));
|
||||
m_sender.reset(new SenderThread(*m_senderCallback));
|
||||
m_receiver.reset(new ReceiverHelper);
|
||||
}
|
||||
|
||||
@@ -304,13 +305,6 @@ ServerLobbyThread::RemoveGame(unsigned id)
|
||||
m_removeGameList.push_back(id);
|
||||
}
|
||||
|
||||
void
|
||||
ServerLobbyThread::RemoveSender(unsigned session)
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_removeSenderListMutex);
|
||||
m_removeSenderList.push_back(session);
|
||||
}
|
||||
|
||||
AvatarManager &
|
||||
ServerLobbyThread::GetAvatarManager()
|
||||
{
|
||||
@@ -356,6 +350,8 @@ ServerLobbyThread::Main()
|
||||
{
|
||||
try
|
||||
{
|
||||
m_sender->Start();
|
||||
|
||||
while (!ShouldTerminate())
|
||||
{
|
||||
// Process new connections.
|
||||
@@ -368,8 +364,6 @@ ServerLobbyThread::Main()
|
||||
RemoveGameLoop();
|
||||
// Kick players.
|
||||
RemovePlayerLoop();
|
||||
// Remove sender threads.
|
||||
RemoveSenderLoop();
|
||||
// Resubscribe Lobby Messages if needed.
|
||||
ResubscribeLobbyMsgLoop();
|
||||
// Check session timeouts.
|
||||
@@ -388,6 +382,8 @@ ServerLobbyThread::Main()
|
||||
}
|
||||
|
||||
TerminateGames();
|
||||
m_sender->SignalStop();
|
||||
m_sender->WaitStop();
|
||||
|
||||
CleanupConnectQueue();
|
||||
}
|
||||
@@ -875,30 +871,6 @@ ServerLobbyThread::RemovePlayerLoop()
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
ServerLobbyThread::RemoveSenderLoop()
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_removeSenderListMutex);
|
||||
|
||||
RemoveSenderList::iterator i = m_removeSenderList.begin();
|
||||
RemoveSenderList::iterator end = m_removeSenderList.end();
|
||||
|
||||
// Synchronously remove Sender Threads whose Sessions have been closed.
|
||||
while (i != end)
|
||||
{
|
||||
SenderMap::iterator pos = m_senderMap.find(*i);
|
||||
if (pos != m_senderMap.end())
|
||||
{
|
||||
boost::shared_ptr<SenderInterface> tmpSender = pos->second;
|
||||
tmpSender->SignalStop();
|
||||
tmpSender->WaitStop();
|
||||
m_senderMap.erase(pos);
|
||||
}
|
||||
++i;
|
||||
}
|
||||
m_removeSenderList.clear();
|
||||
}
|
||||
|
||||
void
|
||||
ServerLobbyThread::ResubscribeLobbyMsgLoop()
|
||||
{
|
||||
@@ -1084,11 +1056,8 @@ ServerLobbyThread::HandleNewConnection(boost::shared_ptr<ConnectData> connData)
|
||||
//}
|
||||
|
||||
// Create a new session.
|
||||
boost::shared_ptr<SenderInterface> senderThread(new SenderThread(*m_senderCallback));
|
||||
senderThread->Start();
|
||||
boost::shared_ptr<SessionData> sessionData(new SessionData(connData->ReleaseSocket(), m_curSessionId++, senderThread, *m_senderCallback));
|
||||
boost::shared_ptr<SessionData> sessionData(new SessionData(connData->ReleaseSocket(), m_curSessionId++, m_sender, *m_senderCallback));
|
||||
m_sessionManager.AddSession(sessionData);
|
||||
m_senderMap[sessionData->GetId()] = senderThread;
|
||||
|
||||
LOG_VERBOSE("Accepted connection - session #" << sessionData->GetId() << ".");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user