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 | |
David Reiss | c88eb8c | 2008-06-11 01:18:54 +0000 | [diff] [blame] | 7 | #include <cstring> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 8 | #include <sys/socket.h> |
David Reiss | 1677ac9 | 2008-04-08 06:26:27 +0000 | [diff] [blame] | 9 | #include <sys/poll.h> |
Mark Slee | dd56497 | 2007-08-21 02:39:57 +0000 | [diff] [blame] | 10 | #include <sys/types.h> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 11 | #include <netinet/in.h> |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 12 | #include <netinet/tcp.h> |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 13 | #include <netdb.h> |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 14 | #include <fcntl.h> |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 15 | #include <errno.h> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 16 | |
Marc Slemko | d42a2c2 | 2006-08-10 03:30:18 +0000 | [diff] [blame] | 17 | #include "TSocket.h" |
| 18 | #include "TServerSocket.h" |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 19 | #include <boost/shared_ptr.hpp> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 20 | |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 21 | namespace facebook { namespace thrift { namespace transport { |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 22 | |
Martin Kraemer | e6c4fa6 | 2007-07-09 19:08:25 +0000 | [diff] [blame] | 23 | using namespace std; |
David Reiss | d4a269c | 2007-08-23 02:37:19 +0000 | [diff] [blame] | 24 | using boost::shared_ptr; |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 25 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 26 | TServerSocket::TServerSocket(int port) : |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 27 | port_(port), |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 28 | serverSocket_(-1), |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 29 | acceptBacklog_(1024), |
| 30 | sendTimeout_(0), |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 31 | recvTimeout_(0), |
boz | 1ea81ce | 2007-05-14 23:04:33 +0000 | [diff] [blame] | 32 | retryLimit_(0), |
| 33 | retryDelay_(0), |
Christopher Piro | 9cc63b5 | 2008-03-21 00:40:42 +0000 | [diff] [blame] | 34 | tcpSendBuffer_(0), |
| 35 | tcpRecvBuffer_(0), |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 36 | intSock1_(-1), |
| 37 | intSock2_(-1) {} |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 38 | |
| 39 | TServerSocket::TServerSocket(int port, int sendTimeout, int recvTimeout) : |
| 40 | port_(port), |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 41 | serverSocket_(-1), |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 42 | acceptBacklog_(1024), |
| 43 | sendTimeout_(sendTimeout), |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 44 | recvTimeout_(recvTimeout), |
boz | 1ea81ce | 2007-05-14 23:04:33 +0000 | [diff] [blame] | 45 | retryLimit_(0), |
| 46 | retryDelay_(0), |
Christopher Piro | 9cc63b5 | 2008-03-21 00:40:42 +0000 | [diff] [blame] | 47 | tcpSendBuffer_(0), |
| 48 | tcpRecvBuffer_(0), |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 49 | intSock1_(-1), |
| 50 | intSock2_(-1) {} |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 51 | |
| 52 | TServerSocket::~TServerSocket() { |
| 53 | close(); |
| 54 | } |
| 55 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 56 | void TServerSocket::setSendTimeout(int sendTimeout) { |
| 57 | sendTimeout_ = sendTimeout; |
| 58 | } |
| 59 | |
| 60 | void TServerSocket::setRecvTimeout(int recvTimeout) { |
| 61 | recvTimeout_ = recvTimeout; |
| 62 | } |
| 63 | |
boz | 1ea81ce | 2007-05-14 23:04:33 +0000 | [diff] [blame] | 64 | void TServerSocket::setRetryLimit(int retryLimit) { |
| 65 | retryLimit_ = retryLimit; |
| 66 | } |
| 67 | |
| 68 | void TServerSocket::setRetryDelay(int retryDelay) { |
| 69 | retryDelay_ = retryDelay; |
| 70 | } |
| 71 | |
Christopher Piro | 9cc63b5 | 2008-03-21 00:40:42 +0000 | [diff] [blame] | 72 | void TServerSocket::setTcpSendBuffer(int tcpSendBuffer) { |
| 73 | tcpSendBuffer_ = tcpSendBuffer; |
| 74 | } |
| 75 | |
| 76 | void TServerSocket::setTcpRecvBuffer(int tcpRecvBuffer) { |
| 77 | tcpRecvBuffer_ = tcpRecvBuffer; |
| 78 | } |
| 79 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 80 | void TServerSocket::listen() { |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 81 | int sv[2]; |
| 82 | if (-1 == socketpair(AF_LOCAL, SOCK_STREAM, 0, sv)) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 83 | int errno_copy = errno; |
| 84 | string errStr = "TServerSocket::listen() socketpair() " + TOutput::strerror_s(errno_copy); |
| 85 | GlobalOutput(errStr.c_str()); |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 86 | intSock1_ = -1; |
| 87 | intSock2_ = -1; |
| 88 | } else { |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 89 | intSock1_ = sv[1]; |
| 90 | intSock2_ = sv[0]; |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 93 | struct addrinfo hints, *res, *res0; |
| 94 | int error; |
| 95 | char port[sizeof("65536") + 1]; |
David Reiss | c88eb8c | 2008-06-11 01:18:54 +0000 | [diff] [blame] | 96 | std::memset(&hints, 0, sizeof(hints)); |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 97 | hints.ai_family = PF_UNSPEC; |
| 98 | hints.ai_socktype = SOCK_STREAM; |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 99 | hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG; |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 100 | sprintf(port, "%d", port_); |
| 101 | |
| 102 | // Wildcard address |
| 103 | error = getaddrinfo(NULL, port, &hints, &res0); |
| 104 | if (error) { |
| 105 | fprintf(stderr, "getaddrinfo %d: %s\n", error, gai_strerror(error)); |
| 106 | close(); |
| 107 | throw TTransportException(TTransportException::NOT_OPEN, "Could not resolve host for server socket."); |
| 108 | } |
| 109 | |
| 110 | // Pick the ipv6 address first since ipv4 addresses can be mapped |
| 111 | // into ipv6 space. |
| 112 | for (res = res0; res; res = res->ai_next) { |
| 113 | if (res->ai_family == AF_INET6 || res->ai_next == NULL) |
| 114 | break; |
| 115 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 116 | |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 117 | serverSocket_ = socket(res->ai_family, res->ai_socktype, res->ai_protocol); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 118 | if (serverSocket_ == -1) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 119 | int errno_copy = errno; |
| 120 | string errStr = "TServerSocket::listen() socket() " + TOutput::strerror_s(errno_copy); |
| 121 | GlobalOutput(errStr.c_str()); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 122 | close(); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 123 | throw TTransportException(TTransportException::NOT_OPEN, "Could not create server socket.", errno_copy); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | // Set reusaddress to prevent 2MSL delay on accept |
| 127 | int one = 1; |
| 128 | if (-1 == setsockopt(serverSocket_, SOL_SOCKET, SO_REUSEADDR, |
| 129 | &one, sizeof(one))) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 130 | int errno_copy = errno; |
| 131 | string errStr = "TServerSocket::listen() setsockopt() SO_REUSEADDR " + TOutput::strerror_s(errno_copy); |
| 132 | GlobalOutput(errStr.c_str()); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 133 | close(); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 134 | throw TTransportException(TTransportException::NOT_OPEN, "Could not set SO_REUSEADDR", errno_copy); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Christopher Piro | 9cc63b5 | 2008-03-21 00:40:42 +0000 | [diff] [blame] | 137 | // Set TCP buffer sizes |
| 138 | if (tcpSendBuffer_ > 0) { |
| 139 | if (-1 == setsockopt(serverSocket_, SOL_SOCKET, SO_SNDBUF, |
| 140 | &tcpSendBuffer_, sizeof(tcpSendBuffer_))) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 141 | int errno_copy = errno; |
| 142 | string errStr = "TServerSocket::listen() setsockopt() SO_SNDBUF " + TOutput::strerror_s(errno_copy); |
| 143 | GlobalOutput(errStr.c_str()); |
Christopher Piro | 9cc63b5 | 2008-03-21 00:40:42 +0000 | [diff] [blame] | 144 | close(); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 145 | throw TTransportException(TTransportException::NOT_OPEN, "Could not set SO_SNDBUF", errno_copy); |
Christopher Piro | 9cc63b5 | 2008-03-21 00:40:42 +0000 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
| 149 | if (tcpRecvBuffer_ > 0) { |
| 150 | if (-1 == setsockopt(serverSocket_, SOL_SOCKET, SO_RCVBUF, |
| 151 | &tcpRecvBuffer_, sizeof(tcpRecvBuffer_))) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 152 | int errno_copy = errno; |
| 153 | string errStr = "TServerSocket::listen() setsockopt() SO_RCVBUF " + TOutput::strerror_s(errno_copy); |
| 154 | GlobalOutput(errStr.c_str()); |
Christopher Piro | 9cc63b5 | 2008-03-21 00:40:42 +0000 | [diff] [blame] | 155 | close(); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 156 | throw TTransportException(TTransportException::NOT_OPEN, "Could not set SO_RCVBUF", errno_copy); |
Christopher Piro | 9cc63b5 | 2008-03-21 00:40:42 +0000 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 160 | // Defer accept |
| 161 | #ifdef TCP_DEFER_ACCEPT |
| 162 | if (-1 == setsockopt(serverSocket_, SOL_SOCKET, TCP_DEFER_ACCEPT, |
| 163 | &one, sizeof(one))) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 164 | int errno_copy = errno; |
| 165 | string errStr = "TServerSocket::listen() setsockopt() TCP_DEFER_ACCEPT " + TOutput::strerror_s(errno_copy); |
| 166 | GlobalOutput(errStr.c_str()); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 167 | close(); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 168 | throw TTransportException(TTransportException::NOT_OPEN, "Could not set TCP_DEFER_ACCEPT", errno_copy); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 169 | } |
| 170 | #endif // #ifdef TCP_DEFER_ACCEPT |
| 171 | |
David Reiss | 13aea46 | 2008-06-10 22:56:04 +0000 | [diff] [blame] | 172 | #ifdef IPV6_V6ONLY |
| 173 | int zero = 0; |
David Reiss | 1a2f218 | 2008-06-11 01:14:01 +0000 | [diff] [blame] | 174 | if (-1 == setsockopt(serverSocket_, IPPROTO_IPV6, IPV6_V6ONLY, |
David Reiss | 13aea46 | 2008-06-10 22:56:04 +0000 | [diff] [blame] | 175 | &zero, sizeof(zero))) { |
| 176 | GlobalOutput("TServerSocket::listen() IPV6_V6ONLY"); |
| 177 | } |
| 178 | #endif // #ifdef IPV6_V6ONLY |
| 179 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 180 | // Turn linger off, don't want to block on calls to close |
| 181 | struct linger ling = {0, 0}; |
| 182 | if (-1 == setsockopt(serverSocket_, SOL_SOCKET, SO_LINGER, |
| 183 | &ling, sizeof(ling))) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 184 | int errno_copy = errno; |
| 185 | string errStr = "TServerSocket::listen() setsockopt() SO_LINGER " + TOutput::strerror_s(errno_copy); |
| 186 | GlobalOutput(errStr.c_str()); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 187 | close(); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 188 | throw TTransportException(TTransportException::NOT_OPEN, "Could not set SO_LINGER", errno_copy); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 191 | // TCP Nodelay, speed over bandwidth |
| 192 | if (-1 == setsockopt(serverSocket_, IPPROTO_TCP, TCP_NODELAY, |
| 193 | &one, sizeof(one))) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 194 | int errno_copy = errno; |
| 195 | string errStr = "TServerSocket::listen() setsockopt() TCP_NODELAY " + TOutput::strerror_s(errno_copy); |
| 196 | GlobalOutput(errStr.c_str()); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 197 | close(); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 198 | throw TTransportException(TTransportException::NOT_OPEN, "Could not set TCP_NODELAY", errno_copy); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 201 | // Set NONBLOCK on the accept socket |
| 202 | int flags = fcntl(serverSocket_, F_GETFL, 0); |
| 203 | if (flags == -1) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 204 | int errno_copy = errno; |
| 205 | string errStr = "TServerSocket::listen() fcntl() F_GETFL " + TOutput::strerror_s(errno_copy); |
| 206 | GlobalOutput(errStr.c_str()); |
| 207 | throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed", errno_copy); |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 208 | } |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 209 | |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 210 | if (-1 == fcntl(serverSocket_, F_SETFL, flags | O_NONBLOCK)) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 211 | int errno_copy = errno; |
| 212 | string errStr = "TServerSocket::listen() fcntl() O_NONBLOCK " + TOutput::strerror_s(errno_copy); |
| 213 | GlobalOutput(errStr.c_str()); |
| 214 | throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed", errno_copy); |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 215 | } |
| 216 | |
boz | 1ea81ce | 2007-05-14 23:04:33 +0000 | [diff] [blame] | 217 | // prepare the port information |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 218 | // we may want to try to bind more than once, since SO_REUSEADDR doesn't |
boz | 1ea81ce | 2007-05-14 23:04:33 +0000 | [diff] [blame] | 219 | // always seem to work. The client can configure the retry variables. |
| 220 | int retries = 0; |
| 221 | do { |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 222 | if (0 == bind(serverSocket_, res->ai_addr, res->ai_addrlen)) { |
boz | 1ea81ce | 2007-05-14 23:04:33 +0000 | [diff] [blame] | 223 | break; |
| 224 | } |
| 225 | |
| 226 | // use short circuit evaluation here to only sleep if we need to |
| 227 | } while ((retries++ < retryLimit_) && (sleep(retryDelay_) == 0)); |
| 228 | |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 229 | // free addrinfo |
| 230 | freeaddrinfo(res0); |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 231 | |
boz | 1ea81ce | 2007-05-14 23:04:33 +0000 | [diff] [blame] | 232 | // throw an error if we failed to bind properly |
| 233 | if (retries > retryLimit_) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 234 | char errbuf[1024]; |
| 235 | sprintf(errbuf, "TServerSocket::listen() BIND %d", port_); |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 236 | GlobalOutput(errbuf); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 237 | close(); |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 238 | throw TTransportException(TTransportException::NOT_OPEN, "Could not bind"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | // Call listen |
| 242 | if (-1 == ::listen(serverSocket_, acceptBacklog_)) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 243 | int errno_copy = errno; |
| 244 | string errStr = "TServerSocket::listen() listen() " + TOutput::strerror_s(errno_copy); |
| 245 | GlobalOutput(errStr.c_str()); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 246 | close(); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 247 | throw TTransportException(TTransportException::NOT_OPEN, "Could not listen", errno_copy); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | // The socket is now listening! |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 253 | shared_ptr<TTransport> TServerSocket::acceptImpl() { |
Martin Kraemer | 10640d8 | 2007-02-03 01:59:12 +0000 | [diff] [blame] | 254 | if (serverSocket_ < 0) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 255 | throw TTransportException(TTransportException::NOT_OPEN, "TServerSocket not listening"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 256 | } |
| 257 | |
David Reiss | 1677ac9 | 2008-04-08 06:26:27 +0000 | [diff] [blame] | 258 | struct pollfd fds[2]; |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 259 | |
Aditya Agarwal | 7859a57 | 2007-05-31 01:33:07 +0000 | [diff] [blame] | 260 | int maxEintrs = 5; |
| 261 | int numEintrs = 0; |
| 262 | |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 263 | while (true) { |
David Reiss | c88eb8c | 2008-06-11 01:18:54 +0000 | [diff] [blame] | 264 | std::memset(fds, 0 , sizeof(fds)); |
David Reiss | 1677ac9 | 2008-04-08 06:26:27 +0000 | [diff] [blame] | 265 | fds[0].fd = serverSocket_; |
| 266 | fds[0].events = POLLIN; |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 267 | if (intSock2_ >= 0) { |
David Reiss | 1677ac9 | 2008-04-08 06:26:27 +0000 | [diff] [blame] | 268 | fds[1].fd = intSock2_; |
David Reiss | 2724c7a | 2008-04-18 17:48:03 +0000 | [diff] [blame] | 269 | fds[1].events = POLLIN; |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 270 | } |
David Reiss | 1677ac9 | 2008-04-08 06:26:27 +0000 | [diff] [blame] | 271 | int ret = poll(fds, 2, -1); |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 272 | |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 273 | if (ret < 0) { |
Aditya Agarwal | 7859a57 | 2007-05-31 01:33:07 +0000 | [diff] [blame] | 274 | // error cases |
boz | f83c9db | 2007-05-31 23:38:37 +0000 | [diff] [blame] | 275 | if (errno == EINTR && (numEintrs++ < maxEintrs)) { |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 276 | // EINTR needs to be handled manually and we can tolerate |
Aditya Agarwal | 7859a57 | 2007-05-31 01:33:07 +0000 | [diff] [blame] | 277 | // a certain number |
| 278 | continue; |
| 279 | } |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 280 | int errno_copy = errno; |
| 281 | string errStr = "TServerSocket::acceptImpl() poll() " + TOutput::strerror_s(errno_copy); |
| 282 | GlobalOutput(errStr.c_str()); |
| 283 | throw TTransportException(TTransportException::UNKNOWN, "Unknown", errno_copy); |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 284 | } else if (ret > 0) { |
| 285 | // Check for an interrupt signal |
David Reiss | 1677ac9 | 2008-04-08 06:26:27 +0000 | [diff] [blame] | 286 | if (intSock2_ >= 0 && (fds[1].revents & POLLIN)) { |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 287 | int8_t buf; |
| 288 | if (-1 == recv(intSock2_, &buf, sizeof(int8_t), 0)) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 289 | int errno_copy = errno; |
| 290 | string errStr = "TServerSocket::acceptImpl() recv() interrupt " + TOutput::strerror_s(errno_copy); |
| 291 | GlobalOutput(errStr.c_str()); |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 292 | } |
| 293 | throw TTransportException(TTransportException::INTERRUPTED); |
| 294 | } |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 295 | |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 296 | // Check for the actual server socket being ready |
David Reiss | 1677ac9 | 2008-04-08 06:26:27 +0000 | [diff] [blame] | 297 | if (fds[0].revents & POLLIN) { |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 298 | break; |
| 299 | } |
| 300 | } else { |
David Reiss | 1677ac9 | 2008-04-08 06:26:27 +0000 | [diff] [blame] | 301 | GlobalOutput("TServerSocket::acceptImpl() poll 0"); |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 302 | throw TTransportException(TTransportException::UNKNOWN); |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 306 | struct sockaddr_storage clientAddress; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 307 | int size = sizeof(clientAddress); |
| 308 | int clientSocket = ::accept(serverSocket_, |
| 309 | (struct sockaddr *) &clientAddress, |
| 310 | (socklen_t *) &size); |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 311 | |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 312 | if (clientSocket < 0) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 313 | int errno_copy = errno; |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 314 | string errStr = "TServerSocket::acceptImpl() ::accept() " + TOutput::strerror_s(errno_copy); |
| 315 | GlobalOutput(errStr.c_str()); |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 316 | throw TTransportException(TTransportException::UNKNOWN, "accept()", errno_copy); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 317 | } |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 318 | |
| 319 | // Make sure client socket is blocking |
| 320 | int flags = fcntl(clientSocket, F_GETFL, 0); |
| 321 | if (flags == -1) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 322 | int errno_copy = errno; |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 323 | string errStr = "TServerSocket::acceptImpl() fcntl() F_GETFL " + TOutput::strerror_s(errno_copy); |
| 324 | GlobalOutput(errStr.c_str()); |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 325 | throw TTransportException(TTransportException::UNKNOWN, "fcntl(F_GETFL)", errno_copy); |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 326 | } |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 327 | |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 328 | if (-1 == fcntl(clientSocket, F_SETFL, flags & ~O_NONBLOCK)) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 329 | int errno_copy = errno; |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 330 | string errStr = "TServerSocket::acceptImpl() fcntl() F_SETFL ~O_NONBLOCK " + TOutput::strerror_s(errno_copy); |
| 331 | GlobalOutput(errStr.c_str()); |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 332 | throw TTransportException(TTransportException::UNKNOWN, "fcntl(F_SETFL)", errno_copy); |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 333 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 334 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 335 | shared_ptr<TSocket> client(new TSocket(clientSocket)); |
| 336 | if (sendTimeout_ > 0) { |
| 337 | client->setSendTimeout(sendTimeout_); |
| 338 | } |
| 339 | if (recvTimeout_ > 0) { |
| 340 | client->setRecvTimeout(recvTimeout_); |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 341 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 342 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 343 | return client; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 346 | void TServerSocket::interrupt() { |
| 347 | if (intSock1_ >= 0) { |
| 348 | int8_t byte = 0; |
| 349 | if (-1 == send(intSock1_, &byte, sizeof(int8_t), 0)) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 350 | int errno_copy = errno; |
| 351 | string errStr = "TServerSocket::interrupt() send() " + TOutput::strerror_s(errno_copy); |
| 352 | GlobalOutput(errStr.c_str()); |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 353 | } |
| 354 | } |
| 355 | } |
| 356 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 357 | void TServerSocket::close() { |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 358 | if (serverSocket_ >= 0) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 359 | shutdown(serverSocket_, SHUT_RDWR); |
| 360 | ::close(serverSocket_); |
| 361 | } |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 362 | if (intSock1_ >= 0) { |
| 363 | ::close(intSock1_); |
| 364 | } |
| 365 | if (intSock2_ >= 0) { |
| 366 | ::close(intSock2_); |
| 367 | } |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 368 | serverSocket_ = -1; |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 369 | intSock1_ = -1; |
| 370 | intSock2_ = -1; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 371 | } |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 372 | |
| 373 | }}} // facebook::thrift::transport |