Mark Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 1 | // 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 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 7 | #include <sys/socket.h> |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 8 | #include <sys/select.h> |
Mark Slee | dd56497 | 2007-08-21 02:39:57 +0000 | [diff] [blame] | 9 | #include <sys/types.h> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 10 | #include <netinet/in.h> |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 11 | #include <netinet/tcp.h> |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 12 | #include <netdb.h> |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 13 | #include <fcntl.h> |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 14 | #include <errno.h> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 15 | |
Marc Slemko | d42a2c2 | 2006-08-10 03:30:18 +0000 | [diff] [blame] | 16 | #include "TSocket.h" |
| 17 | #include "TServerSocket.h" |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 18 | #include <boost/shared_ptr.hpp> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 19 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 20 | namespace facebook { namespace thrift { namespace transport { |
| 21 | |
Martin Kraemer | e6c4fa6 | 2007-07-09 19:08:25 +0000 | [diff] [blame] | 22 | using namespace std; |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 23 | using namespace boost; |
| 24 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 25 | TServerSocket::TServerSocket(int port) : |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 26 | port_(port), |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 27 | serverSocket_(-1), |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 28 | acceptBacklog_(1024), |
| 29 | sendTimeout_(0), |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 30 | recvTimeout_(0), |
boz | 1ea81ce | 2007-05-14 23:04:33 +0000 | [diff] [blame] | 31 | retryLimit_(0), |
| 32 | retryDelay_(0), |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 33 | intSock1_(-1), |
| 34 | intSock2_(-1) {} |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 35 | |
| 36 | TServerSocket::TServerSocket(int port, int sendTimeout, int recvTimeout) : |
| 37 | port_(port), |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 38 | serverSocket_(-1), |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 39 | acceptBacklog_(1024), |
| 40 | sendTimeout_(sendTimeout), |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 41 | recvTimeout_(recvTimeout), |
boz | 1ea81ce | 2007-05-14 23:04:33 +0000 | [diff] [blame] | 42 | retryLimit_(0), |
| 43 | retryDelay_(0), |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 44 | intSock1_(-1), |
| 45 | intSock2_(-1) {} |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 46 | |
| 47 | TServerSocket::~TServerSocket() { |
| 48 | close(); |
| 49 | } |
| 50 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 51 | void TServerSocket::setSendTimeout(int sendTimeout) { |
| 52 | sendTimeout_ = sendTimeout; |
| 53 | } |
| 54 | |
| 55 | void TServerSocket::setRecvTimeout(int recvTimeout) { |
| 56 | recvTimeout_ = recvTimeout; |
| 57 | } |
| 58 | |
boz | 1ea81ce | 2007-05-14 23:04:33 +0000 | [diff] [blame] | 59 | void TServerSocket::setRetryLimit(int retryLimit) { |
| 60 | retryLimit_ = retryLimit; |
| 61 | } |
| 62 | |
| 63 | void TServerSocket::setRetryDelay(int retryDelay) { |
| 64 | retryDelay_ = retryDelay; |
| 65 | } |
| 66 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 67 | void TServerSocket::listen() { |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 68 | int sv[2]; |
| 69 | if (-1 == socketpair(AF_LOCAL, SOCK_STREAM, 0, sv)) { |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 70 | GlobalOutput("TServerSocket::init()"); |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 71 | intSock1_ = -1; |
| 72 | intSock2_ = -1; |
| 73 | } else { |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 74 | intSock1_ = sv[1]; |
| 75 | intSock2_ = sv[0]; |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 78 | struct addrinfo hints, *res, *res0; |
| 79 | int error; |
| 80 | char port[sizeof("65536") + 1]; |
| 81 | memset(&hints, 0, sizeof(hints)); |
| 82 | hints.ai_family = PF_UNSPEC; |
| 83 | hints.ai_socktype = SOCK_STREAM; |
| 84 | hints.ai_flags = AI_PASSIVE; |
| 85 | sprintf(port, "%d", port_); |
| 86 | |
| 87 | // Wildcard address |
| 88 | error = getaddrinfo(NULL, port, &hints, &res0); |
| 89 | if (error) { |
| 90 | fprintf(stderr, "getaddrinfo %d: %s\n", error, gai_strerror(error)); |
| 91 | close(); |
| 92 | throw TTransportException(TTransportException::NOT_OPEN, "Could not resolve host for server socket."); |
| 93 | } |
| 94 | |
| 95 | // Pick the ipv6 address first since ipv4 addresses can be mapped |
| 96 | // into ipv6 space. |
| 97 | for (res = res0; res; res = res->ai_next) { |
| 98 | if (res->ai_family == AF_INET6 || res->ai_next == NULL) |
| 99 | break; |
| 100 | } |
| 101 | |
| 102 | serverSocket_ = socket(res->ai_family, res->ai_socktype, res->ai_protocol); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 103 | if (serverSocket_ == -1) { |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 104 | GlobalOutput("TServerSocket::listen() socket"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 105 | close(); |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 106 | throw TTransportException(TTransportException::NOT_OPEN, "Could not create server socket."); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | // Set reusaddress to prevent 2MSL delay on accept |
| 110 | int one = 1; |
| 111 | if (-1 == setsockopt(serverSocket_, SOL_SOCKET, SO_REUSEADDR, |
| 112 | &one, sizeof(one))) { |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 113 | GlobalOutput("TServerSocket::listen() SO_REUSEADDR"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 114 | close(); |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 115 | throw TTransportException(TTransportException::NOT_OPEN, "Could not set SO_REUSEADDR"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 118 | // Defer accept |
| 119 | #ifdef TCP_DEFER_ACCEPT |
| 120 | if (-1 == setsockopt(serverSocket_, SOL_SOCKET, TCP_DEFER_ACCEPT, |
| 121 | &one, sizeof(one))) { |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 122 | GlobalOutput("TServerSocket::listen() TCP_DEFER_ACCEPT"); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 123 | close(); |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 124 | throw TTransportException(TTransportException::NOT_OPEN, "Could not set TCP_DEFER_ACCEPT"); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 125 | } |
| 126 | #endif // #ifdef TCP_DEFER_ACCEPT |
| 127 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 128 | // Turn linger off, don't want to block on calls to close |
| 129 | struct linger ling = {0, 0}; |
| 130 | if (-1 == setsockopt(serverSocket_, SOL_SOCKET, SO_LINGER, |
| 131 | &ling, sizeof(ling))) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 132 | close(); |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 133 | GlobalOutput("TServerSocket::listen() SO_LINGER"); |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 134 | throw TTransportException(TTransportException::NOT_OPEN, "Could not set SO_LINGER"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 137 | // TCP Nodelay, speed over bandwidth |
| 138 | if (-1 == setsockopt(serverSocket_, IPPROTO_TCP, TCP_NODELAY, |
| 139 | &one, sizeof(one))) { |
| 140 | close(); |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 141 | GlobalOutput("setsockopt TCP_NODELAY"); |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 142 | throw TTransportException(TTransportException::NOT_OPEN, "Could not set TCP_NODELAY"); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 145 | // Set NONBLOCK on the accept socket |
| 146 | int flags = fcntl(serverSocket_, F_GETFL, 0); |
| 147 | if (flags == -1) { |
| 148 | throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed"); |
| 149 | } |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 150 | |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 151 | if (-1 == fcntl(serverSocket_, F_SETFL, flags | O_NONBLOCK)) { |
| 152 | throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed"); |
| 153 | } |
| 154 | |
boz | 1ea81ce | 2007-05-14 23:04:33 +0000 | [diff] [blame] | 155 | // prepare the port information |
boz | 1ea81ce | 2007-05-14 23:04:33 +0000 | [diff] [blame] | 156 | // we may want to try to bind more than once, since SO_REUSEADDR doesn't |
| 157 | // always seem to work. The client can configure the retry variables. |
| 158 | int retries = 0; |
| 159 | do { |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 160 | if (0 == bind(serverSocket_, res->ai_addr, res->ai_addrlen)) { |
boz | 1ea81ce | 2007-05-14 23:04:33 +0000 | [diff] [blame] | 161 | break; |
| 162 | } |
| 163 | |
| 164 | // use short circuit evaluation here to only sleep if we need to |
| 165 | } while ((retries++ < retryLimit_) && (sleep(retryDelay_) == 0)); |
| 166 | |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 167 | // free addrinfo |
| 168 | freeaddrinfo(res0); |
| 169 | |
boz | 1ea81ce | 2007-05-14 23:04:33 +0000 | [diff] [blame] | 170 | // throw an error if we failed to bind properly |
| 171 | if (retries > retryLimit_) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 172 | char errbuf[1024]; |
| 173 | sprintf(errbuf, "TServerSocket::listen() BIND %d", port_); |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 174 | GlobalOutput(errbuf); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 175 | close(); |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 176 | throw TTransportException(TTransportException::NOT_OPEN, "Could not bind"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | // Call listen |
| 180 | if (-1 == ::listen(serverSocket_, acceptBacklog_)) { |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 181 | GlobalOutput("TServerSocket::listen() LISTEN"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 182 | close(); |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 183 | throw TTransportException(TTransportException::NOT_OPEN, "Could not listen"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | // The socket is now listening! |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 189 | shared_ptr<TTransport> TServerSocket::acceptImpl() { |
Martin Kraemer | 10640d8 | 2007-02-03 01:59:12 +0000 | [diff] [blame] | 190 | if (serverSocket_ < 0) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 191 | throw TTransportException(TTransportException::NOT_OPEN, "TServerSocket not listening"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 194 | fd_set fds; |
| 195 | |
Aditya Agarwal | 7859a57 | 2007-05-31 01:33:07 +0000 | [diff] [blame] | 196 | int maxEintrs = 5; |
| 197 | int numEintrs = 0; |
| 198 | |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 199 | while (true) { |
| 200 | FD_ZERO(&fds); |
| 201 | FD_SET(serverSocket_, &fds); |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 202 | if (intSock2_ >= 0) { |
| 203 | FD_SET(intSock2_, &fds); |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 204 | } |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 205 | int ret = select(serverSocket_+1, &fds, NULL, NULL, NULL); |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 206 | |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 207 | if (ret < 0) { |
Aditya Agarwal | 7859a57 | 2007-05-31 01:33:07 +0000 | [diff] [blame] | 208 | // error cases |
boz | f83c9db | 2007-05-31 23:38:37 +0000 | [diff] [blame] | 209 | if (errno == EINTR && (numEintrs++ < maxEintrs)) { |
Aditya Agarwal | 7859a57 | 2007-05-31 01:33:07 +0000 | [diff] [blame] | 210 | // EINTR needs to be handled manually and we can tolerate |
| 211 | // a certain number |
| 212 | continue; |
| 213 | } |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 214 | GlobalOutput("TServerSocket::acceptImpl() select -1"); |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 215 | throw TTransportException(TTransportException::UNKNOWN); |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 216 | } else if (ret > 0) { |
| 217 | // Check for an interrupt signal |
| 218 | if (intSock2_ >= 0 && FD_ISSET(intSock2_, &fds)) { |
| 219 | int8_t buf; |
| 220 | if (-1 == recv(intSock2_, &buf, sizeof(int8_t), 0)) { |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 221 | GlobalOutput("TServerSocket::acceptImpl() interrupt receive"); |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 222 | } |
| 223 | throw TTransportException(TTransportException::INTERRUPTED); |
| 224 | } |
| 225 | // Check for the actual server socket being ready |
| 226 | if (FD_ISSET(serverSocket_, &fds)) { |
| 227 | break; |
| 228 | } |
| 229 | } else { |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 230 | GlobalOutput("TServerSocket::acceptImpl() select 0"); |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 231 | throw TTransportException(TTransportException::UNKNOWN); |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 235 | struct sockaddr_storage clientAddress; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 236 | int size = sizeof(clientAddress); |
| 237 | int clientSocket = ::accept(serverSocket_, |
| 238 | (struct sockaddr *) &clientAddress, |
| 239 | (socklen_t *) &size); |
| 240 | |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 241 | if (clientSocket < 0) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 242 | int errno_copy = errno; |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 243 | GlobalOutput("TServerSocket::accept()"); |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 244 | throw TTransportException(TTransportException::UNKNOWN, "accept()", errno_copy); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 245 | } |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 246 | |
| 247 | // Make sure client socket is blocking |
| 248 | int flags = fcntl(clientSocket, F_GETFL, 0); |
| 249 | if (flags == -1) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 250 | int errno_copy = errno; |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 251 | GlobalOutput("TServerSocket::select() fcntl GETFL"); |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 252 | throw TTransportException(TTransportException::UNKNOWN, "fcntl(F_GETFL)", errno_copy); |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 253 | } |
| 254 | if (-1 == fcntl(clientSocket, F_SETFL, flags & ~O_NONBLOCK)) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 255 | int errno_copy = errno; |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 256 | GlobalOutput("TServerSocket::select() fcntl SETFL"); |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 257 | throw TTransportException(TTransportException::UNKNOWN, "fcntl(F_SETFL)", errno_copy); |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 258 | } |
Martin Kraemer | 10640d8 | 2007-02-03 01:59:12 +0000 | [diff] [blame] | 259 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 260 | shared_ptr<TSocket> client(new TSocket(clientSocket)); |
| 261 | if (sendTimeout_ > 0) { |
| 262 | client->setSendTimeout(sendTimeout_); |
| 263 | } |
| 264 | if (recvTimeout_ > 0) { |
| 265 | client->setRecvTimeout(recvTimeout_); |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 266 | } |
Aditya Agarwal | 7859a57 | 2007-05-31 01:33:07 +0000 | [diff] [blame] | 267 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 268 | return client; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 271 | void TServerSocket::interrupt() { |
| 272 | if (intSock1_ >= 0) { |
| 273 | int8_t byte = 0; |
| 274 | if (-1 == send(intSock1_, &byte, sizeof(int8_t), 0)) { |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 275 | GlobalOutput("TServerSocket::interrupt()"); |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 280 | void TServerSocket::close() { |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 281 | if (serverSocket_ >= 0) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 282 | shutdown(serverSocket_, SHUT_RDWR); |
| 283 | ::close(serverSocket_); |
| 284 | } |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 285 | if (intSock1_ >= 0) { |
| 286 | ::close(intSock1_); |
| 287 | } |
| 288 | if (intSock2_ >= 0) { |
| 289 | ::close(intSock2_); |
| 290 | } |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 291 | serverSocket_ = -1; |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 292 | intSock1_ = -1; |
| 293 | intSock2_ = -1; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 294 | } |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 295 | |
| 296 | }}} // facebook::thrift::transport |