blob: b59bc819031951f5b4399605897c724edd0d6244 [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
T Jake Lucianib5e62212009-01-31 22:36:20 +000025namespace apache { 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 *
Mark Slee8d7e1f62006-06-07 06:48:56 +000035 */
36
Mark Slee256bdc42007-11-27 08:42:19 +000037TSocket::TSocket(string host, int port) :
Mark Slee29050782006-09-29 00:12:30 +000038 host_(host),
39 port_(port),
Martin Kraemeree341cb2007-02-05 21:40:38 +000040 socket_(-1),
Mark Slee29050782006-09-29 00:12:30 +000041 connTimeout_(0),
42 sendTimeout_(0),
43 recvTimeout_(0),
44 lingerOn_(1),
45 lingerVal_(0),
Aditya Agarwale04475b2007-05-23 02:14:58 +000046 noDelay_(1),
47 maxRecvRetries_(5) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +000048 recvTimeval_.tv_sec = (int)(recvTimeout_/1000);
49 recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000);
Mark Sleee8540632006-05-30 09:24:40 +000050}
51
Mark Slee256bdc42007-11-27 08:42:19 +000052TSocket::TSocket() :
Aditya Agarwalebc99e02007-01-15 23:14:58 +000053 host_(""),
54 port_(0),
Martin Kraemeree341cb2007-02-05 21:40:38 +000055 socket_(-1),
Aditya Agarwalebc99e02007-01-15 23:14:58 +000056 connTimeout_(0),
57 sendTimeout_(0),
58 recvTimeout_(0),
59 lingerOn_(1),
60 lingerVal_(0),
Aditya Agarwale04475b2007-05-23 02:14:58 +000061 noDelay_(1),
62 maxRecvRetries_(5) {
Aditya Agarwalebc99e02007-01-15 23:14:58 +000063 recvTimeval_.tv_sec = (int)(recvTimeout_/1000);
64 recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000);
65}
66
Mark Slee29050782006-09-29 00:12:30 +000067TSocket::TSocket(int socket) :
68 host_(""),
69 port_(0),
70 socket_(socket),
71 connTimeout_(0),
72 sendTimeout_(0),
73 recvTimeout_(0),
74 lingerOn_(1),
75 lingerVal_(0),
Aditya Agarwale04475b2007-05-23 02:14:58 +000076 noDelay_(1),
77 maxRecvRetries_(5) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +000078 recvTimeval_.tv_sec = (int)(recvTimeout_/1000);
79 recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000);
Mark Slee29050782006-09-29 00:12:30 +000080}
Mark Slee256bdc42007-11-27 08:42:19 +000081
Mark Sleee8540632006-05-30 09:24:40 +000082TSocket::~TSocket() {
83 close();
84}
85
Mark Slee256bdc42007-11-27 08:42:19 +000086bool TSocket::isOpen() {
87 return (socket_ >= 0);
Mark Slee8d7e1f62006-06-07 06:48:56 +000088}
89
Mark Sleeb9ff32a2006-11-16 01:00:24 +000090bool TSocket::peek() {
91 if (!isOpen()) {
92 return false;
93 }
94 uint8_t buf;
95 int r = recv(socket_, &buf, 1, MSG_PEEK);
96 if (r == -1) {
David Reissbc3dddb2007-08-22 23:20:24 +000097 int errno_copy = errno;
Kevin Clark022b2242009-03-05 21:05:37 +000098 #ifdef __FreeBSD__
99 /* shigin:
100 * freebsd returns -1 and ECONNRESET if socket was closed by
101 * the other side
102 */
103 if (errno_copy == ECONNRESET)
104 {
105 close();
106 return false;
107 }
108 #endif
David Reiss01e55c12008-07-13 22:18:51 +0000109 GlobalOutput.perror("TSocket::peek() recv() " + getSocketInfo(), errno_copy);
David Reissbc3dddb2007-08-22 23:20:24 +0000110 throw TTransportException(TTransportException::UNKNOWN, "recv()", errno_copy);
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000111 }
112 return (r > 0);
113}
114
Mark Slee6d56eb92007-07-06 22:28:15 +0000115void TSocket::openConnection(struct addrinfo *res) {
Mark Sleea9848d72007-02-21 04:54:05 +0000116 if (isOpen()) {
117 throw TTransportException(TTransportException::ALREADY_OPEN);
118 }
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000119
Mark Slee6d56eb92007-07-06 22:28:15 +0000120 socket_ = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
Mark Sleee8540632006-05-30 09:24:40 +0000121 if (socket_ == -1) {
David Reissbc3dddb2007-08-22 23:20:24 +0000122 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000123 GlobalOutput.perror("TSocket::open() socket() " + getSocketInfo(), errno_copy);
David Reissbc3dddb2007-08-22 23:20:24 +0000124 throw TTransportException(TTransportException::NOT_OPEN, "socket()", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000125 }
Mark Slee29050782006-09-29 00:12:30 +0000126
127 // Send timeout
128 if (sendTimeout_ > 0) {
129 setSendTimeout(sendTimeout_);
130 }
131
132 // Recv timeout
133 if (recvTimeout_ > 0) {
134 setRecvTimeout(recvTimeout_);
135 }
136
137 // Linger
138 setLinger(lingerOn_, lingerVal_);
139
140 // No delay
141 setNoDelay(noDelay_);
142
Mark Slee29050782006-09-29 00:12:30 +0000143 // Set the socket to be non blocking for connect if a timeout exists
Mark Slee256bdc42007-11-27 08:42:19 +0000144 int flags = fcntl(socket_, F_GETFL, 0);
Mark Slee29050782006-09-29 00:12:30 +0000145 if (connTimeout_ > 0) {
Mark Sleea5a783f2007-03-02 19:41:08 +0000146 if (-1 == fcntl(socket_, F_SETFL, flags | O_NONBLOCK)) {
David Reiss9b209552008-04-08 06:26:05 +0000147 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000148 GlobalOutput.perror("TSocket::open() fcntl() " + getSocketInfo(), errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000149 throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed", errno_copy);
Mark Sleea5a783f2007-03-02 19:41:08 +0000150 }
Mark Slee29050782006-09-29 00:12:30 +0000151 } else {
Mark Sleea5a783f2007-03-02 19:41:08 +0000152 if (-1 == fcntl(socket_, F_SETFL, flags & ~O_NONBLOCK)) {
David Reiss9b209552008-04-08 06:26:05 +0000153 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000154 GlobalOutput.perror("TSocket::open() fcntl " + getSocketInfo(), errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000155 throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed", errno_copy);
Mark Sleea5a783f2007-03-02 19:41:08 +0000156 }
Mark Slee29050782006-09-29 00:12:30 +0000157 }
158
Mark Sleee8540632006-05-30 09:24:40 +0000159 // Connect the socket
Mark Slee6d56eb92007-07-06 22:28:15 +0000160 int ret = connect(socket_, res->ai_addr, res->ai_addrlen);
Mark Slee256bdc42007-11-27 08:42:19 +0000161
David Reiss9b209552008-04-08 06:26:05 +0000162 // success case
Mark Slee29050782006-09-29 00:12:30 +0000163 if (ret == 0) {
164 goto done;
165 }
166
167 if (errno != EINPROGRESS) {
David Reissbc3dddb2007-08-22 23:20:24 +0000168 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000169 GlobalOutput.perror("TSocket::open() connect() " + getSocketInfo(), errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000170 throw TTransportException(TTransportException::NOT_OPEN, "connect() failed", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000171 }
172
David Reiss22b18862008-04-08 06:25:45 +0000173
174 struct pollfd fds[1];
David Reissc88eb8c2008-06-11 01:18:54 +0000175 std::memset(fds, 0 , sizeof(fds));
David Reiss22b18862008-04-08 06:25:45 +0000176 fds[0].fd = socket_;
177 fds[0].events = POLLOUT;
178 ret = poll(fds, 1, connTimeout_);
Mark Slee29050782006-09-29 00:12:30 +0000179
180 if (ret > 0) {
David Reiss9b209552008-04-08 06:26:05 +0000181 // Ensure the socket is connected and that there are no errors set
Mark Slee29050782006-09-29 00:12:30 +0000182 int val;
183 socklen_t lon;
184 lon = sizeof(int);
185 int ret2 = getsockopt(socket_, SOL_SOCKET, SO_ERROR, (void *)&val, &lon);
186 if (ret2 == -1) {
David Reissbc3dddb2007-08-22 23:20:24 +0000187 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000188 GlobalOutput.perror("TSocket::open() getsockopt() " + getSocketInfo(), errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000189 throw TTransportException(TTransportException::NOT_OPEN, "getsockopt()", errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000190 }
David Reiss9b209552008-04-08 06:26:05 +0000191 // no errors on socket, go to town
Mark Slee29050782006-09-29 00:12:30 +0000192 if (val == 0) {
193 goto done;
194 }
David Reiss01e55c12008-07-13 22:18:51 +0000195 GlobalOutput.perror("TSocket::open() error on socket (after poll) " + getSocketInfo(), val);
David Reiss9b209552008-04-08 06:26:05 +0000196 throw TTransportException(TTransportException::NOT_OPEN, "socket open() error", val);
Mark Slee29050782006-09-29 00:12:30 +0000197 } else if (ret == 0) {
David Reiss9b209552008-04-08 06:26:05 +0000198 // socket timed out
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000199 string errStr = "TSocket::open() timed out " + getSocketInfo();
200 GlobalOutput(errStr.c_str());
David Reiss9b209552008-04-08 06:26:05 +0000201 throw TTransportException(TTransportException::NOT_OPEN, "open() timed out");
Mark Slee29050782006-09-29 00:12:30 +0000202 } else {
David Reiss9b209552008-04-08 06:26:05 +0000203 // error on poll()
David Reissbc3dddb2007-08-22 23:20:24 +0000204 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000205 GlobalOutput.perror("TSocket::open() poll() " + getSocketInfo(), errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000206 throw TTransportException(TTransportException::NOT_OPEN, "poll() failed", errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000207 }
208
209 done:
210 // Set socket back to normal mode (blocking)
211 fcntl(socket_, F_SETFL, flags);
Mark Sleee8540632006-05-30 09:24:40 +0000212}
213
Mark Slee6d56eb92007-07-06 22:28:15 +0000214void TSocket::open() {
215 if (isOpen()) {
216 throw TTransportException(TTransportException::ALREADY_OPEN);
217 }
218
219 // Validate port number
220 if (port_ < 0 || port_ > 65536) {
221 throw TTransportException(TTransportException::NOT_OPEN, "Specified port is invalid");
222 }
223
224 struct addrinfo hints, *res, *res0;
David Reiss9b209552008-04-08 06:26:05 +0000225 res = NULL;
226 res0 = NULL;
Mark Slee6d56eb92007-07-06 22:28:15 +0000227 int error;
David Reiss9b209552008-04-08 06:26:05 +0000228 char port[sizeof("65536")];
David Reissc88eb8c2008-06-11 01:18:54 +0000229 std::memset(&hints, 0, sizeof(hints));
Mark Slee6d56eb92007-07-06 22:28:15 +0000230 hints.ai_family = PF_UNSPEC;
231 hints.ai_socktype = SOCK_STREAM;
Mark Slee256bdc42007-11-27 08:42:19 +0000232 hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
Mark Slee6d56eb92007-07-06 22:28:15 +0000233 sprintf(port, "%d", port_);
Mark Slee256bdc42007-11-27 08:42:19 +0000234
Mark Sleec37b4c52007-12-05 23:03:37 +0000235 error = getaddrinfo(host_.c_str(), port, &hints, &res0);
236
Mark Slee6d56eb92007-07-06 22:28:15 +0000237 if (error) {
David Reiss9b209552008-04-08 06:26:05 +0000238 string errStr = "TSocket::open() getaddrinfo() " + getSocketInfo() + string(gai_strerror(error));
239 GlobalOutput(errStr.c_str());
Mark Slee6d56eb92007-07-06 22:28:15 +0000240 close();
241 throw TTransportException(TTransportException::NOT_OPEN, "Could not resolve host for client socket.");
242 }
Mark Slee256bdc42007-11-27 08:42:19 +0000243
Mark Slee6d56eb92007-07-06 22:28:15 +0000244 // Cycle through all the returned addresses until one
245 // connects or push the exception up.
246 for (res = res0; res; res = res->ai_next) {
247 try {
248 openConnection(res);
249 break;
250 } catch (TTransportException& ttx) {
251 if (res->ai_next) {
252 close();
253 } else {
254 close();
Mark Slee85287d32007-07-09 19:50:30 +0000255 freeaddrinfo(res0); // cleanup on failure
Mark Slee6d56eb92007-07-06 22:28:15 +0000256 throw;
257 }
258 }
259 }
Mark Slee85287d32007-07-09 19:50:30 +0000260
261 // Free address structure memory
262 freeaddrinfo(res0);
Mark Slee6d56eb92007-07-06 22:28:15 +0000263}
264
Mark Sleee8540632006-05-30 09:24:40 +0000265void TSocket::close() {
Martin Kraemeree341cb2007-02-05 21:40:38 +0000266 if (socket_ >= 0) {
Mark Sleee8540632006-05-30 09:24:40 +0000267 shutdown(socket_, SHUT_RDWR);
268 ::close(socket_);
269 }
Martin Kraemeree341cb2007-02-05 21:40:38 +0000270 socket_ = -1;
Mark Sleee8540632006-05-30 09:24:40 +0000271}
272
Mark Slee8d7e1f62006-06-07 06:48:56 +0000273uint32_t TSocket::read(uint8_t* buf, uint32_t len) {
Martin Kraemeree341cb2007-02-05 21:40:38 +0000274 if (socket_ < 0) {
Mark Sleef9831082007-02-20 20:59:21 +0000275 throw TTransportException(TTransportException::NOT_OPEN, "Called read on non-open socket");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000276 }
Mark Sleee8540632006-05-30 09:24:40 +0000277
Aditya Agarwale04475b2007-05-23 02:14:58 +0000278 int32_t retries = 0;
279
280 // EAGAIN can be signalled both when a timeout has occurred and when
281 // the system is out of resources (an awesome undocumented feature).
282 // The following is an approximation of the time interval under which
283 // EAGAIN is taken to indicate an out of resources error.
284 uint32_t eagainThresholdMicros = 0;
285 if (recvTimeout_) {
Mark Slee256bdc42007-11-27 08:42:19 +0000286 // if a readTimeout is specified along with a max number of recv retries, then
Aditya Agarwale04475b2007-05-23 02:14:58 +0000287 // the threshold will ensure that the read timeout is not exceeded even in the
288 // case of resource errors
289 eagainThresholdMicros = (recvTimeout_*1000)/ ((maxRecvRetries_>0) ? maxRecvRetries_ : 2);
290 }
291
Mark Slee256bdc42007-11-27 08:42:19 +0000292 try_again:
Mark Slee8d7e1f62006-06-07 06:48:56 +0000293 // Read from the socket
Aditya Agarwale04475b2007-05-23 02:14:58 +0000294 struct timeval begin;
295 gettimeofday(&begin, NULL);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000296 int got = recv(socket_, buf, len, 0);
Kevin Clark022b2242009-03-05 21:05:37 +0000297 int errno_copy = errno; //gettimeofday can change errno
Aditya Agarwale04475b2007-05-23 02:14:58 +0000298 struct timeval end;
299 gettimeofday(&end, NULL);
300 uint32_t readElapsedMicros = (((end.tv_sec - begin.tv_sec) * 1000 * 1000)
301 + (((uint64_t)(end.tv_usec - begin.tv_usec))));
Mark Slee8d7e1f62006-06-07 06:48:56 +0000302 ++g_socket_syscalls;
Aditya Agarwale04475b2007-05-23 02:14:58 +0000303
Mark Slee8d7e1f62006-06-07 06:48:56 +0000304 // Check for error on read
Mark Slee256bdc42007-11-27 08:42:19 +0000305 if (got < 0) {
Kevin Clark022b2242009-03-05 21:05:37 +0000306 if (errno_copy == EAGAIN) {
Aditya Agarwale04475b2007-05-23 02:14:58 +0000307 // check if this is the lack of resources or timeout case
308 if (!eagainThresholdMicros || (readElapsedMicros < eagainThresholdMicros)) {
309 if (retries++ < maxRecvRetries_) {
310 usleep(50);
311 goto try_again;
312 } else {
Mark Slee256bdc42007-11-27 08:42:19 +0000313 throw TTransportException(TTransportException::TIMED_OUT,
Aditya Agarwale04475b2007-05-23 02:14:58 +0000314 "EAGAIN (unavailable resources)");
315 }
316 } else {
317 // infer that timeout has been hit
Mark Slee256bdc42007-11-27 08:42:19 +0000318 throw TTransportException(TTransportException::TIMED_OUT,
Aditya Agarwale04475b2007-05-23 02:14:58 +0000319 "EAGAIN (timed out)");
320 }
Mark Sleee8540632006-05-30 09:24:40 +0000321 }
Mark Slee256bdc42007-11-27 08:42:19 +0000322
Mark Slee8d7e1f62006-06-07 06:48:56 +0000323 // If interrupted, try again
Kevin Clark022b2242009-03-05 21:05:37 +0000324 if (errno_copy == EINTR && retries++ < maxRecvRetries_) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000325 goto try_again;
Mark Sleee8540632006-05-30 09:24:40 +0000326 }
Mark Slee256bdc42007-11-27 08:42:19 +0000327
Mark Sleec4257802007-01-24 23:14:30 +0000328 // Now it's not a try again case, but a real probblez
David Reiss01e55c12008-07-13 22:18:51 +0000329 GlobalOutput.perror("TSocket::read() recv() " + getSocketInfo(), errno_copy);
Mark Sleec4257802007-01-24 23:14:30 +0000330
Mark Slee8d7e1f62006-06-07 06:48:56 +0000331 // If we disconnect with no linger time
Kevin Clark022b2242009-03-05 21:05:37 +0000332 if (errno_copy == ECONNRESET) {
333 #ifdef __FreeBSD__
334 /* shigin: freebsd doesn't follow POSIX semantic of recv and fails with
335 * ECONNRESET if peer performed shutdown
336 */
337 close();
338 return 0;
339 #else
Mark Sleef9831082007-02-20 20:59:21 +0000340 throw TTransportException(TTransportException::NOT_OPEN, "ECONNRESET");
Kevin Clark022b2242009-03-05 21:05:37 +0000341 #endif
Mark Slee8d7e1f62006-06-07 06:48:56 +0000342 }
Mark Slee256bdc42007-11-27 08:42:19 +0000343
Mark Slee8d7e1f62006-06-07 06:48:56 +0000344 // This ish isn't open
Kevin Clark022b2242009-03-05 21:05:37 +0000345 if (errno_copy == ENOTCONN) {
Mark Sleef9831082007-02-20 20:59:21 +0000346 throw TTransportException(TTransportException::NOT_OPEN, "ENOTCONN");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000347 }
Mark Slee256bdc42007-11-27 08:42:19 +0000348
Mark Slee8d7e1f62006-06-07 06:48:56 +0000349 // Timed out!
Kevin Clark022b2242009-03-05 21:05:37 +0000350 if (errno_copy == ETIMEDOUT) {
Mark Sleef9831082007-02-20 20:59:21 +0000351 throw TTransportException(TTransportException::TIMED_OUT, "ETIMEDOUT");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000352 }
Mark Slee256bdc42007-11-27 08:42:19 +0000353
Mark Slee8d7e1f62006-06-07 06:48:56 +0000354 // Some other error, whatevz
David Reiss01e55c12008-07-13 22:18:51 +0000355 throw TTransportException(TTransportException::UNKNOWN, "Unknown", errno_copy);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000356 }
Mark Slee256bdc42007-11-27 08:42:19 +0000357
Mark Slee8d7e1f62006-06-07 06:48:56 +0000358 // The remote host has closed the socket
359 if (got == 0) {
360 close();
361 return 0;
Mark Sleee8540632006-05-30 09:24:40 +0000362 }
Mark Slee256bdc42007-11-27 08:42:19 +0000363
Mark Sleee8540632006-05-30 09:24:40 +0000364 // Pack data into string
Mark Slee8d7e1f62006-06-07 06:48:56 +0000365 return got;
Mark Sleee8540632006-05-30 09:24:40 +0000366}
367
Mark Slee8d7e1f62006-06-07 06:48:56 +0000368void TSocket::write(const uint8_t* buf, uint32_t len) {
Martin Kraemeree341cb2007-02-05 21:40:38 +0000369 if (socket_ < 0) {
Mark Sleef9831082007-02-20 20:59:21 +0000370 throw TTransportException(TTransportException::NOT_OPEN, "Called write on non-open socket");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000371 }
372
Mark Sleee8540632006-05-30 09:24:40 +0000373 uint32_t sent = 0;
Mark Slee256bdc42007-11-27 08:42:19 +0000374
Mark Slee8d7e1f62006-06-07 06:48:56 +0000375 while (sent < len) {
Marc Slemko9d4a3e22006-07-21 19:53:48 +0000376
377 int flags = 0;
Mark Slee29050782006-09-29 00:12:30 +0000378 #ifdef MSG_NOSIGNAL
Mark Slee8d7e1f62006-06-07 06:48:56 +0000379 // Note the use of MSG_NOSIGNAL to suppress SIGPIPE errors, instead we
380 // check for the EPIPE return condition and close the socket in that case
Marc Slemko9d4a3e22006-07-21 19:53:48 +0000381 flags |= MSG_NOSIGNAL;
Mark Slee29050782006-09-29 00:12:30 +0000382 #endif // ifdef MSG_NOSIGNAL
Marc Slemko9d4a3e22006-07-21 19:53:48 +0000383
384 int b = send(socket_, buf + sent, len - sent, flags);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000385 ++g_socket_syscalls;
386
Mark Sleee8540632006-05-30 09:24:40 +0000387 // Fail on a send error
388 if (b < 0) {
David Reiss9b209552008-04-08 06:26:05 +0000389 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000390 GlobalOutput.perror("TSocket::write() send() " + getSocketInfo(), errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000391
David Reissbc3dddb2007-08-22 23:20:24 +0000392 if (errno == EPIPE || errno == ECONNRESET || errno == ENOTCONN) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000393 close();
David Reiss9b209552008-04-08 06:26:05 +0000394 throw TTransportException(TTransportException::NOT_OPEN, "write() send()", errno_copy);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000395 }
396
David Reiss9b209552008-04-08 06:26:05 +0000397 throw TTransportException(TTransportException::UNKNOWN, "write() send()", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000398 }
Mark Slee256bdc42007-11-27 08:42:19 +0000399
Mark Sleee8540632006-05-30 09:24:40 +0000400 // Fail on blocked send
401 if (b == 0) {
Mark Sleef9831082007-02-20 20:59:21 +0000402 throw TTransportException(TTransportException::NOT_OPEN, "Socket send returned 0.");
Mark Sleee8540632006-05-30 09:24:40 +0000403 }
Mark Sleee8540632006-05-30 09:24:40 +0000404 sent += b;
405 }
406}
407
dweatherford14b0ed62007-10-19 01:03:32 +0000408std::string TSocket::getHost() {
409 return host_;
410}
411
412int TSocket::getPort() {
413 return port_;
414}
415
Aditya Agarwalebc99e02007-01-15 23:14:58 +0000416void TSocket::setHost(string host) {
417 host_ = host;
418}
419
420void TSocket::setPort(int port) {
421 port_ = port;
422}
423
Mark Slee8d7e1f62006-06-07 06:48:56 +0000424void TSocket::setLinger(bool on, int linger) {
Mark Slee29050782006-09-29 00:12:30 +0000425 lingerOn_ = on;
426 lingerVal_ = linger;
Martin Kraemeree341cb2007-02-05 21:40:38 +0000427 if (socket_ < 0) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000428 return;
429 }
430
Mark Slee29050782006-09-29 00:12:30 +0000431 struct linger l = {(lingerOn_ ? 1 : 0), lingerVal_};
432 int ret = setsockopt(socket_, SOL_SOCKET, SO_LINGER, &l, sizeof(l));
433 if (ret == -1) {
David Reiss01e55c12008-07-13 22:18:51 +0000434 int errno_copy = errno; // Copy errno because we're allocating memory.
435 GlobalOutput.perror("TSocket::setLinger() setsockopt() " + getSocketInfo(), errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000436 }
Mark Sleee8540632006-05-30 09:24:40 +0000437}
438
Mark Slee8d7e1f62006-06-07 06:48:56 +0000439void TSocket::setNoDelay(bool noDelay) {
Mark Slee29050782006-09-29 00:12:30 +0000440 noDelay_ = noDelay;
Martin Kraemeree341cb2007-02-05 21:40:38 +0000441 if (socket_ < 0) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000442 return;
443 }
444
Mark Sleee8540632006-05-30 09:24:40 +0000445 // Set socket to NODELAY
Mark Slee29050782006-09-29 00:12:30 +0000446 int v = noDelay_ ? 1 : 0;
447 int ret = setsockopt(socket_, IPPROTO_TCP, TCP_NODELAY, &v, sizeof(v));
448 if (ret == -1) {
David Reiss01e55c12008-07-13 22:18:51 +0000449 int errno_copy = errno; // Copy errno because we're allocating memory.
450 GlobalOutput.perror("TSocket::setNoDelay() setsockopt() " + getSocketInfo(), errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000451 }
Mark Sleee8540632006-05-30 09:24:40 +0000452}
Mark Slee29050782006-09-29 00:12:30 +0000453
454void TSocket::setConnTimeout(int ms) {
455 connTimeout_ = ms;
456}
457
458void TSocket::setRecvTimeout(int ms) {
Aditya Agarwalc31769c2007-12-11 22:23:51 +0000459 if (ms < 0) {
460 char errBuf[512];
461 sprintf(errBuf, "TSocket::setRecvTimeout with negative input: %d\n", ms);
462 GlobalOutput(errBuf);
463 return;
464 }
Mark Slee29050782006-09-29 00:12:30 +0000465 recvTimeout_ = ms;
Aditya Agarwalc31769c2007-12-11 22:23:51 +0000466
Martin Kraemeree341cb2007-02-05 21:40:38 +0000467 if (socket_ < 0) {
Mark Slee29050782006-09-29 00:12:30 +0000468 return;
469 }
470
Aditya Agarwalc31769c2007-12-11 22:23:51 +0000471 recvTimeval_.tv_sec = (int)(recvTimeout_/1000);
472 recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000);
473
David Reiss22b18862008-04-08 06:25:45 +0000474 // Copy because poll may modify
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000475 struct timeval r = recvTimeval_;
Mark Slee29050782006-09-29 00:12:30 +0000476 int ret = setsockopt(socket_, SOL_SOCKET, SO_RCVTIMEO, &r, sizeof(r));
477 if (ret == -1) {
David Reiss01e55c12008-07-13 22:18:51 +0000478 int errno_copy = errno; // Copy errno because we're allocating memory.
479 GlobalOutput.perror("TSocket::setRecvTimeout() setsockopt() " + getSocketInfo(), errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000480 }
481}
482
483void TSocket::setSendTimeout(int ms) {
Aditya Agarwalc31769c2007-12-11 22:23:51 +0000484 if (ms < 0) {
485 char errBuf[512];
486 sprintf(errBuf, "TSocket::setSendTimeout with negative input: %d\n", ms);
487 GlobalOutput(errBuf);
488 return;
489 }
Mark Slee29050782006-09-29 00:12:30 +0000490 sendTimeout_ = ms;
Aditya Agarwalc31769c2007-12-11 22:23:51 +0000491
Martin Kraemeree341cb2007-02-05 21:40:38 +0000492 if (socket_ < 0) {
Mark Slee29050782006-09-29 00:12:30 +0000493 return;
494 }
Mark Slee256bdc42007-11-27 08:42:19 +0000495
Mark Slee29050782006-09-29 00:12:30 +0000496 struct timeval s = {(int)(sendTimeout_/1000),
497 (int)((sendTimeout_%1000)*1000)};
498 int ret = setsockopt(socket_, SOL_SOCKET, SO_SNDTIMEO, &s, sizeof(s));
499 if (ret == -1) {
David Reiss01e55c12008-07-13 22:18:51 +0000500 int errno_copy = errno; // Copy errno because we're allocating memory.
501 GlobalOutput.perror("TSocket::setSendTimeout() setsockopt() " + getSocketInfo(), errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000502 }
503}
504
Aditya Agarwale04475b2007-05-23 02:14:58 +0000505void TSocket::setMaxRecvRetries(int maxRecvRetries) {
506 maxRecvRetries_ = maxRecvRetries;
507}
508
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000509string TSocket::getSocketInfo() {
510 std::ostringstream oss;
511 oss << "<Host: " << host_ << " Port: " << port_ << ">";
512 return oss.str();
513}
514
Mark Sleeb4552922007-11-28 00:12:11 +0000515std::string TSocket::getPeerHost() {
516 if (peerHost_.empty()) {
517 struct sockaddr_storage addr;
518 socklen_t addrLen = sizeof(addr);
519
520 if (socket_ < 0) {
521 return host_;
522 }
523
524 int rv = getpeername(socket_, (sockaddr*) &addr, &addrLen);
525
526 if (rv != 0) {
527 return peerHost_;
528 }
529
530 char clienthost[NI_MAXHOST];
531 char clientservice[NI_MAXSERV];
532
533 getnameinfo((sockaddr*) &addr, addrLen,
534 clienthost, sizeof(clienthost),
535 clientservice, sizeof(clientservice), 0);
536
537 peerHost_ = clienthost;
538 }
539 return peerHost_;
540}
541
542std::string TSocket::getPeerAddress() {
543 if (peerAddress_.empty()) {
544 struct sockaddr_storage addr;
545 socklen_t addrLen = sizeof(addr);
546
547 if (socket_ < 0) {
548 return peerAddress_;
549 }
550
551 int rv = getpeername(socket_, (sockaddr*) &addr, &addrLen);
552
553 if (rv != 0) {
554 return peerAddress_;
555 }
556
557 char clienthost[NI_MAXHOST];
558 char clientservice[NI_MAXSERV];
559
560 getnameinfo((sockaddr*) &addr, addrLen,
561 clienthost, sizeof(clienthost),
562 clientservice, sizeof(clientservice),
563 NI_NUMERICHOST|NI_NUMERICSERV);
564
565 peerAddress_ = clienthost;
566 peerPort_ = std::atoi(clientservice);
567 }
568 return peerAddress_;
569}
570
571int TSocket::getPeerPort() {
572 getPeerAddress();
573 return peerPort_;
574}
575
T Jake Lucianib5e62212009-01-31 22:36:20 +0000576}}} // apache::thrift::transport