blob: 540bb6e94fe67edd67afb12c3190b116f0178a7e [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
Aditya Agarwal4529c4b2007-09-05 01:01:15 +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;
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000101 string errStr = "TSocket::peek() " + getSocketInfo();
102 GlobalOutput(errStr.c_str());
David Reissbc3dddb2007-08-22 23:20:24 +0000103 throw TTransportException(TTransportException::UNKNOWN, "recv()", errno_copy);
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 }
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000112
Mark Slee6d56eb92007-07-06 22:28:15 +0000113 socket_ = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
Mark Sleee8540632006-05-30 09:24:40 +0000114 if (socket_ == -1) {
David Reissbc3dddb2007-08-22 23:20:24 +0000115 int errno_copy = errno;
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000116 string errStr = "TSocket::open() socket " + getSocketInfo();
117 GlobalOutput(errStr.c_str());
David Reissbc3dddb2007-08-22 23:20:24 +0000118 throw TTransportException(TTransportException::NOT_OPEN, "socket()", errno_copy);
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) {
David Reissbc3dddb2007-08-22 23:20:24 +0000161 int errno_copy = errno;
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000162 string errStr = "TSocket::open() connect " + getSocketInfo();
163 GlobalOutput(errStr.c_str());
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;
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000180 string errStr = "TSocket::open() getsockopt SO_ERROR " + getSocketInfo();
181 GlobalOutput(errStr.c_str());
David Reissbc3dddb2007-08-22 23:20:24 +0000182 throw TTransportException(TTransportException::NOT_OPEN, "open()", errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000183 }
184 if (val == 0) {
185 goto done;
186 }
David Reissbc3dddb2007-08-22 23:20:24 +0000187 int errno_copy = errno;
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000188 string errStr = "TSocket::open() SO_ERROR was set " + getSocketInfo();
189 GlobalOutput(errStr.c_str());
David Reissbc3dddb2007-08-22 23:20:24 +0000190 throw TTransportException(TTransportException::NOT_OPEN, "open()", errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000191 } else if (ret == 0) {
David Reissbc3dddb2007-08-22 23:20:24 +0000192 int errno_copy = errno;
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000193 string errStr = "TSocket::open() timed out " + getSocketInfo();
194 GlobalOutput(errStr.c_str());
David Reissbc3dddb2007-08-22 23:20:24 +0000195 throw TTransportException(TTransportException::NOT_OPEN, "open()", errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000196 } else {
David Reissbc3dddb2007-08-22 23:20:24 +0000197 int errno_copy = errno;
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000198 string errStr = "TSocket::open() select error " + getSocketInfo();
199 GlobalOutput(errStr.c_str());
David Reissbc3dddb2007-08-22 23:20:24 +0000200 throw TTransportException(TTransportException::NOT_OPEN, "open()", errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000201 }
202
203 done:
204 // Set socket back to normal mode (blocking)
205 fcntl(socket_, F_SETFL, flags);
Mark Sleee8540632006-05-30 09:24:40 +0000206}
207
Mark Slee6d56eb92007-07-06 22:28:15 +0000208void TSocket::open() {
209 if (isOpen()) {
210 throw TTransportException(TTransportException::ALREADY_OPEN);
211 }
212
213 // Validate port number
214 if (port_ < 0 || port_ > 65536) {
215 throw TTransportException(TTransportException::NOT_OPEN, "Specified port is invalid");
216 }
217
218 struct addrinfo hints, *res, *res0;
219 int error;
220 char port[sizeof("65536") + 1];
221 memset(&hints, 0, sizeof(hints));
222 hints.ai_family = PF_UNSPEC;
223 hints.ai_socktype = SOCK_STREAM;
224 hints.ai_flags = AI_PASSIVE;
225 sprintf(port, "%d", port_);
226
227 {
228 // Scope lock on host entry lookup
229 Synchronized s(s_netdb_monitor);
230 error = getaddrinfo(host_.c_str(), port, &hints, &res0);
231 }
232 if (error) {
233 fprintf(stderr, "getaddrinfo %d: %s\n", error, gai_strerror(error));
234 close();
235 throw TTransportException(TTransportException::NOT_OPEN, "Could not resolve host for client socket.");
236 }
237
238 // Cycle through all the returned addresses until one
239 // connects or push the exception up.
240 for (res = res0; res; res = res->ai_next) {
241 try {
242 openConnection(res);
243 break;
244 } catch (TTransportException& ttx) {
245 if (res->ai_next) {
246 close();
247 } else {
248 close();
Mark Slee85287d32007-07-09 19:50:30 +0000249 freeaddrinfo(res0); // cleanup on failure
Mark Slee6d56eb92007-07-06 22:28:15 +0000250 throw;
251 }
252 }
253 }
Mark Slee85287d32007-07-09 19:50:30 +0000254
255 // Free address structure memory
256 freeaddrinfo(res0);
Mark Slee6d56eb92007-07-06 22:28:15 +0000257}
258
Mark Sleee8540632006-05-30 09:24:40 +0000259void TSocket::close() {
Martin Kraemeree341cb2007-02-05 21:40:38 +0000260 if (socket_ >= 0) {
Mark Sleee8540632006-05-30 09:24:40 +0000261 shutdown(socket_, SHUT_RDWR);
262 ::close(socket_);
263 }
Martin Kraemeree341cb2007-02-05 21:40:38 +0000264 socket_ = -1;
Mark Sleee8540632006-05-30 09:24:40 +0000265}
266
Mark Slee8d7e1f62006-06-07 06:48:56 +0000267uint32_t TSocket::read(uint8_t* buf, uint32_t len) {
Martin Kraemeree341cb2007-02-05 21:40:38 +0000268 if (socket_ < 0) {
Mark Sleef9831082007-02-20 20:59:21 +0000269 throw TTransportException(TTransportException::NOT_OPEN, "Called read on non-open socket");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000270 }
Mark Sleee8540632006-05-30 09:24:40 +0000271
Aditya Agarwale04475b2007-05-23 02:14:58 +0000272 int32_t retries = 0;
273
274 // EAGAIN can be signalled both when a timeout has occurred and when
275 // the system is out of resources (an awesome undocumented feature).
276 // The following is an approximation of the time interval under which
277 // EAGAIN is taken to indicate an out of resources error.
278 uint32_t eagainThresholdMicros = 0;
279 if (recvTimeout_) {
280 // if a readTimeout is specified along with a max number of recv retries, then
281 // the threshold will ensure that the read timeout is not exceeded even in the
282 // case of resource errors
283 eagainThresholdMicros = (recvTimeout_*1000)/ ((maxRecvRetries_>0) ? maxRecvRetries_ : 2);
284 }
285
286 try_again:
Mark Slee8d7e1f62006-06-07 06:48:56 +0000287 // Read from the socket
Aditya Agarwale04475b2007-05-23 02:14:58 +0000288 struct timeval begin;
289 gettimeofday(&begin, NULL);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000290 int got = recv(socket_, buf, len, 0);
Aditya Agarwale04475b2007-05-23 02:14:58 +0000291 struct timeval end;
292 gettimeofday(&end, NULL);
293 uint32_t readElapsedMicros = (((end.tv_sec - begin.tv_sec) * 1000 * 1000)
294 + (((uint64_t)(end.tv_usec - begin.tv_usec))));
Mark Slee8d7e1f62006-06-07 06:48:56 +0000295 ++g_socket_syscalls;
Aditya Agarwale04475b2007-05-23 02:14:58 +0000296
Mark Slee8d7e1f62006-06-07 06:48:56 +0000297 // Check for error on read
Mark Sleec4257802007-01-24 23:14:30 +0000298 if (got < 0) {
Aditya Agarwale04475b2007-05-23 02:14:58 +0000299 if (errno == EAGAIN) {
300 // check if this is the lack of resources or timeout case
301 if (!eagainThresholdMicros || (readElapsedMicros < eagainThresholdMicros)) {
302 if (retries++ < maxRecvRetries_) {
303 usleep(50);
304 goto try_again;
305 } else {
306 throw TTransportException(TTransportException::TIMED_OUT,
307 "EAGAIN (unavailable resources)");
308 }
309 } else {
310 // infer that timeout has been hit
311 throw TTransportException(TTransportException::TIMED_OUT,
312 "EAGAIN (timed out)");
313 }
Mark Sleee8540632006-05-30 09:24:40 +0000314 }
315
Mark Slee8d7e1f62006-06-07 06:48:56 +0000316 // If interrupted, try again
Aditya Agarwale04475b2007-05-23 02:14:58 +0000317 if (errno == EINTR && retries++ < maxRecvRetries_) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000318 goto try_again;
Mark Sleee8540632006-05-30 09:24:40 +0000319 }
320
Mark Sleec4257802007-01-24 23:14:30 +0000321 // Now it's not a try again case, but a real probblez
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000322 string errStr = "TSocket::read() " + getSocketInfo();
323 GlobalOutput(errStr.c_str());
Mark Sleec4257802007-01-24 23:14:30 +0000324
Mark Slee8d7e1f62006-06-07 06:48:56 +0000325 // If we disconnect with no linger time
326 if (errno == ECONNRESET) {
Mark Sleef9831082007-02-20 20:59:21 +0000327 throw TTransportException(TTransportException::NOT_OPEN, "ECONNRESET");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000328 }
329
330 // This ish isn't open
331 if (errno == ENOTCONN) {
Mark Sleef9831082007-02-20 20:59:21 +0000332 throw TTransportException(TTransportException::NOT_OPEN, "ENOTCONN");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000333 }
334
335 // Timed out!
336 if (errno == ETIMEDOUT) {
Mark Sleef9831082007-02-20 20:59:21 +0000337 throw TTransportException(TTransportException::TIMED_OUT, "ETIMEDOUT");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000338 }
339
340 // Some other error, whatevz
Mark Slee5f68c712007-05-11 17:58:54 +0000341 char buff[1024];
342 sprintf(buff, "ERROR errno: %d", errno);
343 throw TTransportException(TTransportException::UNKNOWN, buff);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000344 }
345
346 // The remote host has closed the socket
347 if (got == 0) {
348 close();
349 return 0;
Mark Sleee8540632006-05-30 09:24:40 +0000350 }
351
352 // Pack data into string
Mark Slee8d7e1f62006-06-07 06:48:56 +0000353 return got;
Mark Sleee8540632006-05-30 09:24:40 +0000354}
355
Mark Slee8d7e1f62006-06-07 06:48:56 +0000356void TSocket::write(const uint8_t* buf, uint32_t len) {
Martin Kraemeree341cb2007-02-05 21:40:38 +0000357 if (socket_ < 0) {
Mark Sleef9831082007-02-20 20:59:21 +0000358 throw TTransportException(TTransportException::NOT_OPEN, "Called write on non-open socket");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000359 }
360
Mark Sleee8540632006-05-30 09:24:40 +0000361 uint32_t sent = 0;
362
Mark Slee8d7e1f62006-06-07 06:48:56 +0000363 while (sent < len) {
Marc Slemko9d4a3e22006-07-21 19:53:48 +0000364
365 int flags = 0;
Mark Slee29050782006-09-29 00:12:30 +0000366 #ifdef MSG_NOSIGNAL
Mark Slee8d7e1f62006-06-07 06:48:56 +0000367 // Note the use of MSG_NOSIGNAL to suppress SIGPIPE errors, instead we
368 // check for the EPIPE return condition and close the socket in that case
Marc Slemko9d4a3e22006-07-21 19:53:48 +0000369 flags |= MSG_NOSIGNAL;
Mark Slee29050782006-09-29 00:12:30 +0000370 #endif // ifdef MSG_NOSIGNAL
Marc Slemko9d4a3e22006-07-21 19:53:48 +0000371
372 int b = send(socket_, buf + sent, len - sent, flags);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000373 ++g_socket_syscalls;
374
Mark Sleee8540632006-05-30 09:24:40 +0000375 // Fail on a send error
376 if (b < 0) {
David Reissbc3dddb2007-08-22 23:20:24 +0000377 if (errno == EPIPE || errno == ECONNRESET || errno == ENOTCONN) {
378 int errno_copy = errno;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000379 close();
David Reissbc3dddb2007-08-22 23:20:24 +0000380 throw TTransportException(TTransportException::NOT_OPEN, "send()", errno_copy);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000381 }
382
David Reissbc3dddb2007-08-22 23:20:24 +0000383 int errno_copy = errno;
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000384 string errStr = "TSocket::write() send < 0 " + getSocketInfo();
385 GlobalOutput(errStr.c_str());
David Reissbc3dddb2007-08-22 23:20:24 +0000386 throw TTransportException(TTransportException::UNKNOWN, "send", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000387 }
388
389 // Fail on blocked send
390 if (b == 0) {
Mark Sleef9831082007-02-20 20:59:21 +0000391 throw TTransportException(TTransportException::NOT_OPEN, "Socket send returned 0.");
Mark Sleee8540632006-05-30 09:24:40 +0000392 }
Mark Sleee8540632006-05-30 09:24:40 +0000393 sent += b;
394 }
395}
396
Aditya Agarwalebc99e02007-01-15 23:14:58 +0000397void TSocket::setHost(string host) {
398 host_ = host;
399}
400
401void TSocket::setPort(int port) {
402 port_ = port;
403}
404
Mark Slee8d7e1f62006-06-07 06:48:56 +0000405void TSocket::setLinger(bool on, int linger) {
Mark Slee29050782006-09-29 00:12:30 +0000406 lingerOn_ = on;
407 lingerVal_ = linger;
Martin Kraemeree341cb2007-02-05 21:40:38 +0000408 if (socket_ < 0) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000409 return;
410 }
411
Mark Slee29050782006-09-29 00:12:30 +0000412 struct linger l = {(lingerOn_ ? 1 : 0), lingerVal_};
413 int ret = setsockopt(socket_, SOL_SOCKET, SO_LINGER, &l, sizeof(l));
414 if (ret == -1) {
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000415 string errStr = "TSocket::setLinger() " + getSocketInfo();
416 GlobalOutput(errStr.c_str());
Mark Sleee8540632006-05-30 09:24:40 +0000417 }
Mark Sleee8540632006-05-30 09:24:40 +0000418}
419
Mark Slee8d7e1f62006-06-07 06:48:56 +0000420void TSocket::setNoDelay(bool noDelay) {
Mark Slee29050782006-09-29 00:12:30 +0000421 noDelay_ = noDelay;
Martin Kraemeree341cb2007-02-05 21:40:38 +0000422 if (socket_ < 0) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000423 return;
424 }
425
Mark Sleee8540632006-05-30 09:24:40 +0000426 // Set socket to NODELAY
Mark Slee29050782006-09-29 00:12:30 +0000427 int v = noDelay_ ? 1 : 0;
428 int ret = setsockopt(socket_, IPPROTO_TCP, TCP_NODELAY, &v, sizeof(v));
429 if (ret == -1) {
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000430 string errStr = "TSocket::setNoDelay() " + getSocketInfo();
431 GlobalOutput(errStr.c_str());
Mark Sleee8540632006-05-30 09:24:40 +0000432 }
Mark Sleee8540632006-05-30 09:24:40 +0000433}
Mark Slee29050782006-09-29 00:12:30 +0000434
435void TSocket::setConnTimeout(int ms) {
436 connTimeout_ = ms;
437}
438
439void TSocket::setRecvTimeout(int ms) {
440 recvTimeout_ = ms;
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000441 recvTimeval_.tv_sec = (int)(recvTimeout_/1000);
442 recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000);
Martin Kraemeree341cb2007-02-05 21:40:38 +0000443 if (socket_ < 0) {
Mark Slee29050782006-09-29 00:12:30 +0000444 return;
445 }
446
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000447 // Copy because select may modify
448 struct timeval r = recvTimeval_;
Mark Slee29050782006-09-29 00:12:30 +0000449 int ret = setsockopt(socket_, SOL_SOCKET, SO_RCVTIMEO, &r, sizeof(r));
450 if (ret == -1) {
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000451 string errStr = "TSocket::setRecvTimeout() " + getSocketInfo();
452 GlobalOutput(errStr.c_str());
Mark Slee29050782006-09-29 00:12:30 +0000453 }
454}
455
456void TSocket::setSendTimeout(int ms) {
457 sendTimeout_ = ms;
Martin Kraemeree341cb2007-02-05 21:40:38 +0000458 if (socket_ < 0) {
Mark Slee29050782006-09-29 00:12:30 +0000459 return;
460 }
461
462 struct timeval s = {(int)(sendTimeout_/1000),
463 (int)((sendTimeout_%1000)*1000)};
464 int ret = setsockopt(socket_, SOL_SOCKET, SO_SNDTIMEO, &s, sizeof(s));
465 if (ret == -1) {
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000466 string errStr = "TSocket::setSendTimeout() " + getSocketInfo();
467 GlobalOutput(errStr.c_str());
Mark Slee29050782006-09-29 00:12:30 +0000468 }
469}
470
Aditya Agarwale04475b2007-05-23 02:14:58 +0000471void TSocket::setMaxRecvRetries(int maxRecvRetries) {
472 maxRecvRetries_ = maxRecvRetries;
473}
474
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000475string TSocket::getSocketInfo() {
476 std::ostringstream oss;
477 oss << "<Host: " << host_ << " Port: " << port_ << ">";
478 return oss.str();
479}
480
Marc Slemko6f038a72006-08-03 18:58:09 +0000481}}} // facebook::thrift::transport