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 | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 11 | #include <fcntl.h> |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 12 | #include <errno.h> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 13 | |
Marc Slemko | d42a2c2 | 2006-08-10 03:30:18 +0000 | [diff] [blame] | 14 | #include "TSocket.h" |
| 15 | #include "TServerSocket.h" |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 16 | #include <boost/shared_ptr.hpp> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 17 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 18 | namespace facebook { namespace thrift { namespace transport { |
| 19 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 20 | using namespace boost; |
| 21 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 22 | TServerSocket::TServerSocket(int port) : |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 23 | port_(port), |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 24 | serverSocket_(-1), |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 25 | acceptBacklog_(1024), |
| 26 | sendTimeout_(0), |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 27 | recvTimeout_(0), |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 28 | intSock1_(-1), |
| 29 | intSock2_(-1) {} |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 30 | |
| 31 | TServerSocket::TServerSocket(int port, int sendTimeout, int recvTimeout) : |
| 32 | port_(port), |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 33 | serverSocket_(-1), |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 34 | acceptBacklog_(1024), |
| 35 | sendTimeout_(sendTimeout), |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 36 | recvTimeout_(recvTimeout), |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 37 | intSock1_(-1), |
| 38 | intSock2_(-1) {} |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 39 | |
| 40 | TServerSocket::~TServerSocket() { |
| 41 | close(); |
| 42 | } |
| 43 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 44 | void TServerSocket::setSendTimeout(int sendTimeout) { |
| 45 | sendTimeout_ = sendTimeout; |
| 46 | } |
| 47 | |
| 48 | void TServerSocket::setRecvTimeout(int recvTimeout) { |
| 49 | recvTimeout_ = recvTimeout; |
| 50 | } |
| 51 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 52 | void TServerSocket::listen() { |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 53 | int sv[2]; |
| 54 | if (-1 == socketpair(AF_LOCAL, SOCK_STREAM, 0, sv)) { |
| 55 | perror("TServerSocket::init()"); |
| 56 | intSock1_ = -1; |
| 57 | intSock2_ = -1; |
| 58 | } else { |
| 59 | intSock1_ = sv[0]; |
| 60 | intSock2_ = sv[1]; |
| 61 | } |
| 62 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 63 | serverSocket_ = socket(AF_INET, SOCK_STREAM, 0); |
| 64 | if (serverSocket_ == -1) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 65 | perror("TServerSocket::listen() socket"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 66 | close(); |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 67 | throw TTransportException(TTransportException::NOT_OPEN, "Could not create server socket."); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | // Set reusaddress to prevent 2MSL delay on accept |
| 71 | int one = 1; |
| 72 | if (-1 == setsockopt(serverSocket_, SOL_SOCKET, SO_REUSEADDR, |
| 73 | &one, sizeof(one))) { |
| 74 | perror("TServerSocket::listen() SO_REUSEADDR"); |
| 75 | close(); |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 76 | throw TTransportException(TTransportException::NOT_OPEN, "Could not set SO_REUSEADDR"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 79 | // Defer accept |
| 80 | #ifdef TCP_DEFER_ACCEPT |
| 81 | if (-1 == setsockopt(serverSocket_, SOL_SOCKET, TCP_DEFER_ACCEPT, |
| 82 | &one, sizeof(one))) { |
| 83 | perror("TServerSocket::listen() TCP_DEFER_ACCEPT"); |
| 84 | close(); |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 85 | throw TTransportException(TTransportException::NOT_OPEN, "Could not set TCP_DEFER_ACCEPT"); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 86 | } |
| 87 | #endif // #ifdef TCP_DEFER_ACCEPT |
| 88 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 89 | // Turn linger off, don't want to block on calls to close |
| 90 | struct linger ling = {0, 0}; |
| 91 | if (-1 == setsockopt(serverSocket_, SOL_SOCKET, SO_LINGER, |
| 92 | &ling, sizeof(ling))) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 93 | close(); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 94 | perror("TServerSocket::listen() SO_LINGER"); |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 95 | throw TTransportException(TTransportException::NOT_OPEN, "Could not set SO_LINGER"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 98 | // TCP Nodelay, speed over bandwidth |
| 99 | if (-1 == setsockopt(serverSocket_, IPPROTO_TCP, TCP_NODELAY, |
| 100 | &one, sizeof(one))) { |
| 101 | close(); |
| 102 | perror("setsockopt TCP_NODELAY"); |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 103 | throw TTransportException(TTransportException::NOT_OPEN, "Could not set TCP_NODELAY"); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 106 | // Set NONBLOCK on the accept socket |
| 107 | int flags = fcntl(serverSocket_, F_GETFL, 0); |
| 108 | if (flags == -1) { |
| 109 | throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed"); |
| 110 | } |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 111 | |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 112 | if (-1 == fcntl(serverSocket_, F_SETFL, flags | O_NONBLOCK)) { |
| 113 | throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed"); |
| 114 | } |
| 115 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 116 | // Bind to a port |
| 117 | struct sockaddr_in addr; |
| 118 | memset(&addr, 0, sizeof(addr)); |
| 119 | addr.sin_family = AF_INET; |
| 120 | addr.sin_port = htons(port_); |
| 121 | addr.sin_addr.s_addr = INADDR_ANY; |
| 122 | if (-1 == bind(serverSocket_, (struct sockaddr *)&addr, sizeof(addr))) { |
| 123 | char errbuf[1024]; |
| 124 | sprintf(errbuf, "TServerSocket::listen() BIND %d", port_); |
| 125 | perror(errbuf); |
| 126 | close(); |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 127 | throw TTransportException(TTransportException::NOT_OPEN, "Could not bind"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | // Call listen |
| 131 | if (-1 == ::listen(serverSocket_, acceptBacklog_)) { |
| 132 | perror("TServerSocket::listen() LISTEN"); |
| 133 | close(); |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 134 | throw TTransportException(TTransportException::NOT_OPEN, "Could not listen"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | // The socket is now listening! |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 140 | shared_ptr<TTransport> TServerSocket::acceptImpl() { |
Martin Kraemer | 10640d8 | 2007-02-03 01:59:12 +0000 | [diff] [blame] | 141 | if (serverSocket_ < 0) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 142 | throw TTransportException(TTransportException::NOT_OPEN, "TServerSocket not listening"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 145 | fd_set fds; |
| 146 | |
| 147 | while (true) { |
| 148 | FD_ZERO(&fds); |
| 149 | FD_SET(serverSocket_, &fds); |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 150 | if (intSock2_ >= 0) { |
| 151 | FD_SET(intSock2_, &fds); |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 152 | } |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 153 | int ret = select(serverSocket_+1, &fds, NULL, NULL, NULL); |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 154 | |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 155 | if (ret < 0) { |
| 156 | perror("TServerSocket::acceptImpl() select -1"); |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 157 | throw TTransportException(TTransportException::UNKNOWN); |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 158 | } else if (ret > 0) { |
| 159 | // Check for an interrupt signal |
| 160 | if (intSock2_ >= 0 && FD_ISSET(intSock2_, &fds)) { |
| 161 | int8_t buf; |
| 162 | if (-1 == recv(intSock2_, &buf, sizeof(int8_t), 0)) { |
| 163 | perror("TServerSocket::acceptImpl() interrupt receive"); |
| 164 | } |
| 165 | throw TTransportException(TTransportException::INTERRUPTED); |
| 166 | } |
| 167 | // Check for the actual server socket being ready |
| 168 | if (FD_ISSET(serverSocket_, &fds)) { |
| 169 | break; |
| 170 | } |
| 171 | } else { |
| 172 | perror("TServerSocket::acceptImpl() select 0"); |
| 173 | throw TTransportException(TTransportException::UNKNOWN); |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 177 | struct sockaddr_in clientAddress; |
| 178 | int size = sizeof(clientAddress); |
| 179 | int clientSocket = ::accept(serverSocket_, |
| 180 | (struct sockaddr *) &clientAddress, |
| 181 | (socklen_t *) &size); |
| 182 | |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 183 | if (clientSocket < 0) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 184 | perror("TServerSocket::accept()"); |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 185 | throw TTransportException(TTransportException::UNKNOWN, "ERROR:" + errno); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 186 | } |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 187 | |
| 188 | // Make sure client socket is blocking |
| 189 | int flags = fcntl(clientSocket, F_GETFL, 0); |
| 190 | if (flags == -1) { |
| 191 | perror("TServerSocket::select() fcntl GETFL"); |
| 192 | throw TTransportException(TTransportException::UNKNOWN, "ERROR:" + errno); |
| 193 | } |
| 194 | if (-1 == fcntl(clientSocket, F_SETFL, flags & ~O_NONBLOCK)) { |
| 195 | perror("TServerSocket::select() fcntl SETFL"); |
| 196 | throw TTransportException(TTransportException::UNKNOWN, "ERROR:" + errno); |
| 197 | } |
Martin Kraemer | 10640d8 | 2007-02-03 01:59:12 +0000 | [diff] [blame] | 198 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 199 | shared_ptr<TSocket> client(new TSocket(clientSocket)); |
| 200 | if (sendTimeout_ > 0) { |
| 201 | client->setSendTimeout(sendTimeout_); |
| 202 | } |
| 203 | if (recvTimeout_ > 0) { |
| 204 | client->setRecvTimeout(recvTimeout_); |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 207 | return client; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 210 | void TServerSocket::interrupt() { |
| 211 | if (intSock1_ >= 0) { |
| 212 | int8_t byte = 0; |
| 213 | if (-1 == send(intSock1_, &byte, sizeof(int8_t), 0)) { |
| 214 | perror("TServerSocket::interrupt()"); |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 219 | void TServerSocket::close() { |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 220 | if (serverSocket_ >= 0) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 221 | shutdown(serverSocket_, SHUT_RDWR); |
| 222 | ::close(serverSocket_); |
| 223 | } |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 224 | if (intSock1_ >= 0) { |
| 225 | ::close(intSock1_); |
| 226 | } |
| 227 | if (intSock2_ >= 0) { |
| 228 | ::close(intSock2_); |
| 229 | } |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 230 | serverSocket_ = -1; |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 231 | intSock1_ = -1; |
| 232 | intSock2_ = -1; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 233 | } |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 234 | |
| 235 | }}} // facebook::thrift::transport |