Marc Slemko | e03da18 | 2006-07-21 21:32:36 +0000 | [diff] [blame] | 1 | #include <config.h> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 2 | #include <sys/socket.h> |
| 3 | #include <arpa/inet.h> |
| 4 | #include <netinet/in.h> |
| 5 | #include <netinet/tcp.h> |
| 6 | #include <netdb.h> |
| 7 | #include <unistd.h> |
| 8 | #include <errno.h> |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame^] | 9 | #include <fcntl.h> |
| 10 | #include <sys/select.h> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 11 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame^] | 12 | #include "concurrency/Monitor.h" |
Marc Slemko | d42a2c2 | 2006-08-10 03:30:18 +0000 | [diff] [blame] | 13 | #include "TSocket.h" |
| 14 | #include "TTransportException.h" |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 15 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 16 | namespace facebook { namespace thrift { namespace transport { |
| 17 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 18 | using namespace std; |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame^] | 19 | using namespace facebook::thrift::concurrency; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 20 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame^] | 21 | // Global var to track total socket sys calls |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 22 | uint32_t g_socket_syscalls = 0; |
| 23 | |
| 24 | /** |
| 25 | * TSocket implementation. |
| 26 | * |
| 27 | * @author Mark Slee <mcslee@facebook.com> |
| 28 | */ |
| 29 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 30 | // Mutex to protect syscalls to netdb |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame^] | 31 | static Monitor s_netdb_monitor; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 32 | |
| 33 | // TODO(mcslee): Make this an option to the socket class |
| 34 | #define MAX_RECV_RETRIES 20 |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame^] | 35 | |
| 36 | TSocket::TSocket(string host, int port) : |
| 37 | host_(host), |
| 38 | port_(port), |
| 39 | socket_(0), |
| 40 | connTimeout_(0), |
| 41 | sendTimeout_(0), |
| 42 | recvTimeout_(0), |
| 43 | lingerOn_(1), |
| 44 | lingerVal_(0), |
| 45 | noDelay_(1) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame^] | 48 | TSocket::TSocket(int socket) : |
| 49 | host_(""), |
| 50 | port_(0), |
| 51 | socket_(socket), |
| 52 | connTimeout_(0), |
| 53 | sendTimeout_(0), |
| 54 | recvTimeout_(0), |
| 55 | lingerOn_(1), |
| 56 | lingerVal_(0), |
| 57 | noDelay_(1) { |
| 58 | } |
| 59 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 60 | TSocket::~TSocket() { |
| 61 | close(); |
| 62 | } |
| 63 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 64 | bool TSocket::isOpen() { |
| 65 | return (socket_ > 0); |
| 66 | } |
| 67 | |
| 68 | void TSocket::open() { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 69 | // Create socket |
| 70 | socket_ = socket(AF_INET, SOCK_STREAM, 0); |
| 71 | if (socket_ == -1) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 72 | perror("TSocket::open() socket"); |
| 73 | close(); |
| 74 | throw TTransportException(TTX_NOT_OPEN, "socket() ERROR:" + errno); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 75 | } |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame^] | 76 | |
| 77 | // Send timeout |
| 78 | if (sendTimeout_ > 0) { |
| 79 | setSendTimeout(sendTimeout_); |
| 80 | } |
| 81 | |
| 82 | // Recv timeout |
| 83 | if (recvTimeout_ > 0) { |
| 84 | setRecvTimeout(recvTimeout_); |
| 85 | } |
| 86 | |
| 87 | // Linger |
| 88 | setLinger(lingerOn_, lingerVal_); |
| 89 | |
| 90 | // No delay |
| 91 | setNoDelay(noDelay_); |
| 92 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 93 | // Lookup the hostname |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 94 | struct sockaddr_in addr; |
| 95 | addr.sin_family = AF_INET; |
| 96 | addr.sin_port = htons(port_); |
| 97 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 98 | { |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame^] | 99 | // Scope lock on host entry lookup |
| 100 | Synchronized s(s_netdb_monitor); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 101 | struct hostent *host_entry = gethostbyname(host_.c_str()); |
| 102 | |
| 103 | if (host_entry == NULL) { |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame^] | 104 | perror("TSocket: dns error: failed call to gethostbyname."); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 105 | close(); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 106 | throw TTransportException(TTX_NOT_OPEN, "gethostbyname() failed"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | addr.sin_port = htons(port_); |
| 110 | memcpy(&addr.sin_addr.s_addr, |
| 111 | host_entry->h_addr_list[0], |
| 112 | host_entry->h_length); |
| 113 | } |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame^] | 114 | |
| 115 | // Set the socket to be non blocking for connect if a timeout exists |
| 116 | int flags = fcntl(socket_, F_GETFL, 0); |
| 117 | if (connTimeout_ > 0) { |
| 118 | fcntl(socket_, F_SETFL, flags | O_NONBLOCK); |
| 119 | } else { |
| 120 | fcntl(socket_, F_SETFL, flags | ~O_NONBLOCK); |
| 121 | } |
| 122 | |
| 123 | // Conn timeout |
| 124 | struct timeval c = {(int)(connTimeout_/1000), |
| 125 | (int)((connTimeout_%1000)*1000)}; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 126 | |
| 127 | // Connect the socket |
| 128 | int ret = connect(socket_, (struct sockaddr *)&addr, sizeof(addr)); |
| 129 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame^] | 130 | if (ret == 0) { |
| 131 | goto done; |
| 132 | } |
| 133 | |
| 134 | if (errno != EINPROGRESS) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 135 | close(); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame^] | 136 | char buff[1024]; |
| 137 | sprintf(buff, "TSocket::open() connect %s %d", host_.c_str(), port_); |
| 138 | perror(buff); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 139 | throw TTransportException(TTX_NOT_OPEN, "open() ERROR: " + errno); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame^] | 142 | fd_set fds; |
| 143 | FD_ZERO(&fds); |
| 144 | FD_SET(socket_, &fds); |
| 145 | ret = select(socket_+1, NULL, &fds, NULL, &c); |
| 146 | |
| 147 | if (ret > 0) { |
| 148 | // Ensure connected |
| 149 | int val; |
| 150 | socklen_t lon; |
| 151 | lon = sizeof(int); |
| 152 | int ret2 = getsockopt(socket_, SOL_SOCKET, SO_ERROR, (void *)&val, &lon); |
| 153 | if (ret2 == -1) { |
| 154 | close(); |
| 155 | perror("TSocket::open() getsockopt SO_ERROR"); |
| 156 | throw TTransportException(TTX_NOT_OPEN, "open() ERROR: " + errno); |
| 157 | } |
| 158 | if (val == 0) { |
| 159 | goto done; |
| 160 | } |
| 161 | close(); |
| 162 | perror("TSocket::open() SO_ERROR was set"); |
| 163 | throw TTransportException(TTX_NOT_OPEN, "open() ERROR: " + errno); |
| 164 | } else if (ret == 0) { |
| 165 | close(); |
| 166 | perror("TSocket::open() timeed out"); |
| 167 | throw TTransportException(TTX_NOT_OPEN, "open() ERROR: " + errno); |
| 168 | } else { |
| 169 | close(); |
| 170 | perror("TSocket::open() select error"); |
| 171 | throw TTransportException(TTX_NOT_OPEN, "open() ERROR: " + errno); |
| 172 | } |
| 173 | |
| 174 | done: |
| 175 | // Set socket back to normal mode (blocking) |
| 176 | fcntl(socket_, F_SETFL, flags); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | void TSocket::close() { |
| 180 | if (socket_ > 0) { |
| 181 | shutdown(socket_, SHUT_RDWR); |
| 182 | ::close(socket_); |
| 183 | } |
| 184 | socket_ = 0; |
| 185 | } |
| 186 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 187 | uint32_t TSocket::read(uint8_t* buf, uint32_t len) { |
| 188 | if (socket_ <= 0) { |
| 189 | throw TTransportException(TTX_NOT_OPEN, "Called read on non-open socket"); |
| 190 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 191 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 192 | uint32_t retries = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 193 | |
| 194 | try_again: |
| 195 | // Read from the socket |
| 196 | int got = recv(socket_, buf, len, 0); |
| 197 | ++g_socket_syscalls; |
| 198 | |
| 199 | // Check for error on read |
| 200 | if (got < 0) { |
| 201 | perror("TSocket::read()"); |
| 202 | |
| 203 | // If temporarily out of resources, sleep a bit and try again |
| 204 | if (errno == EAGAIN && retries++ < MAX_RECV_RETRIES) { |
| 205 | usleep(50); |
| 206 | goto try_again; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 209 | // If interrupted, try again |
| 210 | if (errno == EINTR && retries++ < MAX_RECV_RETRIES) { |
| 211 | goto try_again; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 214 | // If we disconnect with no linger time |
| 215 | if (errno == ECONNRESET) { |
| 216 | throw TTransportException(TTX_NOT_OPEN, "ECONNRESET"); |
| 217 | } |
| 218 | |
| 219 | // This ish isn't open |
| 220 | if (errno == ENOTCONN) { |
| 221 | throw TTransportException(TTX_NOT_OPEN, "ENOTCONN"); |
| 222 | } |
| 223 | |
| 224 | // Timed out! |
| 225 | if (errno == ETIMEDOUT) { |
| 226 | throw TTransportException(TTX_TIMED_OUT, "ETIMEDOUT"); |
| 227 | } |
| 228 | |
| 229 | // Some other error, whatevz |
| 230 | throw TTransportException(TTX_UNKNOWN, "ERROR:" + errno); |
| 231 | } |
| 232 | |
| 233 | // The remote host has closed the socket |
| 234 | if (got == 0) { |
| 235 | close(); |
| 236 | return 0; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | // Pack data into string |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 240 | return got; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 243 | void TSocket::write(const uint8_t* buf, uint32_t len) { |
| 244 | if (socket_ <= 0) { |
| 245 | throw TTransportException(TTX_NOT_OPEN, "Called write on non-open socket"); |
| 246 | } |
| 247 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 248 | uint32_t sent = 0; |
| 249 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 250 | while (sent < len) { |
Marc Slemko | 9d4a3e2 | 2006-07-21 19:53:48 +0000 | [diff] [blame] | 251 | |
| 252 | int flags = 0; |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame^] | 253 | #ifdef MSG_NOSIGNAL |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 254 | // Note the use of MSG_NOSIGNAL to suppress SIGPIPE errors, instead we |
| 255 | // 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] | 256 | flags |= MSG_NOSIGNAL; |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame^] | 257 | #endif // ifdef MSG_NOSIGNAL |
Marc Slemko | 9d4a3e2 | 2006-07-21 19:53:48 +0000 | [diff] [blame] | 258 | |
| 259 | int b = send(socket_, buf + sent, len - sent, flags); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 260 | ++g_socket_syscalls; |
| 261 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 262 | // Fail on a send error |
| 263 | if (b < 0) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 264 | if (errno == EPIPE) { |
| 265 | close(); |
| 266 | throw TTransportException(TTX_NOT_OPEN, "EPIPE"); |
| 267 | } |
| 268 | |
| 269 | if (errno == ECONNRESET) { |
| 270 | close(); |
| 271 | throw TTransportException(TTX_NOT_OPEN, "ECONNRESET"); |
| 272 | } |
| 273 | |
| 274 | if (errno == ENOTCONN) { |
| 275 | close(); |
| 276 | throw TTransportException(TTX_NOT_OPEN, "ENOTCONN"); |
| 277 | } |
| 278 | |
| 279 | perror("TSocket::write() send < 0"); |
| 280 | throw TTransportException(TTX_UNKNOWN, "ERROR:" + errno); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | // Fail on blocked send |
| 284 | if (b == 0) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 285 | throw TTransportException(TTX_NOT_OPEN, "Socket send returned 0."); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 286 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 287 | sent += b; |
| 288 | } |
| 289 | } |
| 290 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 291 | void TSocket::setLinger(bool on, int linger) { |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame^] | 292 | lingerOn_ = on; |
| 293 | lingerVal_ = linger; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 294 | if (socket_ <= 0) { |
| 295 | return; |
| 296 | } |
| 297 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame^] | 298 | struct linger l = {(lingerOn_ ? 1 : 0), lingerVal_}; |
| 299 | int ret = setsockopt(socket_, SOL_SOCKET, SO_LINGER, &l, sizeof(l)); |
| 300 | if (ret == -1) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 301 | perror("TSocket::setLinger()"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 302 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 305 | void TSocket::setNoDelay(bool noDelay) { |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame^] | 306 | noDelay_ = noDelay; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 307 | if (socket_ <= 0) { |
| 308 | return; |
| 309 | } |
| 310 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 311 | // Set socket to NODELAY |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame^] | 312 | int v = noDelay_ ? 1 : 0; |
| 313 | int ret = setsockopt(socket_, IPPROTO_TCP, TCP_NODELAY, &v, sizeof(v)); |
| 314 | if (ret == -1) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 315 | perror("TSocket::setNoDelay()"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 316 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 317 | } |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame^] | 318 | |
| 319 | void TSocket::setConnTimeout(int ms) { |
| 320 | connTimeout_ = ms; |
| 321 | } |
| 322 | |
| 323 | void TSocket::setRecvTimeout(int ms) { |
| 324 | recvTimeout_ = ms; |
| 325 | if (socket_ <= 0) { |
| 326 | return; |
| 327 | } |
| 328 | |
| 329 | struct timeval r = {(int)(recvTimeout_/1000), |
| 330 | (int)((recvTimeout_%1000)*1000)}; |
| 331 | int ret = setsockopt(socket_, SOL_SOCKET, SO_RCVTIMEO, &r, sizeof(r)); |
| 332 | if (ret == -1) { |
| 333 | perror("TSocket::setRecvTimeout()"); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | void TSocket::setSendTimeout(int ms) { |
| 338 | sendTimeout_ = ms; |
| 339 | if (socket_ <= 0) { |
| 340 | return; |
| 341 | } |
| 342 | |
| 343 | struct timeval s = {(int)(sendTimeout_/1000), |
| 344 | (int)((sendTimeout_%1000)*1000)}; |
| 345 | int ret = setsockopt(socket_, SOL_SOCKET, SO_SNDTIMEO, &s, sizeof(s)); |
| 346 | if (ret == -1) { |
| 347 | perror("TSocket::setSendTimeout()"); |
| 348 | } |
| 349 | } |
| 350 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 351 | }}} // facebook::thrift::transport |