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_TSOCKET_H_
0021 #define _THRIFT_TRANSPORT_TSOCKET_H_ 1
0022 
0023 #include <string>
0024 
0025 #include <thrift/transport/TTransport.h>
0026 #include <thrift/transport/TVirtualTransport.h>
0027 #include <thrift/transport/TServerSocket.h>
0028 #include <thrift/transport/PlatformSocket.h>
0029 
0030 #ifdef HAVE_ARPA_INET_H
0031 #include <arpa/inet.h>
0032 #endif
0033 #ifdef HAVE_SYS_TIME_H
0034 #include <sys/time.h>
0035 #endif
0036 #ifdef HAVE_NETDB_H
0037 #include <netdb.h>
0038 #endif
0039 
0040 namespace apache {
0041 namespace thrift {
0042 namespace transport {
0043 
0044 /**
0045  * TCP Socket implementation of the TTransport interface.
0046  *
0047  */
0048 class TSocket : public TVirtualTransport<TSocket> {
0049 public:
0050   /**
0051    * Constructs a new socket. Note that this does NOT actually connect the
0052    * socket.
0053    *
0054    */
0055   TSocket(std::shared_ptr<TConfiguration> config = nullptr);
0056 
0057   /**
0058    * Constructs a new socket. Note that this does NOT actually connect the
0059    * socket.
0060    *
0061    * @param host An IP address or hostname to connect to
0062    * @param port The port to connect on
0063    */
0064   TSocket(const std::string& host, int port, std::shared_ptr<TConfiguration> config = nullptr);
0065 
0066   /**
0067    * Constructs a new Unix domain socket.
0068    * Note that this does NOT actually connect the socket.
0069    *
0070    * @param path The Unix domain socket e.g. "/tmp/ThriftTest.binary.thrift"
0071    * or a zero-prefixed string to create an abstract domain socket on Linux.
0072    */
0073   TSocket(const std::string& path, std::shared_ptr<TConfiguration> config = nullptr);
0074 
0075   /**
0076    * Destroyes the socket object, closing it if necessary.
0077    */
0078   ~TSocket() override;
0079 
0080   /**
0081    * Whether the socket is alive.
0082    *
0083    * @return Is the socket alive?
0084    */
0085   bool isOpen() const override;
0086 
0087   /**
0088    * Checks whether there is more data available in the socket to read.
0089    *
0090    * This call blocks until at least one byte is available or the socket is closed.
0091    */
0092   bool peek() override;
0093 
0094   /**
0095    * Creates and opens the UNIX socket.
0096    *
0097    * @throws TTransportException If the socket could not connect
0098    */
0099   void open() override;
0100 
0101   /**
0102    * Shuts down communications on the socket.
0103    */
0104   void close() override;
0105 
0106   /**
0107    * Determines whether there is pending data to read or not.
0108    *
0109    * This call does not block.
0110    * \throws TTransportException of types:
0111    *           NOT_OPEN means the socket has been closed
0112    *           UNKNOWN means something unexpected happened
0113    * \returns true if there is pending data to read, false otherwise
0114    */
0115   virtual bool hasPendingDataToRead();
0116 
0117   /**
0118    * Reads from the underlying socket.
0119    * \returns the number of bytes read or 0 indicates EOF
0120    * \throws TTransportException of types:
0121    *           INTERRUPTED means the socket was interrupted
0122    *                       out of a blocking call
0123    *           NOT_OPEN means the socket has been closed
0124    *           TIMED_OUT means the receive timeout expired
0125    *           UNKNOWN means something unexpected happened
0126    */
0127   virtual uint32_t read(uint8_t* buf, uint32_t len);
0128 
0129   /**
0130    * Writes to the underlying socket.  Loops until done or fail.
0131    */
0132   virtual void write(const uint8_t* buf, uint32_t len);
0133 
0134   /**
0135    * Writes to the underlying socket.  Does single send() and returns result.
0136    */
0137   virtual uint32_t write_partial(const uint8_t* buf, uint32_t len);
0138 
0139   /**
0140    * Get the host that the socket is connected to
0141    *
0142    * @return string host identifier
0143    */
0144   std::string getHost() const;
0145 
0146   /**
0147    * Get the port that the socket is connected to
0148    *
0149    * @return int port number
0150    */
0151   int getPort() const;
0152 
0153   /**
0154    * Get the Unix domain socket path that the socket is connected to
0155    *
0156    * @return std::string path
0157    */
0158   std::string getPath() const;
0159 
0160   /**
0161    * Whether the socket is a Unix domain socket. This is the same as checking
0162    * if getPath() is not empty.
0163    *
0164    * @return Is the socket a Unix domain socket?
0165    */
0166   bool isUnixDomainSocket() const;
0167 
0168   /**
0169    * Set the host that socket will connect to
0170    *
0171    * @param host host identifier
0172    */
0173   void setHost(std::string host);
0174 
0175   /**
0176    * Set the port that socket will connect to
0177    *
0178    * @param port port number
0179    */
0180   void setPort(int port);
0181 
0182   /**
0183    * Set the Unix domain socket path for the socket
0184    *
0185    * @param path std::string path
0186    */
0187   void setPath(std::string path);
0188 
0189   /**
0190    * Controls whether the linger option is set on the socket.
0191    *
0192    * @param on      Whether SO_LINGER is on
0193    * @param linger  If linger is active, the number of seconds to linger for
0194    */
0195   void setLinger(bool on, int linger);
0196 
0197   /**
0198    * Whether to enable/disable Nagle's algorithm.
0199    *
0200    * @param noDelay Whether or not to disable the algorithm.
0201    * @return
0202    */
0203   void setNoDelay(bool noDelay);
0204 
0205   /**
0206    * Set the connect timeout
0207    */
0208   void setConnTimeout(int ms);
0209 
0210   /**
0211    * Set the receive timeout
0212    */
0213   void setRecvTimeout(int ms);
0214 
0215   /**
0216    * Set the send timeout
0217    */
0218   void setSendTimeout(int ms);
0219 
0220   /**
0221    * Set the max number of recv retries in case of an THRIFT_EAGAIN
0222    * error
0223    */
0224   void setMaxRecvRetries(int maxRecvRetries);
0225 
0226   /**
0227    * Set SO_KEEPALIVE
0228    */
0229   void setKeepAlive(bool keepAlive);
0230 
0231   /**
0232    * Get socket information formatted as a string <Host: x Port: x>
0233    */
0234   std::string getSocketInfo() const;
0235 
0236   /**
0237    * Returns the DNS name of the host to which the socket is connected
0238    */
0239   std::string getPeerHost() const;
0240 
0241   /**
0242    * Returns the address of the host to which the socket is connected
0243    */
0244   std::string getPeerAddress() const;
0245 
0246   /**
0247    * Returns the port of the host to which the socket is connected
0248    **/
0249   int getPeerPort() const;
0250 
0251   /**
0252    * Returns the underlying socket file descriptor.
0253    */
0254   THRIFT_SOCKET getSocketFD() { return socket_; }
0255 
0256   /**
0257    * (Re-)initialize a TSocket for the supplied descriptor.  This is only
0258    * intended for use by TNonblockingServer -- other use may result in
0259    * unfortunate surprises.
0260    *
0261    * @param fd the descriptor for an already-connected socket
0262    */
0263   void setSocketFD(THRIFT_SOCKET fd);
0264 
0265   /*
0266    * Returns a cached copy of the peer address.
0267    */
0268   sockaddr* getCachedAddress(socklen_t* len) const;
0269 
0270   /**
0271    * Sets whether to use a low minimum TCP retransmission timeout.
0272    */
0273   static void setUseLowMinRto(bool useLowMinRto);
0274 
0275   /**
0276    * Gets whether to use a low minimum TCP retransmission timeout.
0277    */
0278   static bool getUseLowMinRto();
0279 
0280   /**
0281    * Get the origin the socket is connected to
0282    *
0283    * @return string peer host identifier and port
0284    */
0285   const std::string getOrigin() const override;
0286 
0287   /**
0288    * Constructor to create socket from file descriptor.
0289    */
0290   TSocket(THRIFT_SOCKET socket, std::shared_ptr<TConfiguration> config = nullptr);
0291 
0292   /**
0293    * Constructor to create socket from file descriptor that
0294    * can be interrupted safely.
0295    */
0296   TSocket(THRIFT_SOCKET socket, std::shared_ptr<THRIFT_SOCKET> interruptListener,
0297          std::shared_ptr<TConfiguration> config = nullptr);
0298 
0299   /**
0300    * Set a cache of the peer address (used when trivially available: e.g.
0301    * accept() or connect()). Only caches IPV4 and IPV6; unset for others.
0302    */
0303   void setCachedAddress(const sockaddr* addr, socklen_t len);
0304 
0305 protected:
0306   /** connect, called by open */
0307   void openConnection(struct addrinfo* res);
0308 
0309   /** Host to connect to */
0310   std::string host_;
0311 
0312   /** Port number to connect on */
0313   int port_;
0314 
0315   /** UNIX domain socket path */
0316   std::string path_;
0317 
0318   /** Underlying socket handle */
0319   THRIFT_SOCKET socket_;
0320 
0321   /** Peer hostname */
0322   mutable std::string peerHost_;
0323 
0324   /** Peer address */
0325   mutable std::string peerAddress_;
0326 
0327   /** Peer port */
0328   mutable int peerPort_;
0329 
0330   /**
0331    * A shared socket pointer that will interrupt a blocking read if data
0332    * becomes available on it
0333    */
0334   std::shared_ptr<THRIFT_SOCKET> interruptListener_;
0335 
0336   /** Connect timeout in ms */
0337   int connTimeout_;
0338 
0339   /** Send timeout in ms */
0340   int sendTimeout_;
0341 
0342   /** Recv timeout in ms */
0343   int recvTimeout_;
0344 
0345   /** Keep alive on */
0346   bool keepAlive_;
0347 
0348   /** Linger on */
0349   bool lingerOn_;
0350 
0351   /** Linger val */
0352   int lingerVal_;
0353 
0354   /** Nodelay */
0355   bool noDelay_;
0356 
0357   /** Recv EGAIN retries */
0358   int maxRecvRetries_;
0359 
0360   /** Cached peer address */
0361   union {
0362     sockaddr_in ipv4;
0363     sockaddr_in6 ipv6;
0364   } cachedPeerAddr_;
0365 
0366   /** Whether to use low minimum TCP retransmission timeout */
0367   static bool useLowMinRto_;
0368 
0369 private:
0370   void unix_open();
0371   void local_open();
0372 };
0373 }
0374 }
0375 } // apache::thrift::transport
0376 
0377 #endif // #ifndef _THRIFT_TRANSPORT_TSOCKET_H_