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_TNONBLOCKINGSERVERSOCKET_H_
21 #define _THRIFT_TRANSPORT_TNONBLOCKINGSERVERSOCKET_H_ 1
23 #include <thrift/transport/TNonblockingServerTransport.h>
24 #include <thrift/transport/PlatformSocket.h>
33 * Nonblocking Server socket implementation of TNonblockingServerTransport. Wrapper around a unix
34 * socket listen and accept calls.
37 class TNonblockingServerSocket
: public TNonblockingServerTransport
{
39 typedef std::function
<void(THRIFT_SOCKET fd
)> socket_func_t
;
41 const static int DEFAULT_BACKLOG
= 1024;
46 * @param port Port number to bind to
48 TNonblockingServerSocket(int port
);
53 * @param port Port number to bind to
54 * @param sendTimeout Socket send timeout
55 * @param recvTimeout Socket receive timeout
57 TNonblockingServerSocket(int port
, int sendTimeout
, int recvTimeout
);
62 * @param address Address to bind to
63 * @param port Port number to bind to
65 TNonblockingServerSocket(const std::string
& address
, int port
);
68 * Constructor used for unix sockets.
70 * @param path Pathname for unix socket.
72 TNonblockingServerSocket(const std::string
& path
);
74 ~TNonblockingServerSocket() override
;
76 void setSendTimeout(int sendTimeout
);
77 void setRecvTimeout(int recvTimeout
);
79 void setAcceptBacklog(int accBacklog
);
81 void setRetryLimit(int retryLimit
);
82 void setRetryDelay(int retryDelay
);
84 void setKeepAlive(bool keepAlive
) { keepAlive_
= keepAlive
; }
86 void setTcpSendBuffer(int tcpSendBuffer
);
87 void setTcpRecvBuffer(int tcpRecvBuffer
);
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.
92 void setListenCallback(const socket_func_t
& listenCallback
) { listenCallback_
= listenCallback
; }
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.
98 void setAcceptCallback(const socket_func_t
& acceptCallback
) { acceptCallback_
= acceptCallback
; }
100 THRIFT_SOCKET
getSocketFD() override
{ return serverSocket_
; }
102 int getPort() override
;
104 int getListenPort() override
;
106 void listen() override
;
107 void close() override
;
110 std::shared_ptr
<TSocket
> acceptImpl() override
;
111 virtual std::shared_ptr
<TSocket
> createSocket(THRIFT_SOCKET client
);
116 std::string address_
;
118 THRIFT_SOCKET serverSocket_
;
129 socket_func_t listenCallback_
;
130 socket_func_t acceptCallback_
;
134 } // apache::thrift::transport
136 #endif // #ifndef _THRIFT_TRANSPORT_TNONBLOCKINGSERVERSOCKET_H_