Fixed one thread per session implementation. Kind of. Still seems to have a handle leak on Windows. However, it does not scale at all and needs to be reverted. It won't even handle 500 players with 500 games on a dual core high end machine.
This commit is contained in:
@@ -18,9 +18,9 @@
|
||||
***************************************************************************/
|
||||
|
||||
#include <net/clientcontext.h>
|
||||
#include <net/sendercallback.h>
|
||||
#include <net/senderthread.h>
|
||||
|
||||
class ClientSenderCallback : public SenderCallback
|
||||
class ClientSenderCallback : public SenderCallback, public SessionDataCallback
|
||||
{
|
||||
public:
|
||||
ClientSenderCallback() {}
|
||||
@@ -30,6 +30,10 @@ public:
|
||||
{
|
||||
}
|
||||
|
||||
virtual void SignalSessionTerminated(unsigned /*session*/)
|
||||
{
|
||||
}
|
||||
|
||||
private:
|
||||
};
|
||||
|
||||
@@ -40,10 +44,14 @@ ClientContext::ClientContext()
|
||||
{
|
||||
bzero(&m_clientSockaddr, sizeof(m_clientSockaddr));
|
||||
m_senderCallback.reset(new ClientSenderCallback());
|
||||
m_senderThread.reset(new SenderThread(*m_senderCallback));
|
||||
m_senderThread->Start();
|
||||
}
|
||||
|
||||
ClientContext::~ClientContext()
|
||||
{
|
||||
m_senderThread->SignalStop();
|
||||
m_senderThread->WaitStop();
|
||||
}
|
||||
|
||||
SOCKET
|
||||
@@ -56,7 +64,7 @@ ClientContext::GetSocket() const
|
||||
void
|
||||
ClientContext::SetSocket(SOCKET sockfd)
|
||||
{
|
||||
m_sessionData.reset(new SessionData(sockfd, SESSION_ID_GENERIC, *m_senderCallback));
|
||||
m_sessionData.reset(new SessionData(sockfd, SESSION_ID_GENERIC, m_senderThread, *m_senderCallback));
|
||||
}
|
||||
|
||||
boost::shared_ptr<SessionData>
|
||||
|
||||
@@ -74,8 +74,9 @@ SenderThread::Send(boost::shared_ptr<SessionData> session, boost::shared_ptr<Net
|
||||
}
|
||||
}
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_sessionMutex);
|
||||
m_session = session;
|
||||
boost::mutex::scoped_lock lock(m_sessionDataMutex);
|
||||
m_sessionSocket = session->GetSocket();
|
||||
m_sessionId = session->GetId();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -97,8 +98,9 @@ SenderThread::Send(boost::shared_ptr<SessionData> session, const NetPacketList &
|
||||
}
|
||||
}
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_sessionMutex);
|
||||
m_session = session;
|
||||
boost::mutex::scoped_lock lock(m_sessionDataMutex);
|
||||
m_sessionSocket = session->GetSocket();
|
||||
m_sessionId = session->GetId();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -127,8 +129,8 @@ SenderThread::Main()
|
||||
{
|
||||
SOCKET tmpSocket;
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_sessionMutex);
|
||||
tmpSocket = m_session->GetSocket();
|
||||
boost::mutex::scoped_lock lock(m_sessionDataMutex);
|
||||
tmpSocket = m_sessionSocket;
|
||||
}
|
||||
|
||||
// send next chunk of data
|
||||
@@ -159,8 +161,8 @@ SenderThread::Main()
|
||||
// Ignore invalid or not connected sockets.
|
||||
if (errCode != SOCKET_ERR_NOTCONN && errCode != SOCKET_ERR_NOTSOCK)
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_sessionMutex);
|
||||
m_callback.SignalNetError(m_session->GetId(), ERR_SOCK_SELECT_FAILED, errCode);
|
||||
boost::mutex::scoped_lock lock(m_sessionDataMutex);
|
||||
m_callback.SignalNetError(m_sessionId, ERR_SOCK_SELECT_FAILED, errCode);
|
||||
}
|
||||
}
|
||||
Msleep(SEND_TIMEOUT_MSEC);
|
||||
@@ -172,8 +174,8 @@ SenderThread::Main()
|
||||
// Ignore invalid or not connected sockets.
|
||||
if (errCode != SOCKET_ERR_NOTCONN && errCode != SOCKET_ERR_NOTSOCK)
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_sessionMutex);
|
||||
m_callback.SignalNetError(m_session->GetId(), ERR_SOCK_SEND_FAILED, errCode);
|
||||
boost::mutex::scoped_lock lock(m_sessionDataMutex);
|
||||
m_callback.SignalNetError(m_sessionId, ERR_SOCK_SEND_FAILED, errCode);
|
||||
}
|
||||
Msleep(SEND_TIMEOUT_MSEC);
|
||||
}
|
||||
@@ -198,5 +200,8 @@ SenderThread::Main()
|
||||
else
|
||||
Msleep(SEND_TIMEOUT_MSEC);
|
||||
}
|
||||
boost::mutex::scoped_lock lock(m_sessionDataMutex);
|
||||
if (m_sessionSocket != INVALID_SOCKET)
|
||||
CLOSESOCKET(m_sessionSocket);
|
||||
}
|
||||
|
||||
|
||||
@@ -57,7 +57,7 @@
|
||||
using namespace std;
|
||||
|
||||
|
||||
class ServerSenderCallback : public SenderCallback
|
||||
class ServerSenderCallback : public SenderCallback, public SessionDataCallback
|
||||
{
|
||||
public:
|
||||
ServerSenderCallback(ServerLobbyThread &server) : m_server(server) {}
|
||||
@@ -69,6 +69,10 @@ 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)
|
||||
{
|
||||
m_server.RemoveSender(session);
|
||||
}
|
||||
|
||||
private:
|
||||
ServerLobbyThread &m_server;
|
||||
@@ -300,6 +304,13 @@ 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()
|
||||
{
|
||||
@@ -357,6 +368,8 @@ ServerLobbyThread::Main()
|
||||
RemoveGameLoop();
|
||||
// Kick players.
|
||||
RemovePlayerLoop();
|
||||
// Remove sender threads.
|
||||
RemoveSenderLoop();
|
||||
// Resubscribe Lobby Messages if needed.
|
||||
ResubscribeLobbyMsgLoop();
|
||||
// Check session timeouts.
|
||||
@@ -862,6 +875,30 @@ 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()
|
||||
{
|
||||
@@ -1047,8 +1084,11 @@ ServerLobbyThread::HandleNewConnection(boost::shared_ptr<ConnectData> connData)
|
||||
//}
|
||||
|
||||
// Create a new session.
|
||||
boost::shared_ptr<SessionData> sessionData(new SessionData(connData->ReleaseSocket(), m_curSessionId++, *m_senderCallback));
|
||||
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));
|
||||
m_sessionManager.AddSession(sessionData);
|
||||
m_senderMap[sessionData->GetId()] = senderThread;
|
||||
|
||||
LOG_VERBOSE("Accepted connection - session #" << sessionData->GetId() << ".");
|
||||
|
||||
|
||||
@@ -18,22 +18,18 @@
|
||||
***************************************************************************/
|
||||
|
||||
#include <net/sessiondata.h>
|
||||
#include <net/senderthread.h>
|
||||
#include <net/senderinterface.h>
|
||||
|
||||
SessionData::SessionData(SOCKET sockfd, SessionId id, SenderCallback &cb)
|
||||
SessionData::SessionData(SOCKET sockfd, SessionId id, boost::shared_ptr<SenderInterface> sender, SessionDataCallback &cb)
|
||||
: m_sockfd(sockfd), m_id(id), m_state(SessionData::Init), m_readyFlag(false),
|
||||
m_wantsLobbyMsg(true), m_activityTimeoutNoticeSent(false)
|
||||
m_wantsLobbyMsg(true), m_activityTimeoutNoticeSent(false), m_callback(cb)
|
||||
{
|
||||
m_sender.reset(new SenderThread(cb));
|
||||
m_sender->Start();
|
||||
m_sender = sender;
|
||||
}
|
||||
|
||||
SessionData::~SessionData()
|
||||
{
|
||||
m_sender->SignalStop();
|
||||
m_sender->WaitStop();
|
||||
if (m_sockfd != INVALID_SOCKET)
|
||||
CLOSESOCKET(m_sockfd);
|
||||
m_callback.SignalSessionTerminated(m_id);
|
||||
}
|
||||
|
||||
SessionId
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2009 by Lothar May *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program; if not, write to the *
|
||||
* Free Software Foundation, Inc., *
|
||||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
||||
***************************************************************************/
|
||||
|
||||
#include <net/sessiondatacallback.h>
|
||||
|
||||
|
||||
SessionDataCallback::~SessionDataCallback()
|
||||
{
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user