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