blob: 90f07f8483013681c86a83b6c90796a80548bee5 [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>
Mark Sleedd564972007-08-21 02:39:57 +00009#include <sys/select.h>
10#include <sys/types.h>
Mark Sleee8540632006-05-30 09:24:40 +000011#include <arpa/inet.h>
12#include <netinet/in.h>
13#include <netinet/tcp.h>
14#include <netdb.h>
15#include <unistd.h>
16#include <errno.h>
Mark Slee29050782006-09-29 00:12:30 +000017#include <fcntl.h>
Mark Sleee8540632006-05-30 09:24:40 +000018
Mark Slee29050782006-09-29 00:12:30 +000019#include "concurrency/Monitor.h"
Marc Slemkod42a2c22006-08-10 03:30:18 +000020#include "TSocket.h"
21#include "TTransportException.h"
Mark Sleee8540632006-05-30 09:24:40 +000022
Marc Slemko6f038a72006-08-03 18:58:09 +000023namespace facebook { namespace thrift { namespace transport {
24
Mark Sleee8540632006-05-30 09:24:40 +000025using namespace std;
Mark Slee29050782006-09-29 00:12:30 +000026using namespace facebook::thrift::concurrency;
Mark Sleee8540632006-05-30 09:24:40 +000027
Mark Slee29050782006-09-29 00:12:30 +000028// Global var to track total socket sys calls
Mark Slee8d7e1f62006-06-07 06:48:56 +000029uint32_t g_socket_syscalls = 0;
30
31/**
32 * TSocket implementation.
33 *
34 * @author Mark Slee <mcslee@facebook.com>
35 */
36
Mark Sleee8540632006-05-30 09:24:40 +000037// Mutex to protect syscalls to netdb
Mark Slee29050782006-09-29 00:12:30 +000038static Monitor s_netdb_monitor;
Mark Sleee8540632006-05-30 09:24:40 +000039
Mark Slee29050782006-09-29 00:12:30 +000040TSocket::TSocket(string host, int port) :
41 host_(host),
42 port_(port),
Martin Kraemeree341cb2007-02-05 21:40:38 +000043 socket_(-1),
Mark Slee29050782006-09-29 00:12:30 +000044 connTimeout_(0),
45 sendTimeout_(0),
46 recvTimeout_(0),
47 lingerOn_(1),
48 lingerVal_(0),
Aditya Agarwale04475b2007-05-23 02:14:58 +000049 noDelay_(1),
50 maxRecvRetries_(5) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +000051 recvTimeval_.tv_sec = (int)(recvTimeout_/1000);
52 recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000);
Mark Sleee8540632006-05-30 09:24:40 +000053}
54
Aditya Agarwalebc99e02007-01-15 23:14:58 +000055TSocket::TSocket() :
56 host_(""),
57 port_(0),
Martin Kraemeree341cb2007-02-05 21:40:38 +000058 socket_(-1),
Aditya Agarwalebc99e02007-01-15 23:14:58 +000059 connTimeout_(0),
60 sendTimeout_(0),
61 recvTimeout_(0),
62 lingerOn_(1),
63 lingerVal_(0),
Aditya Agarwale04475b2007-05-23 02:14:58 +000064 noDelay_(1),
65 maxRecvRetries_(5) {
Aditya Agarwalebc99e02007-01-15 23:14:58 +000066 recvTimeval_.tv_sec = (int)(recvTimeout_/1000);
67 recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000);
68}
69
Mark Slee29050782006-09-29 00:12:30 +000070TSocket::TSocket(int socket) :
71 host_(""),
72 port_(0),
73 socket_(socket),
74 connTimeout_(0),
75 sendTimeout_(0),
76 recvTimeout_(0),
77 lingerOn_(1),
78 lingerVal_(0),
Aditya Agarwale04475b2007-05-23 02:14:58 +000079 noDelay_(1),
80 maxRecvRetries_(5) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +000081 recvTimeval_.tv_sec = (int)(recvTimeout_/1000);
82 recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000);
Mark Slee29050782006-09-29 00:12:30 +000083}
84
Mark Sleee8540632006-05-30 09:24:40 +000085TSocket::~TSocket() {
86 close();
87}
88
Mark Slee8d7e1f62006-06-07 06:48:56 +000089bool TSocket::isOpen() {
Martin Kraemeree341cb2007-02-05 21:40:38 +000090 return (socket_ >= 0);
Mark Slee8d7e1f62006-06-07 06:48:56 +000091}
92
Mark Sleeb9ff32a2006-11-16 01:00:24 +000093bool TSocket::peek() {
94 if (!isOpen()) {
95 return false;
96 }
97 uint8_t buf;
98 int r = recv(socket_, &buf, 1, MSG_PEEK);
99 if (r == -1) {
David Reissbc3dddb2007-08-22 23:20:24 +0000100 int errno_copy = errno;
boz6ded7752007-06-05 22:41:18 +0000101 GlobalOutput("TSocket::peek()");
David Reissbc3dddb2007-08-22 23:20:24 +0000102 throw TTransportException(TTransportException::UNKNOWN, "recv()", errno_copy);
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000103 }
104 return (r > 0);
105}
106
Mark Slee6d56eb92007-07-06 22:28:15 +0000107void TSocket::openConnection(struct addrinfo *res) {
Mark Sleea9848d72007-02-21 04:54:05 +0000108 if (isOpen()) {
109 throw TTransportException(TTransportException::ALREADY_OPEN);
110 }
Mark Slee6d56eb92007-07-06 22:28:15 +0000111
112 socket_ = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
Mark Sleee8540632006-05-30 09:24:40 +0000113 if (socket_ == -1) {
David Reissbc3dddb2007-08-22 23:20:24 +0000114 int errno_copy = errno;
boz6ded7752007-06-05 22:41:18 +0000115 GlobalOutput("TSocket::open() socket");
David Reissbc3dddb2007-08-22 23:20:24 +0000116 throw TTransportException(TTransportException::NOT_OPEN, "socket()", errno_copy);
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 Slee29050782006-09-29 00:12:30 +0000135 // Set the socket to be non blocking for connect if a timeout exists
136 int flags = fcntl(socket_, F_GETFL, 0);
137 if (connTimeout_ > 0) {
Mark Sleea5a783f2007-03-02 19:41:08 +0000138 if (-1 == fcntl(socket_, F_SETFL, flags | O_NONBLOCK)) {
139 throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed");
140 }
Mark Slee29050782006-09-29 00:12:30 +0000141 } else {
Mark Sleea5a783f2007-03-02 19:41:08 +0000142 if (-1 == fcntl(socket_, F_SETFL, flags & ~O_NONBLOCK)) {
143 throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed");
144 }
Mark Slee29050782006-09-29 00:12:30 +0000145 }
146
147 // Conn timeout
148 struct timeval c = {(int)(connTimeout_/1000),
149 (int)((connTimeout_%1000)*1000)};
Mark Sleee8540632006-05-30 09:24:40 +0000150
151 // Connect the socket
Mark Slee6d56eb92007-07-06 22:28:15 +0000152 int ret = connect(socket_, res->ai_addr, res->ai_addrlen);
Mark Sleee8540632006-05-30 09:24:40 +0000153
Mark Slee29050782006-09-29 00:12:30 +0000154 if (ret == 0) {
155 goto done;
156 }
157
158 if (errno != EINPROGRESS) {
David Reissbc3dddb2007-08-22 23:20:24 +0000159 int errno_copy = errno;
Mark Slee29050782006-09-29 00:12:30 +0000160 char buff[1024];
Martin Kraemere6c4fa62007-07-09 19:08:25 +0000161 sprintf(buff, "TSocket::open() connect %s %d", host_.c_str(), port_);
David Reissbc3dddb2007-08-22 23:20:24 +0000162 GlobalOutput(buff);
Martin Kraemere6c4fa62007-07-09 19:08:25 +0000163
David Reissbc3dddb2007-08-22 23:20:24 +0000164 throw TTransportException(TTransportException::NOT_OPEN, "connect()", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000165 }
166
Mark Slee29050782006-09-29 00:12:30 +0000167 fd_set fds;
168 FD_ZERO(&fds);
169 FD_SET(socket_, &fds);
170 ret = select(socket_+1, NULL, &fds, NULL, &c);
171
172 if (ret > 0) {
173 // Ensure connected
174 int val;
175 socklen_t lon;
176 lon = sizeof(int);
177 int ret2 = getsockopt(socket_, SOL_SOCKET, SO_ERROR, (void *)&val, &lon);
178 if (ret2 == -1) {
David Reissbc3dddb2007-08-22 23:20:24 +0000179 int errno_copy = errno;
boz6ded7752007-06-05 22:41:18 +0000180 GlobalOutput("TSocket::open() getsockopt SO_ERROR");
David Reissbc3dddb2007-08-22 23:20:24 +0000181 throw TTransportException(TTransportException::NOT_OPEN, "open()", errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000182 }
183 if (val == 0) {
184 goto done;
185 }
David Reissbc3dddb2007-08-22 23:20:24 +0000186 int errno_copy = errno;
boz6ded7752007-06-05 22:41:18 +0000187 GlobalOutput("TSocket::open() SO_ERROR was set");
David Reissbc3dddb2007-08-22 23:20:24 +0000188 throw TTransportException(TTransportException::NOT_OPEN, "open()", errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000189 } else if (ret == 0) {
David Reissbc3dddb2007-08-22 23:20:24 +0000190 int errno_copy = errno;
191 GlobalOutput("TSocket::open() timed out");
192 throw TTransportException(TTransportException::NOT_OPEN, "open()", errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000193 } else {
David Reissbc3dddb2007-08-22 23:20:24 +0000194 int errno_copy = errno;
boz6ded7752007-06-05 22:41:18 +0000195 GlobalOutput("TSocket::open() select error");
David Reissbc3dddb2007-08-22 23:20:24 +0000196 throw TTransportException(TTransportException::NOT_OPEN, "open()", errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000197 }
198
199 done:
200 // Set socket back to normal mode (blocking)
201 fcntl(socket_, F_SETFL, flags);
Mark Sleee8540632006-05-30 09:24:40 +0000202}
203
Mark Slee6d56eb92007-07-06 22:28:15 +0000204void TSocket::open() {
205 if (isOpen()) {
206 throw TTransportException(TTransportException::ALREADY_OPEN);
207 }
208
209 // Validate port number
210 if (port_ < 0 || port_ > 65536) {
211 throw TTransportException(TTransportException::NOT_OPEN, "Specified port is invalid");
212 }
213
214 struct addrinfo hints, *res, *res0;
215 int error;
216 char port[sizeof("65536") + 1];
217 memset(&hints, 0, sizeof(hints));
218 hints.ai_family = PF_UNSPEC;
219 hints.ai_socktype = SOCK_STREAM;
220 hints.ai_flags = AI_PASSIVE;
221 sprintf(port, "%d", port_);
222
223 {
224 // Scope lock on host entry lookup
225 Synchronized s(s_netdb_monitor);
226 error = getaddrinfo(host_.c_str(), port, &hints, &res0);
227 }
228 if (error) {
229 fprintf(stderr, "getaddrinfo %d: %s\n", error, gai_strerror(error));
230 close();
231 throw TTransportException(TTransportException::NOT_OPEN, "Could not resolve host for client socket.");
232 }
233
234 // Cycle through all the returned addresses until one
235 // connects or push the exception up.
236 for (res = res0; res; res = res->ai_next) {
237 try {
238 openConnection(res);
239 break;
240 } catch (TTransportException& ttx) {
241 if (res->ai_next) {
242 close();
243 } else {
244 close();
Mark Slee85287d32007-07-09 19:50:30 +0000245 freeaddrinfo(res0); // cleanup on failure
Mark Slee6d56eb92007-07-06 22:28:15 +0000246 throw;
247 }
248 }
249 }
Mark Slee85287d32007-07-09 19:50:30 +0000250
251 // Free address structure memory
252 freeaddrinfo(res0);
Mark Slee6d56eb92007-07-06 22:28:15 +0000253}
254
Mark Sleee8540632006-05-30 09:24:40 +0000255void TSocket::close() {
Martin Kraemeree341cb2007-02-05 21:40:38 +0000256 if (socket_ >= 0) {
Mark Sleee8540632006-05-30 09:24:40 +0000257 shutdown(socket_, SHUT_RDWR);
258 ::close(socket_);
259 }
Martin Kraemeree341cb2007-02-05 21:40:38 +0000260 socket_ = -1;
Mark Sleee8540632006-05-30 09:24:40 +0000261}
262
Mark Slee8d7e1f62006-06-07 06:48:56 +0000263uint32_t TSocket::read(uint8_t* buf, uint32_t len) {
Martin Kraemeree341cb2007-02-05 21:40:38 +0000264 if (socket_ < 0) {
Mark Sleef9831082007-02-20 20:59:21 +0000265 throw TTransportException(TTransportException::NOT_OPEN, "Called read on non-open socket");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000266 }
Mark Sleee8540632006-05-30 09:24:40 +0000267
Aditya Agarwale04475b2007-05-23 02:14:58 +0000268 int32_t retries = 0;
269
270 // EAGAIN can be signalled both when a timeout has occurred and when
271 // the system is out of resources (an awesome undocumented feature).
272 // The following is an approximation of the time interval under which
273 // EAGAIN is taken to indicate an out of resources error.
274 uint32_t eagainThresholdMicros = 0;
275 if (recvTimeout_) {
276 // if a readTimeout is specified along with a max number of recv retries, then
277 // the threshold will ensure that the read timeout is not exceeded even in the
278 // case of resource errors
279 eagainThresholdMicros = (recvTimeout_*1000)/ ((maxRecvRetries_>0) ? maxRecvRetries_ : 2);
280 }
281
282 try_again:
Mark Slee8d7e1f62006-06-07 06:48:56 +0000283 // Read from the socket
Aditya Agarwale04475b2007-05-23 02:14:58 +0000284 struct timeval begin;
285 gettimeofday(&begin, NULL);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000286 int got = recv(socket_, buf, len, 0);
Aditya Agarwale04475b2007-05-23 02:14:58 +0000287 struct timeval end;
288 gettimeofday(&end, NULL);
289 uint32_t readElapsedMicros = (((end.tv_sec - begin.tv_sec) * 1000 * 1000)
290 + (((uint64_t)(end.tv_usec - begin.tv_usec))));
Mark Slee8d7e1f62006-06-07 06:48:56 +0000291 ++g_socket_syscalls;
Aditya Agarwale04475b2007-05-23 02:14:58 +0000292
Mark Slee8d7e1f62006-06-07 06:48:56 +0000293 // Check for error on read
Mark Sleec4257802007-01-24 23:14:30 +0000294 if (got < 0) {
Aditya Agarwale04475b2007-05-23 02:14:58 +0000295 if (errno == EAGAIN) {
296 // check if this is the lack of resources or timeout case
297 if (!eagainThresholdMicros || (readElapsedMicros < eagainThresholdMicros)) {
298 if (retries++ < maxRecvRetries_) {
299 usleep(50);
300 goto try_again;
301 } else {
302 throw TTransportException(TTransportException::TIMED_OUT,
303 "EAGAIN (unavailable resources)");
304 }
305 } else {
306 // infer that timeout has been hit
307 throw TTransportException(TTransportException::TIMED_OUT,
308 "EAGAIN (timed out)");
309 }
Mark Sleee8540632006-05-30 09:24:40 +0000310 }
311
Mark Slee8d7e1f62006-06-07 06:48:56 +0000312 // If interrupted, try again
Aditya Agarwale04475b2007-05-23 02:14:58 +0000313 if (errno == EINTR && retries++ < maxRecvRetries_) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000314 goto try_again;
Mark Sleee8540632006-05-30 09:24:40 +0000315 }
316
Mark Sleec4257802007-01-24 23:14:30 +0000317 // Now it's not a try again case, but a real probblez
boz6ded7752007-06-05 22:41:18 +0000318 GlobalOutput("TSocket::read()");
Mark Sleec4257802007-01-24 23:14:30 +0000319
Mark Slee8d7e1f62006-06-07 06:48:56 +0000320 // If we disconnect with no linger time
321 if (errno == ECONNRESET) {
Mark Sleef9831082007-02-20 20:59:21 +0000322 throw TTransportException(TTransportException::NOT_OPEN, "ECONNRESET");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000323 }
324
325 // This ish isn't open
326 if (errno == ENOTCONN) {
Mark Sleef9831082007-02-20 20:59:21 +0000327 throw TTransportException(TTransportException::NOT_OPEN, "ENOTCONN");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000328 }
329
330 // Timed out!
331 if (errno == ETIMEDOUT) {
Mark Sleef9831082007-02-20 20:59:21 +0000332 throw TTransportException(TTransportException::TIMED_OUT, "ETIMEDOUT");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000333 }
334
335 // Some other error, whatevz
Mark Slee5f68c712007-05-11 17:58:54 +0000336 char buff[1024];
337 sprintf(buff, "ERROR errno: %d", errno);
338 throw TTransportException(TTransportException::UNKNOWN, buff);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000339 }
340
341 // The remote host has closed the socket
342 if (got == 0) {
343 close();
344 return 0;
Mark Sleee8540632006-05-30 09:24:40 +0000345 }
346
347 // Pack data into string
Mark Slee8d7e1f62006-06-07 06:48:56 +0000348 return got;
Mark Sleee8540632006-05-30 09:24:40 +0000349}
350
Mark Slee8d7e1f62006-06-07 06:48:56 +0000351void TSocket::write(const uint8_t* buf, uint32_t len) {
Martin Kraemeree341cb2007-02-05 21:40:38 +0000352 if (socket_ < 0) {
Mark Sleef9831082007-02-20 20:59:21 +0000353 throw TTransportException(TTransportException::NOT_OPEN, "Called write on non-open socket");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000354 }
355
Mark Sleee8540632006-05-30 09:24:40 +0000356 uint32_t sent = 0;
357
Mark Slee8d7e1f62006-06-07 06:48:56 +0000358 while (sent < len) {
Marc Slemko9d4a3e22006-07-21 19:53:48 +0000359
360 int flags = 0;
Mark Slee29050782006-09-29 00:12:30 +0000361 #ifdef MSG_NOSIGNAL
Mark Slee8d7e1f62006-06-07 06:48:56 +0000362 // Note the use of MSG_NOSIGNAL to suppress SIGPIPE errors, instead we
363 // check for the EPIPE return condition and close the socket in that case
Marc Slemko9d4a3e22006-07-21 19:53:48 +0000364 flags |= MSG_NOSIGNAL;
Mark Slee29050782006-09-29 00:12:30 +0000365 #endif // ifdef MSG_NOSIGNAL
Marc Slemko9d4a3e22006-07-21 19:53:48 +0000366
367 int b = send(socket_, buf + sent, len - sent, flags);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000368 ++g_socket_syscalls;
369
Mark Sleee8540632006-05-30 09:24:40 +0000370 // Fail on a send error
371 if (b < 0) {
David Reissbc3dddb2007-08-22 23:20:24 +0000372 if (errno == EPIPE || errno == ECONNRESET || errno == ENOTCONN) {
373 int errno_copy = errno;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000374 close();
David Reissbc3dddb2007-08-22 23:20:24 +0000375 throw TTransportException(TTransportException::NOT_OPEN, "send()", errno_copy);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000376 }
377
David Reissbc3dddb2007-08-22 23:20:24 +0000378 int errno_copy = errno;
boz6ded7752007-06-05 22:41:18 +0000379 GlobalOutput("TSocket::write() send < 0");
David Reissbc3dddb2007-08-22 23:20:24 +0000380 throw TTransportException(TTransportException::UNKNOWN, "send", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000381 }
382
383 // Fail on blocked send
384 if (b == 0) {
Mark Sleef9831082007-02-20 20:59:21 +0000385 throw TTransportException(TTransportException::NOT_OPEN, "Socket send returned 0.");
Mark Sleee8540632006-05-30 09:24:40 +0000386 }
Mark Sleee8540632006-05-30 09:24:40 +0000387 sent += b;
388 }
389}
390
Aditya Agarwalebc99e02007-01-15 23:14:58 +0000391void TSocket::setHost(string host) {
392 host_ = host;
393}
394
395void TSocket::setPort(int port) {
396 port_ = port;
397}
398
Mark Slee8d7e1f62006-06-07 06:48:56 +0000399void TSocket::setLinger(bool on, int linger) {
Mark Slee29050782006-09-29 00:12:30 +0000400 lingerOn_ = on;
401 lingerVal_ = linger;
Martin Kraemeree341cb2007-02-05 21:40:38 +0000402 if (socket_ < 0) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000403 return;
404 }
405
Mark Slee29050782006-09-29 00:12:30 +0000406 struct linger l = {(lingerOn_ ? 1 : 0), lingerVal_};
407 int ret = setsockopt(socket_, SOL_SOCKET, SO_LINGER, &l, sizeof(l));
408 if (ret == -1) {
boz6ded7752007-06-05 22:41:18 +0000409 GlobalOutput("TSocket::setLinger()");
Mark Sleee8540632006-05-30 09:24:40 +0000410 }
Mark Sleee8540632006-05-30 09:24:40 +0000411}
412
Mark Slee8d7e1f62006-06-07 06:48:56 +0000413void TSocket::setNoDelay(bool noDelay) {
Mark Slee29050782006-09-29 00:12:30 +0000414 noDelay_ = noDelay;
Martin Kraemeree341cb2007-02-05 21:40:38 +0000415 if (socket_ < 0) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000416 return;
417 }
418
Mark Sleee8540632006-05-30 09:24:40 +0000419 // Set socket to NODELAY
Mark Slee29050782006-09-29 00:12:30 +0000420 int v = noDelay_ ? 1 : 0;
421 int ret = setsockopt(socket_, IPPROTO_TCP, TCP_NODELAY, &v, sizeof(v));
422 if (ret == -1) {
boz6ded7752007-06-05 22:41:18 +0000423 GlobalOutput("TSocket::setNoDelay()");
Mark Sleee8540632006-05-30 09:24:40 +0000424 }
Mark Sleee8540632006-05-30 09:24:40 +0000425}
Mark Slee29050782006-09-29 00:12:30 +0000426
427void TSocket::setConnTimeout(int ms) {
428 connTimeout_ = ms;
429}
430
431void TSocket::setRecvTimeout(int ms) {
432 recvTimeout_ = ms;
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000433 recvTimeval_.tv_sec = (int)(recvTimeout_/1000);
434 recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000);
Martin Kraemeree341cb2007-02-05 21:40:38 +0000435 if (socket_ < 0) {
Mark Slee29050782006-09-29 00:12:30 +0000436 return;
437 }
438
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000439 // Copy because select may modify
440 struct timeval r = recvTimeval_;
Mark Slee29050782006-09-29 00:12:30 +0000441 int ret = setsockopt(socket_, SOL_SOCKET, SO_RCVTIMEO, &r, sizeof(r));
442 if (ret == -1) {
boz6ded7752007-06-05 22:41:18 +0000443 GlobalOutput("TSocket::setRecvTimeout()");
Mark Slee29050782006-09-29 00:12:30 +0000444 }
445}
446
447void TSocket::setSendTimeout(int ms) {
448 sendTimeout_ = ms;
Martin Kraemeree341cb2007-02-05 21:40:38 +0000449 if (socket_ < 0) {
Mark Slee29050782006-09-29 00:12:30 +0000450 return;
451 }
452
453 struct timeval s = {(int)(sendTimeout_/1000),
454 (int)((sendTimeout_%1000)*1000)};
455 int ret = setsockopt(socket_, SOL_SOCKET, SO_SNDTIMEO, &s, sizeof(s));
456 if (ret == -1) {
boz6ded7752007-06-05 22:41:18 +0000457 GlobalOutput("TSocket::setSendTimeout()");
Mark Slee29050782006-09-29 00:12:30 +0000458 }
459}
460
Aditya Agarwale04475b2007-05-23 02:14:58 +0000461void TSocket::setMaxRecvRetries(int maxRecvRetries) {
462 maxRecvRetries_ = maxRecvRetries;
463}
464
Marc Slemko6f038a72006-08-03 18:58:09 +0000465}}} // facebook::thrift::transport