Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 1 | #include <sys/socket.h> |
| 2 | #include <netinet/in.h> |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 3 | #include <netinet/tcp.h> |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 4 | #include <errno.h> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 5 | |
Marc Slemko | d42a2c2 | 2006-08-10 03:30:18 +0000 | [diff] [blame] | 6 | #include "TSocket.h" |
| 7 | #include "TServerSocket.h" |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 8 | #include <boost/shared_ptr.hpp> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 9 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 10 | namespace facebook { namespace thrift { namespace transport { |
| 11 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 12 | using namespace boost; |
| 13 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 14 | TServerSocket::TServerSocket(int port) : |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 15 | port_(port), |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 16 | serverSocket_(-1), |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 17 | acceptBacklog_(1024), |
| 18 | sendTimeout_(0), |
| 19 | recvTimeout_(0) {} |
| 20 | |
| 21 | TServerSocket::TServerSocket(int port, int sendTimeout, int recvTimeout) : |
| 22 | port_(port), |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 23 | serverSocket_(-1), |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 24 | acceptBacklog_(1024), |
| 25 | sendTimeout_(sendTimeout), |
| 26 | recvTimeout_(recvTimeout) {} |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 27 | |
| 28 | TServerSocket::~TServerSocket() { |
| 29 | close(); |
| 30 | } |
| 31 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 32 | void TServerSocket::setSendTimeout(int sendTimeout) { |
| 33 | sendTimeout_ = sendTimeout; |
| 34 | } |
| 35 | |
| 36 | void TServerSocket::setRecvTimeout(int recvTimeout) { |
| 37 | recvTimeout_ = recvTimeout; |
| 38 | } |
| 39 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 40 | void TServerSocket::listen() { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 41 | serverSocket_ = socket(AF_INET, SOCK_STREAM, 0); |
| 42 | if (serverSocket_ == -1) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 43 | perror("TServerSocket::listen() socket"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 44 | close(); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 45 | throw TTransportException(TTX_NOT_OPEN, "Could not create server socket."); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | // Set reusaddress to prevent 2MSL delay on accept |
| 49 | int one = 1; |
| 50 | if (-1 == setsockopt(serverSocket_, SOL_SOCKET, SO_REUSEADDR, |
| 51 | &one, sizeof(one))) { |
| 52 | perror("TServerSocket::listen() SO_REUSEADDR"); |
| 53 | close(); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 54 | throw TTransportException(TTX_NOT_OPEN, "Could not set SO_REUSEADDR"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 57 | // Defer accept |
| 58 | #ifdef TCP_DEFER_ACCEPT |
| 59 | if (-1 == setsockopt(serverSocket_, SOL_SOCKET, TCP_DEFER_ACCEPT, |
| 60 | &one, sizeof(one))) { |
| 61 | perror("TServerSocket::listen() TCP_DEFER_ACCEPT"); |
| 62 | close(); |
| 63 | throw TTransportException(TTX_NOT_OPEN, "Could not set TCP_DEFER_ACCEPT"); |
| 64 | } |
| 65 | #endif // #ifdef TCP_DEFER_ACCEPT |
| 66 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 67 | // Turn linger off, don't want to block on calls to close |
| 68 | struct linger ling = {0, 0}; |
| 69 | if (-1 == setsockopt(serverSocket_, SOL_SOCKET, SO_LINGER, |
| 70 | &ling, sizeof(ling))) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 71 | close(); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 72 | perror("TServerSocket::listen() SO_LINGER"); |
| 73 | throw TTransportException(TTX_NOT_OPEN, "Could not set SO_LINGER"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 76 | // TCP Nodelay, speed over bandwidth |
| 77 | if (-1 == setsockopt(serverSocket_, IPPROTO_TCP, TCP_NODELAY, |
| 78 | &one, sizeof(one))) { |
| 79 | close(); |
| 80 | perror("setsockopt TCP_NODELAY"); |
| 81 | throw TTransportException(TTX_NOT_OPEN, "Could not set TCP_NODELAY"); |
| 82 | } |
| 83 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 84 | // Bind to a port |
| 85 | struct sockaddr_in addr; |
| 86 | memset(&addr, 0, sizeof(addr)); |
| 87 | addr.sin_family = AF_INET; |
| 88 | addr.sin_port = htons(port_); |
| 89 | addr.sin_addr.s_addr = INADDR_ANY; |
| 90 | if (-1 == bind(serverSocket_, (struct sockaddr *)&addr, sizeof(addr))) { |
| 91 | char errbuf[1024]; |
| 92 | sprintf(errbuf, "TServerSocket::listen() BIND %d", port_); |
| 93 | perror(errbuf); |
| 94 | close(); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 95 | throw TTransportException(TTX_NOT_OPEN, "Could not bind"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | // Call listen |
| 99 | if (-1 == ::listen(serverSocket_, acceptBacklog_)) { |
| 100 | perror("TServerSocket::listen() LISTEN"); |
| 101 | close(); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 102 | throw TTransportException(TTX_NOT_OPEN, "Could not listen"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | // The socket is now listening! |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 108 | shared_ptr<TTransport> TServerSocket::acceptImpl() { |
Martin Kraemer | 10640d8 | 2007-02-03 01:59:12 +0000 | [diff] [blame] | 109 | if (serverSocket_ < 0) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 110 | throw TTransportException(TTX_NOT_OPEN, "TServerSocket not listening"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | struct sockaddr_in clientAddress; |
| 114 | int size = sizeof(clientAddress); |
| 115 | int clientSocket = ::accept(serverSocket_, |
| 116 | (struct sockaddr *) &clientAddress, |
| 117 | (socklen_t *) &size); |
| 118 | |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 119 | if (clientSocket < 0) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 120 | perror("TServerSocket::accept()"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 121 | throw TTransportException(TTX_UNKNOWN, "ERROR:" + errno); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 122 | } |
Martin Kraemer | 10640d8 | 2007-02-03 01:59:12 +0000 | [diff] [blame] | 123 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 124 | shared_ptr<TSocket> client(new TSocket(clientSocket)); |
| 125 | if (sendTimeout_ > 0) { |
| 126 | client->setSendTimeout(sendTimeout_); |
| 127 | } |
| 128 | if (recvTimeout_ > 0) { |
| 129 | client->setRecvTimeout(recvTimeout_); |
| 130 | } |
| 131 | return client; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | void TServerSocket::close() { |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 135 | if (serverSocket_ >= 0) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 136 | shutdown(serverSocket_, SHUT_RDWR); |
| 137 | ::close(serverSocket_); |
| 138 | } |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 139 | serverSocket_ = -1; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 140 | } |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 141 | |
| 142 | }}} // facebook::thrift::transport |