From 87d33bd18868a67f1771142e2b2f2b9673b2c8f8 Mon Sep 17 00:00:00 2001 From: Benny Baumann <BenBE@geshi.org> Date: Sun, 19 Feb 2023 22:13:18 +0100 Subject: [PATCH] add: Allow specifying target with socketaddress instance --- src/net/tcp_client.cpp | 18 ++++++++++++++++++ src/net/tcp_client.hpp | 15 ++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/net/tcp_client.cpp b/src/net/tcp_client.cpp index 2c6eca0..69f63fb 100644 --- a/src/net/tcp_client.cpp +++ b/src/net/tcp_client.cpp @@ -38,6 +38,24 @@ tcp_client::tcp_client( // TODO log created client } +tcp_client::tcp_client( + const destructor_cb_type destructor_cb_, + auto_fd &&socket_fd, + const socketaddr &peer_address_ +) : + connection_client{}, + destructor_cb(destructor_cb_), + peer_address{peer_address_.str()}, + port{}, + net_socket(std::forward<auto_fd>(socket_fd)), + io{}, + write_queue{} +{ + io.set<tcp_client, &tcp_client::cb_ev>(this); + io.start(this->net_socket.get(), ::ev::READ); + // TODO log created client +} + tcp_client::tcp_client( const std::string &peer_address_, const std::string &service_or_port, diff --git a/src/net/tcp_client.hpp b/src/net/tcp_client.hpp index b94e966..8123ad7 100644 --- a/src/net/tcp_client.hpp +++ b/src/net/tcp_client.hpp @@ -18,6 +18,7 @@ #include "net/async_fd.hpp" #include "net/connection_client.hpp" #include "net/ioqueue.hpp" +#include "net/socketaddress.hpp" namespace rmrf::net { @@ -41,7 +42,7 @@ public: private: const destructor_cb_type destructor_cb; - const std::string peer_address; + std::string peer_address; uint16_t port; auto_fd net_socket; @@ -62,6 +63,18 @@ public: */ tcp_client(const destructor_cb_type destructor_cb_, auto_fd&& socket_fd, std::string peer_address_, uint16_t port_); + /** + * Construct a new TCP client using an already existing socket. This might be particulary useful if you've + * got a server and accept incoming connections. + * + * @brief Connect to a TCP server + * @param destructor_cb_ The callback that should be issued when the client closes or looses it's connection + * @param socket_fd A file descriptor for an already open socket to be wrapped by this client + * @param peer_address_ The address the socket is bound to on the other end of the connection + * @param port_ The bound port + */ + tcp_client(const destructor_cb_type destructor_cb_, auto_fd&& socket_fd, const socketaddr& peer_address_); + /** * Construct a new TCP client using an address and port pair. * @brief Connect to a TCP server -- GitLab