David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
Mark Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 19 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 20 | #ifndef _THRIFT_TRANSPORT_TSOCKET_H_ |
| 21 | #define _THRIFT_TRANSPORT_TSOCKET_H_ 1 |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 22 | |
| 23 | #include <string> |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 24 | #include <sys/time.h> |
David Reiss | 1c20c87 | 2010-03-09 05:20:14 +0000 | [diff] [blame] | 25 | #include <netdb.h> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 26 | |
Marc Slemko | d42a2c2 | 2006-08-10 03:30:18 +0000 | [diff] [blame] | 27 | #include "TTransport.h" |
David Reiss | e879c2f | 2010-10-06 17:09:50 +0000 | [diff] [blame] | 28 | #include "TVirtualTransport.h" |
Marc Slemko | d42a2c2 | 2006-08-10 03:30:18 +0000 | [diff] [blame] | 29 | #include "TServerSocket.h" |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 30 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 31 | namespace apache { namespace thrift { namespace transport { |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 32 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 33 | /** |
| 34 | * TCP Socket implementation of the TTransport interface. |
| 35 | * |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 36 | */ |
David Reiss | e879c2f | 2010-10-06 17:09:50 +0000 | [diff] [blame] | 37 | class TSocket : public TVirtualTransport<TSocket> { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 38 | public: |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 39 | /** |
| 40 | * Constructs a new socket. Note that this does NOT actually connect the |
| 41 | * socket. |
| 42 | * |
Aditya Agarwal | ebc99e0 | 2007-01-15 23:14:58 +0000 | [diff] [blame] | 43 | */ |
| 44 | TSocket(); |
Mark Slee | b455292 | 2007-11-28 00:12:11 +0000 | [diff] [blame] | 45 | |
Aditya Agarwal | ebc99e0 | 2007-01-15 23:14:58 +0000 | [diff] [blame] | 46 | /** |
| 47 | * Constructs a new socket. Note that this does NOT actually connect the |
| 48 | * socket. |
| 49 | * |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 50 | * @param host An IP address or hostname to connect to |
| 51 | * @param port The port to connect on |
| 52 | */ |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 53 | TSocket(std::string host, int port); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 54 | |
| 55 | /** |
Bryan Duxbury | a18364a | 2010-09-28 14:36:07 +0000 | [diff] [blame] | 56 | * Constructs a new Unix domain socket. |
| 57 | * Note that this does NOT actually connect the socket. |
| 58 | * |
| 59 | * @param path The Unix domain socket e.g. "/tmp/ThriftTest.binary.thrift" |
| 60 | */ |
| 61 | TSocket(std::string path); |
| 62 | |
| 63 | /** |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 64 | * Destroyes the socket object, closing it if necessary. |
| 65 | */ |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 66 | virtual ~TSocket(); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 67 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 68 | /** |
| 69 | * Whether the socket is alive. |
| 70 | * |
| 71 | * @return Is the socket alive? |
| 72 | */ |
| 73 | bool isOpen(); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 74 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 75 | /** |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 76 | * Calls select on the socket to see if there is more data available. |
| 77 | */ |
| 78 | bool peek(); |
| 79 | |
| 80 | /** |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 81 | * Creates and opens the UNIX socket. |
| 82 | * |
| 83 | * @throws TTransportException If the socket could not connect |
| 84 | */ |
jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 85 | virtual void open(); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 86 | |
| 87 | /** |
| 88 | * Shuts down communications on the socket. |
| 89 | */ |
David Reiss | 450c240 | 2010-03-09 05:20:26 +0000 | [diff] [blame] | 90 | virtual void close(); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 91 | |
| 92 | /** |
| 93 | * Reads from the underlying socket. |
| 94 | */ |
| 95 | uint32_t read(uint8_t* buf, uint32_t len); |
| 96 | |
| 97 | /** |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 98 | * Writes to the underlying socket. Loops until done or fail. |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 99 | */ |
| 100 | void write(const uint8_t* buf, uint32_t len); |
| 101 | |
| 102 | /** |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 103 | * Writes to the underlying socket. Does single send() and returns result. |
| 104 | */ |
| 105 | uint32_t write_partial(const uint8_t* buf, uint32_t len); |
| 106 | |
| 107 | /** |
dweatherford | 14b0ed6 | 2007-10-19 01:03:32 +0000 | [diff] [blame] | 108 | * Get the host that the socket is connected to |
| 109 | * |
| 110 | * @return string host identifier |
| 111 | */ |
| 112 | std::string getHost(); |
| 113 | |
| 114 | /** |
| 115 | * Get the port that the socket is connected to |
| 116 | * |
| 117 | * @return int port number |
| 118 | */ |
| 119 | int getPort(); |
| 120 | |
| 121 | /** |
Aditya Agarwal | ebc99e0 | 2007-01-15 23:14:58 +0000 | [diff] [blame] | 122 | * Set the host that socket will connect to |
| 123 | * |
| 124 | * @param host host identifier |
| 125 | */ |
| 126 | void setHost(std::string host); |
| 127 | |
| 128 | /** |
| 129 | * Set the port that socket will connect to |
| 130 | * |
| 131 | * @param port port number |
| 132 | */ |
| 133 | void setPort(int port); |
| 134 | |
| 135 | /** |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 136 | * Controls whether the linger option is set on the socket. |
| 137 | * |
| 138 | * @param on Whether SO_LINGER is on |
| 139 | * @param linger If linger is active, the number of seconds to linger for |
| 140 | */ |
| 141 | void setLinger(bool on, int linger); |
| 142 | |
| 143 | /** |
| 144 | * Whether to enable/disable Nagle's algorithm. |
| 145 | * |
| 146 | * @param noDelay Whether or not to disable the algorithm. |
Mark Slee | b455292 | 2007-11-28 00:12:11 +0000 | [diff] [blame] | 147 | * @return |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 148 | */ |
| 149 | void setNoDelay(bool noDelay); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 150 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 151 | /** |
| 152 | * Set the connect timeout |
| 153 | */ |
| 154 | void setConnTimeout(int ms); |
| 155 | |
| 156 | /** |
| 157 | * Set the receive timeout |
| 158 | */ |
| 159 | void setRecvTimeout(int ms); |
| 160 | |
| 161 | /** |
| 162 | * Set the send timeout |
| 163 | */ |
| 164 | void setSendTimeout(int ms); |
| 165 | |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 166 | /** |
| 167 | * Set the max number of recv retries in case of an EAGAIN |
| 168 | * error |
| 169 | */ |
| 170 | void setMaxRecvRetries(int maxRecvRetries); |
| 171 | |
Mark Slee | b455292 | 2007-11-28 00:12:11 +0000 | [diff] [blame] | 172 | /** |
| 173 | * Get socket information formated as a string <Host: x Port: x> |
| 174 | */ |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 175 | std::string getSocketInfo(); |
| 176 | |
Mark Slee | b455292 | 2007-11-28 00:12:11 +0000 | [diff] [blame] | 177 | /** |
| 178 | * Returns the DNS name of the host to which the socket is connected |
| 179 | */ |
| 180 | std::string getPeerHost(); |
| 181 | |
| 182 | /** |
| 183 | * Returns the address of the host to which the socket is connected |
| 184 | */ |
| 185 | std::string getPeerAddress(); |
| 186 | |
| 187 | /** |
| 188 | * Returns the port of the host to which the socket is connected |
| 189 | **/ |
| 190 | int getPeerPort(); |
| 191 | |
David Reiss | 1c20c87 | 2010-03-09 05:20:14 +0000 | [diff] [blame] | 192 | /** |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 193 | * Returns the underlying socket file descriptor. |
| 194 | */ |
| 195 | int getSocketFD() { |
| 196 | return socket_; |
| 197 | } |
| 198 | |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 199 | /** |
| 200 | * (Re-)initialize a TSocket for the supplied descriptor. This is only |
| 201 | * intended for use by TNonblockingServer -- other use may result in |
| 202 | * unfortunate surprises. |
| 203 | * |
| 204 | * @param fd the descriptor for an already-connected socket |
| 205 | */ |
| 206 | void setSocketFD(int fd); |
| 207 | |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 208 | /* |
| 209 | * Returns a cached copy of the peer address. |
| 210 | */ |
| 211 | sockaddr* getCachedAddress(socklen_t* len) const; |
| 212 | |
| 213 | /** |
David Reiss | 1c20c87 | 2010-03-09 05:20:14 +0000 | [diff] [blame] | 214 | * Sets whether to use a low minimum TCP retransmission timeout. |
| 215 | */ |
| 216 | static void setUseLowMinRto(bool useLowMinRto); |
| 217 | |
| 218 | /** |
| 219 | * Gets whether to use a low minimum TCP retransmission timeout. |
| 220 | */ |
| 221 | static bool getUseLowMinRto(); |
Mark Slee | b455292 | 2007-11-28 00:12:11 +0000 | [diff] [blame] | 222 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 223 | /** |
Bryan Duxbury | 010f1e0 | 2010-09-02 00:56:53 +0000 | [diff] [blame] | 224 | * Constructor to create socket from raw UNIX handle. |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 225 | */ |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 226 | TSocket(int socket); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 227 | |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 228 | /** |
| 229 | * Set a cache of the peer address (used when trivially available: e.g. |
| 230 | * accept() or connect()). Only caches IPV4 and IPV6; unset for others. |
| 231 | */ |
| 232 | void setCachedAddress(const sockaddr* addr, socklen_t len); |
| 233 | |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 234 | protected: |
| 235 | /** connect, called by open */ |
| 236 | void openConnection(struct addrinfo *res); |
| 237 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 238 | /** Host to connect to */ |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 239 | std::string host_; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 240 | |
Mark Slee | b455292 | 2007-11-28 00:12:11 +0000 | [diff] [blame] | 241 | /** Peer hostname */ |
| 242 | std::string peerHost_; |
| 243 | |
| 244 | /** Peer address */ |
| 245 | std::string peerAddress_; |
| 246 | |
| 247 | /** Peer port */ |
| 248 | int peerPort_; |
| 249 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 250 | /** Port number to connect on */ |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 251 | int port_; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 252 | |
Bryan Duxbury | a18364a | 2010-09-28 14:36:07 +0000 | [diff] [blame] | 253 | /** UNIX domain socket path */ |
| 254 | std::string path_; |
| 255 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 256 | /** Underlying UNIX socket handle */ |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 257 | int socket_; |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 258 | |
| 259 | /** Connect timeout in ms */ |
| 260 | int connTimeout_; |
| 261 | |
| 262 | /** Send timeout in ms */ |
| 263 | int sendTimeout_; |
| 264 | |
| 265 | /** Recv timeout in ms */ |
| 266 | int recvTimeout_; |
| 267 | |
| 268 | /** Linger on */ |
| 269 | bool lingerOn_; |
Mark Slee | b455292 | 2007-11-28 00:12:11 +0000 | [diff] [blame] | 270 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 271 | /** Linger val */ |
| 272 | int lingerVal_; |
| 273 | |
| 274 | /** Nodelay */ |
| 275 | bool noDelay_; |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 276 | |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 277 | /** Recv EGAIN retries */ |
| 278 | int maxRecvRetries_; |
| 279 | |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 280 | /** Recv timeout timeval */ |
| 281 | struct timeval recvTimeval_; |
David Reiss | 1c20c87 | 2010-03-09 05:20:14 +0000 | [diff] [blame] | 282 | |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 283 | /** Cached peer address */ |
| 284 | union { |
| 285 | sockaddr_in ipv4; |
| 286 | sockaddr_in6 ipv6; |
| 287 | } cachedPeerAddr_; |
| 288 | |
| 289 | /** Connection start time */ |
| 290 | timespec startTime_; |
| 291 | |
David Reiss | 1c20c87 | 2010-03-09 05:20:14 +0000 | [diff] [blame] | 292 | /** Whether to use low minimum TCP retransmission timeout */ |
| 293 | static bool useLowMinRto_; |
Bryan Duxbury | a18364a | 2010-09-28 14:36:07 +0000 | [diff] [blame] | 294 | |
| 295 | private: |
| 296 | void unix_open(); |
| 297 | void local_open(); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 298 | }; |
| 299 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 300 | }}} // apache::thrift::transport |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 301 | |
| 302 | #endif // #ifndef _THRIFT_TRANSPORT_TSOCKET_H_ |
| 303 | |