Mark Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 1 | // 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 Slemko | e03da18 | 2006-07-21 21:32:36 +0000 | [diff] [blame] | 7 | #include <config.h> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 8 | #include <sys/socket.h> |
David Reiss | 22b1886 | 2008-04-08 06:25:45 +0000 | [diff] [blame^] | 9 | #include <sys/poll.h> |
Mark Slee | dd56497 | 2007-08-21 02:39:57 +0000 | [diff] [blame] | 10 | #include <sys/types.h> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 11 | #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 Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 17 | #include <fcntl.h> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 18 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 19 | #include "concurrency/Monitor.h" |
Marc Slemko | d42a2c2 | 2006-08-10 03:30:18 +0000 | [diff] [blame] | 20 | #include "TSocket.h" |
| 21 | #include "TTransportException.h" |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 22 | |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 23 | namespace facebook { namespace thrift { namespace transport { |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 24 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 25 | using namespace std; |
| 26 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 27 | // Global var to track total socket sys calls |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 28 | uint32_t g_socket_syscalls = 0; |
| 29 | |
| 30 | /** |
| 31 | * TSocket implementation. |
| 32 | * |
| 33 | * @author Mark Slee <mcslee@facebook.com> |
| 34 | */ |
| 35 | |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 36 | TSocket::TSocket(string host, int port) : |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 37 | host_(host), |
| 38 | port_(port), |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 39 | socket_(-1), |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 40 | connTimeout_(0), |
| 41 | sendTimeout_(0), |
| 42 | recvTimeout_(0), |
| 43 | lingerOn_(1), |
| 44 | lingerVal_(0), |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 45 | noDelay_(1), |
| 46 | maxRecvRetries_(5) { |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 47 | recvTimeval_.tv_sec = (int)(recvTimeout_/1000); |
| 48 | recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 51 | TSocket::TSocket() : |
Aditya Agarwal | ebc99e0 | 2007-01-15 23:14:58 +0000 | [diff] [blame] | 52 | host_(""), |
| 53 | port_(0), |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 54 | socket_(-1), |
Aditya Agarwal | ebc99e0 | 2007-01-15 23:14:58 +0000 | [diff] [blame] | 55 | connTimeout_(0), |
| 56 | sendTimeout_(0), |
| 57 | recvTimeout_(0), |
| 58 | lingerOn_(1), |
| 59 | lingerVal_(0), |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 60 | noDelay_(1), |
| 61 | maxRecvRetries_(5) { |
Aditya Agarwal | ebc99e0 | 2007-01-15 23:14:58 +0000 | [diff] [blame] | 62 | recvTimeval_.tv_sec = (int)(recvTimeout_/1000); |
| 63 | recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000); |
| 64 | } |
| 65 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 66 | TSocket::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 Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 75 | noDelay_(1), |
| 76 | maxRecvRetries_(5) { |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 77 | recvTimeval_.tv_sec = (int)(recvTimeout_/1000); |
| 78 | recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 79 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 80 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 81 | TSocket::~TSocket() { |
| 82 | close(); |
| 83 | } |
| 84 | |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 85 | bool TSocket::isOpen() { |
| 86 | return (socket_ >= 0); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 89 | bool 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 Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 96 | int errno_copy = errno; |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 97 | string errStr = "TSocket::peek() " + getSocketInfo(); |
| 98 | GlobalOutput(errStr.c_str()); |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 99 | throw TTransportException(TTransportException::UNKNOWN, "recv()", errno_copy); |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 100 | } |
| 101 | return (r > 0); |
| 102 | } |
| 103 | |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 104 | void TSocket::openConnection(struct addrinfo *res) { |
Mark Slee | a9848d7 | 2007-02-21 04:54:05 +0000 | [diff] [blame] | 105 | if (isOpen()) { |
| 106 | throw TTransportException(TTransportException::ALREADY_OPEN); |
| 107 | } |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 108 | |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 109 | socket_ = socket(res->ai_family, res->ai_socktype, res->ai_protocol); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 110 | if (socket_ == -1) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 111 | int errno_copy = errno; |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 112 | string errStr = "TSocket::open() socket " + getSocketInfo(); |
| 113 | GlobalOutput(errStr.c_str()); |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 114 | throw TTransportException(TTransportException::NOT_OPEN, "socket()", errno_copy); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 115 | } |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 116 | |
| 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 Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 133 | // Set the socket to be non blocking for connect if a timeout exists |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 134 | int flags = fcntl(socket_, F_GETFL, 0); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 135 | if (connTimeout_ > 0) { |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 136 | if (-1 == fcntl(socket_, F_SETFL, flags | O_NONBLOCK)) { |
| 137 | throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed"); |
| 138 | } |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 139 | } else { |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 140 | if (-1 == fcntl(socket_, F_SETFL, flags & ~O_NONBLOCK)) { |
| 141 | throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed"); |
| 142 | } |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 145 | // Connect the socket |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 146 | int ret = connect(socket_, res->ai_addr, res->ai_addrlen); |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 147 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 148 | if (ret == 0) { |
| 149 | goto done; |
| 150 | } |
| 151 | |
| 152 | if (errno != EINPROGRESS) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 153 | int errno_copy = errno; |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 154 | string errStr = "TSocket::open() connect " + getSocketInfo(); |
| 155 | GlobalOutput(errStr.c_str()); |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 156 | throw TTransportException(TTransportException::NOT_OPEN, "connect()", errno_copy); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 157 | } |
| 158 | |
David Reiss | 22b1886 | 2008-04-08 06:25:45 +0000 | [diff] [blame^] | 159 | |
| 160 | struct pollfd fds[1]; |
| 161 | memset(fds, 0 , sizeof(fds)); |
| 162 | fds[0].fd = socket_; |
| 163 | fds[0].events = POLLOUT; |
| 164 | ret = poll(fds, 1, connTimeout_); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 165 | |
| 166 | if (ret > 0) { |
| 167 | // Ensure connected |
| 168 | int val; |
| 169 | socklen_t lon; |
| 170 | lon = sizeof(int); |
| 171 | int ret2 = getsockopt(socket_, SOL_SOCKET, SO_ERROR, (void *)&val, &lon); |
| 172 | if (ret2 == -1) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 173 | int errno_copy = errno; |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 174 | string errStr = "TSocket::open() getsockopt SO_ERROR " + getSocketInfo(); |
| 175 | GlobalOutput(errStr.c_str()); |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 176 | throw TTransportException(TTransportException::NOT_OPEN, "open()", errno_copy); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 177 | } |
| 178 | if (val == 0) { |
| 179 | goto done; |
| 180 | } |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 181 | int errno_copy = errno; |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 182 | string errStr = "TSocket::open() SO_ERROR was set " + getSocketInfo(); |
| 183 | GlobalOutput(errStr.c_str()); |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 184 | throw TTransportException(TTransportException::NOT_OPEN, "open()", errno_copy); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 185 | } else if (ret == 0) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 186 | int errno_copy = errno; |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 187 | string errStr = "TSocket::open() timed out " + getSocketInfo(); |
| 188 | GlobalOutput(errStr.c_str()); |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 189 | throw TTransportException(TTransportException::NOT_OPEN, "open()", errno_copy); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 190 | } else { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 191 | int errno_copy = errno; |
David Reiss | 22b1886 | 2008-04-08 06:25:45 +0000 | [diff] [blame^] | 192 | string errStr = "TSocket::open() poll error " + getSocketInfo(); |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 193 | GlobalOutput(errStr.c_str()); |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 194 | throw TTransportException(TTransportException::NOT_OPEN, "open()", errno_copy); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | done: |
| 198 | // Set socket back to normal mode (blocking) |
| 199 | fcntl(socket_, F_SETFL, flags); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 202 | void TSocket::open() { |
| 203 | if (isOpen()) { |
| 204 | throw TTransportException(TTransportException::ALREADY_OPEN); |
| 205 | } |
| 206 | |
| 207 | // Validate port number |
| 208 | if (port_ < 0 || port_ > 65536) { |
| 209 | throw TTransportException(TTransportException::NOT_OPEN, "Specified port is invalid"); |
| 210 | } |
| 211 | |
| 212 | struct addrinfo hints, *res, *res0; |
| 213 | int error; |
| 214 | char port[sizeof("65536") + 1]; |
| 215 | memset(&hints, 0, sizeof(hints)); |
| 216 | hints.ai_family = PF_UNSPEC; |
| 217 | hints.ai_socktype = SOCK_STREAM; |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 218 | hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG; |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 219 | sprintf(port, "%d", port_); |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 220 | |
Mark Slee | c37b4c5 | 2007-12-05 23:03:37 +0000 | [diff] [blame] | 221 | error = getaddrinfo(host_.c_str(), port, &hints, &res0); |
| 222 | |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 223 | if (error) { |
| 224 | fprintf(stderr, "getaddrinfo %d: %s\n", error, gai_strerror(error)); |
| 225 | close(); |
| 226 | throw TTransportException(TTransportException::NOT_OPEN, "Could not resolve host for client socket."); |
| 227 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 228 | |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 229 | // Cycle through all the returned addresses until one |
| 230 | // connects or push the exception up. |
| 231 | for (res = res0; res; res = res->ai_next) { |
| 232 | try { |
| 233 | openConnection(res); |
| 234 | break; |
| 235 | } catch (TTransportException& ttx) { |
| 236 | if (res->ai_next) { |
| 237 | close(); |
| 238 | } else { |
| 239 | close(); |
Mark Slee | 85287d3 | 2007-07-09 19:50:30 +0000 | [diff] [blame] | 240 | freeaddrinfo(res0); // cleanup on failure |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 241 | throw; |
| 242 | } |
| 243 | } |
| 244 | } |
Mark Slee | 85287d3 | 2007-07-09 19:50:30 +0000 | [diff] [blame] | 245 | |
| 246 | // Free address structure memory |
| 247 | freeaddrinfo(res0); |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 250 | void TSocket::close() { |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 251 | if (socket_ >= 0) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 252 | shutdown(socket_, SHUT_RDWR); |
| 253 | ::close(socket_); |
| 254 | } |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 255 | socket_ = -1; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 258 | uint32_t TSocket::read(uint8_t* buf, uint32_t len) { |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 259 | if (socket_ < 0) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 260 | throw TTransportException(TTransportException::NOT_OPEN, "Called read on non-open socket"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 261 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 262 | |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 263 | int32_t retries = 0; |
| 264 | |
| 265 | // EAGAIN can be signalled both when a timeout has occurred and when |
| 266 | // the system is out of resources (an awesome undocumented feature). |
| 267 | // The following is an approximation of the time interval under which |
| 268 | // EAGAIN is taken to indicate an out of resources error. |
| 269 | uint32_t eagainThresholdMicros = 0; |
| 270 | if (recvTimeout_) { |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 271 | // if a readTimeout is specified along with a max number of recv retries, then |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 272 | // the threshold will ensure that the read timeout is not exceeded even in the |
| 273 | // case of resource errors |
| 274 | eagainThresholdMicros = (recvTimeout_*1000)/ ((maxRecvRetries_>0) ? maxRecvRetries_ : 2); |
| 275 | } |
| 276 | |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 277 | try_again: |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 278 | // Read from the socket |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 279 | struct timeval begin; |
| 280 | gettimeofday(&begin, NULL); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 281 | int got = recv(socket_, buf, len, 0); |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 282 | struct timeval end; |
| 283 | gettimeofday(&end, NULL); |
| 284 | uint32_t readElapsedMicros = (((end.tv_sec - begin.tv_sec) * 1000 * 1000) |
| 285 | + (((uint64_t)(end.tv_usec - begin.tv_usec)))); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 286 | ++g_socket_syscalls; |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 287 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 288 | // Check for error on read |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 289 | if (got < 0) { |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 290 | if (errno == EAGAIN) { |
| 291 | // check if this is the lack of resources or timeout case |
| 292 | if (!eagainThresholdMicros || (readElapsedMicros < eagainThresholdMicros)) { |
| 293 | if (retries++ < maxRecvRetries_) { |
| 294 | usleep(50); |
| 295 | goto try_again; |
| 296 | } else { |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 297 | throw TTransportException(TTransportException::TIMED_OUT, |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 298 | "EAGAIN (unavailable resources)"); |
| 299 | } |
| 300 | } else { |
| 301 | // infer that timeout has been hit |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 302 | throw TTransportException(TTransportException::TIMED_OUT, |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 303 | "EAGAIN (timed out)"); |
| 304 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 305 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 306 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 307 | // If interrupted, try again |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 308 | if (errno == EINTR && retries++ < maxRecvRetries_) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 309 | goto try_again; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 310 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 311 | |
Mark Slee | c425780 | 2007-01-24 23:14:30 +0000 | [diff] [blame] | 312 | // Now it's not a try again case, but a real probblez |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 313 | string errStr = "TSocket::read() " + getSocketInfo(); |
| 314 | GlobalOutput(errStr.c_str()); |
Mark Slee | c425780 | 2007-01-24 23:14:30 +0000 | [diff] [blame] | 315 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 316 | // If we disconnect with no linger time |
| 317 | if (errno == ECONNRESET) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 318 | throw TTransportException(TTransportException::NOT_OPEN, "ECONNRESET"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 319 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 320 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 321 | // This ish isn't open |
| 322 | if (errno == ENOTCONN) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 323 | throw TTransportException(TTransportException::NOT_OPEN, "ENOTCONN"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 324 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 325 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 326 | // Timed out! |
| 327 | if (errno == ETIMEDOUT) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 328 | throw TTransportException(TTransportException::TIMED_OUT, "ETIMEDOUT"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 329 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 330 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 331 | // Some other error, whatevz |
Mark Slee | 5f68c71 | 2007-05-11 17:58:54 +0000 | [diff] [blame] | 332 | char buff[1024]; |
| 333 | sprintf(buff, "ERROR errno: %d", errno); |
| 334 | throw TTransportException(TTransportException::UNKNOWN, buff); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 335 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 336 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 337 | // The remote host has closed the socket |
| 338 | if (got == 0) { |
| 339 | close(); |
| 340 | return 0; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 341 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 342 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 343 | // Pack data into string |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 344 | return got; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 347 | void TSocket::write(const uint8_t* buf, uint32_t len) { |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 348 | if (socket_ < 0) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 349 | throw TTransportException(TTransportException::NOT_OPEN, "Called write on non-open socket"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 352 | uint32_t sent = 0; |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 353 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 354 | while (sent < len) { |
Marc Slemko | 9d4a3e2 | 2006-07-21 19:53:48 +0000 | [diff] [blame] | 355 | |
| 356 | int flags = 0; |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 357 | #ifdef MSG_NOSIGNAL |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 358 | // Note the use of MSG_NOSIGNAL to suppress SIGPIPE errors, instead we |
| 359 | // check for the EPIPE return condition and close the socket in that case |
Marc Slemko | 9d4a3e2 | 2006-07-21 19:53:48 +0000 | [diff] [blame] | 360 | flags |= MSG_NOSIGNAL; |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 361 | #endif // ifdef MSG_NOSIGNAL |
Marc Slemko | 9d4a3e2 | 2006-07-21 19:53:48 +0000 | [diff] [blame] | 362 | |
| 363 | int b = send(socket_, buf + sent, len - sent, flags); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 364 | ++g_socket_syscalls; |
| 365 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 366 | // Fail on a send error |
| 367 | if (b < 0) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 368 | if (errno == EPIPE || errno == ECONNRESET || errno == ENOTCONN) { |
| 369 | int errno_copy = errno; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 370 | close(); |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 371 | throw TTransportException(TTransportException::NOT_OPEN, "send()", errno_copy); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 372 | } |
| 373 | |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 374 | int errno_copy = errno; |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 375 | string errStr = "TSocket::write() send < 0 " + getSocketInfo(); |
| 376 | GlobalOutput(errStr.c_str()); |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 377 | throw TTransportException(TTransportException::UNKNOWN, "send", errno_copy); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 378 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 379 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 380 | // Fail on blocked send |
| 381 | if (b == 0) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 382 | throw TTransportException(TTransportException::NOT_OPEN, "Socket send returned 0."); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 383 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 384 | sent += b; |
| 385 | } |
| 386 | } |
| 387 | |
dweatherford | 14b0ed6 | 2007-10-19 01:03:32 +0000 | [diff] [blame] | 388 | std::string TSocket::getHost() { |
| 389 | return host_; |
| 390 | } |
| 391 | |
| 392 | int TSocket::getPort() { |
| 393 | return port_; |
| 394 | } |
| 395 | |
Aditya Agarwal | ebc99e0 | 2007-01-15 23:14:58 +0000 | [diff] [blame] | 396 | void TSocket::setHost(string host) { |
| 397 | host_ = host; |
| 398 | } |
| 399 | |
| 400 | void TSocket::setPort(int port) { |
| 401 | port_ = port; |
| 402 | } |
| 403 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 404 | void TSocket::setLinger(bool on, int linger) { |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 405 | lingerOn_ = on; |
| 406 | lingerVal_ = linger; |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 407 | if (socket_ < 0) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 408 | return; |
| 409 | } |
| 410 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 411 | struct linger l = {(lingerOn_ ? 1 : 0), lingerVal_}; |
| 412 | int ret = setsockopt(socket_, SOL_SOCKET, SO_LINGER, &l, sizeof(l)); |
| 413 | if (ret == -1) { |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 414 | string errStr = "TSocket::setLinger() " + getSocketInfo(); |
| 415 | GlobalOutput(errStr.c_str()); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 416 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 419 | void TSocket::setNoDelay(bool noDelay) { |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 420 | noDelay_ = noDelay; |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 421 | if (socket_ < 0) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 422 | return; |
| 423 | } |
| 424 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 425 | // Set socket to NODELAY |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 426 | int v = noDelay_ ? 1 : 0; |
| 427 | int ret = setsockopt(socket_, IPPROTO_TCP, TCP_NODELAY, &v, sizeof(v)); |
| 428 | if (ret == -1) { |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 429 | string errStr = "TSocket::setNoDelay() " + getSocketInfo(); |
| 430 | GlobalOutput(errStr.c_str()); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 431 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 432 | } |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 433 | |
| 434 | void TSocket::setConnTimeout(int ms) { |
| 435 | connTimeout_ = ms; |
| 436 | } |
| 437 | |
| 438 | void TSocket::setRecvTimeout(int ms) { |
Aditya Agarwal | c31769c | 2007-12-11 22:23:51 +0000 | [diff] [blame] | 439 | if (ms < 0) { |
| 440 | char errBuf[512]; |
| 441 | sprintf(errBuf, "TSocket::setRecvTimeout with negative input: %d\n", ms); |
| 442 | GlobalOutput(errBuf); |
| 443 | return; |
| 444 | } |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 445 | recvTimeout_ = ms; |
Aditya Agarwal | c31769c | 2007-12-11 22:23:51 +0000 | [diff] [blame] | 446 | |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 447 | if (socket_ < 0) { |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 448 | return; |
| 449 | } |
| 450 | |
Aditya Agarwal | c31769c | 2007-12-11 22:23:51 +0000 | [diff] [blame] | 451 | recvTimeval_.tv_sec = (int)(recvTimeout_/1000); |
| 452 | recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000); |
| 453 | |
David Reiss | 22b1886 | 2008-04-08 06:25:45 +0000 | [diff] [blame^] | 454 | // Copy because poll may modify |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 455 | struct timeval r = recvTimeval_; |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 456 | int ret = setsockopt(socket_, SOL_SOCKET, SO_RCVTIMEO, &r, sizeof(r)); |
| 457 | if (ret == -1) { |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 458 | string errStr = "TSocket::setRecvTimeout() " + getSocketInfo(); |
| 459 | GlobalOutput(errStr.c_str()); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | |
| 463 | void TSocket::setSendTimeout(int ms) { |
Aditya Agarwal | c31769c | 2007-12-11 22:23:51 +0000 | [diff] [blame] | 464 | if (ms < 0) { |
| 465 | char errBuf[512]; |
| 466 | sprintf(errBuf, "TSocket::setSendTimeout with negative input: %d\n", ms); |
| 467 | GlobalOutput(errBuf); |
| 468 | return; |
| 469 | } |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 470 | sendTimeout_ = ms; |
Aditya Agarwal | c31769c | 2007-12-11 22:23:51 +0000 | [diff] [blame] | 471 | |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 472 | if (socket_ < 0) { |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 473 | return; |
| 474 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 475 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 476 | struct timeval s = {(int)(sendTimeout_/1000), |
| 477 | (int)((sendTimeout_%1000)*1000)}; |
| 478 | int ret = setsockopt(socket_, SOL_SOCKET, SO_SNDTIMEO, &s, sizeof(s)); |
| 479 | if (ret == -1) { |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 480 | string errStr = "TSocket::setSendTimeout() " + getSocketInfo(); |
| 481 | GlobalOutput(errStr.c_str()); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 482 | } |
| 483 | } |
| 484 | |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 485 | void TSocket::setMaxRecvRetries(int maxRecvRetries) { |
| 486 | maxRecvRetries_ = maxRecvRetries; |
| 487 | } |
| 488 | |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 489 | string TSocket::getSocketInfo() { |
| 490 | std::ostringstream oss; |
| 491 | oss << "<Host: " << host_ << " Port: " << port_ << ">"; |
| 492 | return oss.str(); |
| 493 | } |
| 494 | |
Mark Slee | b455292 | 2007-11-28 00:12:11 +0000 | [diff] [blame] | 495 | std::string TSocket::getPeerHost() { |
| 496 | if (peerHost_.empty()) { |
| 497 | struct sockaddr_storage addr; |
| 498 | socklen_t addrLen = sizeof(addr); |
| 499 | |
| 500 | if (socket_ < 0) { |
| 501 | return host_; |
| 502 | } |
| 503 | |
| 504 | int rv = getpeername(socket_, (sockaddr*) &addr, &addrLen); |
| 505 | |
| 506 | if (rv != 0) { |
| 507 | return peerHost_; |
| 508 | } |
| 509 | |
| 510 | char clienthost[NI_MAXHOST]; |
| 511 | char clientservice[NI_MAXSERV]; |
| 512 | |
| 513 | getnameinfo((sockaddr*) &addr, addrLen, |
| 514 | clienthost, sizeof(clienthost), |
| 515 | clientservice, sizeof(clientservice), 0); |
| 516 | |
| 517 | peerHost_ = clienthost; |
| 518 | } |
| 519 | return peerHost_; |
| 520 | } |
| 521 | |
| 522 | std::string TSocket::getPeerAddress() { |
| 523 | if (peerAddress_.empty()) { |
| 524 | struct sockaddr_storage addr; |
| 525 | socklen_t addrLen = sizeof(addr); |
| 526 | |
| 527 | if (socket_ < 0) { |
| 528 | return peerAddress_; |
| 529 | } |
| 530 | |
| 531 | int rv = getpeername(socket_, (sockaddr*) &addr, &addrLen); |
| 532 | |
| 533 | if (rv != 0) { |
| 534 | return peerAddress_; |
| 535 | } |
| 536 | |
| 537 | char clienthost[NI_MAXHOST]; |
| 538 | char clientservice[NI_MAXSERV]; |
| 539 | |
| 540 | getnameinfo((sockaddr*) &addr, addrLen, |
| 541 | clienthost, sizeof(clienthost), |
| 542 | clientservice, sizeof(clientservice), |
| 543 | NI_NUMERICHOST|NI_NUMERICSERV); |
| 544 | |
| 545 | peerAddress_ = clienthost; |
| 546 | peerPort_ = std::atoi(clientservice); |
| 547 | } |
| 548 | return peerAddress_; |
| 549 | } |
| 550 | |
| 551 | int TSocket::getPeerPort() { |
| 552 | getPeerAddress(); |
| 553 | return peerPort_; |
| 554 | } |
| 555 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 556 | }}} // facebook::thrift::transport |