blob: 9d07522504a942fbb515616e41e3051ad340547d [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>
Roger Meier2fa9c312011-09-05 19:15:53 +000024
25#ifdef HAVE_SYS_TIME_H
Mark Sleeb9ff32a2006-11-16 01:00:24 +000026#include <sys/time.h>
Roger Meier2fa9c312011-09-05 19:15:53 +000027#endif
28#ifdef HAVE_NETDB_H
David Reiss1c20c872010-03-09 05:20:14 +000029#include <netdb.h>
Roger Meier2fa9c312011-09-05 19:15:53 +000030#endif
Mark Sleee8540632006-05-30 09:24:40 +000031
Marc Slemkod42a2c22006-08-10 03:30:18 +000032#include "TTransport.h"
David Reisse879c2f2010-10-06 17:09:50 +000033#include "TVirtualTransport.h"
Marc Slemkod42a2c22006-08-10 03:30:18 +000034#include "TServerSocket.h"
Mark Sleee8540632006-05-30 09:24:40 +000035
T Jake Lucianib5e62212009-01-31 22:36:20 +000036namespace apache { namespace thrift { namespace transport {
Marc Slemko6f038a72006-08-03 18:58:09 +000037
Mark Sleee8540632006-05-30 09:24:40 +000038/**
39 * TCP Socket implementation of the TTransport interface.
40 *
Mark Sleee8540632006-05-30 09:24:40 +000041 */
David Reisse879c2f2010-10-06 17:09:50 +000042class TSocket : public TVirtualTransport<TSocket> {
Mark Sleee8540632006-05-30 09:24:40 +000043 public:
Mark Slee8d7e1f62006-06-07 06:48:56 +000044 /**
45 * Constructs a new socket. Note that this does NOT actually connect the
46 * socket.
47 *
Aditya Agarwalebc99e02007-01-15 23:14:58 +000048 */
49 TSocket();
Mark Sleeb4552922007-11-28 00:12:11 +000050
Aditya Agarwalebc99e02007-01-15 23:14:58 +000051 /**
52 * Constructs a new socket. Note that this does NOT actually connect the
53 * socket.
54 *
Mark Slee8d7e1f62006-06-07 06:48:56 +000055 * @param host An IP address or hostname to connect to
56 * @param port The port to connect on
57 */
Mark Sleee8540632006-05-30 09:24:40 +000058 TSocket(std::string host, int port);
Mark Slee8d7e1f62006-06-07 06:48:56 +000059
60 /**
Bryan Duxburya18364a2010-09-28 14:36:07 +000061 * Constructs a new Unix domain socket.
62 * Note that this does NOT actually connect the socket.
63 *
64 * @param path The Unix domain socket e.g. "/tmp/ThriftTest.binary.thrift"
65 */
66 TSocket(std::string path);
67
68 /**
Mark Slee8d7e1f62006-06-07 06:48:56 +000069 * Destroyes the socket object, closing it if necessary.
70 */
Mark Slee8a98e1b2007-02-27 05:16:23 +000071 virtual ~TSocket();
Mark Sleee8540632006-05-30 09:24:40 +000072
Mark Slee8d7e1f62006-06-07 06:48:56 +000073 /**
74 * Whether the socket is alive.
75 *
76 * @return Is the socket alive?
77 */
Bryan Duxburycd9aea12011-02-22 18:12:06 +000078 virtual bool isOpen();
Mark Sleee8540632006-05-30 09:24:40 +000079
Mark Slee8d7e1f62006-06-07 06:48:56 +000080 /**
Mark Sleeb9ff32a2006-11-16 01:00:24 +000081 * Calls select on the socket to see if there is more data available.
82 */
Bryan Duxburycd9aea12011-02-22 18:12:06 +000083 virtual bool peek();
Mark Sleeb9ff32a2006-11-16 01:00:24 +000084
85 /**
Mark Slee8d7e1f62006-06-07 06:48:56 +000086 * Creates and opens the UNIX socket.
87 *
88 * @throws TTransportException If the socket could not connect
89 */
jsobele02e4242007-05-08 17:51:49 +000090 virtual void open();
Mark Slee8d7e1f62006-06-07 06:48:56 +000091
92 /**
93 * Shuts down communications on the socket.
94 */
David Reiss450c2402010-03-09 05:20:26 +000095 virtual void close();
Mark Slee8d7e1f62006-06-07 06:48:56 +000096
97 /**
98 * Reads from the underlying socket.
99 */
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000100 virtual uint32_t read(uint8_t* buf, uint32_t len);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000101
102 /**
David Reiss105961d2010-10-06 17:10:17 +0000103 * Writes to the underlying socket. Loops until done or fail.
Mark Slee8d7e1f62006-06-07 06:48:56 +0000104 */
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000105 virtual void write(const uint8_t* buf, uint32_t len);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000106
107 /**
David Reiss105961d2010-10-06 17:10:17 +0000108 * Writes to the underlying socket. Does single send() and returns result.
109 */
110 uint32_t write_partial(const uint8_t* buf, uint32_t len);
111
112 /**
dweatherford14b0ed62007-10-19 01:03:32 +0000113 * Get the host that the socket is connected to
114 *
115 * @return string host identifier
116 */
117 std::string getHost();
118
119 /**
120 * Get the port that the socket is connected to
121 *
122 * @return int port number
123 */
124 int getPort();
125
126 /**
Aditya Agarwalebc99e02007-01-15 23:14:58 +0000127 * Set the host that socket will connect to
128 *
129 * @param host host identifier
130 */
131 void setHost(std::string host);
132
133 /**
134 * Set the port that socket will connect to
135 *
136 * @param port port number
137 */
138 void setPort(int port);
139
140 /**
Mark Slee8d7e1f62006-06-07 06:48:56 +0000141 * Controls whether the linger option is set on the socket.
142 *
143 * @param on Whether SO_LINGER is on
144 * @param linger If linger is active, the number of seconds to linger for
145 */
146 void setLinger(bool on, int linger);
147
148 /**
149 * Whether to enable/disable Nagle's algorithm.
150 *
151 * @param noDelay Whether or not to disable the algorithm.
Mark Sleeb4552922007-11-28 00:12:11 +0000152 * @return
Mark Slee8d7e1f62006-06-07 06:48:56 +0000153 */
154 void setNoDelay(bool noDelay);
Mark Sleee8540632006-05-30 09:24:40 +0000155
Mark Slee29050782006-09-29 00:12:30 +0000156 /**
157 * Set the connect timeout
158 */
159 void setConnTimeout(int ms);
160
161 /**
162 * Set the receive timeout
163 */
164 void setRecvTimeout(int ms);
165
166 /**
167 * Set the send timeout
168 */
169 void setSendTimeout(int ms);
170
Aditya Agarwale04475b2007-05-23 02:14:58 +0000171 /**
172 * Set the max number of recv retries in case of an EAGAIN
173 * error
174 */
175 void setMaxRecvRetries(int maxRecvRetries);
176
Mark Sleeb4552922007-11-28 00:12:11 +0000177 /**
178 * Get socket information formated as a string <Host: x Port: x>
179 */
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000180 std::string getSocketInfo();
181
Mark Sleeb4552922007-11-28 00:12:11 +0000182 /**
183 * Returns the DNS name of the host to which the socket is connected
184 */
185 std::string getPeerHost();
186
187 /**
188 * Returns the address of the host to which the socket is connected
189 */
190 std::string getPeerAddress();
191
192 /**
193 * Returns the port of the host to which the socket is connected
194 **/
195 int getPeerPort();
196
David Reiss1c20c872010-03-09 05:20:14 +0000197 /**
David Reiss23248712010-10-06 17:10:08 +0000198 * Returns the underlying socket file descriptor.
199 */
200 int getSocketFD() {
201 return socket_;
202 }
203
David Reiss105961d2010-10-06 17:10:17 +0000204 /**
205 * (Re-)initialize a TSocket for the supplied descriptor. This is only
206 * intended for use by TNonblockingServer -- other use may result in
207 * unfortunate surprises.
208 *
209 * @param fd the descriptor for an already-connected socket
210 */
211 void setSocketFD(int fd);
212
David Reiss23248712010-10-06 17:10:08 +0000213 /*
214 * Returns a cached copy of the peer address.
215 */
216 sockaddr* getCachedAddress(socklen_t* len) const;
217
218 /**
David Reiss1c20c872010-03-09 05:20:14 +0000219 * Sets whether to use a low minimum TCP retransmission timeout.
220 */
221 static void setUseLowMinRto(bool useLowMinRto);
222
223 /**
224 * Gets whether to use a low minimum TCP retransmission timeout.
225 */
226 static bool getUseLowMinRto();
Mark Sleeb4552922007-11-28 00:12:11 +0000227
Mark Slee8d7e1f62006-06-07 06:48:56 +0000228 /**
Bryan Duxbury010f1e02010-09-02 00:56:53 +0000229 * Constructor to create socket from raw UNIX handle.
Mark Slee8d7e1f62006-06-07 06:48:56 +0000230 */
Mark Sleee8540632006-05-30 09:24:40 +0000231 TSocket(int socket);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000232
David Reiss23248712010-10-06 17:10:08 +0000233 /**
234 * Set a cache of the peer address (used when trivially available: e.g.
235 * accept() or connect()). Only caches IPV4 and IPV6; unset for others.
236 */
237 void setCachedAddress(const sockaddr* addr, socklen_t len);
238
David Reiss105961d2010-10-06 17:10:17 +0000239 protected:
240 /** connect, called by open */
241 void openConnection(struct addrinfo *res);
242
Mark Slee8d7e1f62006-06-07 06:48:56 +0000243 /** Host to connect to */
Mark Sleee8540632006-05-30 09:24:40 +0000244 std::string host_;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000245
Mark Sleeb4552922007-11-28 00:12:11 +0000246 /** Peer hostname */
247 std::string peerHost_;
248
249 /** Peer address */
250 std::string peerAddress_;
251
252 /** Peer port */
253 int peerPort_;
254
Mark Slee8d7e1f62006-06-07 06:48:56 +0000255 /** Port number to connect on */
Mark Sleee8540632006-05-30 09:24:40 +0000256 int port_;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000257
Bryan Duxburya18364a2010-09-28 14:36:07 +0000258 /** UNIX domain socket path */
259 std::string path_;
260
Mark Slee8d7e1f62006-06-07 06:48:56 +0000261 /** Underlying UNIX socket handle */
Mark Sleee8540632006-05-30 09:24:40 +0000262 int socket_;
Mark Slee29050782006-09-29 00:12:30 +0000263
264 /** Connect timeout in ms */
265 int connTimeout_;
266
267 /** Send timeout in ms */
268 int sendTimeout_;
269
270 /** Recv timeout in ms */
271 int recvTimeout_;
272
273 /** Linger on */
274 bool lingerOn_;
Mark Sleeb4552922007-11-28 00:12:11 +0000275
Mark Slee29050782006-09-29 00:12:30 +0000276 /** Linger val */
277 int lingerVal_;
278
279 /** Nodelay */
280 bool noDelay_;
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000281
Aditya Agarwale04475b2007-05-23 02:14:58 +0000282 /** Recv EGAIN retries */
283 int maxRecvRetries_;
284
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000285 /** Recv timeout timeval */
286 struct timeval recvTimeval_;
David Reiss1c20c872010-03-09 05:20:14 +0000287
David Reiss23248712010-10-06 17:10:08 +0000288 /** Cached peer address */
289 union {
290 sockaddr_in ipv4;
291 sockaddr_in6 ipv6;
292 } cachedPeerAddr_;
293
294 /** Connection start time */
295 timespec startTime_;
296
David Reiss1c20c872010-03-09 05:20:14 +0000297 /** Whether to use low minimum TCP retransmission timeout */
298 static bool useLowMinRto_;
Bryan Duxburya18364a2010-09-28 14:36:07 +0000299
300 private:
301 void unix_open();
302 void local_open();
Mark Sleee8540632006-05-30 09:24:40 +0000303};
304
T Jake Lucianib5e62212009-01-31 22:36:20 +0000305}}} // apache::thrift::transport
Mark Sleef5f2be42006-09-05 21:05:31 +0000306
307#endif // #ifndef _THRIFT_TRANSPORT_TSOCKET_H_
308