blob: bfd3387aa52f9633bbd4027d1c5ac2c83a527523 [file] [log] [blame]
Mark Slee9f0c6512007-02-28 23:58:26 +00001// Copyright (c) 2006- Facebook
2// Distributed under the Thrift Software License
3//
4// See accompanying file LICENSE or visit the Thrift site at:
5// http://developers.facebook.com/thrift/
6
Marc Slemkoe03da182006-07-21 21:32:36 +00007#include <config.h>
Mark Sleee8540632006-05-30 09:24:40 +00008#include <sys/socket.h>
9#include <arpa/inet.h>
10#include <netinet/in.h>
11#include <netinet/tcp.h>
12#include <netdb.h>
13#include <unistd.h>
14#include <errno.h>
Mark Slee29050782006-09-29 00:12:30 +000015#include <fcntl.h>
16#include <sys/select.h>
Mark Sleee8540632006-05-30 09:24:40 +000017
Mark Slee29050782006-09-29 00:12:30 +000018#include "concurrency/Monitor.h"
Marc Slemkod42a2c22006-08-10 03:30:18 +000019#include "TSocket.h"
20#include "TTransportException.h"
Mark Sleee8540632006-05-30 09:24:40 +000021
Marc Slemko6f038a72006-08-03 18:58:09 +000022namespace facebook { namespace thrift { namespace transport {
23
Mark Sleee8540632006-05-30 09:24:40 +000024using namespace std;
Mark Slee29050782006-09-29 00:12:30 +000025using namespace facebook::thrift::concurrency;
Mark Sleee8540632006-05-30 09:24:40 +000026
Mark Slee29050782006-09-29 00:12:30 +000027// Global var to track total socket sys calls
Mark Slee8d7e1f62006-06-07 06:48:56 +000028uint32_t g_socket_syscalls = 0;
29
30/**
31 * TSocket implementation.
32 *
33 * @author Mark Slee <mcslee@facebook.com>
34 */
35
Mark Sleee8540632006-05-30 09:24:40 +000036// Mutex to protect syscalls to netdb
Mark Slee29050782006-09-29 00:12:30 +000037static Monitor s_netdb_monitor;
Mark Sleee8540632006-05-30 09:24:40 +000038
Mark Slee29050782006-09-29 00:12:30 +000039TSocket::TSocket(string host, int port) :
40 host_(host),
41 port_(port),
Martin Kraemeree341cb2007-02-05 21:40:38 +000042 socket_(-1),
Mark Slee29050782006-09-29 00:12:30 +000043 connTimeout_(0),
44 sendTimeout_(0),
45 recvTimeout_(0),
46 lingerOn_(1),
47 lingerVal_(0),
Aditya Agarwale04475b2007-05-23 02:14:58 +000048 noDelay_(1),
49 maxRecvRetries_(5) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +000050 recvTimeval_.tv_sec = (int)(recvTimeout_/1000);
51 recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000);
Mark Sleee8540632006-05-30 09:24:40 +000052}
53
Aditya Agarwalebc99e02007-01-15 23:14:58 +000054TSocket::TSocket() :
55 host_(""),
56 port_(0),
Martin Kraemeree341cb2007-02-05 21:40:38 +000057 socket_(-1),
Aditya Agarwalebc99e02007-01-15 23:14:58 +000058 connTimeout_(0),
59 sendTimeout_(0),
60 recvTimeout_(0),
61 lingerOn_(1),
62 lingerVal_(0),
Aditya Agarwale04475b2007-05-23 02:14:58 +000063 noDelay_(1),
64 maxRecvRetries_(5) {
Aditya Agarwalebc99e02007-01-15 23:14:58 +000065 recvTimeval_.tv_sec = (int)(recvTimeout_/1000);
66 recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000);
67}
68
Mark Slee29050782006-09-29 00:12:30 +000069TSocket::TSocket(int socket) :
70 host_(""),
71 port_(0),
72 socket_(socket),
73 connTimeout_(0),
74 sendTimeout_(0),
75 recvTimeout_(0),
76 lingerOn_(1),
77 lingerVal_(0),
Aditya Agarwale04475b2007-05-23 02:14:58 +000078 noDelay_(1),
79 maxRecvRetries_(5) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +000080 recvTimeval_.tv_sec = (int)(recvTimeout_/1000);
81 recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000);
Mark Slee29050782006-09-29 00:12:30 +000082}
83
Mark Sleee8540632006-05-30 09:24:40 +000084TSocket::~TSocket() {
85 close();
86}
87
Mark Slee8d7e1f62006-06-07 06:48:56 +000088bool TSocket::isOpen() {
Martin Kraemeree341cb2007-02-05 21:40:38 +000089 return (socket_ >= 0);
Mark Slee8d7e1f62006-06-07 06:48:56 +000090}
91
Mark Sleeb9ff32a2006-11-16 01:00:24 +000092bool TSocket::peek() {
93 if (!isOpen()) {
94 return false;
95 }
96 uint8_t buf;
97 int r = recv(socket_, &buf, 1, MSG_PEEK);
98 if (r == -1) {
boz6ded7752007-06-05 22:41:18 +000099 GlobalOutput("TSocket::peek()");
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000100 close();
Martin Kraemere6c4fa62007-07-09 19:08:25 +0000101 char b_error[1024];
102 strerror_r(errno, b_error, sizeof(b_error));
103 throw TTransportException(TTransportException::UNKNOWN, string("recv() ERROR:") + b_error);
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000104 }
105 return (r > 0);
106}
107
Mark Slee6d56eb92007-07-06 22:28:15 +0000108void TSocket::openConnection(struct addrinfo *res) {
Mark Sleea9848d72007-02-21 04:54:05 +0000109 if (isOpen()) {
110 throw TTransportException(TTransportException::ALREADY_OPEN);
111 }
Mark Slee6d56eb92007-07-06 22:28:15 +0000112
113 socket_ = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
Mark Sleee8540632006-05-30 09:24:40 +0000114 if (socket_ == -1) {
boz6ded7752007-06-05 22:41:18 +0000115 GlobalOutput("TSocket::open() socket");
Martin Kraemere6c4fa62007-07-09 19:08:25 +0000116 char b_error[1024];
117 strerror_r(errno, b_error, sizeof(b_error));
118 throw TTransportException(TTransportException::NOT_OPEN, string("socket() ERROR:") + b_error);
Mark Sleee8540632006-05-30 09:24:40 +0000119 }
Mark Slee29050782006-09-29 00:12:30 +0000120
121 // Send timeout
122 if (sendTimeout_ > 0) {
123 setSendTimeout(sendTimeout_);
124 }
125
126 // Recv timeout
127 if (recvTimeout_ > 0) {
128 setRecvTimeout(recvTimeout_);
129 }
130
131 // Linger
132 setLinger(lingerOn_, lingerVal_);
133
134 // No delay
135 setNoDelay(noDelay_);
136
Mark Slee29050782006-09-29 00:12:30 +0000137 // Set the socket to be non blocking for connect if a timeout exists
138 int flags = fcntl(socket_, F_GETFL, 0);
139 if (connTimeout_ > 0) {
Mark Sleea5a783f2007-03-02 19:41:08 +0000140 if (-1 == fcntl(socket_, F_SETFL, flags | O_NONBLOCK)) {
141 throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed");
142 }
Mark Slee29050782006-09-29 00:12:30 +0000143 } else {
Mark Sleea5a783f2007-03-02 19:41:08 +0000144 if (-1 == fcntl(socket_, F_SETFL, flags & ~O_NONBLOCK)) {
145 throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed");
146 }
Mark Slee29050782006-09-29 00:12:30 +0000147 }
148
149 // Conn timeout
150 struct timeval c = {(int)(connTimeout_/1000),
151 (int)((connTimeout_%1000)*1000)};
Mark Sleee8540632006-05-30 09:24:40 +0000152
153 // Connect the socket
Mark Slee6d56eb92007-07-06 22:28:15 +0000154 int ret = connect(socket_, res->ai_addr, res->ai_addrlen);
Mark Sleee8540632006-05-30 09:24:40 +0000155
Mark Slee29050782006-09-29 00:12:30 +0000156 if (ret == 0) {
157 goto done;
158 }
159
160 if (errno != EINPROGRESS) {
Mark Slee29050782006-09-29 00:12:30 +0000161 char buff[1024];
boz6ded7752007-06-05 22:41:18 +0000162 GlobalOutput(buff);
Martin Kraemere6c4fa62007-07-09 19:08:25 +0000163 sprintf(buff, "TSocket::open() connect %s %d", host_.c_str(), port_);
164
165 char b_error[1024];
166 strerror_r(errno, b_error, sizeof(b_error));
167 throw TTransportException(TTransportException::NOT_OPEN, string("open() ERROR: ") + b_error);
Mark Sleee8540632006-05-30 09:24:40 +0000168 }
169
Mark Slee29050782006-09-29 00:12:30 +0000170 fd_set fds;
171 FD_ZERO(&fds);
172 FD_SET(socket_, &fds);
173 ret = select(socket_+1, NULL, &fds, NULL, &c);
174
175 if (ret > 0) {
176 // Ensure connected
177 int val;
178 socklen_t lon;
179 lon = sizeof(int);
180 int ret2 = getsockopt(socket_, SOL_SOCKET, SO_ERROR, (void *)&val, &lon);
181 if (ret2 == -1) {
boz6ded7752007-06-05 22:41:18 +0000182 GlobalOutput("TSocket::open() getsockopt SO_ERROR");
Martin Kraemere6c4fa62007-07-09 19:08:25 +0000183 char b_error[1024];
184 strerror_r(errno, b_error, sizeof(b_error));
185 throw TTransportException(TTransportException::NOT_OPEN, string("open() ERROR: ") + b_error);
Mark Slee29050782006-09-29 00:12:30 +0000186 }
187 if (val == 0) {
188 goto done;
189 }
boz6ded7752007-06-05 22:41:18 +0000190 GlobalOutput("TSocket::open() SO_ERROR was set");
Martin Kraemere6c4fa62007-07-09 19:08:25 +0000191 char b_error[1024];
192 strerror_r(errno, b_error, sizeof(b_error));
193 throw TTransportException(TTransportException::NOT_OPEN, string("open() ERROR: ") + b_error);
Mark Slee29050782006-09-29 00:12:30 +0000194 } else if (ret == 0) {
boz6ded7752007-06-05 22:41:18 +0000195 GlobalOutput("TSocket::open() timeed out");
Martin Kraemere6c4fa62007-07-09 19:08:25 +0000196 char b_error[1024];
197 strerror_r(errno, b_error, sizeof(b_error));
198 throw TTransportException(TTransportException::NOT_OPEN, string("open() ERROR: ") + b_error);
Mark Slee29050782006-09-29 00:12:30 +0000199 } else {
boz6ded7752007-06-05 22:41:18 +0000200 GlobalOutput("TSocket::open() select error");
Martin Kraemere6c4fa62007-07-09 19:08:25 +0000201 char b_error[1024];
202 strerror_r(errno, b_error, sizeof(b_error));
203 throw TTransportException(TTransportException::NOT_OPEN, string("open() ERROR: ") + b_error);
Mark Slee29050782006-09-29 00:12:30 +0000204 }
205
206 done:
207 // Set socket back to normal mode (blocking)
208 fcntl(socket_, F_SETFL, flags);
Mark Sleee8540632006-05-30 09:24:40 +0000209}
210
Mark Slee6d56eb92007-07-06 22:28:15 +0000211void TSocket::open() {
212 if (isOpen()) {
213 throw TTransportException(TTransportException::ALREADY_OPEN);
214 }
215
216 // Validate port number
217 if (port_ < 0 || port_ > 65536) {
218 throw TTransportException(TTransportException::NOT_OPEN, "Specified port is invalid");
219 }
220
221 struct addrinfo hints, *res, *res0;
222 int error;
223 char port[sizeof("65536") + 1];
224 memset(&hints, 0, sizeof(hints));
225 hints.ai_family = PF_UNSPEC;
226 hints.ai_socktype = SOCK_STREAM;
227 hints.ai_flags = AI_PASSIVE;
228 sprintf(port, "%d", port_);
229
230 {
231 // Scope lock on host entry lookup
232 Synchronized s(s_netdb_monitor);
233 error = getaddrinfo(host_.c_str(), port, &hints, &res0);
234 }
235 if (error) {
236 fprintf(stderr, "getaddrinfo %d: %s\n", error, gai_strerror(error));
237 close();
238 throw TTransportException(TTransportException::NOT_OPEN, "Could not resolve host for client socket.");
239 }
240
241 // Cycle through all the returned addresses until one
242 // connects or push the exception up.
243 for (res = res0; res; res = res->ai_next) {
244 try {
245 openConnection(res);
246 break;
247 } catch (TTransportException& ttx) {
248 if (res->ai_next) {
249 close();
250 } else {
251 close();
252 throw;
253 }
254 }
255 }
256}
257
Mark Sleee8540632006-05-30 09:24:40 +0000258void TSocket::close() {
Martin Kraemeree341cb2007-02-05 21:40:38 +0000259 if (socket_ >= 0) {
Mark Sleee8540632006-05-30 09:24:40 +0000260 shutdown(socket_, SHUT_RDWR);
261 ::close(socket_);
262 }
Martin Kraemeree341cb2007-02-05 21:40:38 +0000263 socket_ = -1;
Mark Sleee8540632006-05-30 09:24:40 +0000264}
265
Mark Slee8d7e1f62006-06-07 06:48:56 +0000266uint32_t TSocket::read(uint8_t* buf, uint32_t len) {
Martin Kraemeree341cb2007-02-05 21:40:38 +0000267 if (socket_ < 0) {
Mark Sleef9831082007-02-20 20:59:21 +0000268 throw TTransportException(TTransportException::NOT_OPEN, "Called read on non-open socket");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000269 }
Mark Sleee8540632006-05-30 09:24:40 +0000270
Aditya Agarwale04475b2007-05-23 02:14:58 +0000271 int32_t retries = 0;
272
273 // EAGAIN can be signalled both when a timeout has occurred and when
274 // the system is out of resources (an awesome undocumented feature).
275 // The following is an approximation of the time interval under which
276 // EAGAIN is taken to indicate an out of resources error.
277 uint32_t eagainThresholdMicros = 0;
278 if (recvTimeout_) {
279 // if a readTimeout is specified along with a max number of recv retries, then
280 // the threshold will ensure that the read timeout is not exceeded even in the
281 // case of resource errors
282 eagainThresholdMicros = (recvTimeout_*1000)/ ((maxRecvRetries_>0) ? maxRecvRetries_ : 2);
283 }
284
285 try_again:
Mark Slee8d7e1f62006-06-07 06:48:56 +0000286 // Read from the socket
Aditya Agarwale04475b2007-05-23 02:14:58 +0000287 struct timeval begin;
288 gettimeofday(&begin, NULL);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000289 int got = recv(socket_, buf, len, 0);
Aditya Agarwale04475b2007-05-23 02:14:58 +0000290 struct timeval end;
291 gettimeofday(&end, NULL);
292 uint32_t readElapsedMicros = (((end.tv_sec - begin.tv_sec) * 1000 * 1000)
293 + (((uint64_t)(end.tv_usec - begin.tv_usec))));
Mark Slee8d7e1f62006-06-07 06:48:56 +0000294 ++g_socket_syscalls;
Aditya Agarwale04475b2007-05-23 02:14:58 +0000295
Mark Slee8d7e1f62006-06-07 06:48:56 +0000296 // Check for error on read
Mark Sleec4257802007-01-24 23:14:30 +0000297 if (got < 0) {
Aditya Agarwale04475b2007-05-23 02:14:58 +0000298 if (errno == EAGAIN) {
299 // check if this is the lack of resources or timeout case
300 if (!eagainThresholdMicros || (readElapsedMicros < eagainThresholdMicros)) {
301 if (retries++ < maxRecvRetries_) {
302 usleep(50);
303 goto try_again;
304 } else {
305 throw TTransportException(TTransportException::TIMED_OUT,
306 "EAGAIN (unavailable resources)");
307 }
308 } else {
309 // infer that timeout has been hit
310 throw TTransportException(TTransportException::TIMED_OUT,
311 "EAGAIN (timed out)");
312 }
Mark Sleee8540632006-05-30 09:24:40 +0000313 }
314
Mark Slee8d7e1f62006-06-07 06:48:56 +0000315 // If interrupted, try again
Aditya Agarwale04475b2007-05-23 02:14:58 +0000316 if (errno == EINTR && retries++ < maxRecvRetries_) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000317 goto try_again;
Mark Sleee8540632006-05-30 09:24:40 +0000318 }
319
Mark Sleec4257802007-01-24 23:14:30 +0000320 // Now it's not a try again case, but a real probblez
boz6ded7752007-06-05 22:41:18 +0000321 GlobalOutput("TSocket::read()");
Mark Sleec4257802007-01-24 23:14:30 +0000322
Mark Slee8d7e1f62006-06-07 06:48:56 +0000323 // If we disconnect with no linger time
324 if (errno == ECONNRESET) {
Mark Sleef9831082007-02-20 20:59:21 +0000325 throw TTransportException(TTransportException::NOT_OPEN, "ECONNRESET");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000326 }
327
328 // This ish isn't open
329 if (errno == ENOTCONN) {
Mark Sleef9831082007-02-20 20:59:21 +0000330 throw TTransportException(TTransportException::NOT_OPEN, "ENOTCONN");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000331 }
332
333 // Timed out!
334 if (errno == ETIMEDOUT) {
Mark Sleef9831082007-02-20 20:59:21 +0000335 throw TTransportException(TTransportException::TIMED_OUT, "ETIMEDOUT");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000336 }
337
338 // Some other error, whatevz
Mark Slee5f68c712007-05-11 17:58:54 +0000339 char buff[1024];
340 sprintf(buff, "ERROR errno: %d", errno);
341 throw TTransportException(TTransportException::UNKNOWN, buff);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000342 }
343
344 // The remote host has closed the socket
345 if (got == 0) {
346 close();
347 return 0;
Mark Sleee8540632006-05-30 09:24:40 +0000348 }
349
350 // Pack data into string
Mark Slee8d7e1f62006-06-07 06:48:56 +0000351 return got;
Mark Sleee8540632006-05-30 09:24:40 +0000352}
353
Mark Slee8d7e1f62006-06-07 06:48:56 +0000354void TSocket::write(const uint8_t* buf, uint32_t len) {
Martin Kraemeree341cb2007-02-05 21:40:38 +0000355 if (socket_ < 0) {
Mark Sleef9831082007-02-20 20:59:21 +0000356 throw TTransportException(TTransportException::NOT_OPEN, "Called write on non-open socket");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000357 }
358
Mark Sleee8540632006-05-30 09:24:40 +0000359 uint32_t sent = 0;
360
Mark Slee8d7e1f62006-06-07 06:48:56 +0000361 while (sent < len) {
Marc Slemko9d4a3e22006-07-21 19:53:48 +0000362
363 int flags = 0;
Mark Slee29050782006-09-29 00:12:30 +0000364 #ifdef MSG_NOSIGNAL
Mark Slee8d7e1f62006-06-07 06:48:56 +0000365 // Note the use of MSG_NOSIGNAL to suppress SIGPIPE errors, instead we
366 // check for the EPIPE return condition and close the socket in that case
Marc Slemko9d4a3e22006-07-21 19:53:48 +0000367 flags |= MSG_NOSIGNAL;
Mark Slee29050782006-09-29 00:12:30 +0000368 #endif // ifdef MSG_NOSIGNAL
Marc Slemko9d4a3e22006-07-21 19:53:48 +0000369
370 int b = send(socket_, buf + sent, len - sent, flags);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000371 ++g_socket_syscalls;
372
Mark Sleee8540632006-05-30 09:24:40 +0000373 // Fail on a send error
374 if (b < 0) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000375 if (errno == EPIPE) {
376 close();
Mark Sleef9831082007-02-20 20:59:21 +0000377 throw TTransportException(TTransportException::NOT_OPEN, "EPIPE");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000378 }
379
380 if (errno == ECONNRESET) {
381 close();
Mark Sleef9831082007-02-20 20:59:21 +0000382 throw TTransportException(TTransportException::NOT_OPEN, "ECONNRESET");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000383 }
384
385 if (errno == ENOTCONN) {
386 close();
Mark Sleef9831082007-02-20 20:59:21 +0000387 throw TTransportException(TTransportException::NOT_OPEN, "ENOTCONN");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000388 }
389
boz6ded7752007-06-05 22:41:18 +0000390 GlobalOutput("TSocket::write() send < 0");
Martin Kraemere6c4fa62007-07-09 19:08:25 +0000391 char b_error[1024];
392 strerror_r(errno, b_error, sizeof(b_error));
393 throw TTransportException(TTransportException::UNKNOWN, string("ERROR:") + b_error);
Mark Sleee8540632006-05-30 09:24:40 +0000394 }
395
396 // Fail on blocked send
397 if (b == 0) {
Mark Sleef9831082007-02-20 20:59:21 +0000398 throw TTransportException(TTransportException::NOT_OPEN, "Socket send returned 0.");
Mark Sleee8540632006-05-30 09:24:40 +0000399 }
Mark Sleee8540632006-05-30 09:24:40 +0000400 sent += b;
401 }
402}
403
Aditya Agarwalebc99e02007-01-15 23:14:58 +0000404void TSocket::setHost(string host) {
405 host_ = host;
406}
407
408void TSocket::setPort(int port) {
409 port_ = port;
410}
411
Mark Slee8d7e1f62006-06-07 06:48:56 +0000412void TSocket::setLinger(bool on, int linger) {
Mark Slee29050782006-09-29 00:12:30 +0000413 lingerOn_ = on;
414 lingerVal_ = linger;
Martin Kraemeree341cb2007-02-05 21:40:38 +0000415 if (socket_ < 0) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000416 return;
417 }
418
Mark Slee29050782006-09-29 00:12:30 +0000419 struct linger l = {(lingerOn_ ? 1 : 0), lingerVal_};
420 int ret = setsockopt(socket_, SOL_SOCKET, SO_LINGER, &l, sizeof(l));
421 if (ret == -1) {
boz6ded7752007-06-05 22:41:18 +0000422 GlobalOutput("TSocket::setLinger()");
Mark Sleee8540632006-05-30 09:24:40 +0000423 }
Mark Sleee8540632006-05-30 09:24:40 +0000424}
425
Mark Slee8d7e1f62006-06-07 06:48:56 +0000426void TSocket::setNoDelay(bool noDelay) {
Mark Slee29050782006-09-29 00:12:30 +0000427 noDelay_ = noDelay;
Martin Kraemeree341cb2007-02-05 21:40:38 +0000428 if (socket_ < 0) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000429 return;
430 }
431
Mark Sleee8540632006-05-30 09:24:40 +0000432 // Set socket to NODELAY
Mark Slee29050782006-09-29 00:12:30 +0000433 int v = noDelay_ ? 1 : 0;
434 int ret = setsockopt(socket_, IPPROTO_TCP, TCP_NODELAY, &v, sizeof(v));
435 if (ret == -1) {
boz6ded7752007-06-05 22:41:18 +0000436 GlobalOutput("TSocket::setNoDelay()");
Mark Sleee8540632006-05-30 09:24:40 +0000437 }
Mark Sleee8540632006-05-30 09:24:40 +0000438}
Mark Slee29050782006-09-29 00:12:30 +0000439
440void TSocket::setConnTimeout(int ms) {
441 connTimeout_ = ms;
442}
443
444void TSocket::setRecvTimeout(int ms) {
445 recvTimeout_ = ms;
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000446 recvTimeval_.tv_sec = (int)(recvTimeout_/1000);
447 recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000);
Martin Kraemeree341cb2007-02-05 21:40:38 +0000448 if (socket_ < 0) {
Mark Slee29050782006-09-29 00:12:30 +0000449 return;
450 }
451
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000452 // Copy because select may modify
453 struct timeval r = recvTimeval_;
Mark Slee29050782006-09-29 00:12:30 +0000454 int ret = setsockopt(socket_, SOL_SOCKET, SO_RCVTIMEO, &r, sizeof(r));
455 if (ret == -1) {
boz6ded7752007-06-05 22:41:18 +0000456 GlobalOutput("TSocket::setRecvTimeout()");
Mark Slee29050782006-09-29 00:12:30 +0000457 }
458}
459
460void TSocket::setSendTimeout(int ms) {
461 sendTimeout_ = ms;
Martin Kraemeree341cb2007-02-05 21:40:38 +0000462 if (socket_ < 0) {
Mark Slee29050782006-09-29 00:12:30 +0000463 return;
464 }
465
466 struct timeval s = {(int)(sendTimeout_/1000),
467 (int)((sendTimeout_%1000)*1000)};
468 int ret = setsockopt(socket_, SOL_SOCKET, SO_SNDTIMEO, &s, sizeof(s));
469 if (ret == -1) {
boz6ded7752007-06-05 22:41:18 +0000470 GlobalOutput("TSocket::setSendTimeout()");
Mark Slee29050782006-09-29 00:12:30 +0000471 }
472}
473
Aditya Agarwale04475b2007-05-23 02:14:58 +0000474void TSocket::setMaxRecvRetries(int maxRecvRetries) {
475 maxRecvRetries_ = maxRecvRetries;
476}
477
Marc Slemko6f038a72006-08-03 18:58:09 +0000478}}} // facebook::thrift::transport