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 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 7 | #include <sys/socket.h> |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 8 | #include <sys/select.h> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 9 | #include <netinet/in.h> |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 10 | #include <netinet/tcp.h> |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 11 | #include <fcntl.h> |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 12 | #include <errno.h> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 13 | |
Marc Slemko | d42a2c2 | 2006-08-10 03:30:18 +0000 | [diff] [blame] | 14 | #include "TSocket.h" |
| 15 | #include "TServerSocket.h" |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 16 | #include <boost/shared_ptr.hpp> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 17 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 18 | namespace facebook { namespace thrift { namespace transport { |
| 19 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 20 | using namespace boost; |
| 21 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 22 | TServerSocket::TServerSocket(int port) : |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 23 | port_(port), |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 24 | serverSocket_(-1), |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 25 | acceptBacklog_(1024), |
| 26 | sendTimeout_(0), |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 27 | recvTimeout_(0), |
boz | 1ea81ce | 2007-05-14 23:04:33 +0000 | [diff] [blame] | 28 | retryLimit_(0), |
| 29 | retryDelay_(0), |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 30 | intSock1_(-1), |
| 31 | intSock2_(-1) {} |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 32 | |
| 33 | TServerSocket::TServerSocket(int port, int sendTimeout, int recvTimeout) : |
| 34 | port_(port), |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 35 | serverSocket_(-1), |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 36 | acceptBacklog_(1024), |
| 37 | sendTimeout_(sendTimeout), |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 38 | recvTimeout_(recvTimeout), |
boz | 1ea81ce | 2007-05-14 23:04:33 +0000 | [diff] [blame] | 39 | retryLimit_(0), |
| 40 | retryDelay_(0), |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 41 | intSock1_(-1), |
| 42 | intSock2_(-1) {} |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 43 | |
| 44 | TServerSocket::~TServerSocket() { |
| 45 | close(); |
| 46 | } |
| 47 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 48 | void TServerSocket::setSendTimeout(int sendTimeout) { |
| 49 | sendTimeout_ = sendTimeout; |
| 50 | } |
| 51 | |
| 52 | void TServerSocket::setRecvTimeout(int recvTimeout) { |
| 53 | recvTimeout_ = recvTimeout; |
| 54 | } |
| 55 | |
boz | 1ea81ce | 2007-05-14 23:04:33 +0000 | [diff] [blame] | 56 | void TServerSocket::setRetryLimit(int retryLimit) { |
| 57 | retryLimit_ = retryLimit; |
| 58 | } |
| 59 | |
| 60 | void TServerSocket::setRetryDelay(int retryDelay) { |
| 61 | retryDelay_ = retryDelay; |
| 62 | } |
| 63 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 64 | void TServerSocket::listen() { |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 65 | int sv[2]; |
| 66 | if (-1 == socketpair(AF_LOCAL, SOCK_STREAM, 0, sv)) { |
| 67 | perror("TServerSocket::init()"); |
| 68 | intSock1_ = -1; |
| 69 | intSock2_ = -1; |
| 70 | } else { |
| 71 | intSock1_ = sv[0]; |
| 72 | intSock2_ = sv[1]; |
| 73 | } |
| 74 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 75 | serverSocket_ = socket(AF_INET, SOCK_STREAM, 0); |
| 76 | if (serverSocket_ == -1) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 77 | perror("TServerSocket::listen() socket"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 78 | close(); |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 79 | throw TTransportException(TTransportException::NOT_OPEN, "Could not create server socket."); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | // Set reusaddress to prevent 2MSL delay on accept |
| 83 | int one = 1; |
| 84 | if (-1 == setsockopt(serverSocket_, SOL_SOCKET, SO_REUSEADDR, |
| 85 | &one, sizeof(one))) { |
| 86 | perror("TServerSocket::listen() SO_REUSEADDR"); |
| 87 | close(); |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 88 | throw TTransportException(TTransportException::NOT_OPEN, "Could not set SO_REUSEADDR"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 91 | // Defer accept |
| 92 | #ifdef TCP_DEFER_ACCEPT |
| 93 | if (-1 == setsockopt(serverSocket_, SOL_SOCKET, TCP_DEFER_ACCEPT, |
| 94 | &one, sizeof(one))) { |
| 95 | perror("TServerSocket::listen() TCP_DEFER_ACCEPT"); |
| 96 | close(); |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 97 | throw TTransportException(TTransportException::NOT_OPEN, "Could not set TCP_DEFER_ACCEPT"); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 98 | } |
| 99 | #endif // #ifdef TCP_DEFER_ACCEPT |
| 100 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 101 | // Turn linger off, don't want to block on calls to close |
| 102 | struct linger ling = {0, 0}; |
| 103 | if (-1 == setsockopt(serverSocket_, SOL_SOCKET, SO_LINGER, |
| 104 | &ling, sizeof(ling))) { |
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 | perror("TServerSocket::listen() SO_LINGER"); |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 107 | throw TTransportException(TTransportException::NOT_OPEN, "Could not set SO_LINGER"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 110 | // TCP Nodelay, speed over bandwidth |
| 111 | if (-1 == setsockopt(serverSocket_, IPPROTO_TCP, TCP_NODELAY, |
| 112 | &one, sizeof(one))) { |
| 113 | close(); |
| 114 | perror("setsockopt TCP_NODELAY"); |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 115 | throw TTransportException(TTransportException::NOT_OPEN, "Could not set TCP_NODELAY"); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 118 | // Set NONBLOCK on the accept socket |
| 119 | int flags = fcntl(serverSocket_, F_GETFL, 0); |
| 120 | if (flags == -1) { |
| 121 | throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed"); |
| 122 | } |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 123 | |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 124 | if (-1 == fcntl(serverSocket_, F_SETFL, flags | O_NONBLOCK)) { |
| 125 | throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed"); |
| 126 | } |
| 127 | |
boz | 1ea81ce | 2007-05-14 23:04:33 +0000 | [diff] [blame] | 128 | // prepare the port information |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 129 | struct sockaddr_in addr; |
| 130 | memset(&addr, 0, sizeof(addr)); |
| 131 | addr.sin_family = AF_INET; |
| 132 | addr.sin_port = htons(port_); |
| 133 | addr.sin_addr.s_addr = INADDR_ANY; |
boz | 1ea81ce | 2007-05-14 23:04:33 +0000 | [diff] [blame] | 134 | |
| 135 | // we may want to try to bind more than once, since SO_REUSEADDR doesn't |
| 136 | // always seem to work. The client can configure the retry variables. |
| 137 | int retries = 0; |
| 138 | do { |
| 139 | if (0 == bind(serverSocket_, (struct sockaddr *)&addr, sizeof(addr))) { |
| 140 | break; |
| 141 | } |
| 142 | |
| 143 | // use short circuit evaluation here to only sleep if we need to |
| 144 | } while ((retries++ < retryLimit_) && (sleep(retryDelay_) == 0)); |
| 145 | |
| 146 | // throw an error if we failed to bind properly |
| 147 | if (retries > retryLimit_) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 148 | char errbuf[1024]; |
| 149 | sprintf(errbuf, "TServerSocket::listen() BIND %d", port_); |
| 150 | perror(errbuf); |
| 151 | close(); |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 152 | throw TTransportException(TTransportException::NOT_OPEN, "Could not bind"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | // Call listen |
| 156 | if (-1 == ::listen(serverSocket_, acceptBacklog_)) { |
| 157 | perror("TServerSocket::listen() LISTEN"); |
| 158 | close(); |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 159 | throw TTransportException(TTransportException::NOT_OPEN, "Could not listen"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | // The socket is now listening! |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 165 | shared_ptr<TTransport> TServerSocket::acceptImpl() { |
Martin Kraemer | 10640d8 | 2007-02-03 01:59:12 +0000 | [diff] [blame] | 166 | if (serverSocket_ < 0) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 167 | throw TTransportException(TTransportException::NOT_OPEN, "TServerSocket not listening"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 170 | fd_set fds; |
| 171 | |
Aditya Agarwal | 7859a57 | 2007-05-31 01:33:07 +0000 | [diff] [blame] | 172 | int maxEintrs = 5; |
| 173 | int numEintrs = 0; |
| 174 | |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 175 | while (true) { |
| 176 | FD_ZERO(&fds); |
| 177 | FD_SET(serverSocket_, &fds); |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 178 | if (intSock2_ >= 0) { |
| 179 | FD_SET(intSock2_, &fds); |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 180 | } |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 181 | int ret = select(serverSocket_+1, &fds, NULL, NULL, NULL); |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 182 | |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 183 | if (ret < 0) { |
Aditya Agarwal | 7859a57 | 2007-05-31 01:33:07 +0000 | [diff] [blame] | 184 | // error cases |
| 185 | if (error == EINTR && (numEintrs++ < maxEintrs)) { |
| 186 | // EINTR needs to be handled manually and we can tolerate |
| 187 | // a certain number |
| 188 | continue; |
| 189 | } |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 190 | perror("TServerSocket::acceptImpl() select -1"); |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 191 | throw TTransportException(TTransportException::UNKNOWN); |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 192 | } else if (ret > 0) { |
| 193 | // Check for an interrupt signal |
| 194 | if (intSock2_ >= 0 && FD_ISSET(intSock2_, &fds)) { |
| 195 | int8_t buf; |
| 196 | if (-1 == recv(intSock2_, &buf, sizeof(int8_t), 0)) { |
| 197 | perror("TServerSocket::acceptImpl() interrupt receive"); |
| 198 | } |
| 199 | throw TTransportException(TTransportException::INTERRUPTED); |
| 200 | } |
| 201 | // Check for the actual server socket being ready |
| 202 | if (FD_ISSET(serverSocket_, &fds)) { |
| 203 | break; |
| 204 | } |
| 205 | } else { |
| 206 | perror("TServerSocket::acceptImpl() select 0"); |
| 207 | throw TTransportException(TTransportException::UNKNOWN); |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 211 | struct sockaddr_in clientAddress; |
| 212 | int size = sizeof(clientAddress); |
| 213 | int clientSocket = ::accept(serverSocket_, |
| 214 | (struct sockaddr *) &clientAddress, |
| 215 | (socklen_t *) &size); |
| 216 | |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 217 | if (clientSocket < 0) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 218 | perror("TServerSocket::accept()"); |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 219 | throw TTransportException(TTransportException::UNKNOWN, "ERROR:" + errno); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 220 | } |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 221 | |
| 222 | // Make sure client socket is blocking |
| 223 | int flags = fcntl(clientSocket, F_GETFL, 0); |
| 224 | if (flags == -1) { |
| 225 | perror("TServerSocket::select() fcntl GETFL"); |
| 226 | throw TTransportException(TTransportException::UNKNOWN, "ERROR:" + errno); |
| 227 | } |
| 228 | if (-1 == fcntl(clientSocket, F_SETFL, flags & ~O_NONBLOCK)) { |
| 229 | perror("TServerSocket::select() fcntl SETFL"); |
| 230 | throw TTransportException(TTransportException::UNKNOWN, "ERROR:" + errno); |
| 231 | } |
Martin Kraemer | 10640d8 | 2007-02-03 01:59:12 +0000 | [diff] [blame] | 232 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 233 | shared_ptr<TSocket> client(new TSocket(clientSocket)); |
| 234 | if (sendTimeout_ > 0) { |
| 235 | client->setSendTimeout(sendTimeout_); |
| 236 | } |
| 237 | if (recvTimeout_ > 0) { |
| 238 | client->setRecvTimeout(recvTimeout_); |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 239 | } |
Aditya Agarwal | 7859a57 | 2007-05-31 01:33:07 +0000 | [diff] [blame] | 240 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 241 | return client; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 244 | void TServerSocket::interrupt() { |
| 245 | if (intSock1_ >= 0) { |
| 246 | int8_t byte = 0; |
| 247 | if (-1 == send(intSock1_, &byte, sizeof(int8_t), 0)) { |
| 248 | perror("TServerSocket::interrupt()"); |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 253 | void TServerSocket::close() { |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 254 | if (serverSocket_ >= 0) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 255 | shutdown(serverSocket_, SHUT_RDWR); |
| 256 | ::close(serverSocket_); |
| 257 | } |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 258 | if (intSock1_ >= 0) { |
| 259 | ::close(intSock1_); |
| 260 | } |
| 261 | if (intSock2_ >= 0) { |
| 262 | ::close(intSock2_); |
| 263 | } |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 264 | serverSocket_ = -1; |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 265 | intSock1_ = -1; |
| 266 | intSock2_ = -1; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 267 | } |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 268 | |
| 269 | }}} // facebook::thrift::transport |