Preparing integration of SCTP support (again). Note that server error messages are currently broken.
This commit is contained in:
@@ -27,7 +27,6 @@
|
|||||||
#include <boost/swap.hpp>
|
#include <boost/swap.hpp>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using boost::asio::ip::tcp;
|
|
||||||
|
|
||||||
|
|
||||||
ReceiveBuffer::ReceiveBuffer()
|
ReceiveBuffer::ReceiveBuffer()
|
||||||
|
|||||||
@@ -24,7 +24,6 @@
|
|||||||
#include <boost/swap.hpp>
|
#include <boost/swap.hpp>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using boost::asio::ip::tcp;
|
|
||||||
|
|
||||||
|
|
||||||
SendBuffer::SendBuffer()
|
SendBuffer::SendBuffer()
|
||||||
|
|||||||
@@ -18,109 +18,10 @@
|
|||||||
***************************************************************************/
|
***************************************************************************/
|
||||||
|
|
||||||
#include <net/serveraccepthelper.h>
|
#include <net/serveraccepthelper.h>
|
||||||
#include <net/serverlobbythread.h>
|
|
||||||
#include <net/serverexception.h>
|
|
||||||
#include <net/socket_msg.h>
|
|
||||||
#include <core/loghelper.h>
|
|
||||||
|
|
||||||
|
|
||||||
using namespace std;
|
ServerAcceptInterface::~ServerAcceptInterface()
|
||||||
using boost::asio::ip::tcp;
|
|
||||||
|
|
||||||
ServerAcceptHelper::ServerAcceptHelper(ServerCallback &serverCallback, boost::shared_ptr<boost::asio::io_service> ioService)
|
|
||||||
: m_ioService(ioService), m_serverCallback(serverCallback)
|
|
||||||
{
|
|
||||||
m_acceptor.reset(new tcp::acceptor(*m_ioService));
|
|
||||||
}
|
|
||||||
|
|
||||||
ServerAcceptHelper::~ServerAcceptHelper()
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
|
||||||
ServerAcceptHelper::Listen(unsigned serverPort, bool ipv6, bool sctp, const string &/*logDir*/, boost::shared_ptr<ServerLobbyThread> lobbyThread)
|
|
||||||
{
|
|
||||||
m_lobbyThread = lobbyThread;
|
|
||||||
|
|
||||||
try {
|
|
||||||
InternalListen(serverPort, ipv6, sctp);
|
|
||||||
} catch (const PokerTHException &e) {
|
|
||||||
LOG_ERROR(e.what());
|
|
||||||
GetCallback().SignalNetServerError(e.GetErrorId(), e.GetOsErrorCode());
|
|
||||||
} catch (...) {
|
|
||||||
// This is probably an asio exception. Assume that bind failed,
|
|
||||||
// which is the most frequent case.
|
|
||||||
LOG_ERROR("Cannot bind/listen on TCP port.");
|
|
||||||
GetCallback().SignalNetServerError(ERR_SOCK_BIND_FAILED, 0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
ServerAcceptHelper::InternalListen(unsigned serverPort, bool ipv6, bool /*sctp*/)
|
|
||||||
{
|
|
||||||
if (serverPort < 1024)
|
|
||||||
throw ServerException(__FILE__, __LINE__, ERR_SOCK_INVALID_PORT, 0);
|
|
||||||
|
|
||||||
// TODO consider sctp
|
|
||||||
// Prepare Listen.
|
|
||||||
if (ipv6)
|
|
||||||
m_endpoint.reset(new tcp::endpoint(tcp::v6(), serverPort));
|
|
||||||
else
|
|
||||||
m_endpoint.reset(new tcp::endpoint(tcp::v4(), serverPort));
|
|
||||||
|
|
||||||
m_acceptor->open(m_endpoint->protocol());
|
|
||||||
// TODO cannot set non blocking I/O with asio.
|
|
||||||
//boost::asio::socket_base::non_blocking_io command(true);
|
|
||||||
//m_acceptor->io_control(command);
|
|
||||||
m_acceptor->set_option(tcp::acceptor::reuse_address(true));
|
|
||||||
if (ipv6) // In IPv6 mode: Be compatible with IPv4.
|
|
||||||
m_acceptor->set_option(boost::asio::ip::v6_only(false));
|
|
||||||
m_acceptor->bind(*m_endpoint);
|
|
||||||
m_acceptor->listen();
|
|
||||||
|
|
||||||
// Start first asynchronous Accept.
|
|
||||||
boost::shared_ptr<tcp::socket> newSocket(new tcp::socket(*m_ioService));
|
|
||||||
m_acceptor->async_accept(
|
|
||||||
*newSocket,
|
|
||||||
boost::bind(&ServerAcceptHelper::HandleAccept, this, newSocket,
|
|
||||||
boost::asio::placeholders::error)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
ServerAcceptHelper::HandleAccept(boost::shared_ptr<boost::asio::ip::tcp::socket> acceptedSocket,
|
|
||||||
const boost::system::error_code &error)
|
|
||||||
{
|
|
||||||
if (!error) {
|
|
||||||
boost::asio::socket_base::non_blocking_io command(true);
|
|
||||||
acceptedSocket->io_control(command);
|
|
||||||
acceptedSocket->set_option(tcp::no_delay(true));
|
|
||||||
acceptedSocket->set_option(boost::asio::socket_base::keep_alive(true));
|
|
||||||
GetLobbyThread().AddConnection(acceptedSocket);
|
|
||||||
|
|
||||||
boost::shared_ptr<tcp::socket> newSocket(new tcp::socket(*m_ioService));
|
|
||||||
m_acceptor->async_accept(
|
|
||||||
*newSocket,
|
|
||||||
boost::bind(&ServerAcceptHelper::HandleAccept, this, newSocket,
|
|
||||||
boost::asio::placeholders::error)
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
// Accept failed. This is a fatal error.
|
|
||||||
LOG_ERROR("In boost::asio handler: Accept failed.");
|
|
||||||
GetCallback().SignalNetServerError(ERR_SOCK_ACCEPT_FAILED, 0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ServerCallback &
|
|
||||||
ServerAcceptHelper::GetCallback()
|
|
||||||
{
|
|
||||||
return m_serverCallback;
|
|
||||||
}
|
|
||||||
|
|
||||||
ServerLobbyThread &
|
|
||||||
ServerAcceptHelper::GetLobbyThread()
|
|
||||||
{
|
|
||||||
assert(m_lobbyThread.get());
|
|
||||||
return *m_lobbyThread;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|||||||
@@ -17,8 +17,9 @@
|
|||||||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
||||||
***************************************************************************/
|
***************************************************************************/
|
||||||
|
|
||||||
#include <net/socket_helper.h>
|
#include <boost/asio.hpp>
|
||||||
#include <net/servermanager.h>
|
#include <net/servermanager.h>
|
||||||
|
#include <net/socket_helper.h>
|
||||||
#include <net/serverlobbythread.h>
|
#include <net/serverlobbythread.h>
|
||||||
#include <net/serveraccepthelper.h>
|
#include <net/serveraccepthelper.h>
|
||||||
#include <net/serverexception.h>
|
#include <net/serverexception.h>
|
||||||
@@ -53,17 +54,16 @@ ServerManager::Init(unsigned serverPort, bool ipv6, ServerTransportProtocol prot
|
|||||||
m_lobbyBot->Init(m_lobbyThread, ircLobbyThread);
|
m_lobbyBot->Init(m_lobbyThread, ircLobbyThread);
|
||||||
|
|
||||||
if (proto & TRANSPORT_PROTOCOL_TCP) {
|
if (proto & TRANSPORT_PROTOCOL_TCP) {
|
||||||
boost::shared_ptr<ServerAcceptHelper> tcpAcceptHelper(new ServerAcceptHelper(GetGui(), m_ioService));
|
boost::shared_ptr<ServerAcceptInterface> tcpAcceptHelper(new ServerAcceptHelper<boost::asio::ip::tcp>(GetGui(), m_ioService));
|
||||||
tcpAcceptHelper->Listen(serverPort, ipv6, false, logDir, m_lobbyThread);
|
tcpAcceptHelper->Listen(serverPort, ipv6, logDir, m_lobbyThread);
|
||||||
m_acceptHelperPool.push_back(tcpAcceptHelper);
|
m_acceptHelperPool.push_back(tcpAcceptHelper);
|
||||||
}
|
}
|
||||||
// TODO: Re-add SCTP support once asio supports SCTP.
|
/* if (proto & TRANSPORT_PROTOCOL_SCTP)
|
||||||
/* if (mode & TRANSPORT_PROTOCOL_SCTP)
|
{
|
||||||
{
|
boost::shared_ptr<ServerAcceptInterface> sctpAcceptHelper(new ServerAcceptHelper<boost::asio::ip::sctp>(GetGui(), m_ioService));
|
||||||
boost::shared_ptr<ServerAcceptHelper> sctpAcceptHelper(new ServerAcceptHelper(GetGui(), m_ioService));
|
sctpAcceptHelper->Listen(serverPort, ipv6, logDir, m_lobbyThread);
|
||||||
sctpAcceptHelper->Listen(serverPort, ipv6, true, logDir, m_lobbyThread);
|
m_acceptHelperPool.push_back(sctpAcceptHelper);
|
||||||
m_acceptHelperPool.push_back(sctpAcceptHelper);
|
}*/
|
||||||
}*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
GuiInterface &
|
GuiInterface &
|
||||||
|
|||||||
+114
-14
@@ -24,35 +24,135 @@
|
|||||||
#include <boost/asio.hpp>
|
#include <boost/asio.hpp>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
#include <net/serverlobbythread.h>
|
||||||
|
#include <net/serverexception.h>
|
||||||
|
#include <net/socket_msg.h>
|
||||||
|
#include <core/loghelper.h>
|
||||||
#include <game_defs.h>
|
#include <game_defs.h>
|
||||||
#include <gui/guiinterface.h>
|
#include <gui/guiinterface.h>
|
||||||
|
|
||||||
class ServerLobbyThread;
|
class ServerAcceptInterface
|
||||||
|
|
||||||
class ServerAcceptHelper
|
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
ServerAcceptHelper(ServerCallback &serverCallback, boost::shared_ptr<boost::asio::io_service> ioService);
|
virtual ~ServerAcceptInterface();
|
||||||
virtual ~ServerAcceptHelper();
|
|
||||||
|
virtual void Listen(unsigned serverPort, bool ipv6, const std::string &logDir,
|
||||||
|
boost::shared_ptr<ServerLobbyThread> lobbyThread) = 0;
|
||||||
|
|
||||||
|
virtual void Close() = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename P>
|
||||||
|
class ServerAcceptHelper : public ServerAcceptInterface
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
typedef typename P::acceptor P_acceptor;
|
||||||
|
typedef typename P::endpoint P_endpoint;
|
||||||
|
|
||||||
|
ServerAcceptHelper(ServerCallback &serverCallback, boost::shared_ptr<boost::asio::io_service> ioService)
|
||||||
|
: m_ioService(ioService), m_serverCallback(serverCallback)
|
||||||
|
{
|
||||||
|
m_acceptor.reset(new P_acceptor(*m_ioService));
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual ~ServerAcceptHelper()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
// Set the parameters.
|
// Set the parameters.
|
||||||
void Listen(unsigned serverPort, bool ipv6, bool sctp, const std::string &logDir,
|
virtual void Listen(unsigned serverPort, bool ipv6, const std::string &/*logDir*/,
|
||||||
boost::shared_ptr<ServerLobbyThread> lobbyThread);
|
boost::shared_ptr<ServerLobbyThread> lobbyThread)
|
||||||
|
{
|
||||||
|
m_lobbyThread = lobbyThread;
|
||||||
|
|
||||||
|
try {
|
||||||
|
InternalListen(serverPort, ipv6);
|
||||||
|
} catch (const PokerTHException &e) {
|
||||||
|
LOG_ERROR(e.what());
|
||||||
|
GetCallback().SignalNetServerError(e.GetErrorId(), e.GetOsErrorCode());
|
||||||
|
} catch (...) {
|
||||||
|
// This is probably an asio exception. Assume that bind failed,
|
||||||
|
// which is the most frequent case.
|
||||||
|
LOG_ERROR("Cannot bind/listen on port.");
|
||||||
|
GetCallback().SignalNetServerError(ERR_SOCK_BIND_FAILED, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual void Close()
|
||||||
|
{
|
||||||
|
boost::system::error_code ec;
|
||||||
|
m_acceptor->close(ec);
|
||||||
|
// Ignore any error, because we are terminating.
|
||||||
|
}
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
void InternalListen(unsigned serverPort, bool ipv6, bool sctp);
|
void InternalListen(unsigned serverPort, bool ipv6)
|
||||||
void HandleAccept(boost::shared_ptr<boost::asio::ip::tcp::socket> acceptedSocket,
|
{
|
||||||
const boost::system::error_code &error);
|
if (serverPort < 1024)
|
||||||
|
throw ServerException(__FILE__, __LINE__, ERR_SOCK_INVALID_PORT, 0);
|
||||||
|
|
||||||
ServerCallback &GetCallback();
|
// TODO consider sctp
|
||||||
|
// Prepare Listen.
|
||||||
|
if (ipv6) {
|
||||||
|
m_endpoint.reset(new P_endpoint(P::v6(), serverPort));
|
||||||
|
} else {
|
||||||
|
m_endpoint.reset(new P_endpoint(P::v4(), serverPort));
|
||||||
|
}
|
||||||
|
|
||||||
ServerLobbyThread &GetLobbyThread();
|
m_acceptor->open(m_endpoint->protocol());
|
||||||
|
m_acceptor->set_option(typename P::acceptor::reuse_address(true));
|
||||||
|
if (ipv6) { // In IPv6 mode: Be compatible with IPv4.
|
||||||
|
m_acceptor->set_option(boost::asio::ip::v6_only(false));
|
||||||
|
}
|
||||||
|
m_acceptor->bind(*m_endpoint);
|
||||||
|
m_acceptor->listen();
|
||||||
|
|
||||||
|
// Start first asynchronous Accept.
|
||||||
|
boost::shared_ptr<typename P::socket> newSocket(new typename P::socket(*m_ioService));
|
||||||
|
m_acceptor->async_accept(
|
||||||
|
*newSocket,
|
||||||
|
boost::bind(&ServerAcceptHelper::HandleAccept, this, newSocket,
|
||||||
|
boost::asio::placeholders::error)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
void HandleAccept(boost::shared_ptr<typename P::socket> acceptedSocket,
|
||||||
|
const boost::system::error_code &error)
|
||||||
|
{
|
||||||
|
if (!error) {
|
||||||
|
boost::asio::socket_base::non_blocking_io command(true);
|
||||||
|
acceptedSocket->io_control(command);
|
||||||
|
acceptedSocket->set_option(typename P::no_delay(true));
|
||||||
|
acceptedSocket->set_option(boost::asio::socket_base::keep_alive(true));
|
||||||
|
GetLobbyThread().AddConnection(acceptedSocket);
|
||||||
|
|
||||||
|
boost::shared_ptr<typename P::socket> newSocket(new typename P::socket(*m_ioService));
|
||||||
|
m_acceptor->async_accept(
|
||||||
|
*newSocket,
|
||||||
|
boost::bind(&ServerAcceptHelper::HandleAccept, this, newSocket,
|
||||||
|
boost::asio::placeholders::error)
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
// Accept failed. This is a fatal error.
|
||||||
|
LOG_ERROR("In boost::asio handler: Accept failed.");
|
||||||
|
GetCallback().SignalNetServerError(ERR_SOCK_ACCEPT_FAILED, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ServerCallback &GetCallback()
|
||||||
|
{
|
||||||
|
return m_serverCallback;
|
||||||
|
}
|
||||||
|
|
||||||
|
ServerLobbyThread &GetLobbyThread()
|
||||||
|
{
|
||||||
|
return *m_lobbyThread;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
boost::shared_ptr<boost::asio::io_service> m_ioService;
|
boost::shared_ptr<boost::asio::io_service> m_ioService;
|
||||||
boost::shared_ptr<boost::asio::ip::tcp::acceptor> m_acceptor;
|
boost::shared_ptr<P_acceptor> m_acceptor;
|
||||||
boost::shared_ptr<boost::asio::ip::tcp::endpoint> m_endpoint;
|
boost::shared_ptr<P_endpoint> m_endpoint;
|
||||||
ServerCallback &m_serverCallback;
|
ServerCallback &m_serverCallback;
|
||||||
|
|
||||||
boost::shared_ptr<ServerLobbyThread> m_lobbyThread;
|
boost::shared_ptr<ServerLobbyThread> m_lobbyThread;
|
||||||
|
|||||||
@@ -32,7 +32,6 @@
|
|||||||
#include <gui/guiinterface.h>
|
#include <gui/guiinterface.h>
|
||||||
|
|
||||||
class ServerLobbyThread;
|
class ServerLobbyThread;
|
||||||
class ServerAcceptHelper;
|
|
||||||
class SenderThread;
|
class SenderThread;
|
||||||
class ConfigFile;
|
class ConfigFile;
|
||||||
class AvatarManager;
|
class AvatarManager;
|
||||||
|
|||||||
@@ -31,7 +31,6 @@
|
|||||||
#include <gui/guiinterface.h>
|
#include <gui/guiinterface.h>
|
||||||
|
|
||||||
class ServerLobbyThread;
|
class ServerLobbyThread;
|
||||||
class ServerAcceptHelper;
|
|
||||||
class SenderThread;
|
class SenderThread;
|
||||||
class ConfigFile;
|
class ConfigFile;
|
||||||
class AvatarManager;
|
class AvatarManager;
|
||||||
|
|||||||
@@ -32,7 +32,7 @@
|
|||||||
|
|
||||||
class ServerLobbyThread;
|
class ServerLobbyThread;
|
||||||
class IrcThread;
|
class IrcThread;
|
||||||
class ServerAcceptHelper;
|
class ServerAcceptInterface;
|
||||||
class SenderThread;
|
class SenderThread;
|
||||||
class ConfigFile;
|
class ConfigFile;
|
||||||
class AvatarManager;
|
class AvatarManager;
|
||||||
@@ -60,7 +60,7 @@ public:
|
|||||||
ServerLobbyBot &GetLobbyBot();
|
ServerLobbyBot &GetLobbyBot();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
typedef std::list<boost::shared_ptr<ServerAcceptHelper> > AcceptHelperList;
|
typedef std::list<boost::shared_ptr<ServerAcceptInterface> > AcceptHelperList;
|
||||||
|
|
||||||
ServerLobbyThread &GetLobbyThread();
|
ServerLobbyThread &GetLobbyThread();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user