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