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