I'm realizing a simple server backend which can work with tcp and unix sockets.
Here is the listener block:
static asio::awaitable<void> listener() {
const auto executor = co_await asio::this_coro::executor;
asio::local::stream_protocol::acceptor acceptor(executor, asio::local::stream_protocol::endpoint(socket_file_));
for (;;) {
auto socket = co_await acceptor.async_accept(asio::use_awaitable);
...
After that the session is initializing by creating an instance of Session
class with this socket
.
In this case socket
has type asio::local::stream_protocol::socket
, but in tcp case the type will be asio::ip::tcp::socket
.
All of them has one base class - asio::basic_stream_socket
and I want to realize generic Session
class:
template <typename Protocol, typename Executor>
class Session : public std::enable_shared_from_this<Session<Protocol, Executor>> {
using Socket = asio::basic_stream_socket<Protocol, Executor>;
public:
explicit Session(Socket sock) : socket_(std::move(sock)) {}
void start() {
co_spawn(socket_.get_executor(),
[self = this->shared_from_this()] { return self->reader(); },
asio::detached);
}
private:
Socket socket_;
asio::awaitable<void> reader() {
...
In this realization I must to explicitly specify types Protocol
and Executor
in listener
method:
std::make_shared<Session<asio::local::stream_protocol, asio::any_io_executor>>(std::move(socket))->start();
or
std::make_shared<Session<decltype(socket)::protocol_type, decltype(socket)::executor_type>>(std::move(socket))->start();
what looks cumbersome and seems redundant.
Is there any way to optimize the Session
class template definition?