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