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