@@ -112,6 +112,16 @@ void SetNonBlock(SOCKET fd, bool value) {
112112#endif
113113}
114114
115+ void SetTimeout (SOCKET fd, const SocketTimeoutParams& timeout_params) {
116+ #if defined(_unix_)
117+ timeval recv_timeout { .tv_sec = timeout_params.recv_timeout .count (), .tv_usec = 0 };
118+ setsockopt (fd, SOL_SOCKET, SO_RCVTIMEO, &recv_timeout, sizeof (recv_timeout));
119+
120+ timeval send_timeout { .tv_sec = timeout_params.send_timeout .count (), .tv_usec = 0 };
121+ setsockopt (fd, SOL_SOCKET, SO_SNDTIMEO, &send_timeout, sizeof (send_timeout));
122+ #endif
123+ };
124+
115125ssize_t Poll (struct pollfd * fds, int nfds, int timeout) noexcept {
116126#if defined(_win_)
117127 return WSAPoll (fds, nfds, timeout);
@@ -120,7 +130,7 @@ ssize_t Poll(struct pollfd* fds, int nfds, int timeout) noexcept {
120130#endif
121131}
122132
123- SOCKET SocketConnect (const NetworkAddress& addr) {
133+ SOCKET SocketConnect (const NetworkAddress& addr, const SocketTimeoutParams& timeout_params ) {
124134 int last_err = 0 ;
125135 for (auto res = addr.Info (); res != nullptr ; res = res->ai_next ) {
126136 SOCKET s (socket (res->ai_family , res->ai_socktype , res->ai_protocol ));
@@ -130,6 +140,7 @@ SOCKET SocketConnect(const NetworkAddress& addr) {
130140 }
131141
132142 SetNonBlock (s, true );
143+ SetTimeout (s, timeout_params);
133144
134145 if (connect (s, res->ai_addr , (int )res->ai_addrlen ) != 0 ) {
135146 int err = getSocketErrorCode ();
@@ -213,22 +224,24 @@ NetworkAddress::~NetworkAddress() {
213224const struct addrinfo * NetworkAddress::Info () const {
214225 return info_;
215226}
227+
216228const std::string & NetworkAddress::Host () const {
217229 return host_;
218230}
219231
220232
221233SocketBase::~SocketBase () = default ;
222234
235+
223236SocketFactory::~SocketFactory () = default ;
224237
225238void SocketFactory::sleepFor (const std::chrono::milliseconds& duration) {
226239 std::this_thread::sleep_for (duration);
227240}
228241
229242
230- Socket::Socket (const NetworkAddress& addr)
231- : handle_(SocketConnect(addr))
243+ Socket::Socket (const NetworkAddress& addr, const SocketTimeoutParams& timeout_params )
244+ : handle_(SocketConnect(addr, timeout_params ))
232245{}
233246
234247Socket::Socket (Socket&& other) noexcept
@@ -300,19 +313,21 @@ std::unique_ptr<OutputStream> Socket::makeOutputStream() const {
300313 return std::make_unique<SocketOutput>(handle_);
301314}
302315
316+
303317NonSecureSocketFactory::~NonSecureSocketFactory () {}
304318
305319std::unique_ptr<SocketBase> NonSecureSocketFactory::connect (const ClientOptions &opts) {
306320 const auto address = NetworkAddress (opts.host , std::to_string (opts.port ));
307321
308- auto socket = doConnect (address);
322+ auto socket = doConnect (address, opts );
309323 setSocketOptions (*socket, opts);
310324
311325 return socket;
312326}
313327
314- std::unique_ptr<Socket> NonSecureSocketFactory::doConnect (const NetworkAddress& address) {
315- return std::make_unique<Socket>(address);
328+ std::unique_ptr<Socket> NonSecureSocketFactory::doConnect (const NetworkAddress& address, const ClientOptions& opts) {
329+ SocketTimeoutParams timeout_params { opts.connection_recv_timeout , opts.connection_send_timeout };
330+ return std::make_unique<Socket>(address, timeout_params);
316331}
317332
318333void NonSecureSocketFactory::setSocketOptions (Socket &socket, const ClientOptions &opts) {
@@ -327,6 +342,7 @@ void NonSecureSocketFactory::setSocketOptions(Socket &socket, const ClientOption
327342 }
328343}
329344
345+
330346SocketInput::SocketInput (SOCKET s)
331347 : s_(s)
332348{
0 commit comments