blob: 97562c2404977c48b201c7a290fce7fbb660f4ec [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 /**
98 * Writes to the underlying socket.
99 */
100 void write(const uint8_t* buf, uint32_t len);
101
102 /**
dweatherford14b0ed62007-10-19 01:03:32 +0000103 * Get the host that the socket is connected to
104 *
105 * @return string host identifier
106 */
107 std::string getHost();
108
109 /**
110 * Get the port that the socket is connected to
111 *
112 * @return int port number
113 */
114 int getPort();
115
116 /**
Aditya Agarwalebc99e02007-01-15 23:14:58 +0000117 * Set the host that socket will connect to
118 *
119 * @param host host identifier
120 */
121 void setHost(std::string host);
122
123 /**
124 * Set the port that socket will connect to
125 *
126 * @param port port number
127 */
128 void setPort(int port);
129
130 /**
Mark Slee8d7e1f62006-06-07 06:48:56 +0000131 * Controls whether the linger option is set on the socket.
132 *
133 * @param on Whether SO_LINGER is on
134 * @param linger If linger is active, the number of seconds to linger for
135 */
136 void setLinger(bool on, int linger);
137
138 /**
139 * Whether to enable/disable Nagle's algorithm.
140 *
141 * @param noDelay Whether or not to disable the algorithm.
Mark Sleeb4552922007-11-28 00:12:11 +0000142 * @return
Mark Slee8d7e1f62006-06-07 06:48:56 +0000143 */
144 void setNoDelay(bool noDelay);
Mark Sleee8540632006-05-30 09:24:40 +0000145
Mark Slee29050782006-09-29 00:12:30 +0000146 /**
147 * Set the connect timeout
148 */
149 void setConnTimeout(int ms);
150
151 /**
152 * Set the receive timeout
153 */
154 void setRecvTimeout(int ms);
155
156 /**
157 * Set the send timeout
158 */
159 void setSendTimeout(int ms);
160
Aditya Agarwale04475b2007-05-23 02:14:58 +0000161 /**
162 * Set the max number of recv retries in case of an EAGAIN
163 * error
164 */
165 void setMaxRecvRetries(int maxRecvRetries);
166
Mark Sleeb4552922007-11-28 00:12:11 +0000167 /**
168 * Get socket information formated as a string <Host: x Port: x>
169 */
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000170 std::string getSocketInfo();
171
Mark Sleeb4552922007-11-28 00:12:11 +0000172 /**
173 * Returns the DNS name of the host to which the socket is connected
174 */
175 std::string getPeerHost();
176
177 /**
178 * Returns the address of the host to which the socket is connected
179 */
180 std::string getPeerAddress();
181
182 /**
183 * Returns the port of the host to which the socket is connected
184 **/
185 int getPeerPort();
186
David Reiss1c20c872010-03-09 05:20:14 +0000187 /**
David Reiss23248712010-10-06 17:10:08 +0000188 * Returns the underlying socket file descriptor.
189 */
190 int getSocketFD() {
191 return socket_;
192 }
193
194 /*
195 * Returns a cached copy of the peer address.
196 */
197 sockaddr* getCachedAddress(socklen_t* len) const;
198
199 /**
David Reiss1c20c872010-03-09 05:20:14 +0000200 * Sets whether to use a low minimum TCP retransmission timeout.
201 */
202 static void setUseLowMinRto(bool useLowMinRto);
203
204 /**
205 * Gets whether to use a low minimum TCP retransmission timeout.
206 */
207 static bool getUseLowMinRto();
Mark Sleeb4552922007-11-28 00:12:11 +0000208
Mark Slee8d7e1f62006-06-07 06:48:56 +0000209 /**
Bryan Duxbury010f1e02010-09-02 00:56:53 +0000210 * Constructor to create socket from raw UNIX handle.
Mark Slee8d7e1f62006-06-07 06:48:56 +0000211 */
Mark Sleee8540632006-05-30 09:24:40 +0000212 TSocket(int socket);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000213
Bryan Duxbury010f1e02010-09-02 00:56:53 +0000214 protected:
Mark Slee6d56eb92007-07-06 22:28:15 +0000215 /** connect, called by open */
216 void openConnection(struct addrinfo *res);
217
David Reiss23248712010-10-06 17:10:08 +0000218 /**
219 * Set a cache of the peer address (used when trivially available: e.g.
220 * accept() or connect()). Only caches IPV4 and IPV6; unset for others.
221 */
222 void setCachedAddress(const sockaddr* addr, socklen_t len);
223
Mark Slee8d7e1f62006-06-07 06:48:56 +0000224 /** Host to connect to */
Mark Sleee8540632006-05-30 09:24:40 +0000225 std::string host_;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000226
Mark Sleeb4552922007-11-28 00:12:11 +0000227 /** Peer hostname */
228 std::string peerHost_;
229
230 /** Peer address */
231 std::string peerAddress_;
232
233 /** Peer port */
234 int peerPort_;
235
Mark Slee8d7e1f62006-06-07 06:48:56 +0000236 /** Port number to connect on */
Mark Sleee8540632006-05-30 09:24:40 +0000237 int port_;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000238
Bryan Duxburya18364a2010-09-28 14:36:07 +0000239 /** UNIX domain socket path */
240 std::string path_;
241
Mark Slee8d7e1f62006-06-07 06:48:56 +0000242 /** Underlying UNIX socket handle */
Mark Sleee8540632006-05-30 09:24:40 +0000243 int socket_;
Mark Slee29050782006-09-29 00:12:30 +0000244
245 /** Connect timeout in ms */
246 int connTimeout_;
247
248 /** Send timeout in ms */
249 int sendTimeout_;
250
251 /** Recv timeout in ms */
252 int recvTimeout_;
253
254 /** Linger on */
255 bool lingerOn_;
Mark Sleeb4552922007-11-28 00:12:11 +0000256
Mark Slee29050782006-09-29 00:12:30 +0000257 /** Linger val */
258 int lingerVal_;
259
260 /** Nodelay */
261 bool noDelay_;
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000262
Aditya Agarwale04475b2007-05-23 02:14:58 +0000263 /** Recv EGAIN retries */
264 int maxRecvRetries_;
265
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000266 /** Recv timeout timeval */
267 struct timeval recvTimeval_;
David Reiss1c20c872010-03-09 05:20:14 +0000268
David Reiss23248712010-10-06 17:10:08 +0000269 /** Cached peer address */
270 union {
271 sockaddr_in ipv4;
272 sockaddr_in6 ipv6;
273 } cachedPeerAddr_;
274
275 /** Connection start time */
276 timespec startTime_;
277
David Reiss1c20c872010-03-09 05:20:14 +0000278 /** Whether to use low minimum TCP retransmission timeout */
279 static bool useLowMinRto_;
Bryan Duxburya18364a2010-09-28 14:36:07 +0000280
281 private:
282 void unix_open();
283 void local_open();
Mark Sleee8540632006-05-30 09:24:40 +0000284};
285
T Jake Lucianib5e62212009-01-31 22:36:20 +0000286}}} // apache::thrift::transport
Mark Sleef5f2be42006-09-05 21:05:31 +0000287
288#endif // #ifndef _THRIFT_TRANSPORT_TSOCKET_H_
289