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 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 23 | namespace facebook { namespace thrift { namespace transport { |
| 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 | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 40 | TSocket::TSocket(string host, int port) : |
| 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 | |
Aditya Agarwal | ebc99e0 | 2007-01-15 23:14:58 +0000 | [diff] [blame] | 55 | TSocket::TSocket() : |
| 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 | } |
| 84 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 85 | TSocket::~TSocket() { |
| 86 | close(); |
| 87 | } |
| 88 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 89 | bool TSocket::isOpen() { |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 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; |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 101 | GlobalOutput("TSocket::peek()"); |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame^] | 102 | throw TTransportException(TTransportException::UNKNOWN, "recv()", errno_copy); |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 103 | } |
| 104 | return (r > 0); |
| 105 | } |
| 106 | |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 107 | void TSocket::openConnection(struct addrinfo *res) { |
Mark Slee | a9848d7 | 2007-02-21 04:54:05 +0000 | [diff] [blame] | 108 | if (isOpen()) { |
| 109 | throw TTransportException(TTransportException::ALREADY_OPEN); |
| 110 | } |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 111 | |
| 112 | socket_ = socket(res->ai_family, res->ai_socktype, res->ai_protocol); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 113 | if (socket_ == -1) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame^] | 114 | int errno_copy = errno; |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 115 | GlobalOutput("TSocket::open() socket"); |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame^] | 116 | throw TTransportException(TTransportException::NOT_OPEN, "socket()", errno_copy); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 117 | } |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 118 | |
| 119 | // Send timeout |
| 120 | if (sendTimeout_ > 0) { |
| 121 | setSendTimeout(sendTimeout_); |
| 122 | } |
| 123 | |
| 124 | // Recv timeout |
| 125 | if (recvTimeout_ > 0) { |
| 126 | setRecvTimeout(recvTimeout_); |
| 127 | } |
| 128 | |
| 129 | // Linger |
| 130 | setLinger(lingerOn_, lingerVal_); |
| 131 | |
| 132 | // No delay |
| 133 | setNoDelay(noDelay_); |
| 134 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 135 | // Set the socket to be non blocking for connect if a timeout exists |
| 136 | int flags = fcntl(socket_, F_GETFL, 0); |
| 137 | if (connTimeout_ > 0) { |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 138 | if (-1 == fcntl(socket_, F_SETFL, flags | O_NONBLOCK)) { |
| 139 | throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed"); |
| 140 | } |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 141 | } else { |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 142 | if (-1 == fcntl(socket_, F_SETFL, flags & ~O_NONBLOCK)) { |
| 143 | throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed"); |
| 144 | } |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | // Conn timeout |
| 148 | struct timeval c = {(int)(connTimeout_/1000), |
| 149 | (int)((connTimeout_%1000)*1000)}; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 150 | |
| 151 | // Connect the socket |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 152 | int ret = connect(socket_, res->ai_addr, res->ai_addrlen); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 153 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 154 | if (ret == 0) { |
| 155 | goto done; |
| 156 | } |
| 157 | |
| 158 | if (errno != EINPROGRESS) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame^] | 159 | int errno_copy = errno; |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 160 | char buff[1024]; |
Martin Kraemer | e6c4fa6 | 2007-07-09 19:08:25 +0000 | [diff] [blame] | 161 | sprintf(buff, "TSocket::open() connect %s %d", host_.c_str(), port_); |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame^] | 162 | GlobalOutput(buff); |
Martin Kraemer | e6c4fa6 | 2007-07-09 19:08:25 +0000 | [diff] [blame] | 163 | |
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; |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 180 | GlobalOutput("TSocket::open() getsockopt SO_ERROR"); |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame^] | 181 | throw TTransportException(TTransportException::NOT_OPEN, "open()", errno_copy); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 182 | } |
| 183 | if (val == 0) { |
| 184 | goto done; |
| 185 | } |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame^] | 186 | int errno_copy = errno; |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 187 | GlobalOutput("TSocket::open() SO_ERROR was set"); |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame^] | 188 | throw TTransportException(TTransportException::NOT_OPEN, "open()", errno_copy); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 189 | } else if (ret == 0) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame^] | 190 | int errno_copy = errno; |
| 191 | GlobalOutput("TSocket::open() timed out"); |
| 192 | throw TTransportException(TTransportException::NOT_OPEN, "open()", errno_copy); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 193 | } else { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame^] | 194 | int errno_copy = errno; |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 195 | GlobalOutput("TSocket::open() select error"); |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame^] | 196 | throw TTransportException(TTransportException::NOT_OPEN, "open()", errno_copy); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | done: |
| 200 | // Set socket back to normal mode (blocking) |
| 201 | fcntl(socket_, F_SETFL, flags); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 204 | void TSocket::open() { |
| 205 | if (isOpen()) { |
| 206 | throw TTransportException(TTransportException::ALREADY_OPEN); |
| 207 | } |
| 208 | |
| 209 | // Validate port number |
| 210 | if (port_ < 0 || port_ > 65536) { |
| 211 | throw TTransportException(TTransportException::NOT_OPEN, "Specified port is invalid"); |
| 212 | } |
| 213 | |
| 214 | struct addrinfo hints, *res, *res0; |
| 215 | int error; |
| 216 | char port[sizeof("65536") + 1]; |
| 217 | memset(&hints, 0, sizeof(hints)); |
| 218 | hints.ai_family = PF_UNSPEC; |
| 219 | hints.ai_socktype = SOCK_STREAM; |
| 220 | hints.ai_flags = AI_PASSIVE; |
| 221 | sprintf(port, "%d", port_); |
| 222 | |
| 223 | { |
| 224 | // Scope lock on host entry lookup |
| 225 | Synchronized s(s_netdb_monitor); |
| 226 | error = getaddrinfo(host_.c_str(), port, &hints, &res0); |
| 227 | } |
| 228 | if (error) { |
| 229 | fprintf(stderr, "getaddrinfo %d: %s\n", error, gai_strerror(error)); |
| 230 | close(); |
| 231 | throw TTransportException(TTransportException::NOT_OPEN, "Could not resolve host for client socket."); |
| 232 | } |
| 233 | |
| 234 | // Cycle through all the returned addresses until one |
| 235 | // connects or push the exception up. |
| 236 | for (res = res0; res; res = res->ai_next) { |
| 237 | try { |
| 238 | openConnection(res); |
| 239 | break; |
| 240 | } catch (TTransportException& ttx) { |
| 241 | if (res->ai_next) { |
| 242 | close(); |
| 243 | } else { |
| 244 | close(); |
Mark Slee | 85287d3 | 2007-07-09 19:50:30 +0000 | [diff] [blame] | 245 | freeaddrinfo(res0); // cleanup on failure |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 246 | throw; |
| 247 | } |
| 248 | } |
| 249 | } |
Mark Slee | 85287d3 | 2007-07-09 19:50:30 +0000 | [diff] [blame] | 250 | |
| 251 | // Free address structure memory |
| 252 | freeaddrinfo(res0); |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 255 | void TSocket::close() { |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 256 | if (socket_ >= 0) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 257 | shutdown(socket_, SHUT_RDWR); |
| 258 | ::close(socket_); |
| 259 | } |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 260 | socket_ = -1; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 263 | uint32_t TSocket::read(uint8_t* buf, uint32_t len) { |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 264 | if (socket_ < 0) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 265 | throw TTransportException(TTransportException::NOT_OPEN, "Called read on non-open socket"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 266 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 267 | |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 268 | int32_t retries = 0; |
| 269 | |
| 270 | // EAGAIN can be signalled both when a timeout has occurred and when |
| 271 | // the system is out of resources (an awesome undocumented feature). |
| 272 | // The following is an approximation of the time interval under which |
| 273 | // EAGAIN is taken to indicate an out of resources error. |
| 274 | uint32_t eagainThresholdMicros = 0; |
| 275 | if (recvTimeout_) { |
| 276 | // if a readTimeout is specified along with a max number of recv retries, then |
| 277 | // the threshold will ensure that the read timeout is not exceeded even in the |
| 278 | // case of resource errors |
| 279 | eagainThresholdMicros = (recvTimeout_*1000)/ ((maxRecvRetries_>0) ? maxRecvRetries_ : 2); |
| 280 | } |
| 281 | |
| 282 | try_again: |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 283 | // Read from the socket |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 284 | struct timeval begin; |
| 285 | gettimeofday(&begin, NULL); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 286 | int got = recv(socket_, buf, len, 0); |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 287 | struct timeval end; |
| 288 | gettimeofday(&end, NULL); |
| 289 | uint32_t readElapsedMicros = (((end.tv_sec - begin.tv_sec) * 1000 * 1000) |
| 290 | + (((uint64_t)(end.tv_usec - begin.tv_usec)))); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 291 | ++g_socket_syscalls; |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 292 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 293 | // Check for error on read |
Mark Slee | c425780 | 2007-01-24 23:14:30 +0000 | [diff] [blame] | 294 | if (got < 0) { |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 295 | if (errno == EAGAIN) { |
| 296 | // check if this is the lack of resources or timeout case |
| 297 | if (!eagainThresholdMicros || (readElapsedMicros < eagainThresholdMicros)) { |
| 298 | if (retries++ < maxRecvRetries_) { |
| 299 | usleep(50); |
| 300 | goto try_again; |
| 301 | } else { |
| 302 | throw TTransportException(TTransportException::TIMED_OUT, |
| 303 | "EAGAIN (unavailable resources)"); |
| 304 | } |
| 305 | } else { |
| 306 | // infer that timeout has been hit |
| 307 | throw TTransportException(TTransportException::TIMED_OUT, |
| 308 | "EAGAIN (timed out)"); |
| 309 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 312 | // If interrupted, try again |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 313 | if (errno == EINTR && retries++ < maxRecvRetries_) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 314 | goto try_again; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Mark Slee | c425780 | 2007-01-24 23:14:30 +0000 | [diff] [blame] | 317 | // Now it's not a try again case, but a real probblez |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 318 | GlobalOutput("TSocket::read()"); |
Mark Slee | c425780 | 2007-01-24 23:14:30 +0000 | [diff] [blame] | 319 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 320 | // If we disconnect with no linger time |
| 321 | if (errno == ECONNRESET) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 322 | throw TTransportException(TTransportException::NOT_OPEN, "ECONNRESET"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | // This ish isn't open |
| 326 | if (errno == ENOTCONN) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 327 | throw TTransportException(TTransportException::NOT_OPEN, "ENOTCONN"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | // Timed out! |
| 331 | if (errno == ETIMEDOUT) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 332 | throw TTransportException(TTransportException::TIMED_OUT, "ETIMEDOUT"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | // Some other error, whatevz |
Mark Slee | 5f68c71 | 2007-05-11 17:58:54 +0000 | [diff] [blame] | 336 | char buff[1024]; |
| 337 | sprintf(buff, "ERROR errno: %d", errno); |
| 338 | throw TTransportException(TTransportException::UNKNOWN, buff); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | // The remote host has closed the socket |
| 342 | if (got == 0) { |
| 343 | close(); |
| 344 | return 0; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | // Pack data into string |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 348 | return got; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 351 | void TSocket::write(const uint8_t* buf, uint32_t len) { |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 352 | if (socket_ < 0) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 353 | throw TTransportException(TTransportException::NOT_OPEN, "Called write on non-open socket"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 356 | uint32_t sent = 0; |
| 357 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 358 | while (sent < len) { |
Marc Slemko | 9d4a3e2 | 2006-07-21 19:53:48 +0000 | [diff] [blame] | 359 | |
| 360 | int flags = 0; |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 361 | #ifdef MSG_NOSIGNAL |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 362 | // Note the use of MSG_NOSIGNAL to suppress SIGPIPE errors, instead we |
| 363 | // check for the EPIPE return condition and close the socket in that case |
Marc Slemko | 9d4a3e2 | 2006-07-21 19:53:48 +0000 | [diff] [blame] | 364 | flags |= MSG_NOSIGNAL; |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 365 | #endif // ifdef MSG_NOSIGNAL |
Marc Slemko | 9d4a3e2 | 2006-07-21 19:53:48 +0000 | [diff] [blame] | 366 | |
| 367 | int b = send(socket_, buf + sent, len - sent, flags); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 368 | ++g_socket_syscalls; |
| 369 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 370 | // Fail on a send error |
| 371 | if (b < 0) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame^] | 372 | if (errno == EPIPE || errno == ECONNRESET || errno == ENOTCONN) { |
| 373 | int errno_copy = errno; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 374 | close(); |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame^] | 375 | throw TTransportException(TTransportException::NOT_OPEN, "send()", errno_copy); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 376 | } |
| 377 | |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame^] | 378 | int errno_copy = errno; |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 379 | GlobalOutput("TSocket::write() send < 0"); |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame^] | 380 | throw TTransportException(TTransportException::UNKNOWN, "send", errno_copy); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | // Fail on blocked send |
| 384 | if (b == 0) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 385 | throw TTransportException(TTransportException::NOT_OPEN, "Socket send returned 0."); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 386 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 387 | sent += b; |
| 388 | } |
| 389 | } |
| 390 | |
Aditya Agarwal | ebc99e0 | 2007-01-15 23:14:58 +0000 | [diff] [blame] | 391 | void TSocket::setHost(string host) { |
| 392 | host_ = host; |
| 393 | } |
| 394 | |
| 395 | void TSocket::setPort(int port) { |
| 396 | port_ = port; |
| 397 | } |
| 398 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 399 | void TSocket::setLinger(bool on, int linger) { |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 400 | lingerOn_ = on; |
| 401 | lingerVal_ = linger; |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 402 | if (socket_ < 0) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 403 | return; |
| 404 | } |
| 405 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 406 | struct linger l = {(lingerOn_ ? 1 : 0), lingerVal_}; |
| 407 | int ret = setsockopt(socket_, SOL_SOCKET, SO_LINGER, &l, sizeof(l)); |
| 408 | if (ret == -1) { |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 409 | GlobalOutput("TSocket::setLinger()"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 410 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 413 | void TSocket::setNoDelay(bool noDelay) { |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 414 | noDelay_ = noDelay; |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 415 | if (socket_ < 0) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 416 | return; |
| 417 | } |
| 418 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 419 | // Set socket to NODELAY |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 420 | int v = noDelay_ ? 1 : 0; |
| 421 | int ret = setsockopt(socket_, IPPROTO_TCP, TCP_NODELAY, &v, sizeof(v)); |
| 422 | if (ret == -1) { |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 423 | GlobalOutput("TSocket::setNoDelay()"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 424 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 425 | } |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 426 | |
| 427 | void TSocket::setConnTimeout(int ms) { |
| 428 | connTimeout_ = ms; |
| 429 | } |
| 430 | |
| 431 | void TSocket::setRecvTimeout(int ms) { |
| 432 | recvTimeout_ = ms; |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 433 | recvTimeval_.tv_sec = (int)(recvTimeout_/1000); |
| 434 | recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000); |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 435 | if (socket_ < 0) { |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 436 | return; |
| 437 | } |
| 438 | |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 439 | // Copy because select may modify |
| 440 | struct timeval r = recvTimeval_; |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 441 | int ret = setsockopt(socket_, SOL_SOCKET, SO_RCVTIMEO, &r, sizeof(r)); |
| 442 | if (ret == -1) { |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 443 | GlobalOutput("TSocket::setRecvTimeout()"); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 444 | } |
| 445 | } |
| 446 | |
| 447 | void TSocket::setSendTimeout(int ms) { |
| 448 | sendTimeout_ = ms; |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 449 | if (socket_ < 0) { |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 450 | return; |
| 451 | } |
| 452 | |
| 453 | struct timeval s = {(int)(sendTimeout_/1000), |
| 454 | (int)((sendTimeout_%1000)*1000)}; |
| 455 | int ret = setsockopt(socket_, SOL_SOCKET, SO_SNDTIMEO, &s, sizeof(s)); |
| 456 | if (ret == -1) { |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 457 | GlobalOutput("TSocket::setSendTimeout()"); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 458 | } |
| 459 | } |
| 460 | |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 461 | void TSocket::setMaxRecvRetries(int maxRecvRetries) { |
| 462 | maxRecvRetries_ = maxRecvRetries; |
| 463 | } |
| 464 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 465 | }}} // facebook::thrift::transport |