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   bool isOpen() const;
77 
78   void setSendTimeout(int sendTimeout);
79   void setRecvTimeout(int recvTimeout);
80 
81   void setAcceptBacklog(int accBacklog);
82 
83   void setRetryLimit(int retryLimit);
84   void setRetryDelay(int retryDelay);
85 
setKeepAlive(bool keepAlive)86   void setKeepAlive(bool keepAlive) { keepAlive_ = keepAlive; }
87 
88   void setTcpSendBuffer(int tcpSendBuffer);
89   void setTcpRecvBuffer(int tcpRecvBuffer);
90 
91   // listenCallback gets called just before listen, and after all Thrift
92   // setsockopt calls have been made.  If you have custom setsockopt
93   // things that need to happen on the listening socket, this is the place to do it.
setListenCallback(const socket_func_t & listenCallback)94   void setListenCallback(const socket_func_t& listenCallback) { listenCallback_ = listenCallback; }
95 
96   // acceptCallback gets called after each accept call, on the newly created socket.
97   // It is called after all Thrift setsockopt calls have been made.  If you have
98   // custom setsockopt things that need to happen on the accepted
99   // socket, this is the place to do it.
setAcceptCallback(const socket_func_t & acceptCallback)100   void setAcceptCallback(const socket_func_t& acceptCallback) { acceptCallback_ = acceptCallback; }
101 
getSocketFD()102   THRIFT_SOCKET getSocketFD() override { return serverSocket_; }
103 
104   int getPort() override;
105 
106   int getListenPort() override;
107 
108   std::string getPath() const;
109 
110   bool isUnixDomainSocket() const;
111 
112   void listen() override;
113   void close() override;
114 
115 protected:
116   std::shared_ptr<TSocket> acceptImpl() override;
117   virtual std::shared_ptr<TSocket> createSocket(THRIFT_SOCKET client);
118 
119 private:
120   void _setup_sockopts();
121   void _setup_unixdomain_sockopts();
122   void _setup_tcp_sockopts();
123 
124   int port_;
125   int listenPort_;
126   std::string address_;
127   std::string path_;
128   THRIFT_SOCKET serverSocket_;
129   int acceptBacklog_;
130   int sendTimeout_;
131   int recvTimeout_;
132   int retryLimit_;
133   int retryDelay_;
134   int tcpSendBuffer_;
135   int tcpRecvBuffer_;
136   bool keepAlive_;
137   bool listening_;
138 
139   socket_func_t listenCallback_;
140   socket_func_t acceptCallback_;
141 };
142 }
143 }
144 } // apache::thrift::transport
145 
146 #endif // #ifndef _THRIFT_TRANSPORT_TNONBLOCKINGSERVERSOCKET_H_
147