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