blob: a5a07b376f1190fc8f1f24307addf24a75e31a35 [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();
Mark Slee85287d32007-07-09 19:50:30 +0000252 freeaddrinfo(res0); // cleanup on failure
Mark Slee6d56eb92007-07-06 22:28:15 +0000253 throw;
254 }
255 }
256 }
Mark Slee85287d32007-07-09 19:50:30 +0000257
258 // Free address structure memory
259 freeaddrinfo(res0);
Mark Slee6d56eb92007-07-06 22:28:15 +0000260}
261
Mark Sleee8540632006-05-30 09:24:40 +0000262void TSocket::close() {
Martin Kraemeree341cb2007-02-05 21:40:38 +0000263 if (socket_ >= 0) {
Mark Sleee8540632006-05-30 09:24:40 +0000264 shutdown(socket_, SHUT_RDWR);
265 ::close(socket_);
266 }
Martin Kraemeree341cb2007-02-05 21:40:38 +0000267 socket_ = -1;
Mark Sleee8540632006-05-30 09:24:40 +0000268}
269
Mark Slee8d7e1f62006-06-07 06:48:56 +0000270uint32_t TSocket::read(uint8_t* buf, uint32_t len) {
Martin Kraemeree341cb2007-02-05 21:40:38 +0000271 if (socket_ < 0) {
Mark Sleef9831082007-02-20 20:59:21 +0000272 throw TTransportException(TTransportException::NOT_OPEN, "Called read on non-open socket");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000273 }
Mark Sleee8540632006-05-30 09:24:40 +0000274
Aditya Agarwale04475b2007-05-23 02:14:58 +0000275 int32_t retries = 0;
276
277 // EAGAIN can be signalled both when a timeout has occurred and when
278 // the system is out of resources (an awesome undocumented feature).
279 // The following is an approximation of the time interval under which
280 // EAGAIN is taken to indicate an out of resources error.
281 uint32_t eagainThresholdMicros = 0;
282 if (recvTimeout_) {
283 // if a readTimeout is specified along with a max number of recv retries, then
284 // the threshold will ensure that the read timeout is not exceeded even in the
285 // case of resource errors
286 eagainThresholdMicros = (recvTimeout_*1000)/ ((maxRecvRetries_>0) ? maxRecvRetries_ : 2);
287 }
288
289 try_again:
Mark Slee8d7e1f62006-06-07 06:48:56 +0000290 // Read from the socket
Aditya Agarwale04475b2007-05-23 02:14:58 +0000291 struct timeval begin;
292 gettimeofday(&begin, NULL);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000293 int got = recv(socket_, buf, len, 0);
Aditya Agarwale04475b2007-05-23 02:14:58 +0000294 struct timeval end;
295 gettimeofday(&end, NULL);
296 uint32_t readElapsedMicros = (((end.tv_sec - begin.tv_sec) * 1000 * 1000)
297 + (((uint64_t)(end.tv_usec - begin.tv_usec))));
Mark Slee8d7e1f62006-06-07 06:48:56 +0000298 ++g_socket_syscalls;
Aditya Agarwale04475b2007-05-23 02:14:58 +0000299
Mark Slee8d7e1f62006-06-07 06:48:56 +0000300 // Check for error on read
Mark Sleec4257802007-01-24 23:14:30 +0000301 if (got < 0) {
Aditya Agarwale04475b2007-05-23 02:14:58 +0000302 if (errno == EAGAIN) {
303 // check if this is the lack of resources or timeout case
304 if (!eagainThresholdMicros || (readElapsedMicros < eagainThresholdMicros)) {
305 if (retries++ < maxRecvRetries_) {
306 usleep(50);
307 goto try_again;
308 } else {
309 throw TTransportException(TTransportException::TIMED_OUT,
310 "EAGAIN (unavailable resources)");
311 }
312 } else {
313 // infer that timeout has been hit
314 throw TTransportException(TTransportException::TIMED_OUT,
315 "EAGAIN (timed out)");
316 }
Mark Sleee8540632006-05-30 09:24:40 +0000317 }
318
Mark Slee8d7e1f62006-06-07 06:48:56 +0000319 // If interrupted, try again
Aditya Agarwale04475b2007-05-23 02:14:58 +0000320 if (errno == EINTR && retries++ < maxRecvRetries_) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000321 goto try_again;
Mark Sleee8540632006-05-30 09:24:40 +0000322 }
323
Mark Sleec4257802007-01-24 23:14:30 +0000324 // Now it's not a try again case, but a real probblez
boz6ded7752007-06-05 22:41:18 +0000325 GlobalOutput("TSocket::read()");
Mark Sleec4257802007-01-24 23:14:30 +0000326
Mark Slee8d7e1f62006-06-07 06:48:56 +0000327 // If we disconnect with no linger time
328 if (errno == ECONNRESET) {
Mark Sleef9831082007-02-20 20:59:21 +0000329 throw TTransportException(TTransportException::NOT_OPEN, "ECONNRESET");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000330 }
331
332 // This ish isn't open
333 if (errno == ENOTCONN) {
Mark Sleef9831082007-02-20 20:59:21 +0000334 throw TTransportException(TTransportException::NOT_OPEN, "ENOTCONN");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000335 }
336
337 // Timed out!
338 if (errno == ETIMEDOUT) {
Mark Sleef9831082007-02-20 20:59:21 +0000339 throw TTransportException(TTransportException::TIMED_OUT, "ETIMEDOUT");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000340 }
341
342 // Some other error, whatevz
Mark Slee5f68c712007-05-11 17:58:54 +0000343 char buff[1024];
344 sprintf(buff, "ERROR errno: %d", errno);
345 throw TTransportException(TTransportException::UNKNOWN, buff);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000346 }
347
348 // The remote host has closed the socket
349 if (got == 0) {
350 close();
351 return 0;
Mark Sleee8540632006-05-30 09:24:40 +0000352 }
353
354 // Pack data into string
Mark Slee8d7e1f62006-06-07 06:48:56 +0000355 return got;
Mark Sleee8540632006-05-30 09:24:40 +0000356}
357
Mark Slee8d7e1f62006-06-07 06:48:56 +0000358void TSocket::write(const uint8_t* buf, uint32_t len) {
Martin Kraemeree341cb2007-02-05 21:40:38 +0000359 if (socket_ < 0) {
Mark Sleef9831082007-02-20 20:59:21 +0000360 throw TTransportException(TTransportException::NOT_OPEN, "Called write on non-open socket");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000361 }
362
Mark Sleee8540632006-05-30 09:24:40 +0000363 uint32_t sent = 0;
364
Mark Slee8d7e1f62006-06-07 06:48:56 +0000365 while (sent < len) {
Marc Slemko9d4a3e22006-07-21 19:53:48 +0000366
367 int flags = 0;
Mark Slee29050782006-09-29 00:12:30 +0000368 #ifdef MSG_NOSIGNAL
Mark Slee8d7e1f62006-06-07 06:48:56 +0000369 // Note the use of MSG_NOSIGNAL to suppress SIGPIPE errors, instead we
370 // check for the EPIPE return condition and close the socket in that case
Marc Slemko9d4a3e22006-07-21 19:53:48 +0000371 flags |= MSG_NOSIGNAL;
Mark Slee29050782006-09-29 00:12:30 +0000372 #endif // ifdef MSG_NOSIGNAL
Marc Slemko9d4a3e22006-07-21 19:53:48 +0000373
374 int b = send(socket_, buf + sent, len - sent, flags);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000375 ++g_socket_syscalls;
376
Mark Sleee8540632006-05-30 09:24:40 +0000377 // Fail on a send error
378 if (b < 0) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000379 if (errno == EPIPE) {
380 close();
Mark Sleef9831082007-02-20 20:59:21 +0000381 throw TTransportException(TTransportException::NOT_OPEN, "EPIPE");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000382 }
383
384 if (errno == ECONNRESET) {
385 close();
Mark Sleef9831082007-02-20 20:59:21 +0000386 throw TTransportException(TTransportException::NOT_OPEN, "ECONNRESET");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000387 }
388
389 if (errno == ENOTCONN) {
390 close();
Mark Sleef9831082007-02-20 20:59:21 +0000391 throw TTransportException(TTransportException::NOT_OPEN, "ENOTCONN");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000392 }
393
boz6ded7752007-06-05 22:41:18 +0000394 GlobalOutput("TSocket::write() send < 0");
Martin Kraemere6c4fa62007-07-09 19:08:25 +0000395 char b_error[1024];
396 strerror_r(errno, b_error, sizeof(b_error));
397 throw TTransportException(TTransportException::UNKNOWN, string("ERROR:") + b_error);
Mark Sleee8540632006-05-30 09:24:40 +0000398 }
399
400 // Fail on blocked send
401 if (b == 0) {
Mark Sleef9831082007-02-20 20:59:21 +0000402 throw TTransportException(TTransportException::NOT_OPEN, "Socket send returned 0.");
Mark Sleee8540632006-05-30 09:24:40 +0000403 }
Mark Sleee8540632006-05-30 09:24:40 +0000404 sent += b;
405 }
406}
407
Aditya Agarwalebc99e02007-01-15 23:14:58 +0000408void TSocket::setHost(string host) {
409 host_ = host;
410}
411
412void TSocket::setPort(int port) {
413 port_ = port;
414}
415
Mark Slee8d7e1f62006-06-07 06:48:56 +0000416void TSocket::setLinger(bool on, int linger) {
Mark Slee29050782006-09-29 00:12:30 +0000417 lingerOn_ = on;
418 lingerVal_ = linger;
Martin Kraemeree341cb2007-02-05 21:40:38 +0000419 if (socket_ < 0) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000420 return;
421 }
422
Mark Slee29050782006-09-29 00:12:30 +0000423 struct linger l = {(lingerOn_ ? 1 : 0), lingerVal_};
424 int ret = setsockopt(socket_, SOL_SOCKET, SO_LINGER, &l, sizeof(l));
425 if (ret == -1) {
boz6ded7752007-06-05 22:41:18 +0000426 GlobalOutput("TSocket::setLinger()");
Mark Sleee8540632006-05-30 09:24:40 +0000427 }
Mark Sleee8540632006-05-30 09:24:40 +0000428}
429
Mark Slee8d7e1f62006-06-07 06:48:56 +0000430void TSocket::setNoDelay(bool noDelay) {
Mark Slee29050782006-09-29 00:12:30 +0000431 noDelay_ = noDelay;
Martin Kraemeree341cb2007-02-05 21:40:38 +0000432 if (socket_ < 0) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000433 return;
434 }
435
Mark Sleee8540632006-05-30 09:24:40 +0000436 // Set socket to NODELAY
Mark Slee29050782006-09-29 00:12:30 +0000437 int v = noDelay_ ? 1 : 0;
438 int ret = setsockopt(socket_, IPPROTO_TCP, TCP_NODELAY, &v, sizeof(v));
439 if (ret == -1) {
boz6ded7752007-06-05 22:41:18 +0000440 GlobalOutput("TSocket::setNoDelay()");
Mark Sleee8540632006-05-30 09:24:40 +0000441 }
Mark Sleee8540632006-05-30 09:24:40 +0000442}
Mark Slee29050782006-09-29 00:12:30 +0000443
444void TSocket::setConnTimeout(int ms) {
445 connTimeout_ = ms;
446}
447
448void TSocket::setRecvTimeout(int ms) {
449 recvTimeout_ = ms;
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000450 recvTimeval_.tv_sec = (int)(recvTimeout_/1000);
451 recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000);
Martin Kraemeree341cb2007-02-05 21:40:38 +0000452 if (socket_ < 0) {
Mark Slee29050782006-09-29 00:12:30 +0000453 return;
454 }
455
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000456 // Copy because select may modify
457 struct timeval r = recvTimeval_;
Mark Slee29050782006-09-29 00:12:30 +0000458 int ret = setsockopt(socket_, SOL_SOCKET, SO_RCVTIMEO, &r, sizeof(r));
459 if (ret == -1) {
boz6ded7752007-06-05 22:41:18 +0000460 GlobalOutput("TSocket::setRecvTimeout()");
Mark Slee29050782006-09-29 00:12:30 +0000461 }
462}
463
464void TSocket::setSendTimeout(int ms) {
465 sendTimeout_ = ms;
Martin Kraemeree341cb2007-02-05 21:40:38 +0000466 if (socket_ < 0) {
Mark Slee29050782006-09-29 00:12:30 +0000467 return;
468 }
469
470 struct timeval s = {(int)(sendTimeout_/1000),
471 (int)((sendTimeout_%1000)*1000)};
472 int ret = setsockopt(socket_, SOL_SOCKET, SO_SNDTIMEO, &s, sizeof(s));
473 if (ret == -1) {
boz6ded7752007-06-05 22:41:18 +0000474 GlobalOutput("TSocket::setSendTimeout()");
Mark Slee29050782006-09-29 00:12:30 +0000475 }
476}
477
Aditya Agarwale04475b2007-05-23 02:14:58 +0000478void TSocket::setMaxRecvRetries(int maxRecvRetries) {
479 maxRecvRetries_ = maxRecvRetries;
480}
481
Marc Slemko6f038a72006-08-03 18:58:09 +0000482}}} // facebook::thrift::transport