blob: 10d3e9ee09bd2a35ec47bc8253c5da3e5291867a [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>
David Reiss5105b2e2009-05-21 02:28:27 +000029#include <unistd.h>
Mark Sleee8540632006-05-30 09:24:40 +000030
Marc Slemkod42a2c22006-08-10 03:30:18 +000031#include "TSocket.h"
32#include "TServerSocket.h"
Marc Slemko16698852006-08-04 03:16:10 +000033#include <boost/shared_ptr.hpp>
Mark Sleee8540632006-05-30 09:24:40 +000034
T Jake Lucianib5e62212009-01-31 22:36:20 +000035namespace apache { namespace thrift { namespace transport {
Marc Slemko6f038a72006-08-03 18:58:09 +000036
Martin Kraemere6c4fa62007-07-09 19:08:25 +000037using namespace std;
David Reissd4a269c2007-08-23 02:37:19 +000038using boost::shared_ptr;
Marc Slemko16698852006-08-04 03:16:10 +000039
Mark Sleee8540632006-05-30 09:24:40 +000040TServerSocket::TServerSocket(int port) :
Mark Slee29050782006-09-29 00:12:30 +000041 port_(port),
Martin Kraemeree341cb2007-02-05 21:40:38 +000042 serverSocket_(-1),
Mark Slee29050782006-09-29 00:12:30 +000043 acceptBacklog_(1024),
44 sendTimeout_(0),
Mark Sleea5a783f2007-03-02 19:41:08 +000045 recvTimeout_(0),
boz1ea81ce2007-05-14 23:04:33 +000046 retryLimit_(0),
47 retryDelay_(0),
Christopher Piro9cc63b52008-03-21 00:40:42 +000048 tcpSendBuffer_(0),
49 tcpRecvBuffer_(0),
Mark Slee561b5362007-03-09 19:26:29 +000050 intSock1_(-1),
51 intSock2_(-1) {}
Mark Slee29050782006-09-29 00:12:30 +000052
53TServerSocket::TServerSocket(int port, int sendTimeout, int recvTimeout) :
54 port_(port),
Martin Kraemeree341cb2007-02-05 21:40:38 +000055 serverSocket_(-1),
Mark Slee29050782006-09-29 00:12:30 +000056 acceptBacklog_(1024),
57 sendTimeout_(sendTimeout),
Mark Sleea5a783f2007-03-02 19:41:08 +000058 recvTimeout_(recvTimeout),
boz1ea81ce2007-05-14 23:04:33 +000059 retryLimit_(0),
60 retryDelay_(0),
Christopher Piro9cc63b52008-03-21 00:40:42 +000061 tcpSendBuffer_(0),
62 tcpRecvBuffer_(0),
Mark Slee561b5362007-03-09 19:26:29 +000063 intSock1_(-1),
64 intSock2_(-1) {}
Mark Sleee8540632006-05-30 09:24:40 +000065
66TServerSocket::~TServerSocket() {
67 close();
68}
69
Mark Slee29050782006-09-29 00:12:30 +000070void TServerSocket::setSendTimeout(int sendTimeout) {
71 sendTimeout_ = sendTimeout;
72}
73
74void TServerSocket::setRecvTimeout(int recvTimeout) {
75 recvTimeout_ = recvTimeout;
76}
77
boz1ea81ce2007-05-14 23:04:33 +000078void TServerSocket::setRetryLimit(int retryLimit) {
79 retryLimit_ = retryLimit;
80}
81
82void TServerSocket::setRetryDelay(int retryDelay) {
83 retryDelay_ = retryDelay;
84}
85
Christopher Piro9cc63b52008-03-21 00:40:42 +000086void TServerSocket::setTcpSendBuffer(int tcpSendBuffer) {
87 tcpSendBuffer_ = tcpSendBuffer;
88}
89
90void TServerSocket::setTcpRecvBuffer(int tcpRecvBuffer) {
91 tcpRecvBuffer_ = tcpRecvBuffer;
92}
93
Mark Slee8d7e1f62006-06-07 06:48:56 +000094void TServerSocket::listen() {
Mark Slee561b5362007-03-09 19:26:29 +000095 int sv[2];
96 if (-1 == socketpair(AF_LOCAL, SOCK_STREAM, 0, sv)) {
David Reiss01e55c12008-07-13 22:18:51 +000097 GlobalOutput.perror("TServerSocket::listen() socketpair() ", errno);
Mark Slee561b5362007-03-09 19:26:29 +000098 intSock1_ = -1;
99 intSock2_ = -1;
100 } else {
Mark Sleee02385b2007-06-09 01:21:16 +0000101 intSock1_ = sv[1];
102 intSock2_ = sv[0];
Mark Slee561b5362007-03-09 19:26:29 +0000103 }
104
Mark Slee6d56eb92007-07-06 22:28:15 +0000105 struct addrinfo hints, *res, *res0;
106 int error;
107 char port[sizeof("65536") + 1];
David Reissc88eb8c2008-06-11 01:18:54 +0000108 std::memset(&hints, 0, sizeof(hints));
Mark Slee6d56eb92007-07-06 22:28:15 +0000109 hints.ai_family = PF_UNSPEC;
110 hints.ai_socktype = SOCK_STREAM;
Mark Slee256bdc42007-11-27 08:42:19 +0000111 hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
Mark Slee6d56eb92007-07-06 22:28:15 +0000112 sprintf(port, "%d", port_);
113
114 // Wildcard address
115 error = getaddrinfo(NULL, port, &hints, &res0);
116 if (error) {
David Reiss01e55c12008-07-13 22:18:51 +0000117 GlobalOutput.printf("getaddrinfo %d: %s", error, gai_strerror(error));
Mark Slee6d56eb92007-07-06 22:28:15 +0000118 close();
119 throw TTransportException(TTransportException::NOT_OPEN, "Could not resolve host for server socket.");
120 }
121
122 // Pick the ipv6 address first since ipv4 addresses can be mapped
123 // into ipv6 space.
124 for (res = res0; res; res = res->ai_next) {
125 if (res->ai_family == AF_INET6 || res->ai_next == NULL)
126 break;
127 }
Mark Slee256bdc42007-11-27 08:42:19 +0000128
Mark Slee6d56eb92007-07-06 22:28:15 +0000129 serverSocket_ = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
Mark Sleee8540632006-05-30 09:24:40 +0000130 if (serverSocket_ == -1) {
David Reiss9b209552008-04-08 06:26:05 +0000131 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000132 GlobalOutput.perror("TServerSocket::listen() socket() ", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000133 close();
David Reiss9b209552008-04-08 06:26:05 +0000134 throw TTransportException(TTransportException::NOT_OPEN, "Could not create server socket.", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000135 }
136
137 // Set reusaddress to prevent 2MSL delay on accept
138 int one = 1;
139 if (-1 == setsockopt(serverSocket_, SOL_SOCKET, SO_REUSEADDR,
140 &one, sizeof(one))) {
David Reiss9b209552008-04-08 06:26:05 +0000141 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000142 GlobalOutput.perror("TServerSocket::listen() setsockopt() SO_REUSEADDR ", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000143 close();
David Reiss9b209552008-04-08 06:26:05 +0000144 throw TTransportException(TTransportException::NOT_OPEN, "Could not set SO_REUSEADDR", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000145 }
146
Christopher Piro9cc63b52008-03-21 00:40:42 +0000147 // Set TCP buffer sizes
148 if (tcpSendBuffer_ > 0) {
149 if (-1 == setsockopt(serverSocket_, SOL_SOCKET, SO_SNDBUF,
150 &tcpSendBuffer_, sizeof(tcpSendBuffer_))) {
David Reiss9b209552008-04-08 06:26:05 +0000151 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000152 GlobalOutput.perror("TServerSocket::listen() setsockopt() SO_SNDBUF ", errno_copy);
Christopher Piro9cc63b52008-03-21 00:40:42 +0000153 close();
David Reiss9b209552008-04-08 06:26:05 +0000154 throw TTransportException(TTransportException::NOT_OPEN, "Could not set SO_SNDBUF", errno_copy);
Christopher Piro9cc63b52008-03-21 00:40:42 +0000155 }
156 }
157
158 if (tcpRecvBuffer_ > 0) {
159 if (-1 == setsockopt(serverSocket_, SOL_SOCKET, SO_RCVBUF,
160 &tcpRecvBuffer_, sizeof(tcpRecvBuffer_))) {
David Reiss9b209552008-04-08 06:26:05 +0000161 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000162 GlobalOutput.perror("TServerSocket::listen() setsockopt() SO_RCVBUF ", errno_copy);
Christopher Piro9cc63b52008-03-21 00:40:42 +0000163 close();
David Reiss9b209552008-04-08 06:26:05 +0000164 throw TTransportException(TTransportException::NOT_OPEN, "Could not set SO_RCVBUF", errno_copy);
Christopher Piro9cc63b52008-03-21 00:40:42 +0000165 }
166 }
167
Mark Slee29050782006-09-29 00:12:30 +0000168 // Defer accept
169 #ifdef TCP_DEFER_ACCEPT
170 if (-1 == setsockopt(serverSocket_, SOL_SOCKET, TCP_DEFER_ACCEPT,
171 &one, sizeof(one))) {
David Reiss9b209552008-04-08 06:26:05 +0000172 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000173 GlobalOutput.perror("TServerSocket::listen() setsockopt() TCP_DEFER_ACCEPT ", errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000174 close();
David Reiss9b209552008-04-08 06:26:05 +0000175 throw TTransportException(TTransportException::NOT_OPEN, "Could not set TCP_DEFER_ACCEPT", errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000176 }
177 #endif // #ifdef TCP_DEFER_ACCEPT
178
David Reiss13aea462008-06-10 22:56:04 +0000179 #ifdef IPV6_V6ONLY
180 int zero = 0;
David Reiss1a2f2182008-06-11 01:14:01 +0000181 if (-1 == setsockopt(serverSocket_, IPPROTO_IPV6, IPV6_V6ONLY,
David Reiss13aea462008-06-10 22:56:04 +0000182 &zero, sizeof(zero))) {
David Reiss01e55c12008-07-13 22:18:51 +0000183 GlobalOutput.perror("TServerSocket::listen() IPV6_V6ONLY ", errno);
David Reiss13aea462008-06-10 22:56:04 +0000184 }
185 #endif // #ifdef IPV6_V6ONLY
186
Mark Sleee8540632006-05-30 09:24:40 +0000187 // Turn linger off, don't want to block on calls to close
188 struct linger ling = {0, 0};
189 if (-1 == setsockopt(serverSocket_, SOL_SOCKET, SO_LINGER,
190 &ling, sizeof(ling))) {
David Reiss9b209552008-04-08 06:26:05 +0000191 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000192 GlobalOutput.perror("TServerSocket::listen() setsockopt() SO_LINGER ", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000193 close();
David Reiss9b209552008-04-08 06:26:05 +0000194 throw TTransportException(TTransportException::NOT_OPEN, "Could not set SO_LINGER", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000195 }
196
Mark Slee29050782006-09-29 00:12:30 +0000197 // TCP Nodelay, speed over bandwidth
198 if (-1 == setsockopt(serverSocket_, IPPROTO_TCP, TCP_NODELAY,
199 &one, sizeof(one))) {
David Reiss9b209552008-04-08 06:26:05 +0000200 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000201 GlobalOutput.perror("TServerSocket::listen() setsockopt() TCP_NODELAY ", errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000202 close();
David Reiss9b209552008-04-08 06:26:05 +0000203 throw TTransportException(TTransportException::NOT_OPEN, "Could not set TCP_NODELAY", errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000204 }
205
Mark Sleea5a783f2007-03-02 19:41:08 +0000206 // Set NONBLOCK on the accept socket
207 int flags = fcntl(serverSocket_, F_GETFL, 0);
208 if (flags == -1) {
David Reiss9b209552008-04-08 06:26:05 +0000209 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000210 GlobalOutput.perror("TServerSocket::listen() fcntl() F_GETFL ", errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000211 throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed", errno_copy);
Mark Sleea5a783f2007-03-02 19:41:08 +0000212 }
Mark Slee561b5362007-03-09 19:26:29 +0000213
Mark Sleea5a783f2007-03-02 19:41:08 +0000214 if (-1 == fcntl(serverSocket_, F_SETFL, flags | O_NONBLOCK)) {
David Reiss9b209552008-04-08 06:26:05 +0000215 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000216 GlobalOutput.perror("TServerSocket::listen() fcntl() O_NONBLOCK ", errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000217 throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed", errno_copy);
Mark Sleea5a783f2007-03-02 19:41:08 +0000218 }
219
boz1ea81ce2007-05-14 23:04:33 +0000220 // prepare the port information
Mark Slee256bdc42007-11-27 08:42:19 +0000221 // we may want to try to bind more than once, since SO_REUSEADDR doesn't
boz1ea81ce2007-05-14 23:04:33 +0000222 // always seem to work. The client can configure the retry variables.
223 int retries = 0;
224 do {
Mark Slee6d56eb92007-07-06 22:28:15 +0000225 if (0 == bind(serverSocket_, res->ai_addr, res->ai_addrlen)) {
boz1ea81ce2007-05-14 23:04:33 +0000226 break;
227 }
228
229 // use short circuit evaluation here to only sleep if we need to
230 } while ((retries++ < retryLimit_) && (sleep(retryDelay_) == 0));
231
Mark Slee6d56eb92007-07-06 22:28:15 +0000232 // free addrinfo
233 freeaddrinfo(res0);
Mark Slee256bdc42007-11-27 08:42:19 +0000234
boz1ea81ce2007-05-14 23:04:33 +0000235 // throw an error if we failed to bind properly
236 if (retries > retryLimit_) {
Mark Sleee8540632006-05-30 09:24:40 +0000237 char errbuf[1024];
238 sprintf(errbuf, "TServerSocket::listen() BIND %d", port_);
boz6ded7752007-06-05 22:41:18 +0000239 GlobalOutput(errbuf);
Mark Sleee8540632006-05-30 09:24:40 +0000240 close();
Mark Sleef9831082007-02-20 20:59:21 +0000241 throw TTransportException(TTransportException::NOT_OPEN, "Could not bind");
Mark Sleee8540632006-05-30 09:24:40 +0000242 }
243
244 // Call listen
245 if (-1 == ::listen(serverSocket_, acceptBacklog_)) {
David Reiss9b209552008-04-08 06:26:05 +0000246 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000247 GlobalOutput.perror("TServerSocket::listen() listen() ", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000248 close();
David Reiss9b209552008-04-08 06:26:05 +0000249 throw TTransportException(TTransportException::NOT_OPEN, "Could not listen", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000250 }
251
252 // The socket is now listening!
Mark Sleee8540632006-05-30 09:24:40 +0000253}
254
Marc Slemko16698852006-08-04 03:16:10 +0000255shared_ptr<TTransport> TServerSocket::acceptImpl() {
Martin Kraemer10640d82007-02-03 01:59:12 +0000256 if (serverSocket_ < 0) {
Mark Sleef9831082007-02-20 20:59:21 +0000257 throw TTransportException(TTransportException::NOT_OPEN, "TServerSocket not listening");
Mark Sleee8540632006-05-30 09:24:40 +0000258 }
259
David Reiss1677ac92008-04-08 06:26:27 +0000260 struct pollfd fds[2];
Mark Sleea5a783f2007-03-02 19:41:08 +0000261
Aditya Agarwal7859a572007-05-31 01:33:07 +0000262 int maxEintrs = 5;
263 int numEintrs = 0;
264
Mark Sleea5a783f2007-03-02 19:41:08 +0000265 while (true) {
David Reissc88eb8c2008-06-11 01:18:54 +0000266 std::memset(fds, 0 , sizeof(fds));
David Reiss1677ac92008-04-08 06:26:27 +0000267 fds[0].fd = serverSocket_;
268 fds[0].events = POLLIN;
Mark Slee561b5362007-03-09 19:26:29 +0000269 if (intSock2_ >= 0) {
David Reiss1677ac92008-04-08 06:26:27 +0000270 fds[1].fd = intSock2_;
David Reiss2724c7a2008-04-18 17:48:03 +0000271 fds[1].events = POLLIN;
Mark Sleea5a783f2007-03-02 19:41:08 +0000272 }
David Reiss1677ac92008-04-08 06:26:27 +0000273 int ret = poll(fds, 2, -1);
Mark Sleea5a783f2007-03-02 19:41:08 +0000274
Mark Slee561b5362007-03-09 19:26:29 +0000275 if (ret < 0) {
Aditya Agarwal7859a572007-05-31 01:33:07 +0000276 // error cases
bozf83c9db2007-05-31 23:38:37 +0000277 if (errno == EINTR && (numEintrs++ < maxEintrs)) {
Mark Slee256bdc42007-11-27 08:42:19 +0000278 // EINTR needs to be handled manually and we can tolerate
Aditya Agarwal7859a572007-05-31 01:33:07 +0000279 // a certain number
280 continue;
281 }
David Reiss9b209552008-04-08 06:26:05 +0000282 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000283 GlobalOutput.perror("TServerSocket::acceptImpl() poll() ", errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000284 throw TTransportException(TTransportException::UNKNOWN, "Unknown", errno_copy);
Mark Slee561b5362007-03-09 19:26:29 +0000285 } else if (ret > 0) {
286 // Check for an interrupt signal
David Reiss1677ac92008-04-08 06:26:27 +0000287 if (intSock2_ >= 0 && (fds[1].revents & POLLIN)) {
Mark Slee561b5362007-03-09 19:26:29 +0000288 int8_t buf;
289 if (-1 == recv(intSock2_, &buf, sizeof(int8_t), 0)) {
David Reiss01e55c12008-07-13 22:18:51 +0000290 GlobalOutput.perror("TServerSocket::acceptImpl() recv() interrupt ", errno);
Mark Slee561b5362007-03-09 19:26:29 +0000291 }
292 throw TTransportException(TTransportException::INTERRUPTED);
293 }
David Reiss9b209552008-04-08 06:26:05 +0000294
Mark Slee561b5362007-03-09 19:26:29 +0000295 // Check for the actual server socket being ready
David Reiss1677ac92008-04-08 06:26:27 +0000296 if (fds[0].revents & POLLIN) {
Mark Slee561b5362007-03-09 19:26:29 +0000297 break;
298 }
299 } else {
David Reiss1677ac92008-04-08 06:26:27 +0000300 GlobalOutput("TServerSocket::acceptImpl() poll 0");
Mark Slee256bdc42007-11-27 08:42:19 +0000301 throw TTransportException(TTransportException::UNKNOWN);
Mark Sleea5a783f2007-03-02 19:41:08 +0000302 }
303 }
304
Mark Slee6d56eb92007-07-06 22:28:15 +0000305 struct sockaddr_storage clientAddress;
Mark Sleee8540632006-05-30 09:24:40 +0000306 int size = sizeof(clientAddress);
307 int clientSocket = ::accept(serverSocket_,
308 (struct sockaddr *) &clientAddress,
309 (socklen_t *) &size);
Mark Slee256bdc42007-11-27 08:42:19 +0000310
Martin Kraemeree341cb2007-02-05 21:40:38 +0000311 if (clientSocket < 0) {
David Reissbc3dddb2007-08-22 23:20:24 +0000312 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000313 GlobalOutput.perror("TServerSocket::acceptImpl() ::accept() ", errno_copy);
David Reissbc3dddb2007-08-22 23:20:24 +0000314 throw TTransportException(TTransportException::UNKNOWN, "accept()", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000315 }
Mark Sleea5a783f2007-03-02 19:41:08 +0000316
317 // Make sure client socket is blocking
318 int flags = fcntl(clientSocket, F_GETFL, 0);
319 if (flags == -1) {
David Reissbc3dddb2007-08-22 23:20:24 +0000320 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000321 GlobalOutput.perror("TServerSocket::acceptImpl() fcntl() F_GETFL ", errno_copy);
David Reissbc3dddb2007-08-22 23:20:24 +0000322 throw TTransportException(TTransportException::UNKNOWN, "fcntl(F_GETFL)", errno_copy);
Mark Sleea5a783f2007-03-02 19:41:08 +0000323 }
David Reiss9b209552008-04-08 06:26:05 +0000324
Mark Sleea5a783f2007-03-02 19:41:08 +0000325 if (-1 == fcntl(clientSocket, F_SETFL, flags & ~O_NONBLOCK)) {
David Reissbc3dddb2007-08-22 23:20:24 +0000326 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000327 GlobalOutput.perror("TServerSocket::acceptImpl() fcntl() F_SETFL ~O_NONBLOCK ", errno_copy);
David Reissbc3dddb2007-08-22 23:20:24 +0000328 throw TTransportException(TTransportException::UNKNOWN, "fcntl(F_SETFL)", errno_copy);
Mark Sleea5a783f2007-03-02 19:41:08 +0000329 }
Mark Slee256bdc42007-11-27 08:42:19 +0000330
Mark Slee29050782006-09-29 00:12:30 +0000331 shared_ptr<TSocket> client(new TSocket(clientSocket));
332 if (sendTimeout_ > 0) {
333 client->setSendTimeout(sendTimeout_);
334 }
335 if (recvTimeout_ > 0) {
336 client->setRecvTimeout(recvTimeout_);
Mark Sleea5a783f2007-03-02 19:41:08 +0000337 }
Mark Slee256bdc42007-11-27 08:42:19 +0000338
Mark Slee29050782006-09-29 00:12:30 +0000339 return client;
Mark Sleee8540632006-05-30 09:24:40 +0000340}
341
Mark Slee561b5362007-03-09 19:26:29 +0000342void TServerSocket::interrupt() {
343 if (intSock1_ >= 0) {
344 int8_t byte = 0;
345 if (-1 == send(intSock1_, &byte, sizeof(int8_t), 0)) {
David Reiss01e55c12008-07-13 22:18:51 +0000346 GlobalOutput.perror("TServerSocket::interrupt() send() ", errno);
Mark Slee561b5362007-03-09 19:26:29 +0000347 }
348 }
349}
350
Mark Sleee8540632006-05-30 09:24:40 +0000351void TServerSocket::close() {
Martin Kraemeree341cb2007-02-05 21:40:38 +0000352 if (serverSocket_ >= 0) {
Mark Sleee8540632006-05-30 09:24:40 +0000353 shutdown(serverSocket_, SHUT_RDWR);
354 ::close(serverSocket_);
355 }
Mark Slee561b5362007-03-09 19:26:29 +0000356 if (intSock1_ >= 0) {
357 ::close(intSock1_);
358 }
359 if (intSock2_ >= 0) {
360 ::close(intSock2_);
361 }
Martin Kraemeree341cb2007-02-05 21:40:38 +0000362 serverSocket_ = -1;
Mark Slee561b5362007-03-09 19:26:29 +0000363 intSock1_ = -1;
364 intSock2_ = -1;
Mark Sleee8540632006-05-30 09:24:40 +0000365}
Marc Slemko6f038a72006-08-03 18:58:09 +0000366
T Jake Lucianib5e62212009-01-31 22:36:20 +0000367}}} // apache::thrift::transport