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