blob: 46cd2b48dfc45b087d8ac680baf3d979bf1506dd [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>
David Reissc88eb8c2008-06-11 01:18:54 +00008#include <cstring>
9#include <sstream>
Mark Sleee8540632006-05-30 09:24:40 +000010#include <sys/socket.h>
David Reiss22b18862008-04-08 06:25:45 +000011#include <sys/poll.h>
Mark Sleedd564972007-08-21 02:39:57 +000012#include <sys/types.h>
Mark Sleee8540632006-05-30 09:24:40 +000013#include <arpa/inet.h>
14#include <netinet/in.h>
15#include <netinet/tcp.h>
16#include <netdb.h>
17#include <unistd.h>
18#include <errno.h>
Mark Slee29050782006-09-29 00:12:30 +000019#include <fcntl.h>
Mark Sleee8540632006-05-30 09:24:40 +000020
Mark Slee29050782006-09-29 00:12:30 +000021#include "concurrency/Monitor.h"
Marc Slemkod42a2c22006-08-10 03:30:18 +000022#include "TSocket.h"
23#include "TTransportException.h"
Mark Sleee8540632006-05-30 09:24:40 +000024
Mark Slee256bdc42007-11-27 08:42:19 +000025namespace facebook { namespace thrift { namespace transport {
Marc Slemko6f038a72006-08-03 18:58:09 +000026
Mark Sleee8540632006-05-30 09:24:40 +000027using namespace std;
28
Mark Slee29050782006-09-29 00:12:30 +000029// Global var to track total socket sys calls
Mark Slee8d7e1f62006-06-07 06:48:56 +000030uint32_t g_socket_syscalls = 0;
31
32/**
33 * TSocket implementation.
34 *
35 * @author Mark Slee <mcslee@facebook.com>
36 */
37
Mark Slee256bdc42007-11-27 08:42:19 +000038TSocket::TSocket(string host, int port) :
Mark Slee29050782006-09-29 00:12:30 +000039 host_(host),
40 port_(port),
Martin Kraemeree341cb2007-02-05 21:40:38 +000041 socket_(-1),
Mark Slee29050782006-09-29 00:12:30 +000042 connTimeout_(0),
43 sendTimeout_(0),
44 recvTimeout_(0),
45 lingerOn_(1),
46 lingerVal_(0),
Aditya Agarwale04475b2007-05-23 02:14:58 +000047 noDelay_(1),
48 maxRecvRetries_(5) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +000049 recvTimeval_.tv_sec = (int)(recvTimeout_/1000);
50 recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000);
Mark Sleee8540632006-05-30 09:24:40 +000051}
52
Mark Slee256bdc42007-11-27 08:42:19 +000053TSocket::TSocket() :
Aditya Agarwalebc99e02007-01-15 23:14:58 +000054 host_(""),
55 port_(0),
Martin Kraemeree341cb2007-02-05 21:40:38 +000056 socket_(-1),
Aditya Agarwalebc99e02007-01-15 23:14:58 +000057 connTimeout_(0),
58 sendTimeout_(0),
59 recvTimeout_(0),
60 lingerOn_(1),
61 lingerVal_(0),
Aditya Agarwale04475b2007-05-23 02:14:58 +000062 noDelay_(1),
63 maxRecvRetries_(5) {
Aditya Agarwalebc99e02007-01-15 23:14:58 +000064 recvTimeval_.tv_sec = (int)(recvTimeout_/1000);
65 recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000);
66}
67
Mark Slee29050782006-09-29 00:12:30 +000068TSocket::TSocket(int socket) :
69 host_(""),
70 port_(0),
71 socket_(socket),
72 connTimeout_(0),
73 sendTimeout_(0),
74 recvTimeout_(0),
75 lingerOn_(1),
76 lingerVal_(0),
Aditya Agarwale04475b2007-05-23 02:14:58 +000077 noDelay_(1),
78 maxRecvRetries_(5) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +000079 recvTimeval_.tv_sec = (int)(recvTimeout_/1000);
80 recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000);
Mark Slee29050782006-09-29 00:12:30 +000081}
Mark Slee256bdc42007-11-27 08:42:19 +000082
Mark Sleee8540632006-05-30 09:24:40 +000083TSocket::~TSocket() {
84 close();
85}
86
Mark Slee256bdc42007-11-27 08:42:19 +000087bool TSocket::isOpen() {
88 return (socket_ >= 0);
Mark Slee8d7e1f62006-06-07 06:48:56 +000089}
90
Mark Sleeb9ff32a2006-11-16 01:00:24 +000091bool TSocket::peek() {
92 if (!isOpen()) {
93 return false;
94 }
95 uint8_t buf;
96 int r = recv(socket_, &buf, 1, MSG_PEEK);
97 if (r == -1) {
David Reissbc3dddb2007-08-22 23:20:24 +000098 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +000099 GlobalOutput.perror("TSocket::peek() recv() " + getSocketInfo(), errno_copy);
David Reissbc3dddb2007-08-22 23:20:24 +0000100 throw TTransportException(TTransportException::UNKNOWN, "recv()", errno_copy);
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000101 }
102 return (r > 0);
103}
104
Mark Slee6d56eb92007-07-06 22:28:15 +0000105void TSocket::openConnection(struct addrinfo *res) {
Mark Sleea9848d72007-02-21 04:54:05 +0000106 if (isOpen()) {
107 throw TTransportException(TTransportException::ALREADY_OPEN);
108 }
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000109
Mark Slee6d56eb92007-07-06 22:28:15 +0000110 socket_ = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
Mark Sleee8540632006-05-30 09:24:40 +0000111 if (socket_ == -1) {
David Reissbc3dddb2007-08-22 23:20:24 +0000112 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000113 GlobalOutput.perror("TSocket::open() socket() " + getSocketInfo(), errno_copy);
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)) {
David Reiss9b209552008-04-08 06:26:05 +0000137 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000138 GlobalOutput.perror("TSocket::open() fcntl() " + getSocketInfo(), errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000139 throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed", errno_copy);
Mark Sleea5a783f2007-03-02 19:41:08 +0000140 }
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)) {
David Reiss9b209552008-04-08 06:26:05 +0000143 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000144 GlobalOutput.perror("TSocket::open() fcntl " + getSocketInfo(), errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000145 throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed", errno_copy);
Mark Sleea5a783f2007-03-02 19:41:08 +0000146 }
Mark Slee29050782006-09-29 00:12:30 +0000147 }
148
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
David Reiss9b209552008-04-08 06:26:05 +0000152 // success case
Mark Slee29050782006-09-29 00:12:30 +0000153 if (ret == 0) {
154 goto done;
155 }
156
157 if (errno != EINPROGRESS) {
David Reissbc3dddb2007-08-22 23:20:24 +0000158 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000159 GlobalOutput.perror("TSocket::open() connect() " + getSocketInfo(), errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000160 throw TTransportException(TTransportException::NOT_OPEN, "connect() failed", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000161 }
162
David Reiss22b18862008-04-08 06:25:45 +0000163
164 struct pollfd fds[1];
David Reissc88eb8c2008-06-11 01:18:54 +0000165 std::memset(fds, 0 , sizeof(fds));
David Reiss22b18862008-04-08 06:25:45 +0000166 fds[0].fd = socket_;
167 fds[0].events = POLLOUT;
168 ret = poll(fds, 1, connTimeout_);
Mark Slee29050782006-09-29 00:12:30 +0000169
170 if (ret > 0) {
David Reiss9b209552008-04-08 06:26:05 +0000171 // Ensure the socket is connected and that there are no errors set
Mark Slee29050782006-09-29 00:12:30 +0000172 int val;
173 socklen_t lon;
174 lon = sizeof(int);
175 int ret2 = getsockopt(socket_, SOL_SOCKET, SO_ERROR, (void *)&val, &lon);
176 if (ret2 == -1) {
David Reissbc3dddb2007-08-22 23:20:24 +0000177 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000178 GlobalOutput.perror("TSocket::open() getsockopt() " + getSocketInfo(), errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000179 throw TTransportException(TTransportException::NOT_OPEN, "getsockopt()", errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000180 }
David Reiss9b209552008-04-08 06:26:05 +0000181 // no errors on socket, go to town
Mark Slee29050782006-09-29 00:12:30 +0000182 if (val == 0) {
183 goto done;
184 }
David Reiss01e55c12008-07-13 22:18:51 +0000185 GlobalOutput.perror("TSocket::open() error on socket (after poll) " + getSocketInfo(), val);
David Reiss9b209552008-04-08 06:26:05 +0000186 throw TTransportException(TTransportException::NOT_OPEN, "socket open() error", val);
Mark Slee29050782006-09-29 00:12:30 +0000187 } else if (ret == 0) {
David Reiss9b209552008-04-08 06:26:05 +0000188 // socket timed out
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000189 string errStr = "TSocket::open() timed out " + getSocketInfo();
190 GlobalOutput(errStr.c_str());
David Reiss9b209552008-04-08 06:26:05 +0000191 throw TTransportException(TTransportException::NOT_OPEN, "open() timed out");
Mark Slee29050782006-09-29 00:12:30 +0000192 } else {
David Reiss9b209552008-04-08 06:26:05 +0000193 // error on poll()
David Reissbc3dddb2007-08-22 23:20:24 +0000194 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000195 GlobalOutput.perror("TSocket::open() poll() " + getSocketInfo(), errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000196 throw TTransportException(TTransportException::NOT_OPEN, "poll() failed", 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;
David Reiss9b209552008-04-08 06:26:05 +0000215 res = NULL;
216 res0 = NULL;
Mark Slee6d56eb92007-07-06 22:28:15 +0000217 int error;
David Reiss9b209552008-04-08 06:26:05 +0000218 char port[sizeof("65536")];
David Reissc88eb8c2008-06-11 01:18:54 +0000219 std::memset(&hints, 0, sizeof(hints));
Mark Slee6d56eb92007-07-06 22:28:15 +0000220 hints.ai_family = PF_UNSPEC;
221 hints.ai_socktype = SOCK_STREAM;
Mark Slee256bdc42007-11-27 08:42:19 +0000222 hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
Mark Slee6d56eb92007-07-06 22:28:15 +0000223 sprintf(port, "%d", port_);
Mark Slee256bdc42007-11-27 08:42:19 +0000224
Mark Sleec37b4c52007-12-05 23:03:37 +0000225 error = getaddrinfo(host_.c_str(), port, &hints, &res0);
226
Mark Slee6d56eb92007-07-06 22:28:15 +0000227 if (error) {
David Reiss9b209552008-04-08 06:26:05 +0000228 string errStr = "TSocket::open() getaddrinfo() " + getSocketInfo() + string(gai_strerror(error));
229 GlobalOutput(errStr.c_str());
Mark Slee6d56eb92007-07-06 22:28:15 +0000230 close();
231 throw TTransportException(TTransportException::NOT_OPEN, "Could not resolve host for client socket.");
232 }
Mark Slee256bdc42007-11-27 08:42:19 +0000233
Mark Slee6d56eb92007-07-06 22:28:15 +0000234 // 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_) {
Mark Slee256bdc42007-11-27 08:42:19 +0000276 // if a readTimeout is specified along with a max number of recv retries, then
Aditya Agarwale04475b2007-05-23 02:14:58 +0000277 // 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
Mark Slee256bdc42007-11-27 08:42:19 +0000282 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 Slee256bdc42007-11-27 08:42:19 +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 {
Mark Slee256bdc42007-11-27 08:42:19 +0000302 throw TTransportException(TTransportException::TIMED_OUT,
Aditya Agarwale04475b2007-05-23 02:14:58 +0000303 "EAGAIN (unavailable resources)");
304 }
305 } else {
306 // infer that timeout has been hit
Mark Slee256bdc42007-11-27 08:42:19 +0000307 throw TTransportException(TTransportException::TIMED_OUT,
Aditya Agarwale04475b2007-05-23 02:14:58 +0000308 "EAGAIN (timed out)");
309 }
Mark Sleee8540632006-05-30 09:24:40 +0000310 }
Mark Slee256bdc42007-11-27 08:42:19 +0000311
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 }
Mark Slee256bdc42007-11-27 08:42:19 +0000316
Mark Sleec4257802007-01-24 23:14:30 +0000317 // Now it's not a try again case, but a real probblez
David Reiss01e55c12008-07-13 22:18:51 +0000318 int errno_copy = errno; // Copy errno because we're allocating memory.
319 GlobalOutput.perror("TSocket::read() recv() " + getSocketInfo(), errno_copy);
Mark Sleec4257802007-01-24 23:14:30 +0000320
Mark Slee8d7e1f62006-06-07 06:48:56 +0000321 // If we disconnect with no linger time
322 if (errno == ECONNRESET) {
Mark Sleef9831082007-02-20 20:59:21 +0000323 throw TTransportException(TTransportException::NOT_OPEN, "ECONNRESET");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000324 }
Mark Slee256bdc42007-11-27 08:42:19 +0000325
Mark Slee8d7e1f62006-06-07 06:48:56 +0000326 // This ish isn't open
327 if (errno == ENOTCONN) {
Mark Sleef9831082007-02-20 20:59:21 +0000328 throw TTransportException(TTransportException::NOT_OPEN, "ENOTCONN");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000329 }
Mark Slee256bdc42007-11-27 08:42:19 +0000330
Mark Slee8d7e1f62006-06-07 06:48:56 +0000331 // Timed out!
332 if (errno == ETIMEDOUT) {
Mark Sleef9831082007-02-20 20:59:21 +0000333 throw TTransportException(TTransportException::TIMED_OUT, "ETIMEDOUT");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000334 }
Mark Slee256bdc42007-11-27 08:42:19 +0000335
Mark Slee8d7e1f62006-06-07 06:48:56 +0000336 // Some other error, whatevz
David Reiss01e55c12008-07-13 22:18:51 +0000337 errno_copy = errno;
338 throw TTransportException(TTransportException::UNKNOWN, "Unknown", errno_copy);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000339 }
Mark Slee256bdc42007-11-27 08:42:19 +0000340
Mark Slee8d7e1f62006-06-07 06:48:56 +0000341 // The remote host has closed the socket
342 if (got == 0) {
343 close();
344 return 0;
Mark Sleee8540632006-05-30 09:24:40 +0000345 }
Mark Slee256bdc42007-11-27 08:42:19 +0000346
Mark Sleee8540632006-05-30 09:24:40 +0000347 // 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;
Mark Slee256bdc42007-11-27 08:42:19 +0000357
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 Reiss9b209552008-04-08 06:26:05 +0000372 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000373 GlobalOutput.perror("TSocket::write() send() " + getSocketInfo(), errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000374
David Reissbc3dddb2007-08-22 23:20:24 +0000375 if (errno == EPIPE || errno == ECONNRESET || errno == ENOTCONN) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000376 close();
David Reiss9b209552008-04-08 06:26:05 +0000377 throw TTransportException(TTransportException::NOT_OPEN, "write() send()", errno_copy);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000378 }
379
David Reiss9b209552008-04-08 06:26:05 +0000380 throw TTransportException(TTransportException::UNKNOWN, "write() send()", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000381 }
Mark Slee256bdc42007-11-27 08:42:19 +0000382
Mark Sleee8540632006-05-30 09:24:40 +0000383 // 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
dweatherford14b0ed62007-10-19 01:03:32 +0000391std::string TSocket::getHost() {
392 return host_;
393}
394
395int TSocket::getPort() {
396 return port_;
397}
398
Aditya Agarwalebc99e02007-01-15 23:14:58 +0000399void TSocket::setHost(string host) {
400 host_ = host;
401}
402
403void TSocket::setPort(int port) {
404 port_ = port;
405}
406
Mark Slee8d7e1f62006-06-07 06:48:56 +0000407void TSocket::setLinger(bool on, int linger) {
Mark Slee29050782006-09-29 00:12:30 +0000408 lingerOn_ = on;
409 lingerVal_ = linger;
Martin Kraemeree341cb2007-02-05 21:40:38 +0000410 if (socket_ < 0) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000411 return;
412 }
413
Mark Slee29050782006-09-29 00:12:30 +0000414 struct linger l = {(lingerOn_ ? 1 : 0), lingerVal_};
415 int ret = setsockopt(socket_, SOL_SOCKET, SO_LINGER, &l, sizeof(l));
416 if (ret == -1) {
David Reiss01e55c12008-07-13 22:18:51 +0000417 int errno_copy = errno; // Copy errno because we're allocating memory.
418 GlobalOutput.perror("TSocket::setLinger() setsockopt() " + getSocketInfo(), errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000419 }
Mark Sleee8540632006-05-30 09:24:40 +0000420}
421
Mark Slee8d7e1f62006-06-07 06:48:56 +0000422void TSocket::setNoDelay(bool noDelay) {
Mark Slee29050782006-09-29 00:12:30 +0000423 noDelay_ = noDelay;
Martin Kraemeree341cb2007-02-05 21:40:38 +0000424 if (socket_ < 0) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000425 return;
426 }
427
Mark Sleee8540632006-05-30 09:24:40 +0000428 // Set socket to NODELAY
Mark Slee29050782006-09-29 00:12:30 +0000429 int v = noDelay_ ? 1 : 0;
430 int ret = setsockopt(socket_, IPPROTO_TCP, TCP_NODELAY, &v, sizeof(v));
431 if (ret == -1) {
David Reiss01e55c12008-07-13 22:18:51 +0000432 int errno_copy = errno; // Copy errno because we're allocating memory.
433 GlobalOutput.perror("TSocket::setNoDelay() setsockopt() " + getSocketInfo(), errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000434 }
Mark Sleee8540632006-05-30 09:24:40 +0000435}
Mark Slee29050782006-09-29 00:12:30 +0000436
437void TSocket::setConnTimeout(int ms) {
438 connTimeout_ = ms;
439}
440
441void TSocket::setRecvTimeout(int ms) {
Aditya Agarwalc31769c2007-12-11 22:23:51 +0000442 if (ms < 0) {
443 char errBuf[512];
444 sprintf(errBuf, "TSocket::setRecvTimeout with negative input: %d\n", ms);
445 GlobalOutput(errBuf);
446 return;
447 }
Mark Slee29050782006-09-29 00:12:30 +0000448 recvTimeout_ = ms;
Aditya Agarwalc31769c2007-12-11 22:23:51 +0000449
Martin Kraemeree341cb2007-02-05 21:40:38 +0000450 if (socket_ < 0) {
Mark Slee29050782006-09-29 00:12:30 +0000451 return;
452 }
453
Aditya Agarwalc31769c2007-12-11 22:23:51 +0000454 recvTimeval_.tv_sec = (int)(recvTimeout_/1000);
455 recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000);
456
David Reiss22b18862008-04-08 06:25:45 +0000457 // Copy because poll may modify
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000458 struct timeval r = recvTimeval_;
Mark Slee29050782006-09-29 00:12:30 +0000459 int ret = setsockopt(socket_, SOL_SOCKET, SO_RCVTIMEO, &r, sizeof(r));
460 if (ret == -1) {
David Reiss01e55c12008-07-13 22:18:51 +0000461 int errno_copy = errno; // Copy errno because we're allocating memory.
462 GlobalOutput.perror("TSocket::setRecvTimeout() setsockopt() " + getSocketInfo(), errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000463 }
464}
465
466void TSocket::setSendTimeout(int ms) {
Aditya Agarwalc31769c2007-12-11 22:23:51 +0000467 if (ms < 0) {
468 char errBuf[512];
469 sprintf(errBuf, "TSocket::setSendTimeout with negative input: %d\n", ms);
470 GlobalOutput(errBuf);
471 return;
472 }
Mark Slee29050782006-09-29 00:12:30 +0000473 sendTimeout_ = ms;
Aditya Agarwalc31769c2007-12-11 22:23:51 +0000474
Martin Kraemeree341cb2007-02-05 21:40:38 +0000475 if (socket_ < 0) {
Mark Slee29050782006-09-29 00:12:30 +0000476 return;
477 }
Mark Slee256bdc42007-11-27 08:42:19 +0000478
Mark Slee29050782006-09-29 00:12:30 +0000479 struct timeval s = {(int)(sendTimeout_/1000),
480 (int)((sendTimeout_%1000)*1000)};
481 int ret = setsockopt(socket_, SOL_SOCKET, SO_SNDTIMEO, &s, sizeof(s));
482 if (ret == -1) {
David Reiss01e55c12008-07-13 22:18:51 +0000483 int errno_copy = errno; // Copy errno because we're allocating memory.
484 GlobalOutput.perror("TSocket::setSendTimeout() setsockopt() " + getSocketInfo(), errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000485 }
486}
487
Aditya Agarwale04475b2007-05-23 02:14:58 +0000488void TSocket::setMaxRecvRetries(int maxRecvRetries) {
489 maxRecvRetries_ = maxRecvRetries;
490}
491
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000492string TSocket::getSocketInfo() {
493 std::ostringstream oss;
494 oss << "<Host: " << host_ << " Port: " << port_ << ">";
495 return oss.str();
496}
497
Mark Sleeb4552922007-11-28 00:12:11 +0000498std::string TSocket::getPeerHost() {
499 if (peerHost_.empty()) {
500 struct sockaddr_storage addr;
501 socklen_t addrLen = sizeof(addr);
502
503 if (socket_ < 0) {
504 return host_;
505 }
506
507 int rv = getpeername(socket_, (sockaddr*) &addr, &addrLen);
508
509 if (rv != 0) {
510 return peerHost_;
511 }
512
513 char clienthost[NI_MAXHOST];
514 char clientservice[NI_MAXSERV];
515
516 getnameinfo((sockaddr*) &addr, addrLen,
517 clienthost, sizeof(clienthost),
518 clientservice, sizeof(clientservice), 0);
519
520 peerHost_ = clienthost;
521 }
522 return peerHost_;
523}
524
525std::string TSocket::getPeerAddress() {
526 if (peerAddress_.empty()) {
527 struct sockaddr_storage addr;
528 socklen_t addrLen = sizeof(addr);
529
530 if (socket_ < 0) {
531 return peerAddress_;
532 }
533
534 int rv = getpeername(socket_, (sockaddr*) &addr, &addrLen);
535
536 if (rv != 0) {
537 return peerAddress_;
538 }
539
540 char clienthost[NI_MAXHOST];
541 char clientservice[NI_MAXSERV];
542
543 getnameinfo((sockaddr*) &addr, addrLen,
544 clienthost, sizeof(clienthost),
545 clientservice, sizeof(clientservice),
546 NI_NUMERICHOST|NI_NUMERICSERV);
547
548 peerAddress_ = clienthost;
549 peerPort_ = std::atoi(clientservice);
550 }
551 return peerAddress_;
552}
553
554int TSocket::getPeerPort() {
555 getPeerAddress();
556 return peerPort_;
557}
558
Marc Slemko6f038a72006-08-03 18:58:09 +0000559}}} // facebook::thrift::transport