blob: c2169290bbab80c1cf61ea2622956799cb1a5d9f [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;
26
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 Slee256bdc42007-11-27 08:42:19 +000036TSocket::TSocket(string host, int port) :
Mark Slee29050782006-09-29 00:12:30 +000037 host_(host),
38 port_(port),
Martin Kraemeree341cb2007-02-05 21:40:38 +000039 socket_(-1),
Mark Slee29050782006-09-29 00:12:30 +000040 connTimeout_(0),
41 sendTimeout_(0),
42 recvTimeout_(0),
43 lingerOn_(1),
44 lingerVal_(0),
Aditya Agarwale04475b2007-05-23 02:14:58 +000045 noDelay_(1),
46 maxRecvRetries_(5) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +000047 recvTimeval_.tv_sec = (int)(recvTimeout_/1000);
48 recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000);
Mark Sleee8540632006-05-30 09:24:40 +000049}
50
Mark Slee256bdc42007-11-27 08:42:19 +000051TSocket::TSocket() :
Aditya Agarwalebc99e02007-01-15 23:14:58 +000052 host_(""),
53 port_(0),
Martin Kraemeree341cb2007-02-05 21:40:38 +000054 socket_(-1),
Aditya Agarwalebc99e02007-01-15 23:14:58 +000055 connTimeout_(0),
56 sendTimeout_(0),
57 recvTimeout_(0),
58 lingerOn_(1),
59 lingerVal_(0),
Aditya Agarwale04475b2007-05-23 02:14:58 +000060 noDelay_(1),
61 maxRecvRetries_(5) {
Aditya Agarwalebc99e02007-01-15 23:14:58 +000062 recvTimeval_.tv_sec = (int)(recvTimeout_/1000);
63 recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000);
64}
65
Mark Slee29050782006-09-29 00:12:30 +000066TSocket::TSocket(int socket) :
67 host_(""),
68 port_(0),
69 socket_(socket),
70 connTimeout_(0),
71 sendTimeout_(0),
72 recvTimeout_(0),
73 lingerOn_(1),
74 lingerVal_(0),
Aditya Agarwale04475b2007-05-23 02:14:58 +000075 noDelay_(1),
76 maxRecvRetries_(5) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +000077 recvTimeval_.tv_sec = (int)(recvTimeout_/1000);
78 recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000);
Mark Slee29050782006-09-29 00:12:30 +000079}
Mark Slee256bdc42007-11-27 08:42:19 +000080
Mark Sleee8540632006-05-30 09:24:40 +000081TSocket::~TSocket() {
82 close();
83}
84
Mark Slee256bdc42007-11-27 08:42:19 +000085bool TSocket::isOpen() {
86 return (socket_ >= 0);
Mark Slee8d7e1f62006-06-07 06:48:56 +000087}
88
Mark Sleeb9ff32a2006-11-16 01:00:24 +000089bool TSocket::peek() {
90 if (!isOpen()) {
91 return false;
92 }
93 uint8_t buf;
94 int r = recv(socket_, &buf, 1, MSG_PEEK);
95 if (r == -1) {
David Reissbc3dddb2007-08-22 23:20:24 +000096 int errno_copy = errno;
Aditya Agarwal4529c4b2007-09-05 01:01:15 +000097 string errStr = "TSocket::peek() " + getSocketInfo();
98 GlobalOutput(errStr.c_str());
David Reissbc3dddb2007-08-22 23:20:24 +000099 throw TTransportException(TTransportException::UNKNOWN, "recv()", errno_copy);
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000100 }
101 return (r > 0);
102}
103
Mark Slee6d56eb92007-07-06 22:28:15 +0000104void TSocket::openConnection(struct addrinfo *res) {
Mark Sleea9848d72007-02-21 04:54:05 +0000105 if (isOpen()) {
106 throw TTransportException(TTransportException::ALREADY_OPEN);
107 }
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000108
Mark Slee6d56eb92007-07-06 22:28:15 +0000109 socket_ = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
Mark Sleee8540632006-05-30 09:24:40 +0000110 if (socket_ == -1) {
David Reissbc3dddb2007-08-22 23:20:24 +0000111 int errno_copy = errno;
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000112 string errStr = "TSocket::open() socket " + getSocketInfo();
113 GlobalOutput(errStr.c_str());
David Reissbc3dddb2007-08-22 23:20:24 +0000114 throw TTransportException(TTransportException::NOT_OPEN, "socket()", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000115 }
Mark Slee29050782006-09-29 00:12:30 +0000116
117 // Send timeout
118 if (sendTimeout_ > 0) {
119 setSendTimeout(sendTimeout_);
120 }
121
122 // Recv timeout
123 if (recvTimeout_ > 0) {
124 setRecvTimeout(recvTimeout_);
125 }
126
127 // Linger
128 setLinger(lingerOn_, lingerVal_);
129
130 // No delay
131 setNoDelay(noDelay_);
132
Mark Slee29050782006-09-29 00:12:30 +0000133 // Set the socket to be non blocking for connect if a timeout exists
Mark Slee256bdc42007-11-27 08:42:19 +0000134 int flags = fcntl(socket_, F_GETFL, 0);
Mark Slee29050782006-09-29 00:12:30 +0000135 if (connTimeout_ > 0) {
Mark Sleea5a783f2007-03-02 19:41:08 +0000136 if (-1 == fcntl(socket_, F_SETFL, flags | O_NONBLOCK)) {
137 throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed");
138 }
Mark Slee29050782006-09-29 00:12:30 +0000139 } else {
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 }
144
145 // Conn timeout
146 struct timeval c = {(int)(connTimeout_/1000),
147 (int)((connTimeout_%1000)*1000)};
Mark Slee256bdc42007-11-27 08:42:19 +0000148
Mark Sleee8540632006-05-30 09:24:40 +0000149 // Connect the socket
Mark Slee6d56eb92007-07-06 22:28:15 +0000150 int ret = connect(socket_, res->ai_addr, res->ai_addrlen);
Mark Slee256bdc42007-11-27 08:42:19 +0000151
Mark Slee29050782006-09-29 00:12:30 +0000152 if (ret == 0) {
153 goto done;
154 }
155
156 if (errno != EINPROGRESS) {
David Reissbc3dddb2007-08-22 23:20:24 +0000157 int errno_copy = errno;
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000158 string errStr = "TSocket::open() connect " + getSocketInfo();
159 GlobalOutput(errStr.c_str());
David Reissbc3dddb2007-08-22 23:20:24 +0000160 throw TTransportException(TTransportException::NOT_OPEN, "connect()", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000161 }
162
Mark Slee29050782006-09-29 00:12:30 +0000163 fd_set fds;
164 FD_ZERO(&fds);
165 FD_SET(socket_, &fds);
166 ret = select(socket_+1, NULL, &fds, NULL, &c);
167
168 if (ret > 0) {
169 // Ensure connected
170 int val;
171 socklen_t lon;
172 lon = sizeof(int);
173 int ret2 = getsockopt(socket_, SOL_SOCKET, SO_ERROR, (void *)&val, &lon);
174 if (ret2 == -1) {
David Reissbc3dddb2007-08-22 23:20:24 +0000175 int errno_copy = errno;
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000176 string errStr = "TSocket::open() getsockopt SO_ERROR " + getSocketInfo();
177 GlobalOutput(errStr.c_str());
David Reissbc3dddb2007-08-22 23:20:24 +0000178 throw TTransportException(TTransportException::NOT_OPEN, "open()", errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000179 }
180 if (val == 0) {
181 goto done;
182 }
David Reissbc3dddb2007-08-22 23:20:24 +0000183 int errno_copy = errno;
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000184 string errStr = "TSocket::open() SO_ERROR was set " + getSocketInfo();
185 GlobalOutput(errStr.c_str());
David Reissbc3dddb2007-08-22 23:20:24 +0000186 throw TTransportException(TTransportException::NOT_OPEN, "open()", errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000187 } else if (ret == 0) {
David Reissbc3dddb2007-08-22 23:20:24 +0000188 int errno_copy = errno;
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000189 string errStr = "TSocket::open() timed out " + getSocketInfo();
190 GlobalOutput(errStr.c_str());
David Reissbc3dddb2007-08-22 23:20:24 +0000191 throw TTransportException(TTransportException::NOT_OPEN, "open()", errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000192 } else {
David Reissbc3dddb2007-08-22 23:20:24 +0000193 int errno_copy = errno;
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000194 string errStr = "TSocket::open() select error " + getSocketInfo();
195 GlobalOutput(errStr.c_str());
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;
Mark Slee256bdc42007-11-27 08:42:19 +0000220 hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
Mark Slee6d56eb92007-07-06 22:28:15 +0000221 sprintf(port, "%d", port_);
Mark Slee256bdc42007-11-27 08:42:19 +0000222
Mark Sleec37b4c52007-12-05 23:03:37 +0000223 error = getaddrinfo(host_.c_str(), port, &hints, &res0);
224
Mark Slee6d56eb92007-07-06 22:28:15 +0000225 if (error) {
226 fprintf(stderr, "getaddrinfo %d: %s\n", error, gai_strerror(error));
227 close();
228 throw TTransportException(TTransportException::NOT_OPEN, "Could not resolve host for client socket.");
229 }
Mark Slee256bdc42007-11-27 08:42:19 +0000230
Mark Slee6d56eb92007-07-06 22:28:15 +0000231 // Cycle through all the returned addresses until one
232 // connects or push the exception up.
233 for (res = res0; res; res = res->ai_next) {
234 try {
235 openConnection(res);
236 break;
237 } catch (TTransportException& ttx) {
238 if (res->ai_next) {
239 close();
240 } else {
241 close();
Mark Slee85287d32007-07-09 19:50:30 +0000242 freeaddrinfo(res0); // cleanup on failure
Mark Slee6d56eb92007-07-06 22:28:15 +0000243 throw;
244 }
245 }
246 }
Mark Slee85287d32007-07-09 19:50:30 +0000247
248 // Free address structure memory
249 freeaddrinfo(res0);
Mark Slee6d56eb92007-07-06 22:28:15 +0000250}
251
Mark Sleee8540632006-05-30 09:24:40 +0000252void TSocket::close() {
Martin Kraemeree341cb2007-02-05 21:40:38 +0000253 if (socket_ >= 0) {
Mark Sleee8540632006-05-30 09:24:40 +0000254 shutdown(socket_, SHUT_RDWR);
255 ::close(socket_);
256 }
Martin Kraemeree341cb2007-02-05 21:40:38 +0000257 socket_ = -1;
Mark Sleee8540632006-05-30 09:24:40 +0000258}
259
Mark Slee8d7e1f62006-06-07 06:48:56 +0000260uint32_t TSocket::read(uint8_t* buf, uint32_t len) {
Martin Kraemeree341cb2007-02-05 21:40:38 +0000261 if (socket_ < 0) {
Mark Sleef9831082007-02-20 20:59:21 +0000262 throw TTransportException(TTransportException::NOT_OPEN, "Called read on non-open socket");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000263 }
Mark Sleee8540632006-05-30 09:24:40 +0000264
Aditya Agarwale04475b2007-05-23 02:14:58 +0000265 int32_t retries = 0;
266
267 // EAGAIN can be signalled both when a timeout has occurred and when
268 // the system is out of resources (an awesome undocumented feature).
269 // The following is an approximation of the time interval under which
270 // EAGAIN is taken to indicate an out of resources error.
271 uint32_t eagainThresholdMicros = 0;
272 if (recvTimeout_) {
Mark Slee256bdc42007-11-27 08:42:19 +0000273 // if a readTimeout is specified along with a max number of recv retries, then
Aditya Agarwale04475b2007-05-23 02:14:58 +0000274 // the threshold will ensure that the read timeout is not exceeded even in the
275 // case of resource errors
276 eagainThresholdMicros = (recvTimeout_*1000)/ ((maxRecvRetries_>0) ? maxRecvRetries_ : 2);
277 }
278
Mark Slee256bdc42007-11-27 08:42:19 +0000279 try_again:
Mark Slee8d7e1f62006-06-07 06:48:56 +0000280 // Read from the socket
Aditya Agarwale04475b2007-05-23 02:14:58 +0000281 struct timeval begin;
282 gettimeofday(&begin, NULL);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000283 int got = recv(socket_, buf, len, 0);
Aditya Agarwale04475b2007-05-23 02:14:58 +0000284 struct timeval end;
285 gettimeofday(&end, NULL);
286 uint32_t readElapsedMicros = (((end.tv_sec - begin.tv_sec) * 1000 * 1000)
287 + (((uint64_t)(end.tv_usec - begin.tv_usec))));
Mark Slee8d7e1f62006-06-07 06:48:56 +0000288 ++g_socket_syscalls;
Aditya Agarwale04475b2007-05-23 02:14:58 +0000289
Mark Slee8d7e1f62006-06-07 06:48:56 +0000290 // Check for error on read
Mark Slee256bdc42007-11-27 08:42:19 +0000291 if (got < 0) {
Aditya Agarwale04475b2007-05-23 02:14:58 +0000292 if (errno == EAGAIN) {
293 // check if this is the lack of resources or timeout case
294 if (!eagainThresholdMicros || (readElapsedMicros < eagainThresholdMicros)) {
295 if (retries++ < maxRecvRetries_) {
296 usleep(50);
297 goto try_again;
298 } else {
Mark Slee256bdc42007-11-27 08:42:19 +0000299 throw TTransportException(TTransportException::TIMED_OUT,
Aditya Agarwale04475b2007-05-23 02:14:58 +0000300 "EAGAIN (unavailable resources)");
301 }
302 } else {
303 // infer that timeout has been hit
Mark Slee256bdc42007-11-27 08:42:19 +0000304 throw TTransportException(TTransportException::TIMED_OUT,
Aditya Agarwale04475b2007-05-23 02:14:58 +0000305 "EAGAIN (timed out)");
306 }
Mark Sleee8540632006-05-30 09:24:40 +0000307 }
Mark Slee256bdc42007-11-27 08:42:19 +0000308
Mark Slee8d7e1f62006-06-07 06:48:56 +0000309 // If interrupted, try again
Aditya Agarwale04475b2007-05-23 02:14:58 +0000310 if (errno == EINTR && retries++ < maxRecvRetries_) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000311 goto try_again;
Mark Sleee8540632006-05-30 09:24:40 +0000312 }
Mark Slee256bdc42007-11-27 08:42:19 +0000313
Mark Sleec4257802007-01-24 23:14:30 +0000314 // Now it's not a try again case, but a real probblez
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000315 string errStr = "TSocket::read() " + getSocketInfo();
316 GlobalOutput(errStr.c_str());
Mark Sleec4257802007-01-24 23:14:30 +0000317
Mark Slee8d7e1f62006-06-07 06:48:56 +0000318 // If we disconnect with no linger time
319 if (errno == ECONNRESET) {
Mark Sleef9831082007-02-20 20:59:21 +0000320 throw TTransportException(TTransportException::NOT_OPEN, "ECONNRESET");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000321 }
Mark Slee256bdc42007-11-27 08:42:19 +0000322
Mark Slee8d7e1f62006-06-07 06:48:56 +0000323 // This ish isn't open
324 if (errno == ENOTCONN) {
Mark Sleef9831082007-02-20 20:59:21 +0000325 throw TTransportException(TTransportException::NOT_OPEN, "ENOTCONN");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000326 }
Mark Slee256bdc42007-11-27 08:42:19 +0000327
Mark Slee8d7e1f62006-06-07 06:48:56 +0000328 // Timed out!
329 if (errno == ETIMEDOUT) {
Mark Sleef9831082007-02-20 20:59:21 +0000330 throw TTransportException(TTransportException::TIMED_OUT, "ETIMEDOUT");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000331 }
Mark Slee256bdc42007-11-27 08:42:19 +0000332
Mark Slee8d7e1f62006-06-07 06:48:56 +0000333 // Some other error, whatevz
Mark Slee5f68c712007-05-11 17:58:54 +0000334 char buff[1024];
335 sprintf(buff, "ERROR errno: %d", errno);
336 throw TTransportException(TTransportException::UNKNOWN, buff);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000337 }
Mark Slee256bdc42007-11-27 08:42:19 +0000338
Mark Slee8d7e1f62006-06-07 06:48:56 +0000339 // The remote host has closed the socket
340 if (got == 0) {
341 close();
342 return 0;
Mark Sleee8540632006-05-30 09:24:40 +0000343 }
Mark Slee256bdc42007-11-27 08:42:19 +0000344
Mark Sleee8540632006-05-30 09:24:40 +0000345 // Pack data into string
Mark Slee8d7e1f62006-06-07 06:48:56 +0000346 return got;
Mark Sleee8540632006-05-30 09:24:40 +0000347}
348
Mark Slee8d7e1f62006-06-07 06:48:56 +0000349void TSocket::write(const uint8_t* buf, uint32_t len) {
Martin Kraemeree341cb2007-02-05 21:40:38 +0000350 if (socket_ < 0) {
Mark Sleef9831082007-02-20 20:59:21 +0000351 throw TTransportException(TTransportException::NOT_OPEN, "Called write on non-open socket");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000352 }
353
Mark Sleee8540632006-05-30 09:24:40 +0000354 uint32_t sent = 0;
Mark Slee256bdc42007-11-27 08:42:19 +0000355
Mark Slee8d7e1f62006-06-07 06:48:56 +0000356 while (sent < len) {
Marc Slemko9d4a3e22006-07-21 19:53:48 +0000357
358 int flags = 0;
Mark Slee29050782006-09-29 00:12:30 +0000359 #ifdef MSG_NOSIGNAL
Mark Slee8d7e1f62006-06-07 06:48:56 +0000360 // Note the use of MSG_NOSIGNAL to suppress SIGPIPE errors, instead we
361 // check for the EPIPE return condition and close the socket in that case
Marc Slemko9d4a3e22006-07-21 19:53:48 +0000362 flags |= MSG_NOSIGNAL;
Mark Slee29050782006-09-29 00:12:30 +0000363 #endif // ifdef MSG_NOSIGNAL
Marc Slemko9d4a3e22006-07-21 19:53:48 +0000364
365 int b = send(socket_, buf + sent, len - sent, flags);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000366 ++g_socket_syscalls;
367
Mark Sleee8540632006-05-30 09:24:40 +0000368 // Fail on a send error
369 if (b < 0) {
David Reissbc3dddb2007-08-22 23:20:24 +0000370 if (errno == EPIPE || errno == ECONNRESET || errno == ENOTCONN) {
371 int errno_copy = errno;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000372 close();
David Reissbc3dddb2007-08-22 23:20:24 +0000373 throw TTransportException(TTransportException::NOT_OPEN, "send()", errno_copy);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000374 }
375
David Reissbc3dddb2007-08-22 23:20:24 +0000376 int errno_copy = errno;
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000377 string errStr = "TSocket::write() send < 0 " + getSocketInfo();
378 GlobalOutput(errStr.c_str());
David Reissbc3dddb2007-08-22 23:20:24 +0000379 throw TTransportException(TTransportException::UNKNOWN, "send", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000380 }
Mark Slee256bdc42007-11-27 08:42:19 +0000381
Mark Sleee8540632006-05-30 09:24:40 +0000382 // Fail on blocked send
383 if (b == 0) {
Mark Sleef9831082007-02-20 20:59:21 +0000384 throw TTransportException(TTransportException::NOT_OPEN, "Socket send returned 0.");
Mark Sleee8540632006-05-30 09:24:40 +0000385 }
Mark Sleee8540632006-05-30 09:24:40 +0000386 sent += b;
387 }
388}
389
dweatherford14b0ed62007-10-19 01:03:32 +0000390std::string TSocket::getHost() {
391 return host_;
392}
393
394int TSocket::getPort() {
395 return port_;
396}
397
Aditya Agarwalebc99e02007-01-15 23:14:58 +0000398void TSocket::setHost(string host) {
399 host_ = host;
400}
401
402void TSocket::setPort(int port) {
403 port_ = port;
404}
405
Mark Slee8d7e1f62006-06-07 06:48:56 +0000406void TSocket::setLinger(bool on, int linger) {
Mark Slee29050782006-09-29 00:12:30 +0000407 lingerOn_ = on;
408 lingerVal_ = linger;
Martin Kraemeree341cb2007-02-05 21:40:38 +0000409 if (socket_ < 0) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000410 return;
411 }
412
Mark Slee29050782006-09-29 00:12:30 +0000413 struct linger l = {(lingerOn_ ? 1 : 0), lingerVal_};
414 int ret = setsockopt(socket_, SOL_SOCKET, SO_LINGER, &l, sizeof(l));
415 if (ret == -1) {
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000416 string errStr = "TSocket::setLinger() " + getSocketInfo();
417 GlobalOutput(errStr.c_str());
Mark Sleee8540632006-05-30 09:24:40 +0000418 }
Mark Sleee8540632006-05-30 09:24:40 +0000419}
420
Mark Slee8d7e1f62006-06-07 06:48:56 +0000421void TSocket::setNoDelay(bool noDelay) {
Mark Slee29050782006-09-29 00:12:30 +0000422 noDelay_ = noDelay;
Martin Kraemeree341cb2007-02-05 21:40:38 +0000423 if (socket_ < 0) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000424 return;
425 }
426
Mark Sleee8540632006-05-30 09:24:40 +0000427 // Set socket to NODELAY
Mark Slee29050782006-09-29 00:12:30 +0000428 int v = noDelay_ ? 1 : 0;
429 int ret = setsockopt(socket_, IPPROTO_TCP, TCP_NODELAY, &v, sizeof(v));
430 if (ret == -1) {
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000431 string errStr = "TSocket::setNoDelay() " + getSocketInfo();
432 GlobalOutput(errStr.c_str());
Mark Sleee8540632006-05-30 09:24:40 +0000433 }
Mark Sleee8540632006-05-30 09:24:40 +0000434}
Mark Slee29050782006-09-29 00:12:30 +0000435
436void TSocket::setConnTimeout(int ms) {
437 connTimeout_ = ms;
438}
439
440void TSocket::setRecvTimeout(int ms) {
Aditya Agarwalc31769c2007-12-11 22:23:51 +0000441 if (ms < 0) {
442 char errBuf[512];
443 sprintf(errBuf, "TSocket::setRecvTimeout with negative input: %d\n", ms);
444 GlobalOutput(errBuf);
445 return;
446 }
Mark Slee29050782006-09-29 00:12:30 +0000447 recvTimeout_ = ms;
Aditya Agarwalc31769c2007-12-11 22:23:51 +0000448
Martin Kraemeree341cb2007-02-05 21:40:38 +0000449 if (socket_ < 0) {
Mark Slee29050782006-09-29 00:12:30 +0000450 return;
451 }
452
Aditya Agarwalc31769c2007-12-11 22:23:51 +0000453 recvTimeval_.tv_sec = (int)(recvTimeout_/1000);
454 recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000);
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) {
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000460 string errStr = "TSocket::setRecvTimeout() " + getSocketInfo();
461 GlobalOutput(errStr.c_str());
Mark Slee29050782006-09-29 00:12:30 +0000462 }
463}
464
465void TSocket::setSendTimeout(int ms) {
Aditya Agarwalc31769c2007-12-11 22:23:51 +0000466 if (ms < 0) {
467 char errBuf[512];
468 sprintf(errBuf, "TSocket::setSendTimeout with negative input: %d\n", ms);
469 GlobalOutput(errBuf);
470 return;
471 }
Mark Slee29050782006-09-29 00:12:30 +0000472 sendTimeout_ = ms;
Aditya Agarwalc31769c2007-12-11 22:23:51 +0000473
Martin Kraemeree341cb2007-02-05 21:40:38 +0000474 if (socket_ < 0) {
Mark Slee29050782006-09-29 00:12:30 +0000475 return;
476 }
Mark Slee256bdc42007-11-27 08:42:19 +0000477
Mark Slee29050782006-09-29 00:12:30 +0000478 struct timeval s = {(int)(sendTimeout_/1000),
479 (int)((sendTimeout_%1000)*1000)};
480 int ret = setsockopt(socket_, SOL_SOCKET, SO_SNDTIMEO, &s, sizeof(s));
481 if (ret == -1) {
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000482 string errStr = "TSocket::setSendTimeout() " + getSocketInfo();
483 GlobalOutput(errStr.c_str());
Mark Slee29050782006-09-29 00:12:30 +0000484 }
485}
486
Aditya Agarwale04475b2007-05-23 02:14:58 +0000487void TSocket::setMaxRecvRetries(int maxRecvRetries) {
488 maxRecvRetries_ = maxRecvRetries;
489}
490
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000491string TSocket::getSocketInfo() {
492 std::ostringstream oss;
493 oss << "<Host: " << host_ << " Port: " << port_ << ">";
494 return oss.str();
495}
496
Mark Sleeb4552922007-11-28 00:12:11 +0000497std::string TSocket::getPeerHost() {
498 if (peerHost_.empty()) {
499 struct sockaddr_storage addr;
500 socklen_t addrLen = sizeof(addr);
501
502 if (socket_ < 0) {
503 return host_;
504 }
505
506 int rv = getpeername(socket_, (sockaddr*) &addr, &addrLen);
507
508 if (rv != 0) {
509 return peerHost_;
510 }
511
512 char clienthost[NI_MAXHOST];
513 char clientservice[NI_MAXSERV];
514
515 getnameinfo((sockaddr*) &addr, addrLen,
516 clienthost, sizeof(clienthost),
517 clientservice, sizeof(clientservice), 0);
518
519 peerHost_ = clienthost;
520 }
521 return peerHost_;
522}
523
524std::string TSocket::getPeerAddress() {
525 if (peerAddress_.empty()) {
526 struct sockaddr_storage addr;
527 socklen_t addrLen = sizeof(addr);
528
529 if (socket_ < 0) {
530 return peerAddress_;
531 }
532
533 int rv = getpeername(socket_, (sockaddr*) &addr, &addrLen);
534
535 if (rv != 0) {
536 return peerAddress_;
537 }
538
539 char clienthost[NI_MAXHOST];
540 char clientservice[NI_MAXSERV];
541
542 getnameinfo((sockaddr*) &addr, addrLen,
543 clienthost, sizeof(clienthost),
544 clientservice, sizeof(clientservice),
545 NI_NUMERICHOST|NI_NUMERICSERV);
546
547 peerAddress_ = clienthost;
548 peerPort_ = std::atoi(clientservice);
549 }
550 return peerAddress_;
551}
552
553int TSocket::getPeerPort() {
554 getPeerAddress();
555 return peerPort_;
556}
557
Marc Slemko6f038a72006-08-03 18:58:09 +0000558}}} // facebook::thrift::transport