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