Core/Network: Cleanup asio using directives

This commit is contained in:
Shauren
2023-07-15 00:43:19 +02:00
parent fce667b738
commit cdfaecda52
15 changed files with 36 additions and 54 deletions

View File

@@ -25,14 +25,12 @@
#include <atomic>
#include <functional>
using boost::asio::ip::tcp;
#define TRINITY_MAX_LISTEN_CONNECTIONS boost::asio::socket_base::max_listen_connections
class AsyncAcceptor
{
public:
typedef void(*AcceptCallback)(tcp::socket&& newSocket, uint32 threadIndex);
typedef void(*AcceptCallback)(boost::asio::ip::tcp::socket&& newSocket, uint32 threadIndex);
AsyncAcceptor(Trinity::Asio::IoContext& ioContext, std::string const& bindIp, uint16 port) :
_acceptor(ioContext), _endpoint(Trinity::Net::make_address(bindIp), port),
@@ -48,7 +46,7 @@ public:
{
auto [tmpSocket, tmpThreadIndex] = _socketFactory();
// TODO: get rid of temporary variables (clang 15 cannot handle variables from structured bindings as lambda captures)
tcp::socket* socket = tmpSocket;
boost::asio::ip::tcp::socket* socket = tmpSocket;
uint32 threadIndex = tmpThreadIndex;
_acceptor.async_accept(*socket, [this, socket, threadIndex](boost::system::error_code error)
{
@@ -116,16 +114,16 @@ public:
_acceptor.close(err);
}
void SetSocketFactory(std::function<std::pair<tcp::socket*, uint32>()> func) { _socketFactory = func; }
void SetSocketFactory(std::function<std::pair<boost::asio::ip::tcp::socket*, uint32>()> func) { _socketFactory = func; }
private:
std::pair<tcp::socket*, uint32> DefeaultSocketFactory() { return std::make_pair(&_socket, 0); }
std::pair<boost::asio::ip::tcp::socket*, uint32> DefeaultSocketFactory() { return std::make_pair(&_socket, 0); }
tcp::acceptor _acceptor;
tcp::endpoint _endpoint;
tcp::socket _socket;
boost::asio::ip::tcp::acceptor _acceptor;
boost::asio::ip::tcp::endpoint _endpoint;
boost::asio::ip::tcp::socket _socket;
std::atomic<bool> _closed;
std::function<std::pair<tcp::socket*, uint32>()> _socketFactory;
std::function<std::pair<boost::asio::ip::tcp::socket*, uint32>()> _socketFactory;
};
template<class T>

View File

@@ -29,8 +29,6 @@
#include <mutex>
#include <thread>
using boost::asio::ip::tcp;
template<class SocketType>
class NetworkThread
{
@@ -88,7 +86,7 @@ public:
SocketAdded(sock);
}
tcp::socket* GetSocketForAccept() { return &_acceptSocket; }
boost::asio::ip::tcp::socket* GetSocketForAccept() { return &_acceptSocket; }
protected:
virtual void SocketAdded(std::shared_ptr<SocketType> /*sock*/) { }
@@ -169,7 +167,7 @@ private:
SocketContainer _newSockets;
Trinity::Asio::IoContext _ioContext;
tcp::socket _acceptSocket;
boost::asio::ip::tcp::socket _acceptSocket;
Trinity::Asio::DeadlineTimer _updateTimer;
};

View File

@@ -27,8 +27,6 @@
#include <type_traits>
#include <boost/asio/ip/tcp.hpp>
using boost::asio::ip::tcp;
#define READ_BLOCK_SIZE 4096
#ifdef BOOST_ASIO_HAS_IOCP
#define TC_SOCKET_USE_IOCP
@@ -61,11 +59,11 @@ using boost::asio::ip::tcp;
tcp::socket::endpoint_type remote_endpoint() const;
*/
template<class T, class Stream = tcp::socket>
template<class T, class Stream = boost::asio::ip::tcp::socket>
class Socket : public std::enable_shared_from_this<T>
{
public:
explicit Socket(tcp::socket&& socket) : _socket(std::move(socket)), _remoteAddress(_socket.remote_endpoint().address()),
explicit Socket(boost::asio::ip::tcp::socket&& socket) : _socket(std::move(socket)), _remoteAddress(_socket.remote_endpoint().address()),
_remotePort(_socket.remote_endpoint().port()), _readBuffer(), _closed(false), _closing(false), _isWritingAsync(false)
{
_readBuffer.Resize(READ_BLOCK_SIZE);
@@ -185,7 +183,7 @@ protected:
void SetNoDelay(bool enable)
{
boost::system::error_code err;
_socket.set_option(tcp::no_delay(enable), err);
_socket.set_option(boost::asio::ip::tcp::no_delay(enable), err);
if (err)
TC_LOG_DEBUG("network", "Socket::SetNoDelay: failed to set_option(boost::asio::ip::tcp::no_delay) for {} - {} ({})",
GetRemoteIpAddress().to_string(), err.value(), err.message());

View File

@@ -24,8 +24,6 @@
#include <boost/asio/ip/tcp.hpp>
#include <memory>
using boost::asio::ip::tcp;
template<class SocketType>
class SocketMgr
{
@@ -95,7 +93,7 @@ public:
_threads[i].Wait();
}
virtual void OnSocketOpen(tcp::socket&& sock, uint32 threadIndex)
virtual void OnSocketOpen(boost::asio::ip::tcp::socket&& sock, uint32 threadIndex)
{
try
{
@@ -123,7 +121,7 @@ public:
return min;
}
std::pair<tcp::socket*, uint32> GetSocketForAccept()
std::pair<boost::asio::ip::tcp::socket*, uint32> GetSocketForAccept()
{
uint32 threadIndex = SelectThreadWithMinConnections();
return std::make_pair(_threads[threadIndex].GetSocketForAccept(), threadIndex);

View File

@@ -22,14 +22,13 @@
#include <boost/asio/ssl/stream.hpp>
#include <boost/system/error_code.hpp>
using boost::asio::ip::tcp;
namespace boostssl = boost::asio::ssl;
template<class SslContext>
class SslSocket
{
public:
explicit SslSocket(tcp::socket&& socket) : _socket(std::move(socket)), _sslSocket(_socket, SslContext::instance())
explicit SslSocket(boost::asio::ip::tcp::socket&& socket) : _socket(std::move(socket)), _sslSocket(_socket, SslContext::instance())
{
_sslSocket.set_verify_mode(boostssl::verify_none);
}
@@ -70,7 +69,7 @@ public:
_socket.set_option(option, error);
}
tcp::socket::endpoint_type remote_endpoint() const
boost::asio::ip::tcp::socket::endpoint_type remote_endpoint() const
{
return _socket.remote_endpoint();
}
@@ -83,8 +82,8 @@ public:
}
private:
tcp::socket _socket;
boostssl::stream<tcp::socket&> _sslSocket;
boost::asio::ip::tcp::socket _socket;
boostssl::stream<boost::asio::ip::tcp::socket&> _sslSocket;
};
#endif // SslSocket_h__