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 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 16 | namespace apache { namespace thrift { namespace transport { |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 17 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 18 | /** |
| 19 | * TCP Socket implementation of the TTransport interface. |
| 20 | * |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 21 | */ |
| 22 | class TSocket : public TTransport { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 23 | /** |
| 24 | * We allow the TServerSocket acceptImpl() method to access the private |
| 25 | * members of a socket so that it can access the TSocket(int socket) |
| 26 | * constructor which creates a socket object from the raw UNIX socket |
| 27 | * handle. |
| 28 | */ |
| 29 | friend class TServerSocket; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 30 | |
| 31 | public: |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 32 | /** |
| 33 | * Constructs a new socket. Note that this does NOT actually connect the |
| 34 | * socket. |
| 35 | * |
Aditya Agarwal | ebc99e0 | 2007-01-15 23:14:58 +0000 | [diff] [blame] | 36 | */ |
| 37 | TSocket(); |
Mark Slee | b455292 | 2007-11-28 00:12:11 +0000 | [diff] [blame] | 38 | |
Aditya Agarwal | ebc99e0 | 2007-01-15 23:14:58 +0000 | [diff] [blame] | 39 | /** |
| 40 | * Constructs a new socket. Note that this does NOT actually connect the |
| 41 | * socket. |
| 42 | * |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 43 | * @param host An IP address or hostname to connect to |
| 44 | * @param port The port to connect on |
| 45 | */ |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 46 | TSocket(std::string host, int port); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 47 | |
| 48 | /** |
| 49 | * Destroyes the socket object, closing it if necessary. |
| 50 | */ |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 51 | virtual ~TSocket(); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 52 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 53 | /** |
| 54 | * Whether the socket is alive. |
| 55 | * |
| 56 | * @return Is the socket alive? |
| 57 | */ |
| 58 | bool isOpen(); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 59 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 60 | /** |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 61 | * Calls select on the socket to see if there is more data available. |
| 62 | */ |
| 63 | bool peek(); |
| 64 | |
| 65 | /** |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 66 | * Creates and opens the UNIX socket. |
| 67 | * |
| 68 | * @throws TTransportException If the socket could not connect |
| 69 | */ |
jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 70 | virtual void open(); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 71 | |
| 72 | /** |
| 73 | * Shuts down communications on the socket. |
| 74 | */ |
| 75 | void close(); |
| 76 | |
| 77 | /** |
| 78 | * Reads from the underlying socket. |
| 79 | */ |
| 80 | uint32_t read(uint8_t* buf, uint32_t len); |
| 81 | |
| 82 | /** |
| 83 | * Writes to the underlying socket. |
| 84 | */ |
| 85 | void write(const uint8_t* buf, uint32_t len); |
| 86 | |
| 87 | /** |
dweatherford | 14b0ed6 | 2007-10-19 01:03:32 +0000 | [diff] [blame] | 88 | * Get the host that the socket is connected to |
| 89 | * |
| 90 | * @return string host identifier |
| 91 | */ |
| 92 | std::string getHost(); |
| 93 | |
| 94 | /** |
| 95 | * Get the port that the socket is connected to |
| 96 | * |
| 97 | * @return int port number |
| 98 | */ |
| 99 | int getPort(); |
| 100 | |
| 101 | /** |
Aditya Agarwal | ebc99e0 | 2007-01-15 23:14:58 +0000 | [diff] [blame] | 102 | * Set the host that socket will connect to |
| 103 | * |
| 104 | * @param host host identifier |
| 105 | */ |
| 106 | void setHost(std::string host); |
| 107 | |
| 108 | /** |
| 109 | * Set the port that socket will connect to |
| 110 | * |
| 111 | * @param port port number |
| 112 | */ |
| 113 | void setPort(int port); |
| 114 | |
| 115 | /** |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 116 | * Controls whether the linger option is set on the socket. |
| 117 | * |
| 118 | * @param on Whether SO_LINGER is on |
| 119 | * @param linger If linger is active, the number of seconds to linger for |
| 120 | */ |
| 121 | void setLinger(bool on, int linger); |
| 122 | |
| 123 | /** |
| 124 | * Whether to enable/disable Nagle's algorithm. |
| 125 | * |
| 126 | * @param noDelay Whether or not to disable the algorithm. |
Mark Slee | b455292 | 2007-11-28 00:12:11 +0000 | [diff] [blame] | 127 | * @return |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 128 | */ |
| 129 | void setNoDelay(bool noDelay); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 130 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 131 | /** |
| 132 | * Set the connect timeout |
| 133 | */ |
| 134 | void setConnTimeout(int ms); |
| 135 | |
| 136 | /** |
| 137 | * Set the receive timeout |
| 138 | */ |
| 139 | void setRecvTimeout(int ms); |
| 140 | |
| 141 | /** |
| 142 | * Set the send timeout |
| 143 | */ |
| 144 | void setSendTimeout(int ms); |
| 145 | |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 146 | /** |
| 147 | * Set the max number of recv retries in case of an EAGAIN |
| 148 | * error |
| 149 | */ |
| 150 | void setMaxRecvRetries(int maxRecvRetries); |
| 151 | |
Mark Slee | b455292 | 2007-11-28 00:12:11 +0000 | [diff] [blame] | 152 | /** |
| 153 | * Get socket information formated as a string <Host: x Port: x> |
| 154 | */ |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 155 | std::string getSocketInfo(); |
| 156 | |
Mark Slee | b455292 | 2007-11-28 00:12:11 +0000 | [diff] [blame] | 157 | /** |
| 158 | * Returns the DNS name of the host to which the socket is connected |
| 159 | */ |
| 160 | std::string getPeerHost(); |
| 161 | |
| 162 | /** |
| 163 | * Returns the address of the host to which the socket is connected |
| 164 | */ |
| 165 | std::string getPeerAddress(); |
| 166 | |
| 167 | /** |
| 168 | * Returns the port of the host to which the socket is connected |
| 169 | **/ |
| 170 | int getPeerPort(); |
| 171 | |
| 172 | |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 173 | protected: |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 174 | /** |
| 175 | * Constructor to create socket from raw UNIX handle. Never called directly |
| 176 | * but used by the TServerSocket class. |
| 177 | */ |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 178 | TSocket(int socket); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 179 | |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 180 | /** connect, called by open */ |
| 181 | void openConnection(struct addrinfo *res); |
| 182 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 183 | /** Host to connect to */ |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 184 | std::string host_; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 185 | |
Mark Slee | b455292 | 2007-11-28 00:12:11 +0000 | [diff] [blame] | 186 | /** Peer hostname */ |
| 187 | std::string peerHost_; |
| 188 | |
| 189 | /** Peer address */ |
| 190 | std::string peerAddress_; |
| 191 | |
| 192 | /** Peer port */ |
| 193 | int peerPort_; |
| 194 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 195 | /** Port number to connect on */ |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 196 | int port_; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 197 | |
| 198 | /** Underlying UNIX socket handle */ |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 199 | int socket_; |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 200 | |
| 201 | /** Connect timeout in ms */ |
| 202 | int connTimeout_; |
| 203 | |
| 204 | /** Send timeout in ms */ |
| 205 | int sendTimeout_; |
| 206 | |
| 207 | /** Recv timeout in ms */ |
| 208 | int recvTimeout_; |
| 209 | |
| 210 | /** Linger on */ |
| 211 | bool lingerOn_; |
Mark Slee | b455292 | 2007-11-28 00:12:11 +0000 | [diff] [blame] | 212 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 213 | /** Linger val */ |
| 214 | int lingerVal_; |
| 215 | |
| 216 | /** Nodelay */ |
| 217 | bool noDelay_; |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 218 | |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 219 | /** Recv EGAIN retries */ |
| 220 | int maxRecvRetries_; |
| 221 | |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 222 | /** Recv timeout timeval */ |
| 223 | struct timeval recvTimeval_; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 224 | }; |
| 225 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 226 | }}} // apache::thrift::transport |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 227 | |
| 228 | #endif // #ifndef _THRIFT_TRANSPORT_TSOCKET_H_ |
| 229 | |