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> |
| 9 | #include <arpa/inet.h> |
| 10 | #include <netinet/in.h> |
| 11 | #include <netinet/tcp.h> |
| 12 | #include <netdb.h> |
| 13 | #include <unistd.h> |
| 14 | #include <errno.h> |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 15 | #include <fcntl.h> |
| 16 | #include <sys/select.h> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 17 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 18 | #include "concurrency/Monitor.h" |
Marc Slemko | d42a2c2 | 2006-08-10 03:30:18 +0000 | [diff] [blame] | 19 | #include "TSocket.h" |
| 20 | #include "TTransportException.h" |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 21 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 22 | namespace facebook { namespace thrift { namespace transport { |
| 23 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 24 | using namespace std; |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 25 | using namespace facebook::thrift::concurrency; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 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 | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 36 | // Mutex to protect syscalls to netdb |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 37 | static Monitor s_netdb_monitor; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 38 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 39 | TSocket::TSocket(string host, int port) : |
| 40 | host_(host), |
| 41 | port_(port), |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 42 | socket_(-1), |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 43 | connTimeout_(0), |
| 44 | sendTimeout_(0), |
| 45 | recvTimeout_(0), |
| 46 | lingerOn_(1), |
| 47 | lingerVal_(0), |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 48 | noDelay_(1), |
| 49 | maxRecvRetries_(5) { |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 50 | recvTimeval_.tv_sec = (int)(recvTimeout_/1000); |
| 51 | recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Aditya Agarwal | ebc99e0 | 2007-01-15 23:14:58 +0000 | [diff] [blame] | 54 | TSocket::TSocket() : |
| 55 | host_(""), |
| 56 | port_(0), |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 57 | socket_(-1), |
Aditya Agarwal | ebc99e0 | 2007-01-15 23:14:58 +0000 | [diff] [blame] | 58 | connTimeout_(0), |
| 59 | sendTimeout_(0), |
| 60 | recvTimeout_(0), |
| 61 | lingerOn_(1), |
| 62 | lingerVal_(0), |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 63 | noDelay_(1), |
| 64 | maxRecvRetries_(5) { |
Aditya Agarwal | ebc99e0 | 2007-01-15 23:14:58 +0000 | [diff] [blame] | 65 | recvTimeval_.tv_sec = (int)(recvTimeout_/1000); |
| 66 | recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000); |
| 67 | } |
| 68 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 69 | TSocket::TSocket(int socket) : |
| 70 | host_(""), |
| 71 | port_(0), |
| 72 | socket_(socket), |
| 73 | connTimeout_(0), |
| 74 | sendTimeout_(0), |
| 75 | recvTimeout_(0), |
| 76 | lingerOn_(1), |
| 77 | lingerVal_(0), |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 78 | noDelay_(1), |
| 79 | maxRecvRetries_(5) { |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 80 | recvTimeval_.tv_sec = (int)(recvTimeout_/1000); |
| 81 | recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 84 | TSocket::~TSocket() { |
| 85 | close(); |
| 86 | } |
| 87 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 88 | bool TSocket::isOpen() { |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 89 | return (socket_ >= 0); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 92 | bool TSocket::peek() { |
| 93 | if (!isOpen()) { |
| 94 | return false; |
| 95 | } |
| 96 | uint8_t buf; |
| 97 | int r = recv(socket_, &buf, 1, MSG_PEEK); |
| 98 | if (r == -1) { |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 99 | GlobalOutput("TSocket::peek()"); |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 100 | close(); |
Martin Kraemer | e6c4fa6 | 2007-07-09 19:08:25 +0000 | [diff] [blame] | 101 | char b_error[1024]; |
| 102 | strerror_r(errno, b_error, sizeof(b_error)); |
| 103 | throw TTransportException(TTransportException::UNKNOWN, string("recv() ERROR:") + b_error); |
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 | } |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 112 | |
| 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) { |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 115 | GlobalOutput("TSocket::open() socket"); |
Martin Kraemer | e6c4fa6 | 2007-07-09 19:08:25 +0000 | [diff] [blame] | 116 | char b_error[1024]; |
| 117 | strerror_r(errno, b_error, sizeof(b_error)); |
| 118 | throw TTransportException(TTransportException::NOT_OPEN, string("socket() ERROR:") + b_error); |
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 |
| 138 | int flags = fcntl(socket_, F_GETFL, 0); |
| 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 | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 152 | |
| 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 | e854063 | 2006-05-30 09:24:40 +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) { |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 161 | char buff[1024]; |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 162 | GlobalOutput(buff); |
Martin Kraemer | e6c4fa6 | 2007-07-09 19:08:25 +0000 | [diff] [blame] | 163 | sprintf(buff, "TSocket::open() connect %s %d", host_.c_str(), port_); |
| 164 | |
| 165 | char b_error[1024]; |
| 166 | strerror_r(errno, b_error, sizeof(b_error)); |
| 167 | throw TTransportException(TTransportException::NOT_OPEN, string("open() ERROR: ") + b_error); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 170 | fd_set fds; |
| 171 | FD_ZERO(&fds); |
| 172 | FD_SET(socket_, &fds); |
| 173 | ret = select(socket_+1, NULL, &fds, NULL, &c); |
| 174 | |
| 175 | if (ret > 0) { |
| 176 | // Ensure connected |
| 177 | int val; |
| 178 | socklen_t lon; |
| 179 | lon = sizeof(int); |
| 180 | int ret2 = getsockopt(socket_, SOL_SOCKET, SO_ERROR, (void *)&val, &lon); |
| 181 | if (ret2 == -1) { |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 182 | GlobalOutput("TSocket::open() getsockopt SO_ERROR"); |
Martin Kraemer | e6c4fa6 | 2007-07-09 19:08:25 +0000 | [diff] [blame] | 183 | char b_error[1024]; |
| 184 | strerror_r(errno, b_error, sizeof(b_error)); |
| 185 | throw TTransportException(TTransportException::NOT_OPEN, string("open() ERROR: ") + b_error); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 186 | } |
| 187 | if (val == 0) { |
| 188 | goto done; |
| 189 | } |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 190 | GlobalOutput("TSocket::open() SO_ERROR was set"); |
Martin Kraemer | e6c4fa6 | 2007-07-09 19:08:25 +0000 | [diff] [blame] | 191 | char b_error[1024]; |
| 192 | strerror_r(errno, b_error, sizeof(b_error)); |
| 193 | throw TTransportException(TTransportException::NOT_OPEN, string("open() ERROR: ") + b_error); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 194 | } else if (ret == 0) { |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 195 | GlobalOutput("TSocket::open() timeed out"); |
Martin Kraemer | e6c4fa6 | 2007-07-09 19:08:25 +0000 | [diff] [blame] | 196 | char b_error[1024]; |
| 197 | strerror_r(errno, b_error, sizeof(b_error)); |
| 198 | throw TTransportException(TTransportException::NOT_OPEN, string("open() ERROR: ") + b_error); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 199 | } else { |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 200 | GlobalOutput("TSocket::open() select error"); |
Martin Kraemer | e6c4fa6 | 2007-07-09 19:08:25 +0000 | [diff] [blame] | 201 | char b_error[1024]; |
| 202 | strerror_r(errno, b_error, sizeof(b_error)); |
| 203 | throw TTransportException(TTransportException::NOT_OPEN, string("open() ERROR: ") + b_error); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | done: |
| 207 | // Set socket back to normal mode (blocking) |
| 208 | fcntl(socket_, F_SETFL, flags); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 211 | void TSocket::open() { |
| 212 | if (isOpen()) { |
| 213 | throw TTransportException(TTransportException::ALREADY_OPEN); |
| 214 | } |
| 215 | |
| 216 | // Validate port number |
| 217 | if (port_ < 0 || port_ > 65536) { |
| 218 | throw TTransportException(TTransportException::NOT_OPEN, "Specified port is invalid"); |
| 219 | } |
| 220 | |
| 221 | struct addrinfo hints, *res, *res0; |
| 222 | int error; |
| 223 | char port[sizeof("65536") + 1]; |
| 224 | memset(&hints, 0, sizeof(hints)); |
| 225 | hints.ai_family = PF_UNSPEC; |
| 226 | hints.ai_socktype = SOCK_STREAM; |
| 227 | hints.ai_flags = AI_PASSIVE; |
| 228 | sprintf(port, "%d", port_); |
| 229 | |
| 230 | { |
| 231 | // Scope lock on host entry lookup |
| 232 | Synchronized s(s_netdb_monitor); |
| 233 | error = getaddrinfo(host_.c_str(), port, &hints, &res0); |
| 234 | } |
| 235 | if (error) { |
| 236 | fprintf(stderr, "getaddrinfo %d: %s\n", error, gai_strerror(error)); |
| 237 | close(); |
| 238 | throw TTransportException(TTransportException::NOT_OPEN, "Could not resolve host for client socket."); |
| 239 | } |
| 240 | |
| 241 | // Cycle through all the returned addresses until one |
| 242 | // connects or push the exception up. |
| 243 | for (res = res0; res; res = res->ai_next) { |
| 244 | try { |
| 245 | openConnection(res); |
| 246 | break; |
| 247 | } catch (TTransportException& ttx) { |
| 248 | if (res->ai_next) { |
| 249 | close(); |
| 250 | } else { |
| 251 | close(); |
Mark Slee | 85287d3 | 2007-07-09 19:50:30 +0000 | [diff] [blame^] | 252 | freeaddrinfo(res0); // cleanup on failure |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 253 | throw; |
| 254 | } |
| 255 | } |
| 256 | } |
Mark Slee | 85287d3 | 2007-07-09 19:50:30 +0000 | [diff] [blame^] | 257 | |
| 258 | // Free address structure memory |
| 259 | freeaddrinfo(res0); |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 262 | void TSocket::close() { |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 263 | if (socket_ >= 0) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 264 | shutdown(socket_, SHUT_RDWR); |
| 265 | ::close(socket_); |
| 266 | } |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 267 | socket_ = -1; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 270 | uint32_t TSocket::read(uint8_t* buf, uint32_t len) { |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 271 | if (socket_ < 0) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 272 | throw TTransportException(TTransportException::NOT_OPEN, "Called read on non-open socket"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 273 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 274 | |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 275 | int32_t retries = 0; |
| 276 | |
| 277 | // EAGAIN can be signalled both when a timeout has occurred and when |
| 278 | // the system is out of resources (an awesome undocumented feature). |
| 279 | // The following is an approximation of the time interval under which |
| 280 | // EAGAIN is taken to indicate an out of resources error. |
| 281 | uint32_t eagainThresholdMicros = 0; |
| 282 | if (recvTimeout_) { |
| 283 | // if a readTimeout is specified along with a max number of recv retries, then |
| 284 | // the threshold will ensure that the read timeout is not exceeded even in the |
| 285 | // case of resource errors |
| 286 | eagainThresholdMicros = (recvTimeout_*1000)/ ((maxRecvRetries_>0) ? maxRecvRetries_ : 2); |
| 287 | } |
| 288 | |
| 289 | try_again: |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 290 | // Read from the socket |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 291 | struct timeval begin; |
| 292 | gettimeofday(&begin, NULL); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 293 | int got = recv(socket_, buf, len, 0); |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 294 | struct timeval end; |
| 295 | gettimeofday(&end, NULL); |
| 296 | uint32_t readElapsedMicros = (((end.tv_sec - begin.tv_sec) * 1000 * 1000) |
| 297 | + (((uint64_t)(end.tv_usec - begin.tv_usec)))); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 298 | ++g_socket_syscalls; |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 299 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 300 | // Check for error on read |
Mark Slee | c425780 | 2007-01-24 23:14:30 +0000 | [diff] [blame] | 301 | if (got < 0) { |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 302 | if (errno == EAGAIN) { |
| 303 | // check if this is the lack of resources or timeout case |
| 304 | if (!eagainThresholdMicros || (readElapsedMicros < eagainThresholdMicros)) { |
| 305 | if (retries++ < maxRecvRetries_) { |
| 306 | usleep(50); |
| 307 | goto try_again; |
| 308 | } else { |
| 309 | throw TTransportException(TTransportException::TIMED_OUT, |
| 310 | "EAGAIN (unavailable resources)"); |
| 311 | } |
| 312 | } else { |
| 313 | // infer that timeout has been hit |
| 314 | throw TTransportException(TTransportException::TIMED_OUT, |
| 315 | "EAGAIN (timed out)"); |
| 316 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 319 | // If interrupted, try again |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 320 | if (errno == EINTR && retries++ < maxRecvRetries_) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 321 | goto try_again; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Mark Slee | c425780 | 2007-01-24 23:14:30 +0000 | [diff] [blame] | 324 | // Now it's not a try again case, but a real probblez |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 325 | GlobalOutput("TSocket::read()"); |
Mark Slee | c425780 | 2007-01-24 23:14:30 +0000 | [diff] [blame] | 326 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 327 | // If we disconnect with no linger time |
| 328 | if (errno == ECONNRESET) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 329 | throw TTransportException(TTransportException::NOT_OPEN, "ECONNRESET"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | // This ish isn't open |
| 333 | if (errno == ENOTCONN) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 334 | throw TTransportException(TTransportException::NOT_OPEN, "ENOTCONN"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | // Timed out! |
| 338 | if (errno == ETIMEDOUT) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 339 | throw TTransportException(TTransportException::TIMED_OUT, "ETIMEDOUT"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | // Some other error, whatevz |
Mark Slee | 5f68c71 | 2007-05-11 17:58:54 +0000 | [diff] [blame] | 343 | char buff[1024]; |
| 344 | sprintf(buff, "ERROR errno: %d", errno); |
| 345 | throw TTransportException(TTransportException::UNKNOWN, buff); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | // The remote host has closed the socket |
| 349 | if (got == 0) { |
| 350 | close(); |
| 351 | return 0; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | // Pack data into string |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 355 | return got; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 356 | } |
| 357 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 358 | void TSocket::write(const uint8_t* buf, uint32_t len) { |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 359 | if (socket_ < 0) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 360 | throw TTransportException(TTransportException::NOT_OPEN, "Called write on non-open socket"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 363 | uint32_t sent = 0; |
| 364 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 365 | while (sent < len) { |
Marc Slemko | 9d4a3e2 | 2006-07-21 19:53:48 +0000 | [diff] [blame] | 366 | |
| 367 | int flags = 0; |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 368 | #ifdef MSG_NOSIGNAL |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 369 | // Note the use of MSG_NOSIGNAL to suppress SIGPIPE errors, instead we |
| 370 | // 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] | 371 | flags |= MSG_NOSIGNAL; |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 372 | #endif // ifdef MSG_NOSIGNAL |
Marc Slemko | 9d4a3e2 | 2006-07-21 19:53:48 +0000 | [diff] [blame] | 373 | |
| 374 | int b = send(socket_, buf + sent, len - sent, flags); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 375 | ++g_socket_syscalls; |
| 376 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 377 | // Fail on a send error |
| 378 | if (b < 0) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 379 | if (errno == EPIPE) { |
| 380 | close(); |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 381 | throw TTransportException(TTransportException::NOT_OPEN, "EPIPE"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | if (errno == ECONNRESET) { |
| 385 | close(); |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 386 | throw TTransportException(TTransportException::NOT_OPEN, "ECONNRESET"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | if (errno == ENOTCONN) { |
| 390 | close(); |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 391 | throw TTransportException(TTransportException::NOT_OPEN, "ENOTCONN"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 392 | } |
| 393 | |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 394 | GlobalOutput("TSocket::write() send < 0"); |
Martin Kraemer | e6c4fa6 | 2007-07-09 19:08:25 +0000 | [diff] [blame] | 395 | char b_error[1024]; |
| 396 | strerror_r(errno, b_error, sizeof(b_error)); |
| 397 | throw TTransportException(TTransportException::UNKNOWN, string("ERROR:") + b_error); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | // Fail on blocked send |
| 401 | if (b == 0) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 402 | throw TTransportException(TTransportException::NOT_OPEN, "Socket send returned 0."); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 403 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 404 | sent += b; |
| 405 | } |
| 406 | } |
| 407 | |
Aditya Agarwal | ebc99e0 | 2007-01-15 23:14:58 +0000 | [diff] [blame] | 408 | void TSocket::setHost(string host) { |
| 409 | host_ = host; |
| 410 | } |
| 411 | |
| 412 | void TSocket::setPort(int port) { |
| 413 | port_ = port; |
| 414 | } |
| 415 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 416 | void TSocket::setLinger(bool on, int linger) { |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 417 | lingerOn_ = on; |
| 418 | lingerVal_ = linger; |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 419 | if (socket_ < 0) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 420 | return; |
| 421 | } |
| 422 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 423 | struct linger l = {(lingerOn_ ? 1 : 0), lingerVal_}; |
| 424 | int ret = setsockopt(socket_, SOL_SOCKET, SO_LINGER, &l, sizeof(l)); |
| 425 | if (ret == -1) { |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 426 | GlobalOutput("TSocket::setLinger()"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 427 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 430 | void TSocket::setNoDelay(bool noDelay) { |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 431 | noDelay_ = noDelay; |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 432 | if (socket_ < 0) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 433 | return; |
| 434 | } |
| 435 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 436 | // Set socket to NODELAY |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 437 | int v = noDelay_ ? 1 : 0; |
| 438 | int ret = setsockopt(socket_, IPPROTO_TCP, TCP_NODELAY, &v, sizeof(v)); |
| 439 | if (ret == -1) { |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 440 | GlobalOutput("TSocket::setNoDelay()"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 441 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 442 | } |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 443 | |
| 444 | void TSocket::setConnTimeout(int ms) { |
| 445 | connTimeout_ = ms; |
| 446 | } |
| 447 | |
| 448 | void TSocket::setRecvTimeout(int ms) { |
| 449 | recvTimeout_ = ms; |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 450 | recvTimeval_.tv_sec = (int)(recvTimeout_/1000); |
| 451 | recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000); |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 452 | if (socket_ < 0) { |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 453 | return; |
| 454 | } |
| 455 | |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 456 | // Copy because select may modify |
| 457 | struct timeval r = recvTimeval_; |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 458 | int ret = setsockopt(socket_, SOL_SOCKET, SO_RCVTIMEO, &r, sizeof(r)); |
| 459 | if (ret == -1) { |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 460 | GlobalOutput("TSocket::setRecvTimeout()"); |
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 | } |
| 469 | |
| 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) { |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 474 | GlobalOutput("TSocket::setSendTimeout()"); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 475 | } |
| 476 | } |
| 477 | |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 478 | void TSocket::setMaxRecvRetries(int maxRecvRetries) { |
| 479 | maxRecvRetries_ = maxRecvRetries; |
| 480 | } |
| 481 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 482 | }}} // facebook::thrift::transport |