Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-17 08:35:03

0001 /*
0002  * Licensed to the Apache Software Foundation (ASF) under one
0003  * or more contributor license agreements. See the NOTICE file
0004  * distributed with this work for additional information
0005  * regarding copyright ownership. The ASF licenses this file
0006  * to you under the Apache License, Version 2.0 (the
0007  * "License"); you may not use this file except in compliance
0008  * with the License. You may obtain a copy of the License at
0009  *
0010  *   http://www.apache.org/licenses/LICENSE-2.0
0011  *
0012  * Unless required by applicable law or agreed to in writing,
0013  * software distributed under the License is distributed on an
0014  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
0015  * KIND, either express or implied. See the License for the
0016  * specific language governing permissions and limitations
0017  * under the License.
0018  */
0019 
0020 #ifndef _THRIFT_TRANSPORT_TNONBLOCKINGSERVERSOCKET_H_
0021 #define _THRIFT_TRANSPORT_TNONBLOCKINGSERVERSOCKET_H_ 1
0022 
0023 #include <thrift/transport/TNonblockingServerTransport.h>
0024 #include <thrift/transport/PlatformSocket.h>
0025 
0026 namespace apache {
0027 namespace thrift {
0028 namespace transport {
0029 
0030 class TSocket;
0031 
0032 /**
0033  * Nonblocking Server socket implementation of TNonblockingServerTransport. Wrapper around a unix
0034  * socket listen and accept calls.
0035  *
0036  */
0037 class TNonblockingServerSocket : public TNonblockingServerTransport {
0038 public:
0039   typedef std::function<void(THRIFT_SOCKET fd)> socket_func_t;
0040 
0041   const static int DEFAULT_BACKLOG = 1024;
0042 
0043   /**
0044    * Constructor.
0045    *
0046    * @param port    Port number to bind to
0047    */
0048   TNonblockingServerSocket(int port);
0049 
0050   /**
0051    * Constructor.
0052    *
0053    * @param port        Port number to bind to
0054    * @param sendTimeout Socket send timeout
0055    * @param recvTimeout Socket receive timeout
0056    */
0057   TNonblockingServerSocket(int port, int sendTimeout, int recvTimeout);
0058 
0059   /**
0060    * Constructor.
0061    *
0062    * @param address Address to bind to
0063    * @param port    Port number to bind to
0064    */
0065   TNonblockingServerSocket(const std::string& address, int port);
0066 
0067   /**
0068    * Constructor used for unix sockets.
0069    *
0070    * @param path Pathname for unix socket.
0071    */
0072   TNonblockingServerSocket(const std::string& path);
0073 
0074   ~TNonblockingServerSocket() override;
0075 
0076   bool isOpen() const;
0077 
0078   void setSendTimeout(int sendTimeout);
0079   void setRecvTimeout(int recvTimeout);
0080 
0081   void setAcceptBacklog(int accBacklog);
0082 
0083   void setRetryLimit(int retryLimit);
0084   void setRetryDelay(int retryDelay);
0085 
0086   void setKeepAlive(bool keepAlive) { keepAlive_ = keepAlive; }
0087 
0088   void setTcpSendBuffer(int tcpSendBuffer);
0089   void setTcpRecvBuffer(int tcpRecvBuffer);
0090 
0091   // listenCallback gets called just before listen, and after all Thrift
0092   // setsockopt calls have been made.  If you have custom setsockopt
0093   // things that need to happen on the listening socket, this is the place to do it.
0094   void setListenCallback(const socket_func_t& listenCallback) { listenCallback_ = listenCallback; }
0095 
0096   // acceptCallback gets called after each accept call, on the newly created socket.
0097   // It is called after all Thrift setsockopt calls have been made.  If you have
0098   // custom setsockopt things that need to happen on the accepted
0099   // socket, this is the place to do it.
0100   void setAcceptCallback(const socket_func_t& acceptCallback) { acceptCallback_ = acceptCallback; }
0101 
0102   THRIFT_SOCKET getSocketFD() override { return serverSocket_; }
0103 
0104   int getPort() override;
0105 
0106   int getListenPort() override;
0107 
0108   std::string getPath() const;
0109 
0110   bool isUnixDomainSocket() const;
0111 
0112   void listen() override;
0113   void close() override;
0114 
0115 protected:
0116   std::shared_ptr<TSocket> acceptImpl() override;
0117   virtual std::shared_ptr<TSocket> createSocket(THRIFT_SOCKET client);
0118 
0119 private:
0120   void _setup_sockopts();
0121   void _setup_unixdomain_sockopts();
0122   void _setup_tcp_sockopts();
0123 
0124   int port_;
0125   int listenPort_;
0126   std::string address_;
0127   std::string path_;
0128   THRIFT_SOCKET serverSocket_;
0129   int acceptBacklog_;
0130   int sendTimeout_;
0131   int recvTimeout_;
0132   int retryLimit_;
0133   int retryDelay_;
0134   int tcpSendBuffer_;
0135   int tcpRecvBuffer_;
0136   bool keepAlive_;
0137   bool listening_;
0138 
0139   socket_func_t listenCallback_;
0140   socket_func_t acceptCallback_;
0141 };
0142 }
0143 }
0144 } // apache::thrift::transport
0145 
0146 #endif // #ifndef _THRIFT_TRANSPORT_TNONBLOCKINGSERVERSOCKET_H_