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 
26 namespace apache {
27 namespace thrift {
28 namespace transport {
29 
30 class TSocket;
31 
32 /**
33  * Nonblocking Server socket implementation of TNonblockingServerTransport. Wrapper around a unix
34  * socket listen and accept calls.
35  *
36  */
37 class TNonblockingServerSocket : public TNonblockingServerTransport {
38 public:
39   typedef std::function<void(THRIFT_SOCKET fd)> socket_func_t;
40 
41   const static int DEFAULT_BACKLOG = 1024;
42 
43   /**
44    * Constructor.
45    *
46    * @param port    Port number to bind to
47    */
48   TNonblockingServerSocket(int port);
49 
50   /**
51    * Constructor.
52    *
53    * @param port        Port number to bind to
54    * @param sendTimeout Socket send timeout
55    * @param recvTimeout Socket receive timeout
56    */
57   TNonblockingServerSocket(int port, int sendTimeout, int recvTimeout);
58 
59   /**
60    * Constructor.
61    *
62    * @param address Address to bind to
63    * @param port    Port number to bind to
64    */
65   TNonblockingServerSocket(const std::string& address, int port);
66 
67   /**
68    * Constructor used for unix sockets.
69    *
70    * @param path Pathname for unix socket.
71    */
72   TNonblockingServerSocket(const std::string& path);
73 
74   ~TNonblockingServerSocket() override;
75 
76   void setSendTimeout(int sendTimeout);
77   void setRecvTimeout(int recvTimeout);
78 
79   void setAcceptBacklog(int accBacklog);
80 
81   void setRetryLimit(int retryLimit);
82   void setRetryDelay(int retryDelay);
83 
setKeepAlive(bool keepAlive)84   void setKeepAlive(bool keepAlive) { keepAlive_ = keepAlive; }
85 
86   void setTcpSendBuffer(int tcpSendBuffer);
87   void setTcpRecvBuffer(int tcpRecvBuffer);
88 
89   // listenCallback gets called just before listen, and after all Thrift
90   // setsockopt calls have been made.  If you have custom setsockopt
91   // things that need to happen on the listening socket, this is the place to do it.
setListenCallback(const socket_func_t & listenCallback)92   void setListenCallback(const socket_func_t& listenCallback) { listenCallback_ = listenCallback; }
93 
94   // acceptCallback gets called after each accept call, on the newly created socket.
95   // It is called after all Thrift setsockopt calls have been made.  If you have
96   // custom setsockopt things that need to happen on the accepted
97   // socket, this is the place to do it.
setAcceptCallback(const socket_func_t & acceptCallback)98   void setAcceptCallback(const socket_func_t& acceptCallback) { acceptCallback_ = acceptCallback; }
99 
getSocketFD()100   THRIFT_SOCKET getSocketFD() override { return serverSocket_; }
101 
102   int getPort() override;
103 
104   int getListenPort() override;
105 
106   void listen() override;
107   void close() override;
108 
109 protected:
110   std::shared_ptr<TSocket> acceptImpl() override;
111   virtual std::shared_ptr<TSocket> createSocket(THRIFT_SOCKET client);
112 
113 private:
114   int port_;
115   int listenPort_;
116   std::string address_;
117   std::string path_;
118   THRIFT_SOCKET serverSocket_;
119   int acceptBacklog_;
120   int sendTimeout_;
121   int recvTimeout_;
122   int retryLimit_;
123   int retryDelay_;
124   int tcpSendBuffer_;
125   int tcpRecvBuffer_;
126   bool keepAlive_;
127   bool listening_;
128 
129   socket_func_t listenCallback_;
130   socket_func_t acceptCallback_;
131 
132   void _setup_sockopts();
133   void _setup_tcp_sockopts();
134 };
135 }
136 }
137 } // apache::thrift::transport
138 
139 #endif // #ifndef _THRIFT_TRANSPORT_TNONBLOCKINGSERVERSOCKET_H_
140