diff options
author | louiz’ <louiz@louiz.org> | 2016-11-11 02:54:48 +0100 |
---|---|---|
committer | louiz’ <louiz@louiz.org> | 2016-11-11 02:54:48 +0100 |
commit | 0c8adc85f7373a85de8b3edc6cac87d5f7389bb3 (patch) | |
tree | aefa90f4325424716b1304b4eac42d43371df1ca /louloulibs/network/tcp_client_socket_handler.hpp | |
parent | c41d003cbf3b14b83e0f9bf6c4787c3bd60bb7ee (diff) | |
download | biboumi-0c8adc85f7373a85de8b3edc6cac87d5f7389bb3.tar.gz biboumi-0c8adc85f7373a85de8b3edc6cac87d5f7389bb3.tar.bz2 biboumi-0c8adc85f7373a85de8b3edc6cac87d5f7389bb3.tar.xz biboumi-0c8adc85f7373a85de8b3edc6cac87d5f7389bb3.zip |
Move all the connect() logic from TCPSocketHandler into a subclass
This way, TCPSocketHandler only deal with the message sending/receiving, not
the connect() or anything else. This will be used for implementing servers
(because when a client is accepted, we don’t need all the connect() and dns
resolution stuff).
Diffstat (limited to 'louloulibs/network/tcp_client_socket_handler.hpp')
-rw-r--r-- | louloulibs/network/tcp_client_socket_handler.hpp | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/louloulibs/network/tcp_client_socket_handler.hpp b/louloulibs/network/tcp_client_socket_handler.hpp new file mode 100644 index 0000000..7dd476c --- /dev/null +++ b/louloulibs/network/tcp_client_socket_handler.hpp @@ -0,0 +1,75 @@ +#pragma once + +#include <network/tcp_socket_handler.hpp> + +class TCPClientSocketHandler: public TCPSocketHandler +{ + public: + TCPClientSocketHandler(std::shared_ptr<Poller> poller); + ~TCPClientSocketHandler(); + /** + * Connect to the remote server, and call on_connected() if this + * succeeds. If tls is true, we set use_tls to true and will also call + * start_tls() when the connection succeeds. + */ + void connect(const std::string& address, const std::string& port, const bool tls); + void connect() override final; + /** + * Called by a TimedEvent, when the connection did not succeed or fail + * after a given time. + */ + void on_connection_timeout(); + /** + * Called when the connection is successful. + */ + virtual void on_connected() = 0; + bool is_connected() const override; + bool is_connecting() const; + + std::string get_port() const; + + void close() override final; + std::chrono::system_clock::time_point connection_date; + + protected: + bool hostname_resolution_failed; + /** + * Address to bind the socket to, before calling connect(). + * If empty, it’s equivalent to binding to INADDR_ANY. + */ + std::string bind_addr; + /** + * Display the resolved IP, just for information purpose. + */ + void display_resolved_ip(struct addrinfo* rp) const; + private: + /** + * Initialize the socket with the parameters contained in the given + * addrinfo structure. + */ + void init_socket(const struct addrinfo* rp); + /** + * DNS resolver + */ + Resolver resolver; + /** + * Keep the details of the addrinfo returned by the resolver that + * triggered a EINPROGRESS error when connect()ing to it, to reuse it + * directly when connect() is called again. + */ + struct addrinfo addrinfo; + struct sockaddr_in6 ai_addr; + socklen_t ai_addrlen; + + /** + * Hostname we are connected/connecting to + */ + std::string address; + /** + * Port we are connected/connecting to + */ + std::string port; + + bool connected; + bool connecting; +}; |