Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 1 | #include <sys/socket.h> |
| 2 | #include <netinet/in.h> |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 3 | #include <errno.h> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 4 | |
| 5 | #include "transport/TSocket.h" |
| 6 | #include "transport/TServerSocket.h" |
| 7 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame^] | 8 | namespace facebook { namespace thrift { namespace transport { |
| 9 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 10 | TServerSocket::TServerSocket(int port) : |
| 11 | port_(port), serverSocket_(0), acceptBacklog_(1024) {} |
| 12 | |
| 13 | TServerSocket::~TServerSocket() { |
| 14 | close(); |
| 15 | } |
| 16 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 17 | void TServerSocket::listen() { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 18 | serverSocket_ = socket(AF_INET, SOCK_STREAM, 0); |
| 19 | if (serverSocket_ == -1) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 20 | perror("TServerSocket::listen() socket"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 21 | close(); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 22 | throw TTransportException(TTX_NOT_OPEN, "Could not create server socket."); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | // Set reusaddress to prevent 2MSL delay on accept |
| 26 | int one = 1; |
| 27 | if (-1 == setsockopt(serverSocket_, SOL_SOCKET, SO_REUSEADDR, |
| 28 | &one, sizeof(one))) { |
| 29 | perror("TServerSocket::listen() SO_REUSEADDR"); |
| 30 | close(); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 31 | throw TTransportException(TTX_NOT_OPEN, "Could not set SO_REUSEADDR"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | // Turn linger off, don't want to block on calls to close |
| 35 | struct linger ling = {0, 0}; |
| 36 | if (-1 == setsockopt(serverSocket_, SOL_SOCKET, SO_LINGER, |
| 37 | &ling, sizeof(ling))) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 38 | close(); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 39 | perror("TServerSocket::listen() SO_LINGER"); |
| 40 | throw TTransportException(TTX_NOT_OPEN, "Could not set SO_LINGER"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | // Bind to a port |
| 44 | struct sockaddr_in addr; |
| 45 | memset(&addr, 0, sizeof(addr)); |
| 46 | addr.sin_family = AF_INET; |
| 47 | addr.sin_port = htons(port_); |
| 48 | addr.sin_addr.s_addr = INADDR_ANY; |
| 49 | if (-1 == bind(serverSocket_, (struct sockaddr *)&addr, sizeof(addr))) { |
| 50 | char errbuf[1024]; |
| 51 | sprintf(errbuf, "TServerSocket::listen() BIND %d", port_); |
| 52 | perror(errbuf); |
| 53 | close(); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 54 | throw TTransportException(TTX_NOT_OPEN, "Could not bind"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | // Call listen |
| 58 | if (-1 == ::listen(serverSocket_, acceptBacklog_)) { |
| 59 | perror("TServerSocket::listen() LISTEN"); |
| 60 | close(); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 61 | throw TTransportException(TTX_NOT_OPEN, "Could not listen"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | // The socket is now listening! |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 67 | TTransport* TServerSocket::acceptImpl() { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 68 | if (serverSocket_ <= 0) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 69 | throw TTransportException(TTX_NOT_OPEN, "TServerSocket not listening"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | struct sockaddr_in clientAddress; |
| 73 | int size = sizeof(clientAddress); |
| 74 | int clientSocket = ::accept(serverSocket_, |
| 75 | (struct sockaddr *) &clientAddress, |
| 76 | (socklen_t *) &size); |
| 77 | |
| 78 | if (clientSocket <= 0) { |
| 79 | perror("TServerSocket::accept()"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 80 | throw TTransportException(TTX_UNKNOWN, "ERROR:" + errno); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | return new TSocket(clientSocket); |
| 84 | } |
| 85 | |
| 86 | void TServerSocket::close() { |
| 87 | if (serverSocket_ > 0) { |
| 88 | shutdown(serverSocket_, SHUT_RDWR); |
| 89 | ::close(serverSocket_); |
| 90 | } |
| 91 | serverSocket_ = 0; |
| 92 | } |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame^] | 93 | |
| 94 | }}} // facebook::thrift::transport |