blob: 3493fc098e2c22bfffc7d7674778ad8a3fd007bc [file] [log] [blame]
Mark Slee9f0c6512007-02-28 23:58:26 +00001// 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 Reissc88eb8c2008-06-11 01:18:54 +00007#include <cstring>
Mark Sleee8540632006-05-30 09:24:40 +00008#include <sys/socket.h>
David Reiss1677ac92008-04-08 06:26:27 +00009#include <sys/poll.h>
Mark Sleedd564972007-08-21 02:39:57 +000010#include <sys/types.h>
Mark Sleee8540632006-05-30 09:24:40 +000011#include <netinet/in.h>
Mark Slee29050782006-09-29 00:12:30 +000012#include <netinet/tcp.h>
Mark Slee6d56eb92007-07-06 22:28:15 +000013#include <netdb.h>
Mark Sleea5a783f2007-03-02 19:41:08 +000014#include <fcntl.h>
Mark Slee8d7e1f62006-06-07 06:48:56 +000015#include <errno.h>
Mark Sleee8540632006-05-30 09:24:40 +000016
Marc Slemkod42a2c22006-08-10 03:30:18 +000017#include "TSocket.h"
18#include "TServerSocket.h"
Marc Slemko16698852006-08-04 03:16:10 +000019#include <boost/shared_ptr.hpp>
Mark Sleee8540632006-05-30 09:24:40 +000020
Mark Slee256bdc42007-11-27 08:42:19 +000021namespace facebook { namespace thrift { namespace transport {
Marc Slemko6f038a72006-08-03 18:58:09 +000022
Martin Kraemere6c4fa62007-07-09 19:08:25 +000023using namespace std;
David Reissd4a269c2007-08-23 02:37:19 +000024using boost::shared_ptr;
Marc Slemko16698852006-08-04 03:16:10 +000025
Mark Sleee8540632006-05-30 09:24:40 +000026TServerSocket::TServerSocket(int port) :
Mark Slee29050782006-09-29 00:12:30 +000027 port_(port),
Martin Kraemeree341cb2007-02-05 21:40:38 +000028 serverSocket_(-1),
Mark Slee29050782006-09-29 00:12:30 +000029 acceptBacklog_(1024),
30 sendTimeout_(0),
Mark Sleea5a783f2007-03-02 19:41:08 +000031 recvTimeout_(0),
boz1ea81ce2007-05-14 23:04:33 +000032 retryLimit_(0),
33 retryDelay_(0),
Christopher Piro9cc63b52008-03-21 00:40:42 +000034 tcpSendBuffer_(0),
35 tcpRecvBuffer_(0),
Mark Slee561b5362007-03-09 19:26:29 +000036 intSock1_(-1),
37 intSock2_(-1) {}
Mark Slee29050782006-09-29 00:12:30 +000038
39TServerSocket::TServerSocket(int port, int sendTimeout, int recvTimeout) :
40 port_(port),
Martin Kraemeree341cb2007-02-05 21:40:38 +000041 serverSocket_(-1),
Mark Slee29050782006-09-29 00:12:30 +000042 acceptBacklog_(1024),
43 sendTimeout_(sendTimeout),
Mark Sleea5a783f2007-03-02 19:41:08 +000044 recvTimeout_(recvTimeout),
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 Sleee8540632006-05-30 09:24:40 +000051
52TServerSocket::~TServerSocket() {
53 close();
54}
55
Mark Slee29050782006-09-29 00:12:30 +000056void TServerSocket::setSendTimeout(int sendTimeout) {
57 sendTimeout_ = sendTimeout;
58}
59
60void TServerSocket::setRecvTimeout(int recvTimeout) {
61 recvTimeout_ = recvTimeout;
62}
63
boz1ea81ce2007-05-14 23:04:33 +000064void TServerSocket::setRetryLimit(int retryLimit) {
65 retryLimit_ = retryLimit;
66}
67
68void TServerSocket::setRetryDelay(int retryDelay) {
69 retryDelay_ = retryDelay;
70}
71
Christopher Piro9cc63b52008-03-21 00:40:42 +000072void TServerSocket::setTcpSendBuffer(int tcpSendBuffer) {
73 tcpSendBuffer_ = tcpSendBuffer;
74}
75
76void TServerSocket::setTcpRecvBuffer(int tcpRecvBuffer) {
77 tcpRecvBuffer_ = tcpRecvBuffer;
78}
79
Mark Slee8d7e1f62006-06-07 06:48:56 +000080void TServerSocket::listen() {
Mark Slee561b5362007-03-09 19:26:29 +000081 int sv[2];
82 if (-1 == socketpair(AF_LOCAL, SOCK_STREAM, 0, sv)) {
David Reiss01e55c12008-07-13 22:18:51 +000083 GlobalOutput.perror("TServerSocket::listen() socketpair() ", errno);
Mark Slee561b5362007-03-09 19:26:29 +000084 intSock1_ = -1;
85 intSock2_ = -1;
86 } else {
Mark Sleee02385b2007-06-09 01:21:16 +000087 intSock1_ = sv[1];
88 intSock2_ = sv[0];
Mark Slee561b5362007-03-09 19:26:29 +000089 }
90
Mark Slee6d56eb92007-07-06 22:28:15 +000091 struct addrinfo hints, *res, *res0;
92 int error;
93 char port[sizeof("65536") + 1];
David Reissc88eb8c2008-06-11 01:18:54 +000094 std::memset(&hints, 0, sizeof(hints));
Mark Slee6d56eb92007-07-06 22:28:15 +000095 hints.ai_family = PF_UNSPEC;
96 hints.ai_socktype = SOCK_STREAM;
Mark Slee256bdc42007-11-27 08:42:19 +000097 hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
Mark Slee6d56eb92007-07-06 22:28:15 +000098 sprintf(port, "%d", port_);
99
100 // Wildcard address
101 error = getaddrinfo(NULL, port, &hints, &res0);
102 if (error) {
David Reiss01e55c12008-07-13 22:18:51 +0000103 GlobalOutput.printf("getaddrinfo %d: %s", error, gai_strerror(error));
Mark Slee6d56eb92007-07-06 22:28:15 +0000104 close();
105 throw TTransportException(TTransportException::NOT_OPEN, "Could not resolve host for server socket.");
106 }
107
108 // Pick the ipv6 address first since ipv4 addresses can be mapped
109 // into ipv6 space.
110 for (res = res0; res; res = res->ai_next) {
111 if (res->ai_family == AF_INET6 || res->ai_next == NULL)
112 break;
113 }
Mark Slee256bdc42007-11-27 08:42:19 +0000114
Mark Slee6d56eb92007-07-06 22:28:15 +0000115 serverSocket_ = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
Mark Sleee8540632006-05-30 09:24:40 +0000116 if (serverSocket_ == -1) {
David Reiss9b209552008-04-08 06:26:05 +0000117 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000118 GlobalOutput.perror("TServerSocket::listen() socket() ", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000119 close();
David Reiss9b209552008-04-08 06:26:05 +0000120 throw TTransportException(TTransportException::NOT_OPEN, "Could not create server socket.", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000121 }
122
123 // Set reusaddress to prevent 2MSL delay on accept
124 int one = 1;
125 if (-1 == setsockopt(serverSocket_, SOL_SOCKET, SO_REUSEADDR,
126 &one, sizeof(one))) {
David Reiss9b209552008-04-08 06:26:05 +0000127 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000128 GlobalOutput.perror("TServerSocket::listen() setsockopt() SO_REUSEADDR ", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000129 close();
David Reiss9b209552008-04-08 06:26:05 +0000130 throw TTransportException(TTransportException::NOT_OPEN, "Could not set SO_REUSEADDR", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000131 }
132
Christopher Piro9cc63b52008-03-21 00:40:42 +0000133 // Set TCP buffer sizes
134 if (tcpSendBuffer_ > 0) {
135 if (-1 == setsockopt(serverSocket_, SOL_SOCKET, SO_SNDBUF,
136 &tcpSendBuffer_, sizeof(tcpSendBuffer_))) {
David Reiss9b209552008-04-08 06:26:05 +0000137 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000138 GlobalOutput.perror("TServerSocket::listen() setsockopt() SO_SNDBUF ", errno_copy);
Christopher Piro9cc63b52008-03-21 00:40:42 +0000139 close();
David Reiss9b209552008-04-08 06:26:05 +0000140 throw TTransportException(TTransportException::NOT_OPEN, "Could not set SO_SNDBUF", errno_copy);
Christopher Piro9cc63b52008-03-21 00:40:42 +0000141 }
142 }
143
144 if (tcpRecvBuffer_ > 0) {
145 if (-1 == setsockopt(serverSocket_, SOL_SOCKET, SO_RCVBUF,
146 &tcpRecvBuffer_, sizeof(tcpRecvBuffer_))) {
David Reiss9b209552008-04-08 06:26:05 +0000147 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000148 GlobalOutput.perror("TServerSocket::listen() setsockopt() SO_RCVBUF ", errno_copy);
Christopher Piro9cc63b52008-03-21 00:40:42 +0000149 close();
David Reiss9b209552008-04-08 06:26:05 +0000150 throw TTransportException(TTransportException::NOT_OPEN, "Could not set SO_RCVBUF", errno_copy);
Christopher Piro9cc63b52008-03-21 00:40:42 +0000151 }
152 }
153
Mark Slee29050782006-09-29 00:12:30 +0000154 // Defer accept
155 #ifdef TCP_DEFER_ACCEPT
156 if (-1 == setsockopt(serverSocket_, SOL_SOCKET, TCP_DEFER_ACCEPT,
157 &one, sizeof(one))) {
David Reiss9b209552008-04-08 06:26:05 +0000158 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000159 GlobalOutput.perror("TServerSocket::listen() setsockopt() TCP_DEFER_ACCEPT ", errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000160 close();
David Reiss9b209552008-04-08 06:26:05 +0000161 throw TTransportException(TTransportException::NOT_OPEN, "Could not set TCP_DEFER_ACCEPT", errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000162 }
163 #endif // #ifdef TCP_DEFER_ACCEPT
164
David Reiss13aea462008-06-10 22:56:04 +0000165 #ifdef IPV6_V6ONLY
166 int zero = 0;
David Reiss1a2f2182008-06-11 01:14:01 +0000167 if (-1 == setsockopt(serverSocket_, IPPROTO_IPV6, IPV6_V6ONLY,
David Reiss13aea462008-06-10 22:56:04 +0000168 &zero, sizeof(zero))) {
David Reiss01e55c12008-07-13 22:18:51 +0000169 GlobalOutput.perror("TServerSocket::listen() IPV6_V6ONLY ", errno);
David Reiss13aea462008-06-10 22:56:04 +0000170 }
171 #endif // #ifdef IPV6_V6ONLY
172
Mark Sleee8540632006-05-30 09:24:40 +0000173 // Turn linger off, don't want to block on calls to close
174 struct linger ling = {0, 0};
175 if (-1 == setsockopt(serverSocket_, SOL_SOCKET, SO_LINGER,
176 &ling, sizeof(ling))) {
David Reiss9b209552008-04-08 06:26:05 +0000177 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000178 GlobalOutput.perror("TServerSocket::listen() setsockopt() SO_LINGER ", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000179 close();
David Reiss9b209552008-04-08 06:26:05 +0000180 throw TTransportException(TTransportException::NOT_OPEN, "Could not set SO_LINGER", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000181 }
182
Mark Slee29050782006-09-29 00:12:30 +0000183 // TCP Nodelay, speed over bandwidth
184 if (-1 == setsockopt(serverSocket_, IPPROTO_TCP, TCP_NODELAY,
185 &one, sizeof(one))) {
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() TCP_NODELAY ", errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000188 close();
David Reiss9b209552008-04-08 06:26:05 +0000189 throw TTransportException(TTransportException::NOT_OPEN, "Could not set TCP_NODELAY", errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000190 }
191
Mark Sleea5a783f2007-03-02 19:41:08 +0000192 // Set NONBLOCK on the accept socket
193 int flags = fcntl(serverSocket_, F_GETFL, 0);
194 if (flags == -1) {
David Reiss9b209552008-04-08 06:26:05 +0000195 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000196 GlobalOutput.perror("TServerSocket::listen() fcntl() F_GETFL ", errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000197 throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed", errno_copy);
Mark Sleea5a783f2007-03-02 19:41:08 +0000198 }
Mark Slee561b5362007-03-09 19:26:29 +0000199
Mark Sleea5a783f2007-03-02 19:41:08 +0000200 if (-1 == fcntl(serverSocket_, F_SETFL, flags | O_NONBLOCK)) {
David Reiss9b209552008-04-08 06:26:05 +0000201 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000202 GlobalOutput.perror("TServerSocket::listen() fcntl() O_NONBLOCK ", errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000203 throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed", errno_copy);
Mark Sleea5a783f2007-03-02 19:41:08 +0000204 }
205
boz1ea81ce2007-05-14 23:04:33 +0000206 // prepare the port information
Mark Slee256bdc42007-11-27 08:42:19 +0000207 // we may want to try to bind more than once, since SO_REUSEADDR doesn't
boz1ea81ce2007-05-14 23:04:33 +0000208 // always seem to work. The client can configure the retry variables.
209 int retries = 0;
210 do {
Mark Slee6d56eb92007-07-06 22:28:15 +0000211 if (0 == bind(serverSocket_, res->ai_addr, res->ai_addrlen)) {
boz1ea81ce2007-05-14 23:04:33 +0000212 break;
213 }
214
215 // use short circuit evaluation here to only sleep if we need to
216 } while ((retries++ < retryLimit_) && (sleep(retryDelay_) == 0));
217
Mark Slee6d56eb92007-07-06 22:28:15 +0000218 // free addrinfo
219 freeaddrinfo(res0);
Mark Slee256bdc42007-11-27 08:42:19 +0000220
boz1ea81ce2007-05-14 23:04:33 +0000221 // throw an error if we failed to bind properly
222 if (retries > retryLimit_) {
Mark Sleee8540632006-05-30 09:24:40 +0000223 char errbuf[1024];
224 sprintf(errbuf, "TServerSocket::listen() BIND %d", port_);
boz6ded7752007-06-05 22:41:18 +0000225 GlobalOutput(errbuf);
Mark Sleee8540632006-05-30 09:24:40 +0000226 close();
Mark Sleef9831082007-02-20 20:59:21 +0000227 throw TTransportException(TTransportException::NOT_OPEN, "Could not bind");
Mark Sleee8540632006-05-30 09:24:40 +0000228 }
229
230 // Call listen
231 if (-1 == ::listen(serverSocket_, acceptBacklog_)) {
David Reiss9b209552008-04-08 06:26:05 +0000232 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000233 GlobalOutput.perror("TServerSocket::listen() listen() ", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000234 close();
David Reiss9b209552008-04-08 06:26:05 +0000235 throw TTransportException(TTransportException::NOT_OPEN, "Could not listen", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000236 }
237
238 // The socket is now listening!
Mark Sleee8540632006-05-30 09:24:40 +0000239}
240
Marc Slemko16698852006-08-04 03:16:10 +0000241shared_ptr<TTransport> TServerSocket::acceptImpl() {
Martin Kraemer10640d82007-02-03 01:59:12 +0000242 if (serverSocket_ < 0) {
Mark Sleef9831082007-02-20 20:59:21 +0000243 throw TTransportException(TTransportException::NOT_OPEN, "TServerSocket not listening");
Mark Sleee8540632006-05-30 09:24:40 +0000244 }
245
David Reiss1677ac92008-04-08 06:26:27 +0000246 struct pollfd fds[2];
Mark Sleea5a783f2007-03-02 19:41:08 +0000247
Aditya Agarwal7859a572007-05-31 01:33:07 +0000248 int maxEintrs = 5;
249 int numEintrs = 0;
250
Mark Sleea5a783f2007-03-02 19:41:08 +0000251 while (true) {
David Reissc88eb8c2008-06-11 01:18:54 +0000252 std::memset(fds, 0 , sizeof(fds));
David Reiss1677ac92008-04-08 06:26:27 +0000253 fds[0].fd = serverSocket_;
254 fds[0].events = POLLIN;
Mark Slee561b5362007-03-09 19:26:29 +0000255 if (intSock2_ >= 0) {
David Reiss1677ac92008-04-08 06:26:27 +0000256 fds[1].fd = intSock2_;
David Reiss2724c7a2008-04-18 17:48:03 +0000257 fds[1].events = POLLIN;
Mark Sleea5a783f2007-03-02 19:41:08 +0000258 }
David Reiss1677ac92008-04-08 06:26:27 +0000259 int ret = poll(fds, 2, -1);
Mark Sleea5a783f2007-03-02 19:41:08 +0000260
Mark Slee561b5362007-03-09 19:26:29 +0000261 if (ret < 0) {
Aditya Agarwal7859a572007-05-31 01:33:07 +0000262 // error cases
bozf83c9db2007-05-31 23:38:37 +0000263 if (errno == EINTR && (numEintrs++ < maxEintrs)) {
Mark Slee256bdc42007-11-27 08:42:19 +0000264 // EINTR needs to be handled manually and we can tolerate
Aditya Agarwal7859a572007-05-31 01:33:07 +0000265 // a certain number
266 continue;
267 }
David Reiss9b209552008-04-08 06:26:05 +0000268 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000269 GlobalOutput.perror("TServerSocket::acceptImpl() poll() ", errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000270 throw TTransportException(TTransportException::UNKNOWN, "Unknown", errno_copy);
Mark Slee561b5362007-03-09 19:26:29 +0000271 } else if (ret > 0) {
272 // Check for an interrupt signal
David Reiss1677ac92008-04-08 06:26:27 +0000273 if (intSock2_ >= 0 && (fds[1].revents & POLLIN)) {
Mark Slee561b5362007-03-09 19:26:29 +0000274 int8_t buf;
275 if (-1 == recv(intSock2_, &buf, sizeof(int8_t), 0)) {
David Reiss01e55c12008-07-13 22:18:51 +0000276 GlobalOutput.perror("TServerSocket::acceptImpl() recv() interrupt ", errno);
Mark Slee561b5362007-03-09 19:26:29 +0000277 }
278 throw TTransportException(TTransportException::INTERRUPTED);
279 }
David Reiss9b209552008-04-08 06:26:05 +0000280
Mark Slee561b5362007-03-09 19:26:29 +0000281 // Check for the actual server socket being ready
David Reiss1677ac92008-04-08 06:26:27 +0000282 if (fds[0].revents & POLLIN) {
Mark Slee561b5362007-03-09 19:26:29 +0000283 break;
284 }
285 } else {
David Reiss1677ac92008-04-08 06:26:27 +0000286 GlobalOutput("TServerSocket::acceptImpl() poll 0");
Mark Slee256bdc42007-11-27 08:42:19 +0000287 throw TTransportException(TTransportException::UNKNOWN);
Mark Sleea5a783f2007-03-02 19:41:08 +0000288 }
289 }
290
Mark Slee6d56eb92007-07-06 22:28:15 +0000291 struct sockaddr_storage clientAddress;
Mark Sleee8540632006-05-30 09:24:40 +0000292 int size = sizeof(clientAddress);
293 int clientSocket = ::accept(serverSocket_,
294 (struct sockaddr *) &clientAddress,
295 (socklen_t *) &size);
Mark Slee256bdc42007-11-27 08:42:19 +0000296
Martin Kraemeree341cb2007-02-05 21:40:38 +0000297 if (clientSocket < 0) {
David Reissbc3dddb2007-08-22 23:20:24 +0000298 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000299 GlobalOutput.perror("TServerSocket::acceptImpl() ::accept() ", errno_copy);
David Reissbc3dddb2007-08-22 23:20:24 +0000300 throw TTransportException(TTransportException::UNKNOWN, "accept()", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000301 }
Mark Sleea5a783f2007-03-02 19:41:08 +0000302
303 // Make sure client socket is blocking
304 int flags = fcntl(clientSocket, F_GETFL, 0);
305 if (flags == -1) {
David Reissbc3dddb2007-08-22 23:20:24 +0000306 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000307 GlobalOutput.perror("TServerSocket::acceptImpl() fcntl() F_GETFL ", errno_copy);
David Reissbc3dddb2007-08-22 23:20:24 +0000308 throw TTransportException(TTransportException::UNKNOWN, "fcntl(F_GETFL)", errno_copy);
Mark Sleea5a783f2007-03-02 19:41:08 +0000309 }
David Reiss9b209552008-04-08 06:26:05 +0000310
Mark Sleea5a783f2007-03-02 19:41:08 +0000311 if (-1 == fcntl(clientSocket, F_SETFL, flags & ~O_NONBLOCK)) {
David Reissbc3dddb2007-08-22 23:20:24 +0000312 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000313 GlobalOutput.perror("TServerSocket::acceptImpl() fcntl() F_SETFL ~O_NONBLOCK ", errno_copy);
David Reissbc3dddb2007-08-22 23:20:24 +0000314 throw TTransportException(TTransportException::UNKNOWN, "fcntl(F_SETFL)", errno_copy);
Mark Sleea5a783f2007-03-02 19:41:08 +0000315 }
Mark Slee256bdc42007-11-27 08:42:19 +0000316
Mark Slee29050782006-09-29 00:12:30 +0000317 shared_ptr<TSocket> client(new TSocket(clientSocket));
318 if (sendTimeout_ > 0) {
319 client->setSendTimeout(sendTimeout_);
320 }
321 if (recvTimeout_ > 0) {
322 client->setRecvTimeout(recvTimeout_);
Mark Sleea5a783f2007-03-02 19:41:08 +0000323 }
Mark Slee256bdc42007-11-27 08:42:19 +0000324
Mark Slee29050782006-09-29 00:12:30 +0000325 return client;
Mark Sleee8540632006-05-30 09:24:40 +0000326}
327
Mark Slee561b5362007-03-09 19:26:29 +0000328void TServerSocket::interrupt() {
329 if (intSock1_ >= 0) {
330 int8_t byte = 0;
331 if (-1 == send(intSock1_, &byte, sizeof(int8_t), 0)) {
David Reiss01e55c12008-07-13 22:18:51 +0000332 GlobalOutput.perror("TServerSocket::interrupt() send() ", errno);
Mark Slee561b5362007-03-09 19:26:29 +0000333 }
334 }
335}
336
Mark Sleee8540632006-05-30 09:24:40 +0000337void TServerSocket::close() {
Martin Kraemeree341cb2007-02-05 21:40:38 +0000338 if (serverSocket_ >= 0) {
Mark Sleee8540632006-05-30 09:24:40 +0000339 shutdown(serverSocket_, SHUT_RDWR);
340 ::close(serverSocket_);
341 }
Mark Slee561b5362007-03-09 19:26:29 +0000342 if (intSock1_ >= 0) {
343 ::close(intSock1_);
344 }
345 if (intSock2_ >= 0) {
346 ::close(intSock2_);
347 }
Martin Kraemeree341cb2007-02-05 21:40:38 +0000348 serverSocket_ = -1;
Mark Slee561b5362007-03-09 19:26:29 +0000349 intSock1_ = -1;
350 intSock2_ = -1;
Mark Sleee8540632006-05-30 09:24:40 +0000351}
Marc Slemko6f038a72006-08-03 18:58:09 +0000352
353}}} // facebook::thrift::transport