started reactor refactoring
This commit is contained in:
+14
-14
@@ -16,24 +16,24 @@ namespace matador::net {
|
||||
*/
|
||||
class tcp {
|
||||
public:
|
||||
typedef peer_base<tcp> peer; /**< Shortcut to a tcp based peer */
|
||||
typedef socket_stream<tcp> socket; /**< Shortcut to a tcp based socket */
|
||||
typedef socket_acceptor<tcp> acceptor; /**< Shortcut to a tcp based acceptor */
|
||||
typedef address_resolver<tcp> resolver; /**< Shortcut to a tcp based address resolver */
|
||||
typedef peer_base<tcp> peer; /**< Shortcut to a tcp-based peer */
|
||||
typedef socket_stream<tcp> socket; /**< Shortcut to a tcp-based socket */
|
||||
typedef socket_acceptor<tcp> acceptor; /**< Shortcut to a tcp-based acceptor */
|
||||
typedef address_resolver<tcp> resolver; /**< Shortcut to a tcp-based address resolver */
|
||||
|
||||
/**
|
||||
* Returns the type of the socket
|
||||
*
|
||||
* @return Type of the socket
|
||||
*/
|
||||
int type() const { return SOCK_STREAM; }
|
||||
static int type() { return SOCK_STREAM; }
|
||||
|
||||
/**
|
||||
* Returns the socket protocol
|
||||
*
|
||||
* @return Socket protocol
|
||||
*/
|
||||
int protocol() const { return IPPROTO_TCP; }
|
||||
static int protocol() { return IPPROTO_TCP; }
|
||||
|
||||
/**
|
||||
* Returns the socket family
|
||||
@@ -59,7 +59,7 @@ public:
|
||||
static tcp v6() { return tcp(PF_INET6); }
|
||||
|
||||
private:
|
||||
explicit tcp(int family)
|
||||
explicit tcp(const int family)
|
||||
: family_(family)
|
||||
{}
|
||||
|
||||
@@ -74,24 +74,24 @@ private:
|
||||
class OOS_NET_API udp
|
||||
{
|
||||
public:
|
||||
typedef peer_base<udp> peer; /**< Shortcut to a udp based peer */
|
||||
typedef socket_stream<udp> socket; /**< Shortcut to a udp based socket */
|
||||
typedef socket_acceptor<udp> acceptor; /**< Shortcut to a udp based acceptor */
|
||||
typedef address_resolver<udp> resolver; /**< Shortcut to a udp based address resolver */
|
||||
typedef peer_base<udp> peer; /**< Shortcut to an udp-based peer */
|
||||
typedef socket_stream<udp> socket; /**< Shortcut to an udp-based socket */
|
||||
typedef socket_acceptor<udp> acceptor; /**< Shortcut to an udp-based acceptor */
|
||||
typedef address_resolver<udp> resolver; /**< Shortcut to an udp-based address resolver */
|
||||
|
||||
/**
|
||||
* Returns the type of the socket
|
||||
*
|
||||
* @return Type of the socket
|
||||
*/
|
||||
int type() const { return SOCK_DGRAM; }
|
||||
static int type() { return SOCK_DGRAM; }
|
||||
|
||||
/**
|
||||
* Returns the socket protocol
|
||||
*
|
||||
* @return Socket protocol
|
||||
*/
|
||||
int protocol() const { return IPPROTO_UDP; }
|
||||
static int protocol() { return IPPROTO_UDP; }
|
||||
|
||||
/**
|
||||
* Returns the socket family
|
||||
@@ -117,7 +117,7 @@ public:
|
||||
static udp v6() { return udp(PF_INET6); }
|
||||
|
||||
private:
|
||||
explicit udp(int family)
|
||||
explicit udp(const int family)
|
||||
: family_(family)
|
||||
{}
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
|
||||
#include <cstring>
|
||||
|
||||
namespace matador {
|
||||
namespace matador::net {
|
||||
|
||||
/**
|
||||
* The peer_base class acts like the holder
|
||||
@@ -25,7 +25,7 @@ template < class P >
|
||||
class peer_base
|
||||
{
|
||||
public:
|
||||
typedef P protocol_type; /**< Short to protocol type */
|
||||
typedef P protocol_type; /**< Short to a protocol-type */
|
||||
|
||||
/**
|
||||
* Default constructor
|
||||
@@ -48,9 +48,8 @@ public:
|
||||
* @param addr Address to create the peer from
|
||||
* @param port Port of the endpoint
|
||||
*/
|
||||
peer_base(address addr, unsigned short port)
|
||||
: addr_(std::move(addr))
|
||||
{
|
||||
peer_base(address addr, const unsigned short port)
|
||||
: addr_(std::move(addr)) {
|
||||
addr_.port(port);
|
||||
}
|
||||
|
||||
@@ -60,8 +59,7 @@ public:
|
||||
* @param x Peer to copy from
|
||||
*/
|
||||
peer_base(const peer_base &x)
|
||||
: addr_(x.addr_)
|
||||
{}
|
||||
: addr_(x.addr_) {}
|
||||
|
||||
/**
|
||||
* Move creates a peer from a given peer
|
||||
@@ -69,8 +67,7 @@ public:
|
||||
* @param x Peer to move from
|
||||
*/
|
||||
peer_base(peer_base &&x) noexcept
|
||||
: addr_(std::move(x.addr_))
|
||||
{}
|
||||
: addr_(std::move(x.addr_)) {}
|
||||
|
||||
/**
|
||||
* Copy assigns a given peer to this peer
|
||||
@@ -78,8 +75,7 @@ public:
|
||||
* @param x Peer to assign
|
||||
* @return The assigned peer
|
||||
*/
|
||||
peer_base& operator=(const peer_base &x)
|
||||
{
|
||||
peer_base& operator=(const peer_base &x) {
|
||||
addr_ = x.addr_;
|
||||
return *this;
|
||||
}
|
||||
@@ -90,8 +86,7 @@ public:
|
||||
* @param x The peer to move assign
|
||||
* @return The moved peer
|
||||
*/
|
||||
peer_base& operator=(peer_base &&x) noexcept
|
||||
{
|
||||
peer_base& operator=(peer_base &&x) noexcept {
|
||||
addr_ = std::move(x.addr_);
|
||||
return *this;
|
||||
}
|
||||
@@ -114,13 +109,8 @@ public:
|
||||
*
|
||||
* @return The current IP protocol
|
||||
*/
|
||||
protocol_type protocol() const
|
||||
{
|
||||
if (addr_.is_v4()) {
|
||||
return protocol_type::v4();
|
||||
} else {
|
||||
return protocol_type::v6();
|
||||
}
|
||||
protocol_type protocol() const {
|
||||
return addr_.is_v4() ? protocol_type::v4() : protocol_type::v6();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -128,8 +118,7 @@ public:
|
||||
*
|
||||
* @return The raw pointer to the sockaddr structure
|
||||
*/
|
||||
sockaddr* data()
|
||||
{
|
||||
sockaddr* data() {
|
||||
return addr_.addr();
|
||||
}
|
||||
|
||||
@@ -138,8 +127,7 @@ public:
|
||||
*
|
||||
* @return The raw pointer to the sockaddr structure
|
||||
*/
|
||||
const sockaddr* data() const
|
||||
{
|
||||
const sockaddr* data() const {
|
||||
return addr_.addr();
|
||||
}
|
||||
|
||||
@@ -148,8 +136,7 @@ public:
|
||||
*
|
||||
* @return The size of the underlying sockaddr structure
|
||||
*/
|
||||
size_t size() const
|
||||
{
|
||||
size_t size() const {
|
||||
return addr_.size();
|
||||
}
|
||||
|
||||
@@ -158,8 +145,7 @@ public:
|
||||
*
|
||||
* @return A reference to the address
|
||||
*/
|
||||
address& addr()
|
||||
{
|
||||
address& addr() {
|
||||
return addr_;
|
||||
}
|
||||
|
||||
@@ -168,8 +154,7 @@ public:
|
||||
*
|
||||
* @return A reference to the address
|
||||
*/
|
||||
const address& addr() const
|
||||
{
|
||||
const address& addr() const {
|
||||
return addr_;
|
||||
}
|
||||
|
||||
@@ -179,20 +164,19 @@ public:
|
||||
*
|
||||
* @return Returns a string representation of the peer
|
||||
*/
|
||||
std::string to_string() const
|
||||
{
|
||||
char addstr[INET6_ADDRSTRLEN + 8];
|
||||
std::string to_string() const {
|
||||
char address_str[INET6_ADDRSTRLEN + 8];
|
||||
const char *name;
|
||||
if (addr().is_v4()) {
|
||||
name = os::inet_ntop(addr_.addr()->sa_family, &addr_.addr_v4()->sin_addr, addstr, INET6_ADDRSTRLEN);
|
||||
name = os::inet_ntop(addr_.addr()->sa_family, &addr_.addr_v4()->sin_addr, address_str, INET6_ADDRSTRLEN);
|
||||
} else {
|
||||
name = os::inet_ntop(addr_.addr()->sa_family, &addr_.addr_v6()->sin6_addr, addstr, INET6_ADDRSTRLEN);
|
||||
name = os::inet_ntop(addr_.addr()->sa_family, &addr_.addr_v6()->sin6_addr, address_str, INET6_ADDRSTRLEN);
|
||||
}
|
||||
|
||||
size_t pos = strlen(name);
|
||||
const size_t pos = strlen(name);
|
||||
|
||||
snprintf(addstr+pos, INET6_ADDRSTRLEN+8-pos, ":%d", addr_.port());
|
||||
return addstr;
|
||||
snprintf(address_str + pos, INET6_ADDRSTRLEN+8-pos, ":%d", addr_.port());
|
||||
return address_str;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
#include <chrono>
|
||||
#include <queue>
|
||||
#include <list>
|
||||
#include <shared_mutex>
|
||||
|
||||
namespace matador::net {
|
||||
|
||||
@@ -56,8 +57,8 @@ public:
|
||||
void run();
|
||||
void handle_events();
|
||||
void shutdown();
|
||||
bool is_running() const { return running_; }
|
||||
select_fdsets fdsets() const;
|
||||
[[nodiscard]] bool is_running() const { return running_; }
|
||||
select_fdsets fd_sets() const;
|
||||
void mark_handler_for_delete(const handler_ptr& h);
|
||||
void activate_handler(const handler_ptr& h, event_type ev);
|
||||
void deactivate_handler(const handler_ptr& h, event_type ev);
|
||||
@@ -65,7 +66,7 @@ public:
|
||||
|
||||
private:
|
||||
struct TimerEvent {
|
||||
time_t timeout;
|
||||
time_t timeout{};
|
||||
handler_weak_ptr handler;
|
||||
|
||||
bool operator>(const TimerEvent& other) const {
|
||||
@@ -89,7 +90,7 @@ private:
|
||||
|
||||
HandlerEntry* find_handler_entry(const handler_ptr& h);
|
||||
void remove_handler_entry(const handler_ptr& h);
|
||||
void update_handler_events(HandlerEntry* entry, event_type ev, bool activate);
|
||||
static void update_handler_events(HandlerEntry* entry, event_type ev, bool activate);
|
||||
|
||||
private:
|
||||
handler_map handlers_;
|
||||
@@ -100,12 +101,13 @@ private:
|
||||
|
||||
mutable std::shared_mutex handlers_mutex_;
|
||||
std::mutex timers_mutex_;
|
||||
std::condition_variable shutdown_cv_;
|
||||
std::condition_variable_any shutdown_cv_;
|
||||
// std::condition_variable shutdown_cv_;
|
||||
|
||||
logger log_;
|
||||
logger::logger log_;
|
||||
Statistics stats_;
|
||||
|
||||
leader_follower_thread_pool thread_pool_;
|
||||
utils::leader_follower_thread_pool thread_pool_;
|
||||
socket_interrupter interrupter_;
|
||||
|
||||
static constexpr std::chrono::seconds CLEANUP_INTERVAL{60};
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
#include "matador/net/fdset.hpp"
|
||||
|
||||
namespace matador {
|
||||
namespace matador::net {
|
||||
|
||||
/**
|
||||
* This class represents three fd sets
|
||||
|
||||
@@ -25,8 +25,7 @@ namespace matador {
|
||||
* @tparam P Protocol type
|
||||
*/
|
||||
template < class P >
|
||||
class socket_base
|
||||
{
|
||||
class socket_base {
|
||||
public:
|
||||
typedef P protocol_type; /**< Shortcut to the protocol type */
|
||||
typedef typename P::peer peer_type; /**< Shortcut to the peer type */
|
||||
@@ -42,7 +41,7 @@ public:
|
||||
/**
|
||||
* Creates a socket with the given peer
|
||||
*
|
||||
* @param peer Peer used to initialize the socket
|
||||
* @param peer Peer used to initialise the socket
|
||||
*/
|
||||
explicit socket_base(const peer_type &peer);
|
||||
|
||||
|
||||
@@ -207,7 +207,7 @@ socket_type connect(socket_stream<P> &stream, const char* hostname, unsigned sho
|
||||
do {
|
||||
conn_fd = ::socket(res->ai_family, res->ai_socktype, res->ai_protocol);
|
||||
if (!is_valid_socket(conn_fd)) {
|
||||
// error, try next one
|
||||
// error, try the next one
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -220,7 +220,7 @@ socket_type connect(socket_stream<P> &stream, const char* hostname, unsigned sho
|
||||
// throw_logic_error("couldn't execute: " << strerror(errno));
|
||||
}
|
||||
|
||||
// bind error, close and try next one
|
||||
// bind error, close and try the next one
|
||||
os::shutdown(conn_fd, os::shutdown_type::READ_WRITE);
|
||||
} while ( (res = res->ai_next) != nullptr);
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
#include "matador/net/ip.hpp"
|
||||
|
||||
// #include "matador/logger/logger.hpp"
|
||||
#include "matador/logger/logger.hpp"
|
||||
|
||||
#include <array>
|
||||
|
||||
@@ -23,10 +23,10 @@ public:
|
||||
bool reset();
|
||||
|
||||
private:
|
||||
matador::tcp::socket server_;
|
||||
matador::tcp::socket client_;
|
||||
tcp::socket server_;
|
||||
tcp::socket client_;
|
||||
|
||||
// matador::logger log_;
|
||||
logger::logger log_;
|
||||
|
||||
std::array<char, 1> indicator_ = { { 0 } };
|
||||
std::array<char, 1> consumer_ = {};
|
||||
|
||||
Reference in New Issue
Block a user