blob: 1696053da37e49b07aa3b75474be1a39ec5f519c [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 *
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;
Kevin Clark022b2242009-03-05 21:05:37 +000099 #ifdef __FreeBSD__
100 /* shigin:
101 * freebsd returns -1 and ECONNRESET if socket was closed by
102 * the other side
103 */
104 if (errno_copy == ECONNRESET)
105 {
106 close();
107 return false;
108 }
109 #endif
David Reiss01e55c12008-07-13 22:18:51 +0000110 GlobalOutput.perror("TSocket::peek() recv() " + getSocketInfo(), errno_copy);
David Reissbc3dddb2007-08-22 23:20:24 +0000111 throw TTransportException(TTransportException::UNKNOWN, "recv()", errno_copy);
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000112 }
113 return (r > 0);
114}
115
Mark Slee6d56eb92007-07-06 22:28:15 +0000116void TSocket::openConnection(struct addrinfo *res) {
Mark Sleea9848d72007-02-21 04:54:05 +0000117 if (isOpen()) {
118 throw TTransportException(TTransportException::ALREADY_OPEN);
119 }
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000120
Mark Slee6d56eb92007-07-06 22:28:15 +0000121 socket_ = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
Mark Sleee8540632006-05-30 09:24:40 +0000122 if (socket_ == -1) {
David Reissbc3dddb2007-08-22 23:20:24 +0000123 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000124 GlobalOutput.perror("TSocket::open() socket() " + getSocketInfo(), errno_copy);
David Reissbc3dddb2007-08-22 23:20:24 +0000125 throw TTransportException(TTransportException::NOT_OPEN, "socket()", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000126 }
Mark Slee29050782006-09-29 00:12:30 +0000127
128 // Send timeout
129 if (sendTimeout_ > 0) {
130 setSendTimeout(sendTimeout_);
131 }
132
133 // Recv timeout
134 if (recvTimeout_ > 0) {
135 setRecvTimeout(recvTimeout_);
136 }
137
138 // Linger
139 setLinger(lingerOn_, lingerVal_);
140
141 // No delay
142 setNoDelay(noDelay_);
143
Mark Slee29050782006-09-29 00:12:30 +0000144 // Set the socket to be non blocking for connect if a timeout exists
Mark Slee256bdc42007-11-27 08:42:19 +0000145 int flags = fcntl(socket_, F_GETFL, 0);
Mark Slee29050782006-09-29 00:12:30 +0000146 if (connTimeout_ > 0) {
Mark Sleea5a783f2007-03-02 19:41:08 +0000147 if (-1 == fcntl(socket_, F_SETFL, flags | O_NONBLOCK)) {
David Reiss9b209552008-04-08 06:26:05 +0000148 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000149 GlobalOutput.perror("TSocket::open() fcntl() " + getSocketInfo(), errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000150 throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed", errno_copy);
Mark Sleea5a783f2007-03-02 19:41:08 +0000151 }
Mark Slee29050782006-09-29 00:12:30 +0000152 } else {
Mark Sleea5a783f2007-03-02 19:41:08 +0000153 if (-1 == fcntl(socket_, F_SETFL, flags & ~O_NONBLOCK)) {
David Reiss9b209552008-04-08 06:26:05 +0000154 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000155 GlobalOutput.perror("TSocket::open() fcntl " + getSocketInfo(), errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000156 throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed", errno_copy);
Mark Sleea5a783f2007-03-02 19:41:08 +0000157 }
Mark Slee29050782006-09-29 00:12:30 +0000158 }
159
Mark Sleee8540632006-05-30 09:24:40 +0000160 // Connect the socket
Mark Slee6d56eb92007-07-06 22:28:15 +0000161 int ret = connect(socket_, res->ai_addr, res->ai_addrlen);
Mark Slee256bdc42007-11-27 08:42:19 +0000162
David Reiss9b209552008-04-08 06:26:05 +0000163 // success case
Mark Slee29050782006-09-29 00:12:30 +0000164 if (ret == 0) {
165 goto done;
166 }
167
168 if (errno != EINPROGRESS) {
David Reissbc3dddb2007-08-22 23:20:24 +0000169 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000170 GlobalOutput.perror("TSocket::open() connect() " + getSocketInfo(), errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000171 throw TTransportException(TTransportException::NOT_OPEN, "connect() failed", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000172 }
173
David Reiss22b18862008-04-08 06:25:45 +0000174
175 struct pollfd fds[1];
David Reissc88eb8c2008-06-11 01:18:54 +0000176 std::memset(fds, 0 , sizeof(fds));
David Reiss22b18862008-04-08 06:25:45 +0000177 fds[0].fd = socket_;
178 fds[0].events = POLLOUT;
179 ret = poll(fds, 1, connTimeout_);
Mark Slee29050782006-09-29 00:12:30 +0000180
181 if (ret > 0) {
David Reiss9b209552008-04-08 06:26:05 +0000182 // Ensure the socket is connected and that there are no errors set
Mark Slee29050782006-09-29 00:12:30 +0000183 int val;
184 socklen_t lon;
185 lon = sizeof(int);
186 int ret2 = getsockopt(socket_, SOL_SOCKET, SO_ERROR, (void *)&val, &lon);
187 if (ret2 == -1) {
David Reissbc3dddb2007-08-22 23:20:24 +0000188 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000189 GlobalOutput.perror("TSocket::open() getsockopt() " + getSocketInfo(), errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000190 throw TTransportException(TTransportException::NOT_OPEN, "getsockopt()", errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000191 }
David Reiss9b209552008-04-08 06:26:05 +0000192 // no errors on socket, go to town
Mark Slee29050782006-09-29 00:12:30 +0000193 if (val == 0) {
194 goto done;
195 }
David Reiss01e55c12008-07-13 22:18:51 +0000196 GlobalOutput.perror("TSocket::open() error on socket (after poll) " + getSocketInfo(), val);
David Reiss9b209552008-04-08 06:26:05 +0000197 throw TTransportException(TTransportException::NOT_OPEN, "socket open() error", val);
Mark Slee29050782006-09-29 00:12:30 +0000198 } else if (ret == 0) {
David Reiss9b209552008-04-08 06:26:05 +0000199 // socket timed out
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000200 string errStr = "TSocket::open() timed out " + getSocketInfo();
201 GlobalOutput(errStr.c_str());
David Reiss9b209552008-04-08 06:26:05 +0000202 throw TTransportException(TTransportException::NOT_OPEN, "open() timed out");
Mark Slee29050782006-09-29 00:12:30 +0000203 } else {
David Reiss9b209552008-04-08 06:26:05 +0000204 // error on poll()
David Reissbc3dddb2007-08-22 23:20:24 +0000205 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000206 GlobalOutput.perror("TSocket::open() poll() " + getSocketInfo(), errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000207 throw TTransportException(TTransportException::NOT_OPEN, "poll() failed", errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000208 }
209
210 done:
211 // Set socket back to normal mode (blocking)
212 fcntl(socket_, F_SETFL, flags);
Mark Sleee8540632006-05-30 09:24:40 +0000213}
214
Mark Slee6d56eb92007-07-06 22:28:15 +0000215void TSocket::open() {
216 if (isOpen()) {
217 throw TTransportException(TTransportException::ALREADY_OPEN);
218 }
219
220 // Validate port number
221 if (port_ < 0 || port_ > 65536) {
222 throw TTransportException(TTransportException::NOT_OPEN, "Specified port is invalid");
223 }
224
225 struct addrinfo hints, *res, *res0;
David Reiss9b209552008-04-08 06:26:05 +0000226 res = NULL;
227 res0 = NULL;
Mark Slee6d56eb92007-07-06 22:28:15 +0000228 int error;
David Reiss9b209552008-04-08 06:26:05 +0000229 char port[sizeof("65536")];
David Reissc88eb8c2008-06-11 01:18:54 +0000230 std::memset(&hints, 0, sizeof(hints));
Mark Slee6d56eb92007-07-06 22:28:15 +0000231 hints.ai_family = PF_UNSPEC;
232 hints.ai_socktype = SOCK_STREAM;
Mark Slee256bdc42007-11-27 08:42:19 +0000233 hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
Mark Slee6d56eb92007-07-06 22:28:15 +0000234 sprintf(port, "%d", port_);
Mark Slee256bdc42007-11-27 08:42:19 +0000235
Mark Sleec37b4c52007-12-05 23:03:37 +0000236 error = getaddrinfo(host_.c_str(), port, &hints, &res0);
237
Mark Slee6d56eb92007-07-06 22:28:15 +0000238 if (error) {
David Reiss9b209552008-04-08 06:26:05 +0000239 string errStr = "TSocket::open() getaddrinfo() " + getSocketInfo() + string(gai_strerror(error));
240 GlobalOutput(errStr.c_str());
Mark Slee6d56eb92007-07-06 22:28:15 +0000241 close();
242 throw TTransportException(TTransportException::NOT_OPEN, "Could not resolve host for client socket.");
243 }
Mark Slee256bdc42007-11-27 08:42:19 +0000244
Mark Slee6d56eb92007-07-06 22:28:15 +0000245 // Cycle through all the returned addresses until one
246 // connects or push the exception up.
247 for (res = res0; res; res = res->ai_next) {
248 try {
249 openConnection(res);
250 break;
251 } catch (TTransportException& ttx) {
252 if (res->ai_next) {
253 close();
254 } else {
255 close();
Mark Slee85287d32007-07-09 19:50:30 +0000256 freeaddrinfo(res0); // cleanup on failure
Mark Slee6d56eb92007-07-06 22:28:15 +0000257 throw;
258 }
259 }
260 }
Mark Slee85287d32007-07-09 19:50:30 +0000261
262 // Free address structure memory
263 freeaddrinfo(res0);
Mark Slee6d56eb92007-07-06 22:28:15 +0000264}
265
Mark Sleee8540632006-05-30 09:24:40 +0000266void TSocket::close() {
Martin Kraemeree341cb2007-02-05 21:40:38 +0000267 if (socket_ >= 0) {
Mark Sleee8540632006-05-30 09:24:40 +0000268 shutdown(socket_, SHUT_RDWR);
269 ::close(socket_);
270 }
Martin Kraemeree341cb2007-02-05 21:40:38 +0000271 socket_ = -1;
Mark Sleee8540632006-05-30 09:24:40 +0000272}
273
Mark Slee8d7e1f62006-06-07 06:48:56 +0000274uint32_t TSocket::read(uint8_t* buf, uint32_t len) {
Martin Kraemeree341cb2007-02-05 21:40:38 +0000275 if (socket_ < 0) {
Mark Sleef9831082007-02-20 20:59:21 +0000276 throw TTransportException(TTransportException::NOT_OPEN, "Called read on non-open socket");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000277 }
Mark Sleee8540632006-05-30 09:24:40 +0000278
Aditya Agarwale04475b2007-05-23 02:14:58 +0000279 int32_t retries = 0;
280
281 // EAGAIN can be signalled both when a timeout has occurred and when
282 // the system is out of resources (an awesome undocumented feature).
283 // The following is an approximation of the time interval under which
284 // EAGAIN is taken to indicate an out of resources error.
285 uint32_t eagainThresholdMicros = 0;
286 if (recvTimeout_) {
Mark Slee256bdc42007-11-27 08:42:19 +0000287 // if a readTimeout is specified along with a max number of recv retries, then
Aditya Agarwale04475b2007-05-23 02:14:58 +0000288 // the threshold will ensure that the read timeout is not exceeded even in the
289 // case of resource errors
290 eagainThresholdMicros = (recvTimeout_*1000)/ ((maxRecvRetries_>0) ? maxRecvRetries_ : 2);
291 }
292
Mark Slee256bdc42007-11-27 08:42:19 +0000293 try_again:
Mark Slee8d7e1f62006-06-07 06:48:56 +0000294 // Read from the socket
Aditya Agarwale04475b2007-05-23 02:14:58 +0000295 struct timeval begin;
296 gettimeofday(&begin, NULL);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000297 int got = recv(socket_, buf, len, 0);
Kevin Clark022b2242009-03-05 21:05:37 +0000298 int errno_copy = errno; //gettimeofday can change errno
Aditya Agarwale04475b2007-05-23 02:14:58 +0000299 struct timeval end;
300 gettimeofday(&end, NULL);
301 uint32_t readElapsedMicros = (((end.tv_sec - begin.tv_sec) * 1000 * 1000)
302 + (((uint64_t)(end.tv_usec - begin.tv_usec))));
Mark Slee8d7e1f62006-06-07 06:48:56 +0000303 ++g_socket_syscalls;
Aditya Agarwale04475b2007-05-23 02:14:58 +0000304
Mark Slee8d7e1f62006-06-07 06:48:56 +0000305 // Check for error on read
Mark Slee256bdc42007-11-27 08:42:19 +0000306 if (got < 0) {
Kevin Clark022b2242009-03-05 21:05:37 +0000307 if (errno_copy == EAGAIN) {
Aditya Agarwale04475b2007-05-23 02:14:58 +0000308 // check if this is the lack of resources or timeout case
309 if (!eagainThresholdMicros || (readElapsedMicros < eagainThresholdMicros)) {
310 if (retries++ < maxRecvRetries_) {
311 usleep(50);
312 goto try_again;
313 } else {
Mark Slee256bdc42007-11-27 08:42:19 +0000314 throw TTransportException(TTransportException::TIMED_OUT,
Aditya Agarwale04475b2007-05-23 02:14:58 +0000315 "EAGAIN (unavailable resources)");
316 }
317 } else {
318 // infer that timeout has been hit
Mark Slee256bdc42007-11-27 08:42:19 +0000319 throw TTransportException(TTransportException::TIMED_OUT,
Aditya Agarwale04475b2007-05-23 02:14:58 +0000320 "EAGAIN (timed out)");
321 }
Mark Sleee8540632006-05-30 09:24:40 +0000322 }
Mark Slee256bdc42007-11-27 08:42:19 +0000323
Mark Slee8d7e1f62006-06-07 06:48:56 +0000324 // If interrupted, try again
Kevin Clark022b2242009-03-05 21:05:37 +0000325 if (errno_copy == EINTR && retries++ < maxRecvRetries_) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000326 goto try_again;
Mark Sleee8540632006-05-30 09:24:40 +0000327 }
Mark Slee256bdc42007-11-27 08:42:19 +0000328
Mark Sleec4257802007-01-24 23:14:30 +0000329 // Now it's not a try again case, but a real probblez
David Reiss01e55c12008-07-13 22:18:51 +0000330 GlobalOutput.perror("TSocket::read() recv() " + getSocketInfo(), errno_copy);
Mark Sleec4257802007-01-24 23:14:30 +0000331
Mark Slee8d7e1f62006-06-07 06:48:56 +0000332 // If we disconnect with no linger time
Kevin Clark022b2242009-03-05 21:05:37 +0000333 if (errno_copy == ECONNRESET) {
334 #ifdef __FreeBSD__
335 /* shigin: freebsd doesn't follow POSIX semantic of recv and fails with
336 * ECONNRESET if peer performed shutdown
337 */
338 close();
339 return 0;
340 #else
Mark Sleef9831082007-02-20 20:59:21 +0000341 throw TTransportException(TTransportException::NOT_OPEN, "ECONNRESET");
Kevin Clark022b2242009-03-05 21:05:37 +0000342 #endif
Mark Slee8d7e1f62006-06-07 06:48:56 +0000343 }
Mark Slee256bdc42007-11-27 08:42:19 +0000344
Mark Slee8d7e1f62006-06-07 06:48:56 +0000345 // This ish isn't open
Kevin Clark022b2242009-03-05 21:05:37 +0000346 if (errno_copy == ENOTCONN) {
Mark Sleef9831082007-02-20 20:59:21 +0000347 throw TTransportException(TTransportException::NOT_OPEN, "ENOTCONN");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000348 }
Mark Slee256bdc42007-11-27 08:42:19 +0000349
Mark Slee8d7e1f62006-06-07 06:48:56 +0000350 // Timed out!
Kevin Clark022b2242009-03-05 21:05:37 +0000351 if (errno_copy == ETIMEDOUT) {
Mark Sleef9831082007-02-20 20:59:21 +0000352 throw TTransportException(TTransportException::TIMED_OUT, "ETIMEDOUT");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000353 }
Mark Slee256bdc42007-11-27 08:42:19 +0000354
Mark Slee8d7e1f62006-06-07 06:48:56 +0000355 // Some other error, whatevz
David Reiss01e55c12008-07-13 22:18:51 +0000356 throw TTransportException(TTransportException::UNKNOWN, "Unknown", errno_copy);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000357 }
Mark Slee256bdc42007-11-27 08:42:19 +0000358
Mark Slee8d7e1f62006-06-07 06:48:56 +0000359 // The remote host has closed the socket
360 if (got == 0) {
361 close();
362 return 0;
Mark Sleee8540632006-05-30 09:24:40 +0000363 }
Mark Slee256bdc42007-11-27 08:42:19 +0000364
Mark Sleee8540632006-05-30 09:24:40 +0000365 // Pack data into string
Mark Slee8d7e1f62006-06-07 06:48:56 +0000366 return got;
Mark Sleee8540632006-05-30 09:24:40 +0000367}
368
Mark Slee8d7e1f62006-06-07 06:48:56 +0000369void TSocket::write(const uint8_t* buf, uint32_t len) {
Martin Kraemeree341cb2007-02-05 21:40:38 +0000370 if (socket_ < 0) {
Mark Sleef9831082007-02-20 20:59:21 +0000371 throw TTransportException(TTransportException::NOT_OPEN, "Called write on non-open socket");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000372 }
373
Mark Sleee8540632006-05-30 09:24:40 +0000374 uint32_t sent = 0;
Mark Slee256bdc42007-11-27 08:42:19 +0000375
Mark Slee8d7e1f62006-06-07 06:48:56 +0000376 while (sent < len) {
Marc Slemko9d4a3e22006-07-21 19:53:48 +0000377
378 int flags = 0;
Mark Slee29050782006-09-29 00:12:30 +0000379 #ifdef MSG_NOSIGNAL
Mark Slee8d7e1f62006-06-07 06:48:56 +0000380 // Note the use of MSG_NOSIGNAL to suppress SIGPIPE errors, instead we
381 // check for the EPIPE return condition and close the socket in that case
Marc Slemko9d4a3e22006-07-21 19:53:48 +0000382 flags |= MSG_NOSIGNAL;
Mark Slee29050782006-09-29 00:12:30 +0000383 #endif // ifdef MSG_NOSIGNAL
Marc Slemko9d4a3e22006-07-21 19:53:48 +0000384
385 int b = send(socket_, buf + sent, len - sent, flags);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000386 ++g_socket_syscalls;
387
Mark Sleee8540632006-05-30 09:24:40 +0000388 // Fail on a send error
389 if (b < 0) {
David Reiss9b209552008-04-08 06:26:05 +0000390 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000391 GlobalOutput.perror("TSocket::write() send() " + getSocketInfo(), errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000392
David Reissbc3dddb2007-08-22 23:20:24 +0000393 if (errno == EPIPE || errno == ECONNRESET || errno == ENOTCONN) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000394 close();
David Reiss9b209552008-04-08 06:26:05 +0000395 throw TTransportException(TTransportException::NOT_OPEN, "write() send()", errno_copy);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000396 }
397
David Reiss9b209552008-04-08 06:26:05 +0000398 throw TTransportException(TTransportException::UNKNOWN, "write() send()", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000399 }
Mark Slee256bdc42007-11-27 08:42:19 +0000400
Mark Sleee8540632006-05-30 09:24:40 +0000401 // Fail on blocked send
402 if (b == 0) {
Mark Sleef9831082007-02-20 20:59:21 +0000403 throw TTransportException(TTransportException::NOT_OPEN, "Socket send returned 0.");
Mark Sleee8540632006-05-30 09:24:40 +0000404 }
Mark Sleee8540632006-05-30 09:24:40 +0000405 sent += b;
406 }
407}
408
dweatherford14b0ed62007-10-19 01:03:32 +0000409std::string TSocket::getHost() {
410 return host_;
411}
412
413int TSocket::getPort() {
414 return port_;
415}
416
Aditya Agarwalebc99e02007-01-15 23:14:58 +0000417void TSocket::setHost(string host) {
418 host_ = host;
419}
420
421void TSocket::setPort(int port) {
422 port_ = port;
423}
424
Mark Slee8d7e1f62006-06-07 06:48:56 +0000425void TSocket::setLinger(bool on, int linger) {
Mark Slee29050782006-09-29 00:12:30 +0000426 lingerOn_ = on;
427 lingerVal_ = linger;
Martin Kraemeree341cb2007-02-05 21:40:38 +0000428 if (socket_ < 0) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000429 return;
430 }
431
Mark Slee29050782006-09-29 00:12:30 +0000432 struct linger l = {(lingerOn_ ? 1 : 0), lingerVal_};
433 int ret = setsockopt(socket_, SOL_SOCKET, SO_LINGER, &l, sizeof(l));
434 if (ret == -1) {
David Reiss01e55c12008-07-13 22:18:51 +0000435 int errno_copy = errno; // Copy errno because we're allocating memory.
436 GlobalOutput.perror("TSocket::setLinger() setsockopt() " + getSocketInfo(), errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000437 }
Mark Sleee8540632006-05-30 09:24:40 +0000438}
439
Mark Slee8d7e1f62006-06-07 06:48:56 +0000440void TSocket::setNoDelay(bool noDelay) {
Mark Slee29050782006-09-29 00:12:30 +0000441 noDelay_ = noDelay;
Martin Kraemeree341cb2007-02-05 21:40:38 +0000442 if (socket_ < 0) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000443 return;
444 }
445
Mark Sleee8540632006-05-30 09:24:40 +0000446 // Set socket to NODELAY
Mark Slee29050782006-09-29 00:12:30 +0000447 int v = noDelay_ ? 1 : 0;
448 int ret = setsockopt(socket_, IPPROTO_TCP, TCP_NODELAY, &v, sizeof(v));
449 if (ret == -1) {
David Reiss01e55c12008-07-13 22:18:51 +0000450 int errno_copy = errno; // Copy errno because we're allocating memory.
451 GlobalOutput.perror("TSocket::setNoDelay() setsockopt() " + getSocketInfo(), errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000452 }
Mark Sleee8540632006-05-30 09:24:40 +0000453}
Mark Slee29050782006-09-29 00:12:30 +0000454
455void TSocket::setConnTimeout(int ms) {
456 connTimeout_ = ms;
457}
458
459void TSocket::setRecvTimeout(int ms) {
Aditya Agarwalc31769c2007-12-11 22:23:51 +0000460 if (ms < 0) {
461 char errBuf[512];
462 sprintf(errBuf, "TSocket::setRecvTimeout with negative input: %d\n", ms);
463 GlobalOutput(errBuf);
464 return;
465 }
Mark Slee29050782006-09-29 00:12:30 +0000466 recvTimeout_ = ms;
Aditya Agarwalc31769c2007-12-11 22:23:51 +0000467
Martin Kraemeree341cb2007-02-05 21:40:38 +0000468 if (socket_ < 0) {
Mark Slee29050782006-09-29 00:12:30 +0000469 return;
470 }
471
Aditya Agarwalc31769c2007-12-11 22:23:51 +0000472 recvTimeval_.tv_sec = (int)(recvTimeout_/1000);
473 recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000);
474
David Reiss22b18862008-04-08 06:25:45 +0000475 // Copy because poll may modify
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000476 struct timeval r = recvTimeval_;
Mark Slee29050782006-09-29 00:12:30 +0000477 int ret = setsockopt(socket_, SOL_SOCKET, SO_RCVTIMEO, &r, sizeof(r));
478 if (ret == -1) {
David Reiss01e55c12008-07-13 22:18:51 +0000479 int errno_copy = errno; // Copy errno because we're allocating memory.
480 GlobalOutput.perror("TSocket::setRecvTimeout() setsockopt() " + getSocketInfo(), errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000481 }
482}
483
484void TSocket::setSendTimeout(int ms) {
Aditya Agarwalc31769c2007-12-11 22:23:51 +0000485 if (ms < 0) {
486 char errBuf[512];
487 sprintf(errBuf, "TSocket::setSendTimeout with negative input: %d\n", ms);
488 GlobalOutput(errBuf);
489 return;
490 }
Mark Slee29050782006-09-29 00:12:30 +0000491 sendTimeout_ = ms;
Aditya Agarwalc31769c2007-12-11 22:23:51 +0000492
Martin Kraemeree341cb2007-02-05 21:40:38 +0000493 if (socket_ < 0) {
Mark Slee29050782006-09-29 00:12:30 +0000494 return;
495 }
Mark Slee256bdc42007-11-27 08:42:19 +0000496
Mark Slee29050782006-09-29 00:12:30 +0000497 struct timeval s = {(int)(sendTimeout_/1000),
498 (int)((sendTimeout_%1000)*1000)};
499 int ret = setsockopt(socket_, SOL_SOCKET, SO_SNDTIMEO, &s, sizeof(s));
500 if (ret == -1) {
David Reiss01e55c12008-07-13 22:18:51 +0000501 int errno_copy = errno; // Copy errno because we're allocating memory.
502 GlobalOutput.perror("TSocket::setSendTimeout() setsockopt() " + getSocketInfo(), errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000503 }
504}
505
Aditya Agarwale04475b2007-05-23 02:14:58 +0000506void TSocket::setMaxRecvRetries(int maxRecvRetries) {
507 maxRecvRetries_ = maxRecvRetries;
508}
509
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000510string TSocket::getSocketInfo() {
511 std::ostringstream oss;
512 oss << "<Host: " << host_ << " Port: " << port_ << ">";
513 return oss.str();
514}
515
Mark Sleeb4552922007-11-28 00:12:11 +0000516std::string TSocket::getPeerHost() {
517 if (peerHost_.empty()) {
518 struct sockaddr_storage addr;
519 socklen_t addrLen = sizeof(addr);
520
521 if (socket_ < 0) {
522 return host_;
523 }
524
525 int rv = getpeername(socket_, (sockaddr*) &addr, &addrLen);
526
527 if (rv != 0) {
528 return peerHost_;
529 }
530
531 char clienthost[NI_MAXHOST];
532 char clientservice[NI_MAXSERV];
533
534 getnameinfo((sockaddr*) &addr, addrLen,
535 clienthost, sizeof(clienthost),
536 clientservice, sizeof(clientservice), 0);
537
538 peerHost_ = clienthost;
539 }
540 return peerHost_;
541}
542
543std::string TSocket::getPeerAddress() {
544 if (peerAddress_.empty()) {
545 struct sockaddr_storage addr;
546 socklen_t addrLen = sizeof(addr);
547
548 if (socket_ < 0) {
549 return peerAddress_;
550 }
551
552 int rv = getpeername(socket_, (sockaddr*) &addr, &addrLen);
553
554 if (rv != 0) {
555 return peerAddress_;
556 }
557
558 char clienthost[NI_MAXHOST];
559 char clientservice[NI_MAXSERV];
560
561 getnameinfo((sockaddr*) &addr, addrLen,
562 clienthost, sizeof(clienthost),
563 clientservice, sizeof(clientservice),
564 NI_NUMERICHOST|NI_NUMERICSERV);
565
566 peerAddress_ = clienthost;
567 peerPort_ = std::atoi(clientservice);
568 }
569 return peerAddress_;
570}
571
572int TSocket::getPeerPort() {
573 getPeerAddress();
574 return peerPort_;
575}
576
T Jake Lucianib5e62212009-01-31 22:36:20 +0000577}}} // apache::thrift::transport