blob: e89059f3412d806be5886c4c2301cf881413c9d1 [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 Sleee8540632006-05-30 09:24:40 +000038 public:
Mark Slee8d7e1f62006-06-07 06:48:56 +000039 /**
40 * Constructs a new socket. Note that this does NOT actually connect the
41 * socket.
42 *
Aditya Agarwalebc99e02007-01-15 23:14:58 +000043 */
44 TSocket();
Mark Sleeb4552922007-11-28 00:12:11 +000045
Aditya Agarwalebc99e02007-01-15 23:14:58 +000046 /**
47 * Constructs a new socket. Note that this does NOT actually connect the
48 * socket.
49 *
Mark Slee8d7e1f62006-06-07 06:48:56 +000050 * @param host An IP address or hostname to connect to
51 * @param port The port to connect on
52 */
Mark Sleee8540632006-05-30 09:24:40 +000053 TSocket(std::string host, int port);
Mark Slee8d7e1f62006-06-07 06:48:56 +000054
55 /**
Bryan Duxburya18364a2010-09-28 14:36:07 +000056 * Constructs a new Unix domain socket.
57 * Note that this does NOT actually connect the socket.
58 *
59 * @param path The Unix domain socket e.g. "/tmp/ThriftTest.binary.thrift"
60 */
61 TSocket(std::string path);
62
63 /**
Mark Slee8d7e1f62006-06-07 06:48:56 +000064 * Destroyes the socket object, closing it if necessary.
65 */
Mark Slee8a98e1b2007-02-27 05:16:23 +000066 virtual ~TSocket();
Mark Sleee8540632006-05-30 09:24:40 +000067
Mark Slee8d7e1f62006-06-07 06:48:56 +000068 /**
69 * Whether the socket is alive.
70 *
71 * @return Is the socket alive?
72 */
73 bool isOpen();
Mark Sleee8540632006-05-30 09:24:40 +000074
Mark Slee8d7e1f62006-06-07 06:48:56 +000075 /**
Mark Sleeb9ff32a2006-11-16 01:00:24 +000076 * Calls select on the socket to see if there is more data available.
77 */
78 bool peek();
79
80 /**
Mark Slee8d7e1f62006-06-07 06:48:56 +000081 * Creates and opens the UNIX socket.
82 *
83 * @throws TTransportException If the socket could not connect
84 */
jsobele02e4242007-05-08 17:51:49 +000085 virtual void open();
Mark Slee8d7e1f62006-06-07 06:48:56 +000086
87 /**
88 * Shuts down communications on the socket.
89 */
David Reiss450c2402010-03-09 05:20:26 +000090 virtual void close();
Mark Slee8d7e1f62006-06-07 06:48:56 +000091
92 /**
93 * Reads from the underlying socket.
94 */
95 uint32_t read(uint8_t* buf, uint32_t len);
96
97 /**
David Reiss105961d2010-10-06 17:10:17 +000098 * Writes to the underlying socket. Loops until done or fail.
Mark Slee8d7e1f62006-06-07 06:48:56 +000099 */
100 void write(const uint8_t* buf, uint32_t len);
101
102 /**
David Reiss105961d2010-10-06 17:10:17 +0000103 * Writes to the underlying socket. Does single send() and returns result.
104 */
105 uint32_t write_partial(const uint8_t* buf, uint32_t len);
106
107 /**
dweatherford14b0ed62007-10-19 01:03:32 +0000108 * Get the host that the socket is connected to
109 *
110 * @return string host identifier
111 */
112 std::string getHost();
113
114 /**
115 * Get the port that the socket is connected to
116 *
117 * @return int port number
118 */
119 int getPort();
120
121 /**
Aditya Agarwalebc99e02007-01-15 23:14:58 +0000122 * Set the host that socket will connect to
123 *
124 * @param host host identifier
125 */
126 void setHost(std::string host);
127
128 /**
129 * Set the port that socket will connect to
130 *
131 * @param port port number
132 */
133 void setPort(int port);
134
135 /**
Mark Slee8d7e1f62006-06-07 06:48:56 +0000136 * Controls whether the linger option is set on the socket.
137 *
138 * @param on Whether SO_LINGER is on
139 * @param linger If linger is active, the number of seconds to linger for
140 */
141 void setLinger(bool on, int linger);
142
143 /**
144 * Whether to enable/disable Nagle's algorithm.
145 *
146 * @param noDelay Whether or not to disable the algorithm.
Mark Sleeb4552922007-11-28 00:12:11 +0000147 * @return
Mark Slee8d7e1f62006-06-07 06:48:56 +0000148 */
149 void setNoDelay(bool noDelay);
Mark Sleee8540632006-05-30 09:24:40 +0000150
Mark Slee29050782006-09-29 00:12:30 +0000151 /**
152 * Set the connect timeout
153 */
154 void setConnTimeout(int ms);
155
156 /**
157 * Set the receive timeout
158 */
159 void setRecvTimeout(int ms);
160
161 /**
162 * Set the send timeout
163 */
164 void setSendTimeout(int ms);
165
Aditya Agarwale04475b2007-05-23 02:14:58 +0000166 /**
167 * Set the max number of recv retries in case of an EAGAIN
168 * error
169 */
170 void setMaxRecvRetries(int maxRecvRetries);
171
Mark Sleeb4552922007-11-28 00:12:11 +0000172 /**
173 * Get socket information formated as a string <Host: x Port: x>
174 */
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000175 std::string getSocketInfo();
176
Mark Sleeb4552922007-11-28 00:12:11 +0000177 /**
178 * Returns the DNS name of the host to which the socket is connected
179 */
180 std::string getPeerHost();
181
182 /**
183 * Returns the address of the host to which the socket is connected
184 */
185 std::string getPeerAddress();
186
187 /**
188 * Returns the port of the host to which the socket is connected
189 **/
190 int getPeerPort();
191
David Reiss1c20c872010-03-09 05:20:14 +0000192 /**
David Reiss23248712010-10-06 17:10:08 +0000193 * Returns the underlying socket file descriptor.
194 */
195 int getSocketFD() {
196 return socket_;
197 }
198
David Reiss105961d2010-10-06 17:10:17 +0000199 /**
200 * (Re-)initialize a TSocket for the supplied descriptor. This is only
201 * intended for use by TNonblockingServer -- other use may result in
202 * unfortunate surprises.
203 *
204 * @param fd the descriptor for an already-connected socket
205 */
206 void setSocketFD(int fd);
207
David Reiss23248712010-10-06 17:10:08 +0000208 /*
209 * Returns a cached copy of the peer address.
210 */
211 sockaddr* getCachedAddress(socklen_t* len) const;
212
213 /**
David Reiss1c20c872010-03-09 05:20:14 +0000214 * Sets whether to use a low minimum TCP retransmission timeout.
215 */
216 static void setUseLowMinRto(bool useLowMinRto);
217
218 /**
219 * Gets whether to use a low minimum TCP retransmission timeout.
220 */
221 static bool getUseLowMinRto();
Mark Sleeb4552922007-11-28 00:12:11 +0000222
Mark Slee8d7e1f62006-06-07 06:48:56 +0000223 /**
Bryan Duxbury010f1e02010-09-02 00:56:53 +0000224 * Constructor to create socket from raw UNIX handle.
Mark Slee8d7e1f62006-06-07 06:48:56 +0000225 */
Mark Sleee8540632006-05-30 09:24:40 +0000226 TSocket(int socket);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000227
David Reiss23248712010-10-06 17:10:08 +0000228 /**
229 * Set a cache of the peer address (used when trivially available: e.g.
230 * accept() or connect()). Only caches IPV4 and IPV6; unset for others.
231 */
232 void setCachedAddress(const sockaddr* addr, socklen_t len);
233
David Reiss105961d2010-10-06 17:10:17 +0000234 protected:
235 /** connect, called by open */
236 void openConnection(struct addrinfo *res);
237
Mark Slee8d7e1f62006-06-07 06:48:56 +0000238 /** Host to connect to */
Mark Sleee8540632006-05-30 09:24:40 +0000239 std::string host_;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000240
Mark Sleeb4552922007-11-28 00:12:11 +0000241 /** Peer hostname */
242 std::string peerHost_;
243
244 /** Peer address */
245 std::string peerAddress_;
246
247 /** Peer port */
248 int peerPort_;
249
Mark Slee8d7e1f62006-06-07 06:48:56 +0000250 /** Port number to connect on */
Mark Sleee8540632006-05-30 09:24:40 +0000251 int port_;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000252
Bryan Duxburya18364a2010-09-28 14:36:07 +0000253 /** UNIX domain socket path */
254 std::string path_;
255
Mark Slee8d7e1f62006-06-07 06:48:56 +0000256 /** Underlying UNIX socket handle */
Mark Sleee8540632006-05-30 09:24:40 +0000257 int socket_;
Mark Slee29050782006-09-29 00:12:30 +0000258
259 /** Connect timeout in ms */
260 int connTimeout_;
261
262 /** Send timeout in ms */
263 int sendTimeout_;
264
265 /** Recv timeout in ms */
266 int recvTimeout_;
267
268 /** Linger on */
269 bool lingerOn_;
Mark Sleeb4552922007-11-28 00:12:11 +0000270
Mark Slee29050782006-09-29 00:12:30 +0000271 /** Linger val */
272 int lingerVal_;
273
274 /** Nodelay */
275 bool noDelay_;
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000276
Aditya Agarwale04475b2007-05-23 02:14:58 +0000277 /** Recv EGAIN retries */
278 int maxRecvRetries_;
279
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000280 /** Recv timeout timeval */
281 struct timeval recvTimeval_;
David Reiss1c20c872010-03-09 05:20:14 +0000282
David Reiss23248712010-10-06 17:10:08 +0000283 /** Cached peer address */
284 union {
285 sockaddr_in ipv4;
286 sockaddr_in6 ipv6;
287 } cachedPeerAddr_;
288
289 /** Connection start time */
290 timespec startTime_;
291
David Reiss1c20c872010-03-09 05:20:14 +0000292 /** Whether to use low minimum TCP retransmission timeout */
293 static bool useLowMinRto_;
Bryan Duxburya18364a2010-09-28 14:36:07 +0000294
295 private:
296 void unix_open();
297 void local_open();
Mark Sleee8540632006-05-30 09:24:40 +0000298};
299
T Jake Lucianib5e62212009-01-31 22:36:20 +0000300}}} // apache::thrift::transport
Mark Sleef5f2be42006-09-05 21:05:31 +0000301
302#endif // #ifndef _THRIFT_TRANSPORT_TSOCKET_H_
303