blob: 8608898f014264b08acc6027a3e95673c619db78 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
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 Slee9f0c6512007-02-28 23:58:26 +000019
David Reissc88eb8c2008-06-11 01:18:54 +000020#include <cstring>
David Reiss08d2f112009-05-21 02:28:36 +000021#include <sys/types.h>
Mark Sleee8540632006-05-30 09:24:40 +000022#include <sys/socket.h>
Bryan Duxburya18364a2010-09-28 14:36:07 +000023#include <sys/un.h>
David Reiss1677ac92008-04-08 06:26:27 +000024#include <sys/poll.h>
Mark Sleedd564972007-08-21 02:39:57 +000025#include <sys/types.h>
Mark Sleee8540632006-05-30 09:24:40 +000026#include <netinet/in.h>
Mark Slee29050782006-09-29 00:12:30 +000027#include <netinet/tcp.h>
Mark Slee6d56eb92007-07-06 22:28:15 +000028#include <netdb.h>
Mark Sleea5a783f2007-03-02 19:41:08 +000029#include <fcntl.h>
Mark Slee8d7e1f62006-06-07 06:48:56 +000030#include <errno.h>
David Reiss5105b2e2009-05-21 02:28:27 +000031#include <unistd.h>
Mark Sleee8540632006-05-30 09:24:40 +000032
Marc Slemkod42a2c22006-08-10 03:30:18 +000033#include "TSocket.h"
34#include "TServerSocket.h"
Marc Slemko16698852006-08-04 03:16:10 +000035#include <boost/shared_ptr.hpp>
Mark Sleee8540632006-05-30 09:24:40 +000036
David Reiss9b903442009-10-21 05:51:28 +000037#ifndef AF_LOCAL
38#define AF_LOCAL AF_UNIX
39#endif
40
T Jake Lucianib5e62212009-01-31 22:36:20 +000041namespace apache { namespace thrift { namespace transport {
Marc Slemko6f038a72006-08-03 18:58:09 +000042
Martin Kraemere6c4fa62007-07-09 19:08:25 +000043using namespace std;
David Reissd4a269c2007-08-23 02:37:19 +000044using boost::shared_ptr;
Marc Slemko16698852006-08-04 03:16:10 +000045
Mark Sleee8540632006-05-30 09:24:40 +000046TServerSocket::TServerSocket(int port) :
Mark Slee29050782006-09-29 00:12:30 +000047 port_(port),
Martin Kraemeree341cb2007-02-05 21:40:38 +000048 serverSocket_(-1),
Mark Slee29050782006-09-29 00:12:30 +000049 acceptBacklog_(1024),
50 sendTimeout_(0),
Mark Sleea5a783f2007-03-02 19:41:08 +000051 recvTimeout_(0),
boz1ea81ce2007-05-14 23:04:33 +000052 retryLimit_(0),
53 retryDelay_(0),
Christopher Piro9cc63b52008-03-21 00:40:42 +000054 tcpSendBuffer_(0),
55 tcpRecvBuffer_(0),
Mark Slee561b5362007-03-09 19:26:29 +000056 intSock1_(-1),
57 intSock2_(-1) {}
Mark Slee29050782006-09-29 00:12:30 +000058
59TServerSocket::TServerSocket(int port, int sendTimeout, int recvTimeout) :
60 port_(port),
Martin Kraemeree341cb2007-02-05 21:40:38 +000061 serverSocket_(-1),
Mark Slee29050782006-09-29 00:12:30 +000062 acceptBacklog_(1024),
63 sendTimeout_(sendTimeout),
Mark Sleea5a783f2007-03-02 19:41:08 +000064 recvTimeout_(recvTimeout),
boz1ea81ce2007-05-14 23:04:33 +000065 retryLimit_(0),
66 retryDelay_(0),
Christopher Piro9cc63b52008-03-21 00:40:42 +000067 tcpSendBuffer_(0),
68 tcpRecvBuffer_(0),
Mark Slee561b5362007-03-09 19:26:29 +000069 intSock1_(-1),
70 intSock2_(-1) {}
Mark Sleee8540632006-05-30 09:24:40 +000071
Bryan Duxburya18364a2010-09-28 14:36:07 +000072TServerSocket::TServerSocket(string path) :
73 port_(0),
74 path_(path),
75 serverSocket_(-1),
76 acceptBacklog_(1024),
77 sendTimeout_(0),
78 recvTimeout_(0),
79 retryLimit_(0),
80 retryDelay_(0),
81 tcpSendBuffer_(0),
82 tcpRecvBuffer_(0),
83 intSock1_(-1),
84 intSock2_(-1) {}
85
Mark Sleee8540632006-05-30 09:24:40 +000086TServerSocket::~TServerSocket() {
87 close();
88}
89
Mark Slee29050782006-09-29 00:12:30 +000090void TServerSocket::setSendTimeout(int sendTimeout) {
91 sendTimeout_ = sendTimeout;
92}
93
94void TServerSocket::setRecvTimeout(int recvTimeout) {
95 recvTimeout_ = recvTimeout;
96}
97
boz1ea81ce2007-05-14 23:04:33 +000098void TServerSocket::setRetryLimit(int retryLimit) {
99 retryLimit_ = retryLimit;
100}
101
102void TServerSocket::setRetryDelay(int retryDelay) {
103 retryDelay_ = retryDelay;
104}
105
Christopher Piro9cc63b52008-03-21 00:40:42 +0000106void TServerSocket::setTcpSendBuffer(int tcpSendBuffer) {
107 tcpSendBuffer_ = tcpSendBuffer;
108}
109
110void TServerSocket::setTcpRecvBuffer(int tcpRecvBuffer) {
111 tcpRecvBuffer_ = tcpRecvBuffer;
112}
113
Mark Slee8d7e1f62006-06-07 06:48:56 +0000114void TServerSocket::listen() {
Mark Slee561b5362007-03-09 19:26:29 +0000115 int sv[2];
116 if (-1 == socketpair(AF_LOCAL, SOCK_STREAM, 0, sv)) {
David Reiss01e55c12008-07-13 22:18:51 +0000117 GlobalOutput.perror("TServerSocket::listen() socketpair() ", errno);
Mark Slee561b5362007-03-09 19:26:29 +0000118 intSock1_ = -1;
119 intSock2_ = -1;
120 } else {
Mark Sleee02385b2007-06-09 01:21:16 +0000121 intSock1_ = sv[1];
122 intSock2_ = sv[0];
Mark Slee561b5362007-03-09 19:26:29 +0000123 }
124
Mark Slee6d56eb92007-07-06 22:28:15 +0000125 struct addrinfo hints, *res, *res0;
126 int error;
127 char port[sizeof("65536") + 1];
David Reissc88eb8c2008-06-11 01:18:54 +0000128 std::memset(&hints, 0, sizeof(hints));
Mark Slee6d56eb92007-07-06 22:28:15 +0000129 hints.ai_family = PF_UNSPEC;
130 hints.ai_socktype = SOCK_STREAM;
Mark Slee256bdc42007-11-27 08:42:19 +0000131 hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
Mark Slee6d56eb92007-07-06 22:28:15 +0000132 sprintf(port, "%d", port_);
133
134 // Wildcard address
135 error = getaddrinfo(NULL, port, &hints, &res0);
136 if (error) {
David Reiss01e55c12008-07-13 22:18:51 +0000137 GlobalOutput.printf("getaddrinfo %d: %s", error, gai_strerror(error));
Mark Slee6d56eb92007-07-06 22:28:15 +0000138 close();
139 throw TTransportException(TTransportException::NOT_OPEN, "Could not resolve host for server socket.");
140 }
141
142 // Pick the ipv6 address first since ipv4 addresses can be mapped
143 // into ipv6 space.
144 for (res = res0; res; res = res->ai_next) {
145 if (res->ai_family == AF_INET6 || res->ai_next == NULL)
146 break;
147 }
Mark Slee256bdc42007-11-27 08:42:19 +0000148
Bryan Duxburya18364a2010-09-28 14:36:07 +0000149 if (! path_.empty()) {
150 serverSocket_ = socket(PF_UNIX, SOCK_STREAM, IPPROTO_IP);
151 } else {
152 serverSocket_ = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
153 }
154
Mark Sleee8540632006-05-30 09:24:40 +0000155 if (serverSocket_ == -1) {
David Reiss9b209552008-04-08 06:26:05 +0000156 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000157 GlobalOutput.perror("TServerSocket::listen() socket() ", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000158 close();
David Reiss9b209552008-04-08 06:26:05 +0000159 throw TTransportException(TTransportException::NOT_OPEN, "Could not create server socket.", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000160 }
161
162 // Set reusaddress to prevent 2MSL delay on accept
163 int one = 1;
164 if (-1 == setsockopt(serverSocket_, SOL_SOCKET, SO_REUSEADDR,
165 &one, sizeof(one))) {
David Reiss9b209552008-04-08 06:26:05 +0000166 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000167 GlobalOutput.perror("TServerSocket::listen() setsockopt() SO_REUSEADDR ", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000168 close();
David Reiss9b209552008-04-08 06:26:05 +0000169 throw TTransportException(TTransportException::NOT_OPEN, "Could not set SO_REUSEADDR", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000170 }
171
Christopher Piro9cc63b52008-03-21 00:40:42 +0000172 // Set TCP buffer sizes
173 if (tcpSendBuffer_ > 0) {
174 if (-1 == setsockopt(serverSocket_, SOL_SOCKET, SO_SNDBUF,
175 &tcpSendBuffer_, sizeof(tcpSendBuffer_))) {
David Reiss9b209552008-04-08 06:26:05 +0000176 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000177 GlobalOutput.perror("TServerSocket::listen() setsockopt() SO_SNDBUF ", errno_copy);
Christopher Piro9cc63b52008-03-21 00:40:42 +0000178 close();
David Reiss9b209552008-04-08 06:26:05 +0000179 throw TTransportException(TTransportException::NOT_OPEN, "Could not set SO_SNDBUF", errno_copy);
Christopher Piro9cc63b52008-03-21 00:40:42 +0000180 }
181 }
182
183 if (tcpRecvBuffer_ > 0) {
184 if (-1 == setsockopt(serverSocket_, SOL_SOCKET, SO_RCVBUF,
185 &tcpRecvBuffer_, sizeof(tcpRecvBuffer_))) {
David Reiss9b209552008-04-08 06:26:05 +0000186 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000187 GlobalOutput.perror("TServerSocket::listen() setsockopt() SO_RCVBUF ", errno_copy);
Christopher Piro9cc63b52008-03-21 00:40:42 +0000188 close();
David Reiss9b209552008-04-08 06:26:05 +0000189 throw TTransportException(TTransportException::NOT_OPEN, "Could not set SO_RCVBUF", errno_copy);
Christopher Piro9cc63b52008-03-21 00:40:42 +0000190 }
191 }
192
Mark Slee29050782006-09-29 00:12:30 +0000193 // Defer accept
194 #ifdef TCP_DEFER_ACCEPT
195 if (-1 == setsockopt(serverSocket_, SOL_SOCKET, TCP_DEFER_ACCEPT,
196 &one, sizeof(one))) {
David Reiss9b209552008-04-08 06:26:05 +0000197 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000198 GlobalOutput.perror("TServerSocket::listen() setsockopt() TCP_DEFER_ACCEPT ", errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000199 close();
David Reiss9b209552008-04-08 06:26:05 +0000200 throw TTransportException(TTransportException::NOT_OPEN, "Could not set TCP_DEFER_ACCEPT", errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000201 }
202 #endif // #ifdef TCP_DEFER_ACCEPT
203
David Reiss13aea462008-06-10 22:56:04 +0000204 #ifdef IPV6_V6ONLY
David Reisseee98be2010-03-09 05:20:10 +0000205 if (res->ai_family == AF_INET6) {
206 int zero = 0;
207 if (-1 == setsockopt(serverSocket_, IPPROTO_IPV6, IPV6_V6ONLY,
208 &zero, sizeof(zero))) {
209 GlobalOutput.perror("TServerSocket::listen() IPV6_V6ONLY ", errno);
210 }
David Reiss13aea462008-06-10 22:56:04 +0000211 }
212 #endif // #ifdef IPV6_V6ONLY
213
Mark Sleee8540632006-05-30 09:24:40 +0000214 // Turn linger off, don't want to block on calls to close
215 struct linger ling = {0, 0};
216 if (-1 == setsockopt(serverSocket_, SOL_SOCKET, SO_LINGER,
217 &ling, sizeof(ling))) {
David Reiss9b209552008-04-08 06:26:05 +0000218 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000219 GlobalOutput.perror("TServerSocket::listen() setsockopt() SO_LINGER ", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000220 close();
David Reiss9b209552008-04-08 06:26:05 +0000221 throw TTransportException(TTransportException::NOT_OPEN, "Could not set SO_LINGER", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000222 }
223
Bryan Duxburya18364a2010-09-28 14:36:07 +0000224 // Unix Sockets do not need that
225 if (path_.empty()) {
226 // TCP Nodelay, speed over bandwidth
227 if (-1 == setsockopt(serverSocket_, IPPROTO_TCP, TCP_NODELAY,
228 &one, sizeof(one))) {
229 int errno_copy = errno;
230 GlobalOutput.perror("TServerSocket::listen() setsockopt() TCP_NODELAY ", errno_copy);
231 close();
232 throw TTransportException(TTransportException::NOT_OPEN, "Could not set TCP_NODELAY", errno_copy);
233 }
Mark Slee29050782006-09-29 00:12:30 +0000234 }
235
Mark Sleea5a783f2007-03-02 19:41:08 +0000236 // Set NONBLOCK on the accept socket
237 int flags = fcntl(serverSocket_, F_GETFL, 0);
238 if (flags == -1) {
David Reiss9b209552008-04-08 06:26:05 +0000239 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000240 GlobalOutput.perror("TServerSocket::listen() fcntl() F_GETFL ", errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000241 throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed", errno_copy);
Mark Sleea5a783f2007-03-02 19:41:08 +0000242 }
Mark Slee561b5362007-03-09 19:26:29 +0000243
Mark Sleea5a783f2007-03-02 19:41:08 +0000244 if (-1 == fcntl(serverSocket_, F_SETFL, flags | O_NONBLOCK)) {
David Reiss9b209552008-04-08 06:26:05 +0000245 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000246 GlobalOutput.perror("TServerSocket::listen() fcntl() O_NONBLOCK ", errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000247 throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed", errno_copy);
Mark Sleea5a783f2007-03-02 19:41:08 +0000248 }
249
boz1ea81ce2007-05-14 23:04:33 +0000250 // prepare the port information
Mark Slee256bdc42007-11-27 08:42:19 +0000251 // we may want to try to bind more than once, since SO_REUSEADDR doesn't
boz1ea81ce2007-05-14 23:04:33 +0000252 // always seem to work. The client can configure the retry variables.
253 int retries = 0;
Bryan Duxburya18364a2010-09-28 14:36:07 +0000254
255 if (! path_.empty()) {
256 // Unix Domain Socket
257 struct sockaddr_un address;
258 socklen_t len;
259
260 if (path_.length() > sizeof(address.sun_path)) {
261 int errno_copy = errno;
262 GlobalOutput.perror("TSocket::listen() Unix Domain socket path too long", errno_copy);
263 throw TTransportException(TTransportException::NOT_OPEN, " Unix Domain socket path too long");
boz1ea81ce2007-05-14 23:04:33 +0000264 }
265
Bryan Duxburya18364a2010-09-28 14:36:07 +0000266 address.sun_family = AF_UNIX;
Roger Meierd11ca5a2010-10-18 08:22:57 +0000267 snprintf(address.sun_path, sizeof(address.sun_path), "%s", path_.c_str());
Bryan Duxburya18364a2010-09-28 14:36:07 +0000268 len = sizeof(address);
boz1ea81ce2007-05-14 23:04:33 +0000269
Bryan Duxburya18364a2010-09-28 14:36:07 +0000270 do {
271 if (0 == bind(serverSocket_, (struct sockaddr *) &address, len)) {
272 break;
273 }
274 // use short circuit evaluation here to only sleep if we need to
275 } while ((retries++ < retryLimit_) && (sleep(retryDelay_) == 0));
276 } else {
277 do {
278 if (0 == bind(serverSocket_, res->ai_addr, res->ai_addrlen)) {
279 break;
280 }
281 // use short circuit evaluation here to only sleep if we need to
282 } while ((retries++ < retryLimit_) && (sleep(retryDelay_) == 0));
283
284 // free addrinfo
285 freeaddrinfo(res0);
286 }
Mark Slee256bdc42007-11-27 08:42:19 +0000287
boz1ea81ce2007-05-14 23:04:33 +0000288 // throw an error if we failed to bind properly
289 if (retries > retryLimit_) {
Mark Sleee8540632006-05-30 09:24:40 +0000290 char errbuf[1024];
Bryan Duxburya18364a2010-09-28 14:36:07 +0000291 if (! path_.empty()) {
292 sprintf(errbuf, "TServerSocket::listen() PATH %s", path_.c_str());
293 }
294 else {
295 sprintf(errbuf, "TServerSocket::listen() BIND %d", port_);
296 }
boz6ded7752007-06-05 22:41:18 +0000297 GlobalOutput(errbuf);
Mark Sleee8540632006-05-30 09:24:40 +0000298 close();
Mark Sleef9831082007-02-20 20:59:21 +0000299 throw TTransportException(TTransportException::NOT_OPEN, "Could not bind");
Mark Sleee8540632006-05-30 09:24:40 +0000300 }
301
302 // Call listen
303 if (-1 == ::listen(serverSocket_, acceptBacklog_)) {
David Reiss9b209552008-04-08 06:26:05 +0000304 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000305 GlobalOutput.perror("TServerSocket::listen() listen() ", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000306 close();
David Reiss9b209552008-04-08 06:26:05 +0000307 throw TTransportException(TTransportException::NOT_OPEN, "Could not listen", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000308 }
309
310 // The socket is now listening!
Mark Sleee8540632006-05-30 09:24:40 +0000311}
312
Marc Slemko16698852006-08-04 03:16:10 +0000313shared_ptr<TTransport> TServerSocket::acceptImpl() {
Martin Kraemer10640d82007-02-03 01:59:12 +0000314 if (serverSocket_ < 0) {
Mark Sleef9831082007-02-20 20:59:21 +0000315 throw TTransportException(TTransportException::NOT_OPEN, "TServerSocket not listening");
Mark Sleee8540632006-05-30 09:24:40 +0000316 }
317
David Reiss1677ac92008-04-08 06:26:27 +0000318 struct pollfd fds[2];
Mark Sleea5a783f2007-03-02 19:41:08 +0000319
Aditya Agarwal7859a572007-05-31 01:33:07 +0000320 int maxEintrs = 5;
321 int numEintrs = 0;
322
Mark Sleea5a783f2007-03-02 19:41:08 +0000323 while (true) {
David Reissc88eb8c2008-06-11 01:18:54 +0000324 std::memset(fds, 0 , sizeof(fds));
David Reiss1677ac92008-04-08 06:26:27 +0000325 fds[0].fd = serverSocket_;
326 fds[0].events = POLLIN;
Mark Slee561b5362007-03-09 19:26:29 +0000327 if (intSock2_ >= 0) {
David Reiss1677ac92008-04-08 06:26:27 +0000328 fds[1].fd = intSock2_;
David Reiss2724c7a2008-04-18 17:48:03 +0000329 fds[1].events = POLLIN;
Mark Sleea5a783f2007-03-02 19:41:08 +0000330 }
David Reiss1677ac92008-04-08 06:26:27 +0000331 int ret = poll(fds, 2, -1);
Mark Sleea5a783f2007-03-02 19:41:08 +0000332
Mark Slee561b5362007-03-09 19:26:29 +0000333 if (ret < 0) {
Aditya Agarwal7859a572007-05-31 01:33:07 +0000334 // error cases
bozf83c9db2007-05-31 23:38:37 +0000335 if (errno == EINTR && (numEintrs++ < maxEintrs)) {
Mark Slee256bdc42007-11-27 08:42:19 +0000336 // EINTR needs to be handled manually and we can tolerate
Aditya Agarwal7859a572007-05-31 01:33:07 +0000337 // a certain number
338 continue;
339 }
David Reiss9b209552008-04-08 06:26:05 +0000340 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000341 GlobalOutput.perror("TServerSocket::acceptImpl() poll() ", errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000342 throw TTransportException(TTransportException::UNKNOWN, "Unknown", errno_copy);
Mark Slee561b5362007-03-09 19:26:29 +0000343 } else if (ret > 0) {
344 // Check for an interrupt signal
David Reiss1677ac92008-04-08 06:26:27 +0000345 if (intSock2_ >= 0 && (fds[1].revents & POLLIN)) {
Mark Slee561b5362007-03-09 19:26:29 +0000346 int8_t buf;
347 if (-1 == recv(intSock2_, &buf, sizeof(int8_t), 0)) {
David Reiss01e55c12008-07-13 22:18:51 +0000348 GlobalOutput.perror("TServerSocket::acceptImpl() recv() interrupt ", errno);
Mark Slee561b5362007-03-09 19:26:29 +0000349 }
350 throw TTransportException(TTransportException::INTERRUPTED);
351 }
David Reiss9b209552008-04-08 06:26:05 +0000352
Mark Slee561b5362007-03-09 19:26:29 +0000353 // Check for the actual server socket being ready
David Reiss1677ac92008-04-08 06:26:27 +0000354 if (fds[0].revents & POLLIN) {
Mark Slee561b5362007-03-09 19:26:29 +0000355 break;
356 }
357 } else {
David Reiss1677ac92008-04-08 06:26:27 +0000358 GlobalOutput("TServerSocket::acceptImpl() poll 0");
Mark Slee256bdc42007-11-27 08:42:19 +0000359 throw TTransportException(TTransportException::UNKNOWN);
Mark Sleea5a783f2007-03-02 19:41:08 +0000360 }
361 }
362
Mark Slee6d56eb92007-07-06 22:28:15 +0000363 struct sockaddr_storage clientAddress;
Mark Sleee8540632006-05-30 09:24:40 +0000364 int size = sizeof(clientAddress);
365 int clientSocket = ::accept(serverSocket_,
366 (struct sockaddr *) &clientAddress,
367 (socklen_t *) &size);
Mark Slee256bdc42007-11-27 08:42:19 +0000368
Martin Kraemeree341cb2007-02-05 21:40:38 +0000369 if (clientSocket < 0) {
David Reissbc3dddb2007-08-22 23:20:24 +0000370 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000371 GlobalOutput.perror("TServerSocket::acceptImpl() ::accept() ", errno_copy);
David Reissbc3dddb2007-08-22 23:20:24 +0000372 throw TTransportException(TTransportException::UNKNOWN, "accept()", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000373 }
Mark Sleea5a783f2007-03-02 19:41:08 +0000374
375 // Make sure client socket is blocking
376 int flags = fcntl(clientSocket, F_GETFL, 0);
377 if (flags == -1) {
David Reissbc3dddb2007-08-22 23:20:24 +0000378 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000379 GlobalOutput.perror("TServerSocket::acceptImpl() fcntl() F_GETFL ", errno_copy);
David Reissbc3dddb2007-08-22 23:20:24 +0000380 throw TTransportException(TTransportException::UNKNOWN, "fcntl(F_GETFL)", errno_copy);
Mark Sleea5a783f2007-03-02 19:41:08 +0000381 }
David Reiss9b209552008-04-08 06:26:05 +0000382
Mark Sleea5a783f2007-03-02 19:41:08 +0000383 if (-1 == fcntl(clientSocket, F_SETFL, flags & ~O_NONBLOCK)) {
David Reissbc3dddb2007-08-22 23:20:24 +0000384 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000385 GlobalOutput.perror("TServerSocket::acceptImpl() fcntl() F_SETFL ~O_NONBLOCK ", errno_copy);
David Reissbc3dddb2007-08-22 23:20:24 +0000386 throw TTransportException(TTransportException::UNKNOWN, "fcntl(F_SETFL)", errno_copy);
Mark Sleea5a783f2007-03-02 19:41:08 +0000387 }
Mark Slee256bdc42007-11-27 08:42:19 +0000388
Mark Slee29050782006-09-29 00:12:30 +0000389 shared_ptr<TSocket> client(new TSocket(clientSocket));
390 if (sendTimeout_ > 0) {
391 client->setSendTimeout(sendTimeout_);
392 }
393 if (recvTimeout_ > 0) {
394 client->setRecvTimeout(recvTimeout_);
Mark Sleea5a783f2007-03-02 19:41:08 +0000395 }
David Reiss23248712010-10-06 17:10:08 +0000396 client->setCachedAddress((sockaddr*) &clientAddress, size);
397
Mark Slee29050782006-09-29 00:12:30 +0000398 return client;
Mark Sleee8540632006-05-30 09:24:40 +0000399}
400
Mark Slee561b5362007-03-09 19:26:29 +0000401void TServerSocket::interrupt() {
402 if (intSock1_ >= 0) {
403 int8_t byte = 0;
404 if (-1 == send(intSock1_, &byte, sizeof(int8_t), 0)) {
David Reiss01e55c12008-07-13 22:18:51 +0000405 GlobalOutput.perror("TServerSocket::interrupt() send() ", errno);
Mark Slee561b5362007-03-09 19:26:29 +0000406 }
407 }
408}
409
Mark Sleee8540632006-05-30 09:24:40 +0000410void TServerSocket::close() {
Martin Kraemeree341cb2007-02-05 21:40:38 +0000411 if (serverSocket_ >= 0) {
Mark Sleee8540632006-05-30 09:24:40 +0000412 shutdown(serverSocket_, SHUT_RDWR);
413 ::close(serverSocket_);
414 }
Mark Slee561b5362007-03-09 19:26:29 +0000415 if (intSock1_ >= 0) {
416 ::close(intSock1_);
417 }
418 if (intSock2_ >= 0) {
419 ::close(intSock2_);
420 }
Martin Kraemeree341cb2007-02-05 21:40:38 +0000421 serverSocket_ = -1;
Mark Slee561b5362007-03-09 19:26:29 +0000422 intSock1_ = -1;
423 intSock2_ = -1;
Mark Sleee8540632006-05-30 09:24:40 +0000424}
Marc Slemko6f038a72006-08-03 18:58:09 +0000425
T Jake Lucianib5e62212009-01-31 22:36:20 +0000426}}} // apache::thrift::transport