diff --git a/src/net/tcp_client.cpp b/src/net/tcp_client.cpp index 2c6eca044471f0d52bce3b0896bb9b146b452a69..69f63fb629527707370ecfed2bcf404c3acca5fe 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 b94e966d05bb787529536181146ddf1b84c5c5fe..8123ad7305ef9e66f8a438ea31731265b1d3369d 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