blob: acf5288d7d667d000ca7bd95969592671fe45a76 [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
Mark Slee256bdc42007-11-27 08:42:19 +000023namespace facebook { namespace thrift { namespace transport {
Marc Slemko6f038a72006-08-03 18:58:09 +000024
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 Slee256bdc42007-11-27 08:42:19 +000040TSocket::TSocket(string host, int port) :
Mark Slee29050782006-09-29 00:12:30 +000041 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
Mark Slee256bdc42007-11-27 08:42:19 +000055TSocket::TSocket() :
Aditya Agarwalebc99e02007-01-15 23:14:58 +000056 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}
Mark Slee256bdc42007-11-27 08:42:19 +000084
Mark Sleee8540632006-05-30 09:24:40 +000085TSocket::~TSocket() {
86 close();
87}
88
Mark Slee256bdc42007-11-27 08:42:19 +000089bool TSocket::isOpen() {
90 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
Mark Slee256bdc42007-11-27 08:42:19 +0000138 int flags = fcntl(socket_, F_GETFL, 0);
Mark Slee29050782006-09-29 00:12:30 +0000139 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 Slee256bdc42007-11-27 08:42:19 +0000152
Mark Sleee8540632006-05-30 09:24:40 +0000153 // Connect the socket
Mark Slee6d56eb92007-07-06 22:28:15 +0000154 int ret = connect(socket_, res->ai_addr, res->ai_addrlen);
Mark Slee256bdc42007-11-27 08:42:19 +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;
Mark Slee256bdc42007-11-27 08:42:19 +0000224 hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
Mark Slee6d56eb92007-07-06 22:28:15 +0000225 sprintf(port, "%d", port_);
Mark Slee256bdc42007-11-27 08:42:19 +0000226
Mark Slee6d56eb92007-07-06 22:28:15 +0000227 {
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 }
Mark Slee256bdc42007-11-27 08:42:19 +0000237
Mark Slee6d56eb92007-07-06 22:28:15 +0000238 // 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_) {
Mark Slee256bdc42007-11-27 08:42:19 +0000280 // if a readTimeout is specified along with a max number of recv retries, then
Aditya Agarwale04475b2007-05-23 02:14:58 +0000281 // 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
Mark Slee256bdc42007-11-27 08:42:19 +0000286 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 Slee256bdc42007-11-27 08:42:19 +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 {
Mark Slee256bdc42007-11-27 08:42:19 +0000306 throw TTransportException(TTransportException::TIMED_OUT,
Aditya Agarwale04475b2007-05-23 02:14:58 +0000307 "EAGAIN (unavailable resources)");
308 }
309 } else {
310 // infer that timeout has been hit
Mark Slee256bdc42007-11-27 08:42:19 +0000311 throw TTransportException(TTransportException::TIMED_OUT,
Aditya Agarwale04475b2007-05-23 02:14:58 +0000312 "EAGAIN (timed out)");
313 }
Mark Sleee8540632006-05-30 09:24:40 +0000314 }
Mark Slee256bdc42007-11-27 08:42:19 +0000315
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 }
Mark Slee256bdc42007-11-27 08:42:19 +0000320
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 }
Mark Slee256bdc42007-11-27 08:42:19 +0000329
Mark Slee8d7e1f62006-06-07 06:48:56 +0000330 // 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 }
Mark Slee256bdc42007-11-27 08:42:19 +0000334
Mark Slee8d7e1f62006-06-07 06:48:56 +0000335 // 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 }
Mark Slee256bdc42007-11-27 08:42:19 +0000339
Mark Slee8d7e1f62006-06-07 06:48:56 +0000340 // 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 }
Mark Slee256bdc42007-11-27 08:42:19 +0000345
Mark Slee8d7e1f62006-06-07 06:48:56 +0000346 // The remote host has closed the socket
347 if (got == 0) {
348 close();
349 return 0;
Mark Sleee8540632006-05-30 09:24:40 +0000350 }
Mark Slee256bdc42007-11-27 08:42:19 +0000351
Mark Sleee8540632006-05-30 09:24:40 +0000352 // 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;
Mark Slee256bdc42007-11-27 08:42:19 +0000362
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 }
Mark Slee256bdc42007-11-27 08:42:19 +0000388
Mark Sleee8540632006-05-30 09:24:40 +0000389 // 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
dweatherford14b0ed62007-10-19 01:03:32 +0000397std::string TSocket::getHost() {
398 return host_;
399}
400
401int TSocket::getPort() {
402 return port_;
403}
404
Aditya Agarwalebc99e02007-01-15 23:14:58 +0000405void TSocket::setHost(string host) {
406 host_ = host;
407}
408
409void TSocket::setPort(int port) {
410 port_ = port;
411}
412
Mark Slee8d7e1f62006-06-07 06:48:56 +0000413void TSocket::setLinger(bool on, int linger) {
Mark Slee29050782006-09-29 00:12:30 +0000414 lingerOn_ = on;
415 lingerVal_ = linger;
Martin Kraemeree341cb2007-02-05 21:40:38 +0000416 if (socket_ < 0) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000417 return;
418 }
419
Mark Slee29050782006-09-29 00:12:30 +0000420 struct linger l = {(lingerOn_ ? 1 : 0), lingerVal_};
421 int ret = setsockopt(socket_, SOL_SOCKET, SO_LINGER, &l, sizeof(l));
422 if (ret == -1) {
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000423 string errStr = "TSocket::setLinger() " + getSocketInfo();
424 GlobalOutput(errStr.c_str());
Mark Sleee8540632006-05-30 09:24:40 +0000425 }
Mark Sleee8540632006-05-30 09:24:40 +0000426}
427
Mark Slee8d7e1f62006-06-07 06:48:56 +0000428void TSocket::setNoDelay(bool noDelay) {
Mark Slee29050782006-09-29 00:12:30 +0000429 noDelay_ = noDelay;
Martin Kraemeree341cb2007-02-05 21:40:38 +0000430 if (socket_ < 0) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000431 return;
432 }
433
Mark Sleee8540632006-05-30 09:24:40 +0000434 // Set socket to NODELAY
Mark Slee29050782006-09-29 00:12:30 +0000435 int v = noDelay_ ? 1 : 0;
436 int ret = setsockopt(socket_, IPPROTO_TCP, TCP_NODELAY, &v, sizeof(v));
437 if (ret == -1) {
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000438 string errStr = "TSocket::setNoDelay() " + getSocketInfo();
439 GlobalOutput(errStr.c_str());
Mark Sleee8540632006-05-30 09:24:40 +0000440 }
Mark Sleee8540632006-05-30 09:24:40 +0000441}
Mark Slee29050782006-09-29 00:12:30 +0000442
443void TSocket::setConnTimeout(int ms) {
444 connTimeout_ = ms;
445}
446
447void TSocket::setRecvTimeout(int ms) {
448 recvTimeout_ = ms;
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000449 recvTimeval_.tv_sec = (int)(recvTimeout_/1000);
450 recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000);
Martin Kraemeree341cb2007-02-05 21:40:38 +0000451 if (socket_ < 0) {
Mark Slee29050782006-09-29 00:12:30 +0000452 return;
453 }
454
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000455 // Copy because select may modify
456 struct timeval r = recvTimeval_;
Mark Slee29050782006-09-29 00:12:30 +0000457 int ret = setsockopt(socket_, SOL_SOCKET, SO_RCVTIMEO, &r, sizeof(r));
458 if (ret == -1) {
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000459 string errStr = "TSocket::setRecvTimeout() " + getSocketInfo();
460 GlobalOutput(errStr.c_str());
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 }
Mark Slee256bdc42007-11-27 08:42:19 +0000469
Mark Slee29050782006-09-29 00:12:30 +0000470 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) {
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000474 string errStr = "TSocket::setSendTimeout() " + getSocketInfo();
475 GlobalOutput(errStr.c_str());
Mark Slee29050782006-09-29 00:12:30 +0000476 }
477}
478
Aditya Agarwale04475b2007-05-23 02:14:58 +0000479void TSocket::setMaxRecvRetries(int maxRecvRetries) {
480 maxRecvRetries_ = maxRecvRetries;
481}
482
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000483string TSocket::getSocketInfo() {
484 std::ostringstream oss;
485 oss << "<Host: " << host_ << " Port: " << port_ << ">";
486 return oss.str();
487}
488
Marc Slemko6f038a72006-08-03 18:58:09 +0000489}}} // facebook::thrift::transport