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_TSERVERSOCKET_H_
0021 #define _THRIFT_TRANSPORT_TSERVERSOCKET_H_ 1
0022 
0023 #include <functional>
0024 
0025 #include <thrift/concurrency/Mutex.h>
0026 #include <thrift/transport/PlatformSocket.h>
0027 #include <thrift/transport/TServerTransport.h>
0028 
0029 #include <sys/types.h>
0030 #ifdef HAVE_SYS_SOCKET_H
0031 #include <sys/socket.h>
0032 #endif
0033 #ifdef HAVE_NETDB_H
0034 #include <netdb.h>
0035 #endif
0036 
0037 namespace apache {
0038 namespace thrift {
0039 namespace transport {
0040 
0041 class TSocket;
0042 
0043 /**
0044  * Server socket implementation of TServerTransport. Wrapper around a unix
0045  * socket listen and accept calls.
0046  *
0047  */
0048 class TServerSocket : public TServerTransport {
0049 public:
0050   typedef std::function<void(THRIFT_SOCKET fd)> socket_func_t;
0051 
0052   const static int DEFAULT_BACKLOG = 1024;
0053 
0054   /**
0055    * Constructor.
0056    *
0057    * @param port    Port number to bind to
0058    */
0059   TServerSocket(int port);
0060 
0061   /**
0062    * Constructor.
0063    *
0064    * @param port        Port number to bind to
0065    * @param sendTimeout Socket send timeout
0066    * @param recvTimeout Socket receive timeout
0067    */
0068   TServerSocket(int port, int sendTimeout, int recvTimeout);
0069 
0070   /**
0071    * Constructor.
0072    *
0073    * @param address Address to bind to
0074    * @param port    Port number to bind to
0075    */
0076   TServerSocket(const std::string& address, int port);
0077 
0078   /**
0079    * Constructor used for unix sockets.
0080    *
0081    * @param path Pathname for unix socket.
0082    */
0083   TServerSocket(const std::string& path);
0084 
0085   ~TServerSocket() override;
0086 
0087 
0088   bool isOpen() const override;
0089 
0090   void setSendTimeout(int sendTimeout);
0091   void setRecvTimeout(int recvTimeout);
0092 
0093   void setAcceptTimeout(int accTimeout);
0094   void setAcceptBacklog(int accBacklog);
0095 
0096   void setRetryLimit(int retryLimit);
0097   void setRetryDelay(int retryDelay);
0098 
0099   void setKeepAlive(bool keepAlive) { keepAlive_ = keepAlive; }
0100 
0101   void setTcpSendBuffer(int tcpSendBuffer);
0102   void setTcpRecvBuffer(int tcpRecvBuffer);
0103 
0104   // listenCallback gets called just before listen, and after all Thrift
0105   // setsockopt calls have been made.  If you have custom setsockopt
0106   // things that need to happen on the listening socket, this is the place to do it.
0107   void setListenCallback(const socket_func_t& listenCallback) { listenCallback_ = listenCallback; }
0108 
0109   // acceptCallback gets called after each accept call, on the newly created socket.
0110   // It is called after all Thrift setsockopt calls have been made.  If you have
0111   // custom setsockopt things that need to happen on the accepted
0112   // socket, this is the place to do it.
0113   void setAcceptCallback(const socket_func_t& acceptCallback) { acceptCallback_ = acceptCallback; }
0114 
0115   // When enabled (the default), new children TSockets will be constructed so
0116   // they can be interrupted by TServerTransport::interruptChildren().
0117   // This is more expensive in terms of system calls (poll + recv) however
0118   // ensures a connected client cannot interfere with TServer::stop().
0119   //
0120   // When disabled, TSocket children do not incur an additional poll() call.
0121   // Server-side reads are more efficient, however a client can interfere with
0122   // the server's ability to shutdown properly by staying connected.
0123   //
0124   // Must be called before listen(); mode cannot be switched after that.
0125   // \throws std::logic_error if listen() has been called
0126   void setInterruptableChildren(bool enable);
0127 
0128   THRIFT_SOCKET getSocketFD() override { return serverSocket_; }
0129 
0130   int getPort() const;
0131 
0132   std::string getPath() const;
0133 
0134   bool isUnixDomainSocket() const;
0135 
0136   void listen() override;
0137   void interrupt() override;
0138   void interruptChildren() override;
0139   void close() override;
0140 
0141 protected:
0142   std::shared_ptr<TTransport> acceptImpl() override;
0143   virtual std::shared_ptr<TSocket> createSocket(THRIFT_SOCKET client);
0144   bool interruptableChildren_;
0145   std::shared_ptr<THRIFT_SOCKET> pChildInterruptSockReader_; // if interruptableChildren_ this is shared with child TSockets
0146 
0147 private:
0148   void notify(THRIFT_SOCKET notifySock);
0149   void _setup_sockopts();
0150   void _setup_unixdomain_sockopts();
0151   void _setup_tcp_sockopts();
0152 
0153   int port_;
0154   std::string address_;
0155   std::string path_;
0156   THRIFT_SOCKET serverSocket_;
0157   int acceptBacklog_;
0158   int sendTimeout_;
0159   int recvTimeout_;
0160   int accTimeout_;
0161   int retryLimit_;
0162   int retryDelay_;
0163   int tcpSendBuffer_;
0164   int tcpRecvBuffer_;
0165   bool keepAlive_;
0166   bool listening_;
0167 
0168   concurrency::Mutex rwMutex_;                                 // thread-safe interrupt
0169   THRIFT_SOCKET interruptSockWriter_;                          // is notified on interrupt()
0170   THRIFT_SOCKET interruptSockReader_;                          // is used in select/poll with serverSocket_ for interruptability
0171   THRIFT_SOCKET childInterruptSockWriter_;                     // is notified on interruptChildren()
0172 
0173   socket_func_t listenCallback_;
0174   socket_func_t acceptCallback_;
0175 };
0176 }
0177 }
0178 } // apache::thrift::transport
0179 
0180 #endif // #ifndef _THRIFT_TRANSPORT_TSERVERSOCKET_H_