blob: 42364fa7b7838b9e01be274a0d53b0baa17326d7 [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();
Mark Sleef9831082007-02-20 20:59:21 +0000101 throw TTransportException(TTransportException::UNKNOWN, "recv() ERROR:" + errno);
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000102 }
103 return (r > 0);
104}
105
Mark Slee8d7e1f62006-06-07 06:48:56 +0000106void TSocket::open() {
Mark Sleea9848d72007-02-21 04:54:05 +0000107 if (isOpen()) {
108 throw TTransportException(TTransportException::ALREADY_OPEN);
109 }
110
Mark Sleee8540632006-05-30 09:24:40 +0000111 // Create socket
112 socket_ = socket(AF_INET, SOCK_STREAM, 0);
113 if (socket_ == -1) {
boz6ded7752007-06-05 22:41:18 +0000114 GlobalOutput("TSocket::open() socket");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000115 close();
Mark Sleef9831082007-02-20 20:59:21 +0000116 throw TTransportException(TTransportException::NOT_OPEN, "socket() ERROR:" + errno);
Mark Sleee8540632006-05-30 09:24:40 +0000117 }
Mark Slee29050782006-09-29 00:12:30 +0000118
119 // Send timeout
120 if (sendTimeout_ > 0) {
121 setSendTimeout(sendTimeout_);
122 }
123
124 // Recv timeout
125 if (recvTimeout_ > 0) {
126 setRecvTimeout(recvTimeout_);
127 }
128
129 // Linger
130 setLinger(lingerOn_, lingerVal_);
131
132 // No delay
133 setNoDelay(noDelay_);
134
Mark Slee8d7e1f62006-06-07 06:48:56 +0000135 // Lookup the hostname
Mark Sleee8540632006-05-30 09:24:40 +0000136 struct sockaddr_in addr;
137 addr.sin_family = AF_INET;
138 addr.sin_port = htons(port_);
139
Mark Sleee8540632006-05-30 09:24:40 +0000140 {
Mark Slee29050782006-09-29 00:12:30 +0000141 // Scope lock on host entry lookup
142 Synchronized s(s_netdb_monitor);
Mark Sleee8540632006-05-30 09:24:40 +0000143 struct hostent *host_entry = gethostbyname(host_.c_str());
144
145 if (host_entry == NULL) {
boz6ded7752007-06-05 22:41:18 +0000146 GlobalOutput("TSocket: dns error: failed call to gethostbyname.");
Mark Sleee8540632006-05-30 09:24:40 +0000147 close();
Mark Sleef9831082007-02-20 20:59:21 +0000148 throw TTransportException(TTransportException::NOT_OPEN, "gethostbyname() failed");
Mark Sleee8540632006-05-30 09:24:40 +0000149 }
150
151 addr.sin_port = htons(port_);
152 memcpy(&addr.sin_addr.s_addr,
153 host_entry->h_addr_list[0],
154 host_entry->h_length);
155 }
Mark Slee29050782006-09-29 00:12:30 +0000156
157 // Set the socket to be non blocking for connect if a timeout exists
158 int flags = fcntl(socket_, F_GETFL, 0);
159 if (connTimeout_ > 0) {
Mark Sleea5a783f2007-03-02 19:41:08 +0000160 if (-1 == fcntl(socket_, F_SETFL, flags | O_NONBLOCK)) {
161 throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed");
162 }
Mark Slee29050782006-09-29 00:12:30 +0000163 } else {
Mark Sleea5a783f2007-03-02 19:41:08 +0000164 if (-1 == fcntl(socket_, F_SETFL, flags & ~O_NONBLOCK)) {
165 throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed");
166 }
Mark Slee29050782006-09-29 00:12:30 +0000167 }
168
169 // Conn timeout
170 struct timeval c = {(int)(connTimeout_/1000),
171 (int)((connTimeout_%1000)*1000)};
Mark Sleee8540632006-05-30 09:24:40 +0000172
173 // Connect the socket
174 int ret = connect(socket_, (struct sockaddr *)&addr, sizeof(addr));
175
Mark Slee29050782006-09-29 00:12:30 +0000176 if (ret == 0) {
177 goto done;
178 }
179
180 if (errno != EINPROGRESS) {
Mark Sleee8540632006-05-30 09:24:40 +0000181 close();
Mark Slee29050782006-09-29 00:12:30 +0000182 char buff[1024];
183 sprintf(buff, "TSocket::open() connect %s %d", host_.c_str(), port_);
boz6ded7752007-06-05 22:41:18 +0000184 GlobalOutput(buff);
Mark Sleef9831082007-02-20 20:59:21 +0000185 throw TTransportException(TTransportException::NOT_OPEN, "open() ERROR: " + errno);
Mark Sleee8540632006-05-30 09:24:40 +0000186 }
187
Mark Slee29050782006-09-29 00:12:30 +0000188 fd_set fds;
189 FD_ZERO(&fds);
190 FD_SET(socket_, &fds);
191 ret = select(socket_+1, NULL, &fds, NULL, &c);
192
193 if (ret > 0) {
194 // Ensure connected
195 int val;
196 socklen_t lon;
197 lon = sizeof(int);
198 int ret2 = getsockopt(socket_, SOL_SOCKET, SO_ERROR, (void *)&val, &lon);
199 if (ret2 == -1) {
200 close();
boz6ded7752007-06-05 22:41:18 +0000201 GlobalOutput("TSocket::open() getsockopt SO_ERROR");
Mark Sleef9831082007-02-20 20:59:21 +0000202 throw TTransportException(TTransportException::NOT_OPEN, "open() ERROR: " + errno);
Mark Slee29050782006-09-29 00:12:30 +0000203 }
204 if (val == 0) {
205 goto done;
206 }
207 close();
boz6ded7752007-06-05 22:41:18 +0000208 GlobalOutput("TSocket::open() SO_ERROR was set");
Mark Sleef9831082007-02-20 20:59:21 +0000209 throw TTransportException(TTransportException::NOT_OPEN, "open() ERROR: " + errno);
Mark Slee29050782006-09-29 00:12:30 +0000210 } else if (ret == 0) {
211 close();
boz6ded7752007-06-05 22:41:18 +0000212 GlobalOutput("TSocket::open() timeed out");
Mark Sleef9831082007-02-20 20:59:21 +0000213 throw TTransportException(TTransportException::NOT_OPEN, "open() ERROR: " + errno);
Mark Slee29050782006-09-29 00:12:30 +0000214 } else {
215 close();
boz6ded7752007-06-05 22:41:18 +0000216 GlobalOutput("TSocket::open() select error");
Mark Sleef9831082007-02-20 20:59:21 +0000217 throw TTransportException(TTransportException::NOT_OPEN, "open() ERROR: " + errno);
Mark Slee29050782006-09-29 00:12:30 +0000218 }
219
220 done:
221 // Set socket back to normal mode (blocking)
222 fcntl(socket_, F_SETFL, flags);
Mark Sleee8540632006-05-30 09:24:40 +0000223}
224
225void TSocket::close() {
Martin Kraemeree341cb2007-02-05 21:40:38 +0000226 if (socket_ >= 0) {
Mark Sleee8540632006-05-30 09:24:40 +0000227 shutdown(socket_, SHUT_RDWR);
228 ::close(socket_);
229 }
Martin Kraemeree341cb2007-02-05 21:40:38 +0000230 socket_ = -1;
Mark Sleee8540632006-05-30 09:24:40 +0000231}
232
Mark Slee8d7e1f62006-06-07 06:48:56 +0000233uint32_t TSocket::read(uint8_t* buf, uint32_t len) {
Martin Kraemeree341cb2007-02-05 21:40:38 +0000234 if (socket_ < 0) {
Mark Sleef9831082007-02-20 20:59:21 +0000235 throw TTransportException(TTransportException::NOT_OPEN, "Called read on non-open socket");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000236 }
Mark Sleee8540632006-05-30 09:24:40 +0000237
Aditya Agarwale04475b2007-05-23 02:14:58 +0000238 int32_t retries = 0;
239
240 // EAGAIN can be signalled both when a timeout has occurred and when
241 // the system is out of resources (an awesome undocumented feature).
242 // The following is an approximation of the time interval under which
243 // EAGAIN is taken to indicate an out of resources error.
244 uint32_t eagainThresholdMicros = 0;
245 if (recvTimeout_) {
246 // if a readTimeout is specified along with a max number of recv retries, then
247 // the threshold will ensure that the read timeout is not exceeded even in the
248 // case of resource errors
249 eagainThresholdMicros = (recvTimeout_*1000)/ ((maxRecvRetries_>0) ? maxRecvRetries_ : 2);
250 }
251
252 try_again:
Mark Slee8d7e1f62006-06-07 06:48:56 +0000253 // Read from the socket
Aditya Agarwale04475b2007-05-23 02:14:58 +0000254 struct timeval begin;
255 gettimeofday(&begin, NULL);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000256 int got = recv(socket_, buf, len, 0);
Aditya Agarwale04475b2007-05-23 02:14:58 +0000257 struct timeval end;
258 gettimeofday(&end, NULL);
259 uint32_t readElapsedMicros = (((end.tv_sec - begin.tv_sec) * 1000 * 1000)
260 + (((uint64_t)(end.tv_usec - begin.tv_usec))));
Mark Slee8d7e1f62006-06-07 06:48:56 +0000261 ++g_socket_syscalls;
Aditya Agarwale04475b2007-05-23 02:14:58 +0000262
Mark Slee8d7e1f62006-06-07 06:48:56 +0000263 // Check for error on read
Mark Sleec4257802007-01-24 23:14:30 +0000264 if (got < 0) {
Aditya Agarwale04475b2007-05-23 02:14:58 +0000265 if (errno == EAGAIN) {
266 // check if this is the lack of resources or timeout case
267 if (!eagainThresholdMicros || (readElapsedMicros < eagainThresholdMicros)) {
268 if (retries++ < maxRecvRetries_) {
269 usleep(50);
270 goto try_again;
271 } else {
272 throw TTransportException(TTransportException::TIMED_OUT,
273 "EAGAIN (unavailable resources)");
274 }
275 } else {
276 // infer that timeout has been hit
277 throw TTransportException(TTransportException::TIMED_OUT,
278 "EAGAIN (timed out)");
279 }
Mark Sleee8540632006-05-30 09:24:40 +0000280 }
281
Mark Slee8d7e1f62006-06-07 06:48:56 +0000282 // If interrupted, try again
Aditya Agarwale04475b2007-05-23 02:14:58 +0000283 if (errno == EINTR && retries++ < maxRecvRetries_) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000284 goto try_again;
Mark Sleee8540632006-05-30 09:24:40 +0000285 }
286
Mark Sleec4257802007-01-24 23:14:30 +0000287 // Now it's not a try again case, but a real probblez
boz6ded7752007-06-05 22:41:18 +0000288 GlobalOutput("TSocket::read()");
Mark Sleec4257802007-01-24 23:14:30 +0000289
Mark Slee8d7e1f62006-06-07 06:48:56 +0000290 // If we disconnect with no linger time
291 if (errno == ECONNRESET) {
Mark Sleef9831082007-02-20 20:59:21 +0000292 throw TTransportException(TTransportException::NOT_OPEN, "ECONNRESET");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000293 }
294
295 // This ish isn't open
296 if (errno == ENOTCONN) {
Mark Sleef9831082007-02-20 20:59:21 +0000297 throw TTransportException(TTransportException::NOT_OPEN, "ENOTCONN");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000298 }
299
300 // Timed out!
301 if (errno == ETIMEDOUT) {
Mark Sleef9831082007-02-20 20:59:21 +0000302 throw TTransportException(TTransportException::TIMED_OUT, "ETIMEDOUT");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000303 }
304
305 // Some other error, whatevz
Mark Slee5f68c712007-05-11 17:58:54 +0000306 char buff[1024];
307 sprintf(buff, "ERROR errno: %d", errno);
308 throw TTransportException(TTransportException::UNKNOWN, buff);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000309 }
310
311 // The remote host has closed the socket
312 if (got == 0) {
313 close();
314 return 0;
Mark Sleee8540632006-05-30 09:24:40 +0000315 }
316
317 // Pack data into string
Mark Slee8d7e1f62006-06-07 06:48:56 +0000318 return got;
Mark Sleee8540632006-05-30 09:24:40 +0000319}
320
Mark Slee8d7e1f62006-06-07 06:48:56 +0000321void TSocket::write(const uint8_t* buf, uint32_t len) {
Martin Kraemeree341cb2007-02-05 21:40:38 +0000322 if (socket_ < 0) {
Mark Sleef9831082007-02-20 20:59:21 +0000323 throw TTransportException(TTransportException::NOT_OPEN, "Called write on non-open socket");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000324 }
325
Mark Sleee8540632006-05-30 09:24:40 +0000326 uint32_t sent = 0;
327
Mark Slee8d7e1f62006-06-07 06:48:56 +0000328 while (sent < len) {
Marc Slemko9d4a3e22006-07-21 19:53:48 +0000329
330 int flags = 0;
Mark Slee29050782006-09-29 00:12:30 +0000331 #ifdef MSG_NOSIGNAL
Mark Slee8d7e1f62006-06-07 06:48:56 +0000332 // Note the use of MSG_NOSIGNAL to suppress SIGPIPE errors, instead we
333 // check for the EPIPE return condition and close the socket in that case
Marc Slemko9d4a3e22006-07-21 19:53:48 +0000334 flags |= MSG_NOSIGNAL;
Mark Slee29050782006-09-29 00:12:30 +0000335 #endif // ifdef MSG_NOSIGNAL
Marc Slemko9d4a3e22006-07-21 19:53:48 +0000336
337 int b = send(socket_, buf + sent, len - sent, flags);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000338 ++g_socket_syscalls;
339
Mark Sleee8540632006-05-30 09:24:40 +0000340 // Fail on a send error
341 if (b < 0) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000342 if (errno == EPIPE) {
343 close();
Mark Sleef9831082007-02-20 20:59:21 +0000344 throw TTransportException(TTransportException::NOT_OPEN, "EPIPE");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000345 }
346
347 if (errno == ECONNRESET) {
348 close();
Mark Sleef9831082007-02-20 20:59:21 +0000349 throw TTransportException(TTransportException::NOT_OPEN, "ECONNRESET");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000350 }
351
352 if (errno == ENOTCONN) {
353 close();
Mark Sleef9831082007-02-20 20:59:21 +0000354 throw TTransportException(TTransportException::NOT_OPEN, "ENOTCONN");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000355 }
356
boz6ded7752007-06-05 22:41:18 +0000357 GlobalOutput("TSocket::write() send < 0");
Mark Sleef9831082007-02-20 20:59:21 +0000358 throw TTransportException(TTransportException::UNKNOWN, "ERROR:" + errno);
Mark Sleee8540632006-05-30 09:24:40 +0000359 }
360
361 // Fail on blocked send
362 if (b == 0) {
Mark Sleef9831082007-02-20 20:59:21 +0000363 throw TTransportException(TTransportException::NOT_OPEN, "Socket send returned 0.");
Mark Sleee8540632006-05-30 09:24:40 +0000364 }
Mark Sleee8540632006-05-30 09:24:40 +0000365 sent += b;
366 }
367}
368
Aditya Agarwalebc99e02007-01-15 23:14:58 +0000369void TSocket::setHost(string host) {
370 host_ = host;
371}
372
373void TSocket::setPort(int port) {
374 port_ = port;
375}
376
Mark Slee8d7e1f62006-06-07 06:48:56 +0000377void TSocket::setLinger(bool on, int linger) {
Mark Slee29050782006-09-29 00:12:30 +0000378 lingerOn_ = on;
379 lingerVal_ = linger;
Martin Kraemeree341cb2007-02-05 21:40:38 +0000380 if (socket_ < 0) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000381 return;
382 }
383
Mark Slee29050782006-09-29 00:12:30 +0000384 struct linger l = {(lingerOn_ ? 1 : 0), lingerVal_};
385 int ret = setsockopt(socket_, SOL_SOCKET, SO_LINGER, &l, sizeof(l));
386 if (ret == -1) {
boz6ded7752007-06-05 22:41:18 +0000387 GlobalOutput("TSocket::setLinger()");
Mark Sleee8540632006-05-30 09:24:40 +0000388 }
Mark Sleee8540632006-05-30 09:24:40 +0000389}
390
Mark Slee8d7e1f62006-06-07 06:48:56 +0000391void TSocket::setNoDelay(bool noDelay) {
Mark Slee29050782006-09-29 00:12:30 +0000392 noDelay_ = noDelay;
Martin Kraemeree341cb2007-02-05 21:40:38 +0000393 if (socket_ < 0) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000394 return;
395 }
396
Mark Sleee8540632006-05-30 09:24:40 +0000397 // Set socket to NODELAY
Mark Slee29050782006-09-29 00:12:30 +0000398 int v = noDelay_ ? 1 : 0;
399 int ret = setsockopt(socket_, IPPROTO_TCP, TCP_NODELAY, &v, sizeof(v));
400 if (ret == -1) {
boz6ded7752007-06-05 22:41:18 +0000401 GlobalOutput("TSocket::setNoDelay()");
Mark Sleee8540632006-05-30 09:24:40 +0000402 }
Mark Sleee8540632006-05-30 09:24:40 +0000403}
Mark Slee29050782006-09-29 00:12:30 +0000404
405void TSocket::setConnTimeout(int ms) {
406 connTimeout_ = ms;
407}
408
409void TSocket::setRecvTimeout(int ms) {
410 recvTimeout_ = ms;
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000411 recvTimeval_.tv_sec = (int)(recvTimeout_/1000);
412 recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000);
Martin Kraemeree341cb2007-02-05 21:40:38 +0000413 if (socket_ < 0) {
Mark Slee29050782006-09-29 00:12:30 +0000414 return;
415 }
416
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000417 // Copy because select may modify
418 struct timeval r = recvTimeval_;
Mark Slee29050782006-09-29 00:12:30 +0000419 int ret = setsockopt(socket_, SOL_SOCKET, SO_RCVTIMEO, &r, sizeof(r));
420 if (ret == -1) {
boz6ded7752007-06-05 22:41:18 +0000421 GlobalOutput("TSocket::setRecvTimeout()");
Mark Slee29050782006-09-29 00:12:30 +0000422 }
423}
424
425void TSocket::setSendTimeout(int ms) {
426 sendTimeout_ = ms;
Martin Kraemeree341cb2007-02-05 21:40:38 +0000427 if (socket_ < 0) {
Mark Slee29050782006-09-29 00:12:30 +0000428 return;
429 }
430
431 struct timeval s = {(int)(sendTimeout_/1000),
432 (int)((sendTimeout_%1000)*1000)};
433 int ret = setsockopt(socket_, SOL_SOCKET, SO_SNDTIMEO, &s, sizeof(s));
434 if (ret == -1) {
boz6ded7752007-06-05 22:41:18 +0000435 GlobalOutput("TSocket::setSendTimeout()");
Mark Slee29050782006-09-29 00:12:30 +0000436 }
437}
438
Aditya Agarwale04475b2007-05-23 02:14:58 +0000439void TSocket::setMaxRecvRetries(int maxRecvRetries) {
440 maxRecvRetries_ = maxRecvRetries;
441}
442
Marc Slemko6f038a72006-08-03 18:58:09 +0000443}}} // facebook::thrift::transport