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