Core/Misc: Cleanup worldserver/bnetserver main() functions to run destructors in predictable order

* world/bnet servers will now fail to startup if listen ports are in use
* Restored "Network.OutUBuff" config option lost during boost transition
This commit is contained in:
Shauren
2017-05-04 20:41:22 +02:00
parent 3b873add19
commit 6c92a481a3
8 changed files with 293 additions and 198 deletions

View File

@@ -31,7 +31,7 @@ public:
typedef void(*AcceptCallback)(tcp::socket&& newSocket, uint32 threadIndex);
AsyncAcceptor(boost::asio::io_service& ioService, std::string const& bindIp, uint16 port) :
_acceptor(ioService, tcp::endpoint(boost::asio::ip::address::from_string(bindIp), port)),
_acceptor(ioService), _endpoint(boost::asio::ip::address::from_string(bindIp), port),
_socket(ioService), _closed(false), _socketFactory(std::bind(&AsyncAcceptor::DefeaultSocketFactory, this))
{
}
@@ -66,6 +66,33 @@ public:
});
}
bool Bind()
{
boost::system::error_code errorCode;
_acceptor.open(_endpoint.protocol(), errorCode);
if (errorCode)
{
TC_LOG_INFO("network", "Failed to open acceptor %s", errorCode.message().c_str());
return false;
}
_acceptor.bind(_endpoint, errorCode);
if (errorCode)
{
TC_LOG_INFO("network", "Couldn't bind to %s:%u %s", _endpoint.address().to_string(), _endpoint.port(), errorCode.message().c_str());
return false;
}
_acceptor.listen(boost::asio::socket_base::max_connections, errorCode);
if (errorCode)
{
TC_LOG_INFO("network", "Failed to start listening on %s:%u %s", _endpoint.address().to_string(), _endpoint.port(), errorCode.message().c_str());
return false;
}
return true;
}
void Close()
{
if (_closed.exchange(true))
@@ -81,6 +108,7 @@ private:
std::pair<tcp::socket*, uint32> DefeaultSocketFactory() { return std::make_pair(&_socket, 0); }
tcp::acceptor _acceptor;
tcp::endpoint _endpoint;
tcp::socket _socket;
std::atomic<bool> _closed;
std::function<std::pair<tcp::socket*, uint32>()> _socketFactory;

View File

@@ -39,9 +39,10 @@ public:
{
ASSERT(threadCount > 0);
AsyncAcceptor* acceptor = nullptr;
try
{
_acceptor = new AsyncAcceptor(service, bindIp, port);
acceptor = new AsyncAcceptor(service, bindIp, port);
}
catch (boost::system::system_error const& err)
{
@@ -49,6 +50,13 @@ public:
return false;
}
if (!acceptor->Bind())
{
TC_LOG_ERROR("network", "StartNetwork failed to bind socket acceptor");
return false;
}
_acceptor = acceptor;
_threadCount = threadCount;
_threads = CreateThreads();
@@ -119,7 +127,7 @@ public:
}
protected:
SocketMgr() : _acceptor(nullptr), _threads(nullptr), _threadCount(1)
SocketMgr() : _acceptor(nullptr), _threads(nullptr), _threadCount(0)
{
}