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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
20 #ifndef _THRIFT_TRANSPORT_TSERVERSOCKET_H_
21 #define _THRIFT_TRANSPORT_TSERVERSOCKET_H_ 1
23 #include <thrift/concurrency/Mutex.h>
24 #include <thrift/transport/PlatformSocket.h>
25 #include <thrift/transport/TServerTransport.h>
27 #include <sys/types.h>
28 #ifdef HAVE_SYS_SOCKET_H
29 #include <sys/socket.h>
41 class TGetAddrInfoWrapper
{
43 TGetAddrInfoWrapper(const char* node
, const char* service
, const struct addrinfo
* hints
);
45 virtual ~TGetAddrInfoWrapper();
48 const struct addrinfo
* res();
53 const struct addrinfo
* hints_
;
54 struct addrinfo
* res_
;
58 * Server socket implementation of TServerTransport. Wrapper around a unix
59 * socket listen and accept calls.
62 class TServerSocket
: public TServerTransport
{
64 typedef std::function
<void(THRIFT_SOCKET fd
)> socket_func_t
;
66 const static int DEFAULT_BACKLOG
= 1024;
71 * @param port Port number to bind to
73 TServerSocket(int port
);
78 * @param port Port number to bind to
79 * @param sendTimeout Socket send timeout
80 * @param recvTimeout Socket receive timeout
82 TServerSocket(int port
, int sendTimeout
, int recvTimeout
);
87 * @param address Address to bind to
88 * @param port Port number to bind to
90 TServerSocket(const std::string
& address
, int port
);
93 * Constructor used for unix sockets.
95 * @param path Pathname for unix socket.
97 TServerSocket(const std::string
& path
);
99 ~TServerSocket() override
;
101 void setSendTimeout(int sendTimeout
);
102 void setRecvTimeout(int recvTimeout
);
104 void setAcceptTimeout(int accTimeout
);
105 void setAcceptBacklog(int accBacklog
);
107 void setRetryLimit(int retryLimit
);
108 void setRetryDelay(int retryDelay
);
110 void setKeepAlive(bool keepAlive
) { keepAlive_
= keepAlive
; }
112 void setTcpSendBuffer(int tcpSendBuffer
);
113 void setTcpRecvBuffer(int tcpRecvBuffer
);
115 // listenCallback gets called just before listen, and after all Thrift
116 // setsockopt calls have been made. If you have custom setsockopt
117 // things that need to happen on the listening socket, this is the place to do it.
118 void setListenCallback(const socket_func_t
& listenCallback
) { listenCallback_
= listenCallback
; }
120 // acceptCallback gets called after each accept call, on the newly created socket.
121 // It is called after all Thrift setsockopt calls have been made. If you have
122 // custom setsockopt things that need to happen on the accepted
123 // socket, this is the place to do it.
124 void setAcceptCallback(const socket_func_t
& acceptCallback
) { acceptCallback_
= acceptCallback
; }
126 // When enabled (the default), new children TSockets will be constructed so
127 // they can be interrupted by TServerTransport::interruptChildren().
128 // This is more expensive in terms of system calls (poll + recv) however
129 // ensures a connected client cannot interfere with TServer::stop().
131 // When disabled, TSocket children do not incur an additional poll() call.
132 // Server-side reads are more efficient, however a client can interfere with
133 // the server's ability to shutdown properly by staying connected.
135 // Must be called before listen(); mode cannot be switched after that.
136 // \throws std::logic_error if listen() has been called
137 void setInterruptableChildren(bool enable
);
139 THRIFT_SOCKET
getSocketFD() override
{ return serverSocket_
; }
143 void listen() override
;
144 void interrupt() override
;
145 void interruptChildren() override
;
146 void close() override
;
149 std::shared_ptr
<TTransport
> acceptImpl() override
;
150 virtual std::shared_ptr
<TSocket
> createSocket(THRIFT_SOCKET client
);
151 bool interruptableChildren_
;
152 std::shared_ptr
<THRIFT_SOCKET
> pChildInterruptSockReader_
; // if interruptableChildren_ this is shared with child TSockets
155 void notify(THRIFT_SOCKET notifySock
);
158 std::string address_
;
160 THRIFT_SOCKET serverSocket_
;
172 concurrency::Mutex rwMutex_
; // thread-safe interrupt
173 THRIFT_SOCKET interruptSockWriter_
; // is notified on interrupt()
174 THRIFT_SOCKET interruptSockReader_
; // is used in select/poll with serverSocket_ for interruptability
175 THRIFT_SOCKET childInterruptSockWriter_
; // is notified on interruptChildren()
177 socket_func_t listenCallback_
;
178 socket_func_t acceptCallback_
;
182 } // apache::thrift::transport
184 #endif // #ifndef _THRIFT_TRANSPORT_TSERVERSOCKET_H_