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