blob: 9b47aa539d72f651c4b2db6b3e8cc43454df7687 [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>
Mark Sleee8540632006-05-30 09:24:40 +000021#include <sys/socket.h>
David Reiss1677ac92008-04-08 06:26:27 +000022#include <sys/poll.h>
Mark Sleedd564972007-08-21 02:39:57 +000023#include <sys/types.h>
Mark Sleee8540632006-05-30 09:24:40 +000024#include <netinet/in.h>
Mark Slee29050782006-09-29 00:12:30 +000025#include <netinet/tcp.h>
Mark Slee6d56eb92007-07-06 22:28:15 +000026#include <netdb.h>
Mark Sleea5a783f2007-03-02 19:41:08 +000027#include <fcntl.h>
Mark Slee8d7e1f62006-06-07 06:48:56 +000028#include <errno.h>
Mark Sleee8540632006-05-30 09:24:40 +000029
Marc Slemkod42a2c22006-08-10 03:30:18 +000030#include "TSocket.h"
31#include "TServerSocket.h"
Marc Slemko16698852006-08-04 03:16:10 +000032#include <boost/shared_ptr.hpp>
Mark Sleee8540632006-05-30 09:24:40 +000033
T Jake Lucianib5e62212009-01-31 22:36:20 +000034namespace apache { namespace thrift { namespace transport {
Marc Slemko6f038a72006-08-03 18:58:09 +000035
Martin Kraemere6c4fa62007-07-09 19:08:25 +000036using namespace std;
David Reissd4a269c2007-08-23 02:37:19 +000037using boost::shared_ptr;
Marc Slemko16698852006-08-04 03:16:10 +000038
Mark Sleee8540632006-05-30 09:24:40 +000039TServerSocket::TServerSocket(int port) :
Mark Slee29050782006-09-29 00:12:30 +000040 port_(port),
Martin Kraemeree341cb2007-02-05 21:40:38 +000041 serverSocket_(-1),
Mark Slee29050782006-09-29 00:12:30 +000042 acceptBacklog_(1024),
43 sendTimeout_(0),
Mark Sleea5a783f2007-03-02 19:41:08 +000044 recvTimeout_(0),
boz1ea81ce2007-05-14 23:04:33 +000045 retryLimit_(0),
46 retryDelay_(0),
Christopher Piro9cc63b52008-03-21 00:40:42 +000047 tcpSendBuffer_(0),
48 tcpRecvBuffer_(0),
Mark Slee561b5362007-03-09 19:26:29 +000049 intSock1_(-1),
50 intSock2_(-1) {}
Mark Slee29050782006-09-29 00:12:30 +000051
52TServerSocket::TServerSocket(int port, int sendTimeout, int recvTimeout) :
53 port_(port),
Martin Kraemeree341cb2007-02-05 21:40:38 +000054 serverSocket_(-1),
Mark Slee29050782006-09-29 00:12:30 +000055 acceptBacklog_(1024),
56 sendTimeout_(sendTimeout),
Mark Sleea5a783f2007-03-02 19:41:08 +000057 recvTimeout_(recvTimeout),
boz1ea81ce2007-05-14 23:04:33 +000058 retryLimit_(0),
59 retryDelay_(0),
Christopher Piro9cc63b52008-03-21 00:40:42 +000060 tcpSendBuffer_(0),
61 tcpRecvBuffer_(0),
Mark Slee561b5362007-03-09 19:26:29 +000062 intSock1_(-1),
63 intSock2_(-1) {}
Mark Sleee8540632006-05-30 09:24:40 +000064
65TServerSocket::~TServerSocket() {
66 close();
67}
68
Mark Slee29050782006-09-29 00:12:30 +000069void TServerSocket::setSendTimeout(int sendTimeout) {
70 sendTimeout_ = sendTimeout;
71}
72
73void TServerSocket::setRecvTimeout(int recvTimeout) {
74 recvTimeout_ = recvTimeout;
75}
76
boz1ea81ce2007-05-14 23:04:33 +000077void TServerSocket::setRetryLimit(int retryLimit) {
78 retryLimit_ = retryLimit;
79}
80
81void TServerSocket::setRetryDelay(int retryDelay) {
82 retryDelay_ = retryDelay;
83}
84
Christopher Piro9cc63b52008-03-21 00:40:42 +000085void TServerSocket::setTcpSendBuffer(int tcpSendBuffer) {
86 tcpSendBuffer_ = tcpSendBuffer;
87}
88
89void TServerSocket::setTcpRecvBuffer(int tcpRecvBuffer) {
90 tcpRecvBuffer_ = tcpRecvBuffer;
91}
92
Mark Slee8d7e1f62006-06-07 06:48:56 +000093void TServerSocket::listen() {
Mark Slee561b5362007-03-09 19:26:29 +000094 int sv[2];
95 if (-1 == socketpair(AF_LOCAL, SOCK_STREAM, 0, sv)) {
David Reiss01e55c12008-07-13 22:18:51 +000096 GlobalOutput.perror("TServerSocket::listen() socketpair() ", errno);
Mark Slee561b5362007-03-09 19:26:29 +000097 intSock1_ = -1;
98 intSock2_ = -1;
99 } else {
Mark Sleee02385b2007-06-09 01:21:16 +0000100 intSock1_ = sv[1];
101 intSock2_ = sv[0];
Mark Slee561b5362007-03-09 19:26:29 +0000102 }
103
Mark Slee6d56eb92007-07-06 22:28:15 +0000104 struct addrinfo hints, *res, *res0;
105 int error;
106 char port[sizeof("65536") + 1];
David Reissc88eb8c2008-06-11 01:18:54 +0000107 std::memset(&hints, 0, sizeof(hints));
Mark Slee6d56eb92007-07-06 22:28:15 +0000108 hints.ai_family = PF_UNSPEC;
109 hints.ai_socktype = SOCK_STREAM;
Mark Slee256bdc42007-11-27 08:42:19 +0000110 hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
Mark Slee6d56eb92007-07-06 22:28:15 +0000111 sprintf(port, "%d", port_);
112
113 // Wildcard address
114 error = getaddrinfo(NULL, port, &hints, &res0);
115 if (error) {
David Reiss01e55c12008-07-13 22:18:51 +0000116 GlobalOutput.printf("getaddrinfo %d: %s", error, gai_strerror(error));
Mark Slee6d56eb92007-07-06 22:28:15 +0000117 close();
118 throw TTransportException(TTransportException::NOT_OPEN, "Could not resolve host for server socket.");
119 }
120
121 // Pick the ipv6 address first since ipv4 addresses can be mapped
122 // into ipv6 space.
123 for (res = res0; res; res = res->ai_next) {
124 if (res->ai_family == AF_INET6 || res->ai_next == NULL)
125 break;
126 }
Mark Slee256bdc42007-11-27 08:42:19 +0000127
Mark Slee6d56eb92007-07-06 22:28:15 +0000128 serverSocket_ = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
Mark Sleee8540632006-05-30 09:24:40 +0000129 if (serverSocket_ == -1) {
David Reiss9b209552008-04-08 06:26:05 +0000130 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000131 GlobalOutput.perror("TServerSocket::listen() socket() ", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000132 close();
David Reiss9b209552008-04-08 06:26:05 +0000133 throw TTransportException(TTransportException::NOT_OPEN, "Could not create server socket.", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000134 }
135
136 // Set reusaddress to prevent 2MSL delay on accept
137 int one = 1;
138 if (-1 == setsockopt(serverSocket_, SOL_SOCKET, SO_REUSEADDR,
139 &one, sizeof(one))) {
David Reiss9b209552008-04-08 06:26:05 +0000140 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000141 GlobalOutput.perror("TServerSocket::listen() setsockopt() SO_REUSEADDR ", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000142 close();
David Reiss9b209552008-04-08 06:26:05 +0000143 throw TTransportException(TTransportException::NOT_OPEN, "Could not set SO_REUSEADDR", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000144 }
145
Christopher Piro9cc63b52008-03-21 00:40:42 +0000146 // Set TCP buffer sizes
147 if (tcpSendBuffer_ > 0) {
148 if (-1 == setsockopt(serverSocket_, SOL_SOCKET, SO_SNDBUF,
149 &tcpSendBuffer_, sizeof(tcpSendBuffer_))) {
David Reiss9b209552008-04-08 06:26:05 +0000150 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000151 GlobalOutput.perror("TServerSocket::listen() setsockopt() SO_SNDBUF ", errno_copy);
Christopher Piro9cc63b52008-03-21 00:40:42 +0000152 close();
David Reiss9b209552008-04-08 06:26:05 +0000153 throw TTransportException(TTransportException::NOT_OPEN, "Could not set SO_SNDBUF", errno_copy);
Christopher Piro9cc63b52008-03-21 00:40:42 +0000154 }
155 }
156
157 if (tcpRecvBuffer_ > 0) {
158 if (-1 == setsockopt(serverSocket_, SOL_SOCKET, SO_RCVBUF,
159 &tcpRecvBuffer_, sizeof(tcpRecvBuffer_))) {
David Reiss9b209552008-04-08 06:26:05 +0000160 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000161 GlobalOutput.perror("TServerSocket::listen() setsockopt() SO_RCVBUF ", errno_copy);
Christopher Piro9cc63b52008-03-21 00:40:42 +0000162 close();
David Reiss9b209552008-04-08 06:26:05 +0000163 throw TTransportException(TTransportException::NOT_OPEN, "Could not set SO_RCVBUF", errno_copy);
Christopher Piro9cc63b52008-03-21 00:40:42 +0000164 }
165 }
166
Mark Slee29050782006-09-29 00:12:30 +0000167 // Defer accept
168 #ifdef TCP_DEFER_ACCEPT
169 if (-1 == setsockopt(serverSocket_, SOL_SOCKET, TCP_DEFER_ACCEPT,
170 &one, sizeof(one))) {
David Reiss9b209552008-04-08 06:26:05 +0000171 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000172 GlobalOutput.perror("TServerSocket::listen() setsockopt() TCP_DEFER_ACCEPT ", errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000173 close();
David Reiss9b209552008-04-08 06:26:05 +0000174 throw TTransportException(TTransportException::NOT_OPEN, "Could not set TCP_DEFER_ACCEPT", errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000175 }
176 #endif // #ifdef TCP_DEFER_ACCEPT
177
David Reiss13aea462008-06-10 22:56:04 +0000178 #ifdef IPV6_V6ONLY
179 int zero = 0;
David Reiss1a2f2182008-06-11 01:14:01 +0000180 if (-1 == setsockopt(serverSocket_, IPPROTO_IPV6, IPV6_V6ONLY,
David Reiss13aea462008-06-10 22:56:04 +0000181 &zero, sizeof(zero))) {
David Reiss01e55c12008-07-13 22:18:51 +0000182 GlobalOutput.perror("TServerSocket::listen() IPV6_V6ONLY ", errno);
David Reiss13aea462008-06-10 22:56:04 +0000183 }
184 #endif // #ifdef IPV6_V6ONLY
185
Mark Sleee8540632006-05-30 09:24:40 +0000186 // Turn linger off, don't want to block on calls to close
187 struct linger ling = {0, 0};
188 if (-1 == setsockopt(serverSocket_, SOL_SOCKET, SO_LINGER,
189 &ling, sizeof(ling))) {
David Reiss9b209552008-04-08 06:26:05 +0000190 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000191 GlobalOutput.perror("TServerSocket::listen() setsockopt() SO_LINGER ", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000192 close();
David Reiss9b209552008-04-08 06:26:05 +0000193 throw TTransportException(TTransportException::NOT_OPEN, "Could not set SO_LINGER", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000194 }
195
Mark Slee29050782006-09-29 00:12:30 +0000196 // TCP Nodelay, speed over bandwidth
197 if (-1 == setsockopt(serverSocket_, IPPROTO_TCP, TCP_NODELAY,
198 &one, sizeof(one))) {
David Reiss9b209552008-04-08 06:26:05 +0000199 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000200 GlobalOutput.perror("TServerSocket::listen() setsockopt() TCP_NODELAY ", errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000201 close();
David Reiss9b209552008-04-08 06:26:05 +0000202 throw TTransportException(TTransportException::NOT_OPEN, "Could not set TCP_NODELAY", errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000203 }
204
Mark Sleea5a783f2007-03-02 19:41:08 +0000205 // Set NONBLOCK on the accept socket
206 int flags = fcntl(serverSocket_, F_GETFL, 0);
207 if (flags == -1) {
David Reiss9b209552008-04-08 06:26:05 +0000208 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000209 GlobalOutput.perror("TServerSocket::listen() fcntl() F_GETFL ", errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000210 throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed", errno_copy);
Mark Sleea5a783f2007-03-02 19:41:08 +0000211 }
Mark Slee561b5362007-03-09 19:26:29 +0000212
Mark Sleea5a783f2007-03-02 19:41:08 +0000213 if (-1 == fcntl(serverSocket_, F_SETFL, flags | O_NONBLOCK)) {
David Reiss9b209552008-04-08 06:26:05 +0000214 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000215 GlobalOutput.perror("TServerSocket::listen() fcntl() O_NONBLOCK ", errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000216 throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed", errno_copy);
Mark Sleea5a783f2007-03-02 19:41:08 +0000217 }
218
boz1ea81ce2007-05-14 23:04:33 +0000219 // prepare the port information
Mark Slee256bdc42007-11-27 08:42:19 +0000220 // we may want to try to bind more than once, since SO_REUSEADDR doesn't
boz1ea81ce2007-05-14 23:04:33 +0000221 // always seem to work. The client can configure the retry variables.
222 int retries = 0;
223 do {
Mark Slee6d56eb92007-07-06 22:28:15 +0000224 if (0 == bind(serverSocket_, res->ai_addr, res->ai_addrlen)) {
boz1ea81ce2007-05-14 23:04:33 +0000225 break;
226 }
227
228 // use short circuit evaluation here to only sleep if we need to
229 } while ((retries++ < retryLimit_) && (sleep(retryDelay_) == 0));
230
Mark Slee6d56eb92007-07-06 22:28:15 +0000231 // free addrinfo
232 freeaddrinfo(res0);
Mark Slee256bdc42007-11-27 08:42:19 +0000233
boz1ea81ce2007-05-14 23:04:33 +0000234 // throw an error if we failed to bind properly
235 if (retries > retryLimit_) {
Mark Sleee8540632006-05-30 09:24:40 +0000236 char errbuf[1024];
237 sprintf(errbuf, "TServerSocket::listen() BIND %d", port_);
boz6ded7752007-06-05 22:41:18 +0000238 GlobalOutput(errbuf);
Mark Sleee8540632006-05-30 09:24:40 +0000239 close();
Mark Sleef9831082007-02-20 20:59:21 +0000240 throw TTransportException(TTransportException::NOT_OPEN, "Could not bind");
Mark Sleee8540632006-05-30 09:24:40 +0000241 }
242
243 // Call listen
244 if (-1 == ::listen(serverSocket_, acceptBacklog_)) {
David Reiss9b209552008-04-08 06:26:05 +0000245 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000246 GlobalOutput.perror("TServerSocket::listen() listen() ", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000247 close();
David Reiss9b209552008-04-08 06:26:05 +0000248 throw TTransportException(TTransportException::NOT_OPEN, "Could not listen", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000249 }
250
251 // The socket is now listening!
Mark Sleee8540632006-05-30 09:24:40 +0000252}
253
Marc Slemko16698852006-08-04 03:16:10 +0000254shared_ptr<TTransport> TServerSocket::acceptImpl() {
Martin Kraemer10640d82007-02-03 01:59:12 +0000255 if (serverSocket_ < 0) {
Mark Sleef9831082007-02-20 20:59:21 +0000256 throw TTransportException(TTransportException::NOT_OPEN, "TServerSocket not listening");
Mark Sleee8540632006-05-30 09:24:40 +0000257 }
258
David Reiss1677ac92008-04-08 06:26:27 +0000259 struct pollfd fds[2];
Mark Sleea5a783f2007-03-02 19:41:08 +0000260
Aditya Agarwal7859a572007-05-31 01:33:07 +0000261 int maxEintrs = 5;
262 int numEintrs = 0;
263
Mark Sleea5a783f2007-03-02 19:41:08 +0000264 while (true) {
David Reissc88eb8c2008-06-11 01:18:54 +0000265 std::memset(fds, 0 , sizeof(fds));
David Reiss1677ac92008-04-08 06:26:27 +0000266 fds[0].fd = serverSocket_;
267 fds[0].events = POLLIN;
Mark Slee561b5362007-03-09 19:26:29 +0000268 if (intSock2_ >= 0) {
David Reiss1677ac92008-04-08 06:26:27 +0000269 fds[1].fd = intSock2_;
David Reiss2724c7a2008-04-18 17:48:03 +0000270 fds[1].events = POLLIN;
Mark Sleea5a783f2007-03-02 19:41:08 +0000271 }
David Reiss1677ac92008-04-08 06:26:27 +0000272 int ret = poll(fds, 2, -1);
Mark Sleea5a783f2007-03-02 19:41:08 +0000273
Mark Slee561b5362007-03-09 19:26:29 +0000274 if (ret < 0) {
Aditya Agarwal7859a572007-05-31 01:33:07 +0000275 // error cases
bozf83c9db2007-05-31 23:38:37 +0000276 if (errno == EINTR && (numEintrs++ < maxEintrs)) {
Mark Slee256bdc42007-11-27 08:42:19 +0000277 // EINTR needs to be handled manually and we can tolerate
Aditya Agarwal7859a572007-05-31 01:33:07 +0000278 // a certain number
279 continue;
280 }
David Reiss9b209552008-04-08 06:26:05 +0000281 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000282 GlobalOutput.perror("TServerSocket::acceptImpl() poll() ", errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000283 throw TTransportException(TTransportException::UNKNOWN, "Unknown", errno_copy);
Mark Slee561b5362007-03-09 19:26:29 +0000284 } else if (ret > 0) {
285 // Check for an interrupt signal
David Reiss1677ac92008-04-08 06:26:27 +0000286 if (intSock2_ >= 0 && (fds[1].revents & POLLIN)) {
Mark Slee561b5362007-03-09 19:26:29 +0000287 int8_t buf;
288 if (-1 == recv(intSock2_, &buf, sizeof(int8_t), 0)) {
David Reiss01e55c12008-07-13 22:18:51 +0000289 GlobalOutput.perror("TServerSocket::acceptImpl() recv() interrupt ", errno);
Mark Slee561b5362007-03-09 19:26:29 +0000290 }
291 throw TTransportException(TTransportException::INTERRUPTED);
292 }
David Reiss9b209552008-04-08 06:26:05 +0000293
Mark Slee561b5362007-03-09 19:26:29 +0000294 // Check for the actual server socket being ready
David Reiss1677ac92008-04-08 06:26:27 +0000295 if (fds[0].revents & POLLIN) {
Mark Slee561b5362007-03-09 19:26:29 +0000296 break;
297 }
298 } else {
David Reiss1677ac92008-04-08 06:26:27 +0000299 GlobalOutput("TServerSocket::acceptImpl() poll 0");
Mark Slee256bdc42007-11-27 08:42:19 +0000300 throw TTransportException(TTransportException::UNKNOWN);
Mark Sleea5a783f2007-03-02 19:41:08 +0000301 }
302 }
303
Mark Slee6d56eb92007-07-06 22:28:15 +0000304 struct sockaddr_storage clientAddress;
Mark Sleee8540632006-05-30 09:24:40 +0000305 int size = sizeof(clientAddress);
306 int clientSocket = ::accept(serverSocket_,
307 (struct sockaddr *) &clientAddress,
308 (socklen_t *) &size);
Mark Slee256bdc42007-11-27 08:42:19 +0000309
Martin Kraemeree341cb2007-02-05 21:40:38 +0000310 if (clientSocket < 0) {
David Reissbc3dddb2007-08-22 23:20:24 +0000311 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000312 GlobalOutput.perror("TServerSocket::acceptImpl() ::accept() ", errno_copy);
David Reissbc3dddb2007-08-22 23:20:24 +0000313 throw TTransportException(TTransportException::UNKNOWN, "accept()", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000314 }
Mark Sleea5a783f2007-03-02 19:41:08 +0000315
316 // Make sure client socket is blocking
317 int flags = fcntl(clientSocket, F_GETFL, 0);
318 if (flags == -1) {
David Reissbc3dddb2007-08-22 23:20:24 +0000319 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000320 GlobalOutput.perror("TServerSocket::acceptImpl() fcntl() F_GETFL ", errno_copy);
David Reissbc3dddb2007-08-22 23:20:24 +0000321 throw TTransportException(TTransportException::UNKNOWN, "fcntl(F_GETFL)", errno_copy);
Mark Sleea5a783f2007-03-02 19:41:08 +0000322 }
David Reiss9b209552008-04-08 06:26:05 +0000323
Mark Sleea5a783f2007-03-02 19:41:08 +0000324 if (-1 == fcntl(clientSocket, F_SETFL, flags & ~O_NONBLOCK)) {
David Reissbc3dddb2007-08-22 23:20:24 +0000325 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000326 GlobalOutput.perror("TServerSocket::acceptImpl() fcntl() F_SETFL ~O_NONBLOCK ", errno_copy);
David Reissbc3dddb2007-08-22 23:20:24 +0000327 throw TTransportException(TTransportException::UNKNOWN, "fcntl(F_SETFL)", errno_copy);
Mark Sleea5a783f2007-03-02 19:41:08 +0000328 }
Mark Slee256bdc42007-11-27 08:42:19 +0000329
Mark Slee29050782006-09-29 00:12:30 +0000330 shared_ptr<TSocket> client(new TSocket(clientSocket));
331 if (sendTimeout_ > 0) {
332 client->setSendTimeout(sendTimeout_);
333 }
334 if (recvTimeout_ > 0) {
335 client->setRecvTimeout(recvTimeout_);
Mark Sleea5a783f2007-03-02 19:41:08 +0000336 }
Mark Slee256bdc42007-11-27 08:42:19 +0000337
Mark Slee29050782006-09-29 00:12:30 +0000338 return client;
Mark Sleee8540632006-05-30 09:24:40 +0000339}
340
Mark Slee561b5362007-03-09 19:26:29 +0000341void TServerSocket::interrupt() {
342 if (intSock1_ >= 0) {
343 int8_t byte = 0;
344 if (-1 == send(intSock1_, &byte, sizeof(int8_t), 0)) {
David Reiss01e55c12008-07-13 22:18:51 +0000345 GlobalOutput.perror("TServerSocket::interrupt() send() ", errno);
Mark Slee561b5362007-03-09 19:26:29 +0000346 }
347 }
348}
349
Mark Sleee8540632006-05-30 09:24:40 +0000350void TServerSocket::close() {
Martin Kraemeree341cb2007-02-05 21:40:38 +0000351 if (serverSocket_ >= 0) {
Mark Sleee8540632006-05-30 09:24:40 +0000352 shutdown(serverSocket_, SHUT_RDWR);
353 ::close(serverSocket_);
354 }
Mark Slee561b5362007-03-09 19:26:29 +0000355 if (intSock1_ >= 0) {
356 ::close(intSock1_);
357 }
358 if (intSock2_ >= 0) {
359 ::close(intSock2_);
360 }
Martin Kraemeree341cb2007-02-05 21:40:38 +0000361 serverSocket_ = -1;
Mark Slee561b5362007-03-09 19:26:29 +0000362 intSock1_ = -1;
363 intSock2_ = -1;
Mark Sleee8540632006-05-30 09:24:40 +0000364}
Marc Slemko6f038a72006-08-03 18:58:09 +0000365
T Jake Lucianib5e62212009-01-31 22:36:20 +0000366}}} // apache::thrift::transport