| Mark Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 1 | // Copyright (c) 2006- Facebook | 
 | 2 | // Distributed under the Thrift Software License | 
 | 3 | // | 
 | 4 | // See accompanying file LICENSE or visit the Thrift site at: | 
 | 5 | // http://developers.facebook.com/thrift/ | 
 | 6 |  | 
| Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 7 | #ifndef _THRIFT_TRANSPORT_TSOCKET_H_ | 
 | 8 | #define _THRIFT_TRANSPORT_TSOCKET_H_ 1 | 
| Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 9 |  | 
 | 10 | #include <string> | 
| Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 11 | #include <sys/time.h> | 
| Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 12 |  | 
| Marc Slemko | d42a2c2 | 2006-08-10 03:30:18 +0000 | [diff] [blame] | 13 | #include "TTransport.h" | 
 | 14 | #include "TServerSocket.h" | 
| Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 15 |  | 
| Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 16 | namespace facebook { namespace thrift { namespace transport {  | 
 | 17 |  | 
| Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 18 | /** | 
 | 19 |  * TCP Socket implementation of the TTransport interface. | 
 | 20 |  * | 
 | 21 |  * @author Mark Slee <mcslee@facebook.com> | 
| Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 22 |  * @author Aditya Agarwal <aditya@facebook.com> | 
| Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 23 |  */ | 
 | 24 | class TSocket : public TTransport { | 
| Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 25 |   /** | 
 | 26 |    * We allow the TServerSocket acceptImpl() method to access the private | 
 | 27 |    * members of a socket so that it can access the TSocket(int socket) | 
 | 28 |    * constructor which creates a socket object from the raw UNIX socket | 
 | 29 |    * handle. | 
 | 30 |    */ | 
 | 31 |   friend class TServerSocket; | 
| Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 32 |  | 
 | 33 |  public: | 
| Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 34 |   /** | 
 | 35 |    * Constructs a new socket. Note that this does NOT actually connect the | 
 | 36 |    * socket. | 
 | 37 |    * | 
| Aditya Agarwal | ebc99e0 | 2007-01-15 23:14:58 +0000 | [diff] [blame] | 38 |    */ | 
 | 39 |   TSocket(); | 
 | 40 |    | 
 | 41 |   /** | 
 | 42 |    * Constructs a new socket. Note that this does NOT actually connect the | 
 | 43 |    * socket. | 
 | 44 |    * | 
| Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 45 |    * @param host An IP address or hostname to connect to | 
 | 46 |    * @param port The port to connect on | 
 | 47 |    */ | 
| Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 48 |   TSocket(std::string host, int port); | 
| Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 49 |  | 
 | 50 |   /** | 
 | 51 |    * Destroyes the socket object, closing it if necessary. | 
 | 52 |    */ | 
| Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 53 |   virtual ~TSocket(); | 
| Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 54 |  | 
| Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 55 |   /** | 
 | 56 |    * Whether the socket is alive. | 
 | 57 |    * | 
 | 58 |    * @return Is the socket alive? | 
 | 59 |    */ | 
 | 60 |   bool isOpen(); | 
| Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 61 |  | 
| Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 62 |   /** | 
| Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 63 |    * Calls select on the socket to see if there is more data available. | 
 | 64 |    */ | 
 | 65 |   bool peek(); | 
 | 66 |  | 
 | 67 |   /** | 
| Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 68 |    * Creates and opens the UNIX socket. | 
 | 69 |    * | 
 | 70 |    * @throws TTransportException If the socket could not connect | 
 | 71 |    */ | 
| jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 72 |   virtual void open(); | 
| Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 73 |  | 
 | 74 |   /** | 
 | 75 |    * Shuts down communications on the socket. | 
 | 76 |    */ | 
 | 77 |   void close(); | 
 | 78 |  | 
 | 79 |   /** | 
 | 80 |    * Reads from the underlying socket. | 
 | 81 |    */ | 
 | 82 |   uint32_t read(uint8_t* buf, uint32_t len); | 
 | 83 |  | 
 | 84 |   /** | 
 | 85 |    * Writes to the underlying socket. | 
 | 86 |    */ | 
 | 87 |   void write(const uint8_t* buf, uint32_t len); | 
 | 88 |  | 
 | 89 |   /** | 
| Aditya Agarwal | ebc99e0 | 2007-01-15 23:14:58 +0000 | [diff] [blame] | 90 |    * Set the host that socket will connect to | 
 | 91 |    * | 
 | 92 |    * @param host host identifier | 
 | 93 |    */ | 
 | 94 |   void setHost(std::string host); | 
 | 95 |  | 
 | 96 |   /** | 
 | 97 |    * Set the port that socket will connect to | 
 | 98 |    * | 
 | 99 |    * @param port port number | 
 | 100 |    */ | 
 | 101 |   void setPort(int port); | 
 | 102 |  | 
 | 103 |   /** | 
| Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 104 |    * Controls whether the linger option is set on the socket. | 
 | 105 |    * | 
 | 106 |    * @param on      Whether SO_LINGER is on | 
 | 107 |    * @param linger  If linger is active, the number of seconds to linger for | 
 | 108 |    */ | 
 | 109 |   void setLinger(bool on, int linger); | 
 | 110 |  | 
 | 111 |   /** | 
 | 112 |    * Whether to enable/disable Nagle's algorithm. | 
 | 113 |    * | 
 | 114 |    * @param noDelay Whether or not to disable the algorithm. | 
 | 115 |    * @return  | 
 | 116 |    */ | 
 | 117 |   void setNoDelay(bool noDelay); | 
| Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 118 |  | 
| Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 119 |   /** | 
 | 120 |    * Set the connect timeout | 
 | 121 |    */ | 
 | 122 |   void setConnTimeout(int ms); | 
 | 123 |  | 
 | 124 |   /** | 
 | 125 |    * Set the receive timeout | 
 | 126 |    */ | 
 | 127 |   void setRecvTimeout(int ms); | 
 | 128 |  | 
 | 129 |   /** | 
 | 130 |    * Set the send timeout | 
 | 131 |    */ | 
 | 132 |   void setSendTimeout(int ms); | 
 | 133 |  | 
| Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 134 |   /** | 
 | 135 |    * Set the max number of recv retries in case of an EAGAIN | 
 | 136 |    * error | 
 | 137 |    */ | 
 | 138 |   void setMaxRecvRetries(int maxRecvRetries); | 
 | 139 |  | 
| Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 140 |  protected: | 
| Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 141 |   /** | 
 | 142 |    * Constructor to create socket from raw UNIX handle. Never called directly | 
 | 143 |    * but used by the TServerSocket class. | 
 | 144 |    */ | 
| Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 145 |   TSocket(int socket); | 
| Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 146 |  | 
| Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 147 |   /** connect, called by open */ | 
 | 148 |   void openConnection(struct addrinfo *res); | 
 | 149 |  | 
| Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 150 |   /** Host to connect to */ | 
| Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 151 |   std::string host_; | 
| Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 152 |  | 
 | 153 |   /** Port number to connect on */ | 
| Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 154 |   int port_; | 
| Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 155 |  | 
 | 156 |   /** Underlying UNIX socket handle */ | 
| Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 157 |   int socket_; | 
| Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 158 |  | 
 | 159 |   /** Connect timeout in ms */ | 
 | 160 |   int connTimeout_; | 
 | 161 |  | 
 | 162 |   /** Send timeout in ms */ | 
 | 163 |   int sendTimeout_; | 
 | 164 |  | 
 | 165 |   /** Recv timeout in ms */ | 
 | 166 |   int recvTimeout_; | 
 | 167 |  | 
 | 168 |   /** Linger on */ | 
 | 169 |   bool lingerOn_; | 
 | 170 |    | 
 | 171 |   /** Linger val */ | 
 | 172 |   int lingerVal_; | 
 | 173 |  | 
 | 174 |   /** Nodelay */ | 
 | 175 |   bool noDelay_; | 
| Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 176 |  | 
| Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 177 |   /** Recv EGAIN retries */ | 
 | 178 |   int maxRecvRetries_; | 
 | 179 |  | 
| Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 180 |   /** Recv timeout timeval */ | 
 | 181 |   struct timeval recvTimeval_; | 
| Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 182 | }; | 
 | 183 |  | 
| Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 184 | }}} // facebook::thrift::transport | 
| Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 185 |  | 
 | 186 | #endif // #ifndef _THRIFT_TRANSPORT_TSOCKET_H_ | 
 | 187 |  |