0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-29 02:02:38 +01:00

ircd::net: Move acceptor into header; minor cleanup.

This commit is contained in:
Jason Volk 2018-01-19 06:55:48 -08:00
parent f02c31217b
commit 6e2cf45571
3 changed files with 99 additions and 80 deletions

View file

@ -66,6 +66,7 @@ struct ircd::strand
#include <ircd/ctx/continuation.h>
#include <ircd/net/asio.h>
#include <ircd/net/socket.h>
#include <ircd/net/acceptor.h>
inline ircd::strand::operator
asio::io_service &()

View file

@ -0,0 +1,56 @@
// Matrix Construct
//
// Copyright (C) Matrix Construct Developers, Authors & Contributors
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice is present in all copies. The
// full license for this software is available in the LICENSE file.
// This file is not included with the IRCd standard include stack because
// it requires symbols we can't forward declare without boost headers. It
// is part of the <ircd/asio.h> stack which can be included in your
// definition file if you need low level access to this acceptor API.
#pragma once
#define HAVE_IRCD_NET_ACCEPTOR_H
struct ircd::net::listener::acceptor
:std::enable_shared_from_this<struct ircd::net::listener::acceptor>
{
using error_code = boost::system::error_code;
static log::log log;
std::string name;
size_t backlog;
asio::ssl::context ssl;
ip::tcp::endpoint ep;
ip::tcp::acceptor a;
size_t accepting {0};
size_t handshaking {0};
bool interrupting {false};
ctx::dock joining;
explicit operator std::string() const;
void configure(const json::object &opts);
// Handshake stack
void check_handshake_error(const error_code &ec, socket &);
void handshake(const error_code &ec, std::shared_ptr<socket>, std::weak_ptr<acceptor>) noexcept;
// Acceptance stack
bool check_accept_error(const error_code &ec, socket &);
void accept(const error_code &ec, std::shared_ptr<socket>, std::weak_ptr<acceptor>) noexcept;
// Accept next
void next();
// Acceptor shutdown
bool interrupt() noexcept;
void join() noexcept;
acceptor(const json::object &opts);
~acceptor() noexcept;
};

View file

@ -754,49 +754,6 @@ ircd::net::blocking(const socket &socket)
// net/listener.h
//
struct ircd::net::listener::acceptor
:std::enable_shared_from_this<struct ircd::net::listener::acceptor>
{
using error_code = boost::system::error_code;
static log::log log;
std::string name;
size_t backlog;
asio::ssl::context ssl;
ip::tcp::endpoint ep;
ip::tcp::acceptor a;
size_t accepting {0};
size_t handshaking {0};
bool interrupting {false};
ctx::dock joining;
explicit operator std::string() const;
void configure(const json::object &opts);
// Handshake stack
void check_handshake_error(const error_code &ec, socket &);
void handshake(const error_code &ec, std::shared_ptr<socket>, std::weak_ptr<acceptor>) noexcept;
// Acceptance stack
bool check_accept_error(const error_code &ec, socket &);
void accept(const error_code &ec, std::shared_ptr<socket>, std::weak_ptr<acceptor>) noexcept;
// Accept next
void next();
// Acceptor shutdown
bool interrupt() noexcept;
void join() noexcept;
acceptor(const json::object &opts);
~acceptor() noexcept;
};
//
// ircd::net::listener
//
ircd::net::listener::listener(const std::string &opts)
:listener{json::object{opts}}
{
@ -820,42 +777,9 @@ noexcept
acceptor->join();
}
void
ircd::net::listener::acceptor::join()
noexcept try
{
interrupt();
joining.wait([this]
{
return !accepting && !handshaking;
});
}
catch(const std::exception &e)
{
log.error("acceptor(%p) join: %s",
this,
e.what());
}
bool
ircd::net::listener::acceptor::interrupt()
noexcept try
{
a.cancel();
interrupting = true;
return true;
}
catch(const boost::system::system_error &e)
{
log.error("acceptor(%p) interrupt: %s",
this,
string(e));
return false;
}
///////////////////////////////////////////////////////////////////////////////
//
// ircd::net::listener::acceptor
// net/acceptor.h
//
ircd::log::log
@ -930,6 +854,40 @@ noexcept
{
}
void
ircd::net::listener::acceptor::join()
noexcept try
{
interrupt();
joining.wait([this]
{
return !accepting && !handshaking;
});
}
catch(const std::exception &e)
{
log.error("acceptor(%p) join: %s",
this,
e.what());
}
bool
ircd::net::listener::acceptor::interrupt()
noexcept try
{
a.cancel();
interrupting = true;
return true;
}
catch(const boost::system::system_error &e)
{
log.error("acceptor(%p) interrupt: %s",
this,
string(e));
return false;
}
/// Sets the next asynchronous handler to start the next accept sequence.
/// Each call to next() sets one handler which handles the connect for one
/// socket. After the connect, an asynchronous SSL handshake handler is set
@ -1228,12 +1186,16 @@ ircd::net::listener::acceptor::configure(const json::object &opts)
}
}
ircd::net::listener::acceptor::operator std::string()
ircd::net::listener::acceptor::operator
std::string()
const
{
return fmt::snstringf
{
256, "'%s' @ [%s]:%u", name, string(ep.address()), ep.port()
256, "'%s' @ [%s]:%u",
name,
string(ep.address()),
ep.port()
};
}