From e0e956cf785e4b29ae715b0f9992d6c2039589c5 Mon Sep 17 00:00:00 2001 From: Fred Nicolson Date: Sat, 31 Dec 2016 11:14:26 +0000 Subject: [PATCH] Socket::reconfigure_socket called on connection to apply options 'TCP_NODELAY' is set automatically on connection now. --- include/frnetlib/Socket.h | 6 ++++++ src/SSLSocket.cpp | 5 ++++- src/Socket.cpp | 8 ++++++++ src/TcpSocket.cpp | 7 +++++-- 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/include/frnetlib/Socket.h b/include/frnetlib/Socket.h index af384c8..c825f3b 100644 --- a/include/frnetlib/Socket.h +++ b/include/frnetlib/Socket.h @@ -155,6 +155,12 @@ namespace fr virtual bool has_data() const = 0; protected: + /*! + * Applies requested socket options to the socket. + * Should be called when a new socket is created. + */ + void reconfigure_socket(); + std::string remote_address; bool is_blocking; bool is_connected; diff --git a/src/SSLSocket.cpp b/src/SSLSocket.cpp index 782fba0..486eaf0 100644 --- a/src/SSLSocket.cpp +++ b/src/SSLSocket.cpp @@ -152,9 +152,11 @@ namespace fr return Socket::Status::VerificationFailed; } - //Update members + //Update state is_connected = true; remote_address = address + ":" + port; + reconfigure_socket(); + return Socket::Status::Success; } @@ -167,6 +169,7 @@ namespace fr { is_connected = true; ssl_socket_descriptor = std::move(context); + reconfigure_socket(); } bool SSLSocket::has_data() const diff --git a/src/Socket.cpp b/src/Socket.cpp index 3d3ca61..3c2661d 100644 --- a/src/Socket.cpp +++ b/src/Socket.cpp @@ -2,6 +2,7 @@ // Created by fred on 06/12/16. // +#include #include "frnetlib/Socket.h" namespace fr @@ -97,4 +98,11 @@ namespace fr { ::shutdown(get_socket_descriptor(), 0); } + + void Socket::reconfigure_socket() + { + //todo: Perhaps allow for these settings to be modified + int one = 1; + setsockopt(get_socket_descriptor(), SOL_TCP, TCP_NODELAY, &one, sizeof(one)); + } } \ No newline at end of file diff --git a/src/TcpSocket.cpp b/src/TcpSocket.cpp index ead70d3..bf75320 100644 --- a/src/TcpSocket.cpp +++ b/src/TcpSocket.cpp @@ -100,14 +100,13 @@ namespace fr void TcpSocket::set_descriptor(int descriptor) { + reconfigure_socket(); socket_descriptor = descriptor; is_connected = true; } Socket::Status TcpSocket::connect(const std::string &address, const std::string &port) { - remote_address = address + ":" + port; - addrinfo *info; addrinfo hints; @@ -145,7 +144,11 @@ namespace fr //We're done with this now, cleanup freeaddrinfo(info); + //Update state now we've got a valid socket descriptor is_connected = true; + remote_address = address + ":" + port; + reconfigure_socket(); + return Socket::Status::Success; }