1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 20 #ifndef _THRIFT_TRANSPORT_TNONBLOCKINGSERVERSOCKET_H_ 21 #define _THRIFT_TRANSPORT_TNONBLOCKINGSERVERSOCKET_H_ 1 22 23 #include <thrift/transport/TNonblockingServerTransport.h> 24 #include <thrift/transport/PlatformSocket.h> 25 #include <thrift/stdcxx.h> 26 27 namespace apache { 28 namespace thrift { 29 namespace transport { 30 31 class TSocket; 32 33 /** 34 * Nonblocking Server socket implementation of TNonblockingServerTransport. Wrapper around a unix 35 * socket listen and accept calls. 36 * 37 */ 38 class TNonblockingServerSocket : public TNonblockingServerTransport { 39 public: 40 typedef apache::thrift::stdcxx::function<void(THRIFT_SOCKET fd)> socket_func_t; 41 42 const static int DEFAULT_BACKLOG = 1024; 43 44 /** 45 * Constructor. 46 * 47 * @param port Port number to bind to 48 */ 49 TNonblockingServerSocket(int port); 50 51 /** 52 * Constructor. 53 * 54 * @param port Port number to bind to 55 * @param sendTimeout Socket send timeout 56 * @param recvTimeout Socket receive timeout 57 */ 58 TNonblockingServerSocket(int port, int sendTimeout, int recvTimeout); 59 60 /** 61 * Constructor. 62 * 63 * @param address Address to bind to 64 * @param port Port number to bind to 65 */ 66 TNonblockingServerSocket(const std::string& address, int port); 67 68 /** 69 * Constructor used for unix sockets. 70 * 71 * @param path Pathname for unix socket. 72 */ 73 TNonblockingServerSocket(const std::string& path); 74 75 virtual ~TNonblockingServerSocket(); 76 77 void setSendTimeout(int sendTimeout); 78 void setRecvTimeout(int recvTimeout); 79 80 void setAcceptBacklog(int accBacklog); 81 82 void setRetryLimit(int retryLimit); 83 void setRetryDelay(int retryDelay); 84 setKeepAlive(bool keepAlive)85 void setKeepAlive(bool keepAlive) { keepAlive_ = keepAlive; } 86 87 void setTcpSendBuffer(int tcpSendBuffer); 88 void setTcpRecvBuffer(int tcpRecvBuffer); 89 90 // listenCallback gets called just before listen, and after all Thrift 91 // setsockopt calls have been made. If you have custom setsockopt 92 // things that need to happen on the listening socket, this is the place to do it. setListenCallback(const socket_func_t & listenCallback)93 void setListenCallback(const socket_func_t& listenCallback) { listenCallback_ = listenCallback; } 94 95 // acceptCallback gets called after each accept call, on the newly created socket. 96 // It is called after all Thrift setsockopt calls have been made. If you have 97 // custom setsockopt things that need to happen on the accepted 98 // socket, this is the place to do it. setAcceptCallback(const socket_func_t & acceptCallback)99 void setAcceptCallback(const socket_func_t& acceptCallback) { acceptCallback_ = acceptCallback; } 100 getSocketFD()101 THRIFT_SOCKET getSocketFD() { return serverSocket_; } 102 103 int getPort(); 104 105 int getListenPort(); 106 107 void listen(); 108 void close(); 109 110 protected: 111 apache::thrift::stdcxx::shared_ptr<TSocket> acceptImpl(); 112 virtual apache::thrift::stdcxx::shared_ptr<TSocket> createSocket(THRIFT_SOCKET client); 113 114 private: 115 int port_; 116 int listenPort_; 117 std::string address_; 118 std::string path_; 119 THRIFT_SOCKET serverSocket_; 120 int acceptBacklog_; 121 int sendTimeout_; 122 int recvTimeout_; 123 int retryLimit_; 124 int retryDelay_; 125 int tcpSendBuffer_; 126 int tcpRecvBuffer_; 127 bool keepAlive_; 128 bool listening_; 129 130 socket_func_t listenCallback_; 131 socket_func_t acceptCallback_; 132 }; 133 } 134 } 135 } // apache::thrift::transport 136 137 #endif // #ifndef _THRIFT_TRANSPORT_TNONBLOCKINGSERVERSOCKET_H_ 138