blob: 47a702d1176faf24cf575cd2780f07e61f0f2eca [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
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 Slee9f0c6512007-02-28 23:58:26 +000019
Mark Sleef5f2be42006-09-05 21:05:31 +000020#ifndef _THRIFT_TRANSPORT_TSOCKET_H_
21#define _THRIFT_TRANSPORT_TSOCKET_H_ 1
Mark Sleee8540632006-05-30 09:24:40 +000022
23#include <string>
Mark Sleeb9ff32a2006-11-16 01:00:24 +000024#include <sys/time.h>
David Reiss1c20c872010-03-09 05:20:14 +000025#include <netdb.h>
Mark Sleee8540632006-05-30 09:24:40 +000026
Marc Slemkod42a2c22006-08-10 03:30:18 +000027#include "TTransport.h"
David Reisse879c2f2010-10-06 17:09:50 +000028#include "TVirtualTransport.h"
Marc Slemkod42a2c22006-08-10 03:30:18 +000029#include "TServerSocket.h"
Mark Sleee8540632006-05-30 09:24:40 +000030
T Jake Lucianib5e62212009-01-31 22:36:20 +000031namespace apache { namespace thrift { namespace transport {
Marc Slemko6f038a72006-08-03 18:58:09 +000032
Mark Sleee8540632006-05-30 09:24:40 +000033/**
34 * TCP Socket implementation of the TTransport interface.
35 *
Mark Sleee8540632006-05-30 09:24:40 +000036 */
David Reisse879c2f2010-10-06 17:09:50 +000037class TSocket : public TVirtualTransport<TSocket> {
Mark Slee8d7e1f62006-06-07 06:48:56 +000038 /**
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 Sleee8540632006-05-30 09:24:40 +000045
46 public:
Mark Slee8d7e1f62006-06-07 06:48:56 +000047 /**
48 * Constructs a new socket. Note that this does NOT actually connect the
49 * socket.
50 *
Aditya Agarwalebc99e02007-01-15 23:14:58 +000051 */
52 TSocket();
Mark Sleeb4552922007-11-28 00:12:11 +000053
Aditya Agarwalebc99e02007-01-15 23:14:58 +000054 /**
55 * Constructs a new socket. Note that this does NOT actually connect the
56 * socket.
57 *
Mark Slee8d7e1f62006-06-07 06:48:56 +000058 * @param host An IP address or hostname to connect to
59 * @param port The port to connect on
60 */
Mark Sleee8540632006-05-30 09:24:40 +000061 TSocket(std::string host, int port);
Mark Slee8d7e1f62006-06-07 06:48:56 +000062
63 /**
Bryan Duxburya18364a2010-09-28 14:36:07 +000064 * 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 Slee8d7e1f62006-06-07 06:48:56 +000072 * Destroyes the socket object, closing it if necessary.
73 */
Mark Slee8a98e1b2007-02-27 05:16:23 +000074 virtual ~TSocket();
Mark Sleee8540632006-05-30 09:24:40 +000075
Mark Slee8d7e1f62006-06-07 06:48:56 +000076 /**
77 * Whether the socket is alive.
78 *
79 * @return Is the socket alive?
80 */
81 bool isOpen();
Mark Sleee8540632006-05-30 09:24:40 +000082
Mark Slee8d7e1f62006-06-07 06:48:56 +000083 /**
Mark Sleeb9ff32a2006-11-16 01:00:24 +000084 * Calls select on the socket to see if there is more data available.
85 */
86 bool peek();
87
88 /**
Mark Slee8d7e1f62006-06-07 06:48:56 +000089 * Creates and opens the UNIX socket.
90 *
91 * @throws TTransportException If the socket could not connect
92 */
jsobele02e4242007-05-08 17:51:49 +000093 virtual void open();
Mark Slee8d7e1f62006-06-07 06:48:56 +000094
95 /**
96 * Shuts down communications on the socket.
97 */
David Reiss450c2402010-03-09 05:20:26 +000098 virtual void close();
Mark Slee8d7e1f62006-06-07 06:48:56 +000099
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 /**
dweatherford14b0ed62007-10-19 01:03:32 +0000111 * 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 Agarwalebc99e02007-01-15 23:14:58 +0000125 * 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 Slee8d7e1f62006-06-07 06:48:56 +0000139 * 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 Sleeb4552922007-11-28 00:12:11 +0000150 * @return
Mark Slee8d7e1f62006-06-07 06:48:56 +0000151 */
152 void setNoDelay(bool noDelay);
Mark Sleee8540632006-05-30 09:24:40 +0000153
Mark Slee29050782006-09-29 00:12:30 +0000154 /**
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 Agarwale04475b2007-05-23 02:14:58 +0000169 /**
170 * Set the max number of recv retries in case of an EAGAIN
171 * error
172 */
173 void setMaxRecvRetries(int maxRecvRetries);
174
Mark Sleeb4552922007-11-28 00:12:11 +0000175 /**
176 * Get socket information formated as a string <Host: x Port: x>
177 */
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000178 std::string getSocketInfo();
179
Mark Sleeb4552922007-11-28 00:12:11 +0000180 /**
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 Reiss1c20c872010-03-09 05:20:14 +0000195 /**
David Reiss23248712010-10-06 17:10:08 +0000196 * Returns the underlying socket file descriptor.
197 */
198 int getSocketFD() {
199 return socket_;
200 }
201
202 /*
203 * Returns a cached copy of the peer address.
204 */
205 sockaddr* getCachedAddress(socklen_t* len) const;
206
207 /**
David Reiss1c20c872010-03-09 05:20:14 +0000208 * Sets whether to use a low minimum TCP retransmission timeout.
209 */
210 static void setUseLowMinRto(bool useLowMinRto);
211
212 /**
213 * Gets whether to use a low minimum TCP retransmission timeout.
214 */
215 static bool getUseLowMinRto();
Mark Sleeb4552922007-11-28 00:12:11 +0000216
Mark Slee8d7e1f62006-06-07 06:48:56 +0000217 /**
Bryan Duxbury010f1e02010-09-02 00:56:53 +0000218 * Constructor to create socket from raw UNIX handle.
Mark Slee8d7e1f62006-06-07 06:48:56 +0000219 */
Mark Sleee8540632006-05-30 09:24:40 +0000220 TSocket(int socket);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000221
Bryan Duxbury010f1e02010-09-02 00:56:53 +0000222 protected:
Mark Slee6d56eb92007-07-06 22:28:15 +0000223 /** connect, called by open */
224 void openConnection(struct addrinfo *res);
225
David Reiss23248712010-10-06 17:10:08 +0000226 /**
227 * Set a cache of the peer address (used when trivially available: e.g.
228 * accept() or connect()). Only caches IPV4 and IPV6; unset for others.
229 */
230 void setCachedAddress(const sockaddr* addr, socklen_t len);
231
Mark Slee8d7e1f62006-06-07 06:48:56 +0000232 /** Host to connect to */
Mark Sleee8540632006-05-30 09:24:40 +0000233 std::string host_;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000234
Mark Sleeb4552922007-11-28 00:12:11 +0000235 /** Peer hostname */
236 std::string peerHost_;
237
238 /** Peer address */
239 std::string peerAddress_;
240
241 /** Peer port */
242 int peerPort_;
243
Mark Slee8d7e1f62006-06-07 06:48:56 +0000244 /** Port number to connect on */
Mark Sleee8540632006-05-30 09:24:40 +0000245 int port_;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000246
Bryan Duxburya18364a2010-09-28 14:36:07 +0000247 /** UNIX domain socket path */
248 std::string path_;
249
Mark Slee8d7e1f62006-06-07 06:48:56 +0000250 /** Underlying UNIX socket handle */
Mark Sleee8540632006-05-30 09:24:40 +0000251 int socket_;
Mark Slee29050782006-09-29 00:12:30 +0000252
253 /** Connect timeout in ms */
254 int connTimeout_;
255
256 /** Send timeout in ms */
257 int sendTimeout_;
258
259 /** Recv timeout in ms */
260 int recvTimeout_;
261
262 /** Linger on */
263 bool lingerOn_;
Mark Sleeb4552922007-11-28 00:12:11 +0000264
Mark Slee29050782006-09-29 00:12:30 +0000265 /** Linger val */
266 int lingerVal_;
267
268 /** Nodelay */
269 bool noDelay_;
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000270
Aditya Agarwale04475b2007-05-23 02:14:58 +0000271 /** Recv EGAIN retries */
272 int maxRecvRetries_;
273
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000274 /** Recv timeout timeval */
275 struct timeval recvTimeval_;
David Reiss1c20c872010-03-09 05:20:14 +0000276
David Reiss23248712010-10-06 17:10:08 +0000277 /** Cached peer address */
278 union {
279 sockaddr_in ipv4;
280 sockaddr_in6 ipv6;
281 } cachedPeerAddr_;
282
283 /** Connection start time */
284 timespec startTime_;
285
David Reiss1c20c872010-03-09 05:20:14 +0000286 /** Whether to use low minimum TCP retransmission timeout */
287 static bool useLowMinRto_;
Bryan Duxburya18364a2010-09-28 14:36:07 +0000288
289 private:
290 void unix_open();
291 void local_open();
Mark Sleee8540632006-05-30 09:24:40 +0000292};
293
T Jake Lucianib5e62212009-01-31 22:36:20 +0000294}}} // apache::thrift::transport
Mark Sleef5f2be42006-09-05 21:05:31 +0000295
296#endif // #ifndef _THRIFT_TRANSPORT_TSOCKET_H_
297