blob: f69a9a1ac0be4c2ee738d1870940be58ce4f3d06 [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"
28#include "TServerSocket.h"
Mark Sleee8540632006-05-30 09:24:40 +000029
T Jake Lucianib5e62212009-01-31 22:36:20 +000030namespace apache { namespace thrift { namespace transport {
Marc Slemko6f038a72006-08-03 18:58:09 +000031
Mark Sleee8540632006-05-30 09:24:40 +000032/**
33 * TCP Socket implementation of the TTransport interface.
34 *
Mark Sleee8540632006-05-30 09:24:40 +000035 */
36class TSocket : public TTransport {
Mark Slee8d7e1f62006-06-07 06:48:56 +000037 /**
38 * We allow the TServerSocket acceptImpl() method to access the private
39 * members of a socket so that it can access the TSocket(int socket)
40 * constructor which creates a socket object from the raw UNIX socket
41 * handle.
42 */
43 friend class TServerSocket;
Mark Sleee8540632006-05-30 09:24:40 +000044
45 public:
Mark Slee8d7e1f62006-06-07 06:48:56 +000046 /**
47 * Constructs a new socket. Note that this does NOT actually connect the
48 * socket.
49 *
Aditya Agarwalebc99e02007-01-15 23:14:58 +000050 */
51 TSocket();
Mark Sleeb4552922007-11-28 00:12:11 +000052
Aditya Agarwalebc99e02007-01-15 23:14:58 +000053 /**
54 * Constructs a new socket. Note that this does NOT actually connect the
55 * socket.
56 *
Mark Slee8d7e1f62006-06-07 06:48:56 +000057 * @param host An IP address or hostname to connect to
58 * @param port The port to connect on
59 */
Mark Sleee8540632006-05-30 09:24:40 +000060 TSocket(std::string host, int port);
Mark Slee8d7e1f62006-06-07 06:48:56 +000061
62 /**
Bryan Duxburya18364a2010-09-28 14:36:07 +000063 * Constructs a new Unix domain socket.
64 * Note that this does NOT actually connect the socket.
65 *
66 * @param path The Unix domain socket e.g. "/tmp/ThriftTest.binary.thrift"
67 */
68 TSocket(std::string path);
69
70 /**
Mark Slee8d7e1f62006-06-07 06:48:56 +000071 * Destroyes the socket object, closing it if necessary.
72 */
Mark Slee8a98e1b2007-02-27 05:16:23 +000073 virtual ~TSocket();
Mark Sleee8540632006-05-30 09:24:40 +000074
Mark Slee8d7e1f62006-06-07 06:48:56 +000075 /**
76 * Whether the socket is alive.
77 *
78 * @return Is the socket alive?
79 */
80 bool isOpen();
Mark Sleee8540632006-05-30 09:24:40 +000081
Mark Slee8d7e1f62006-06-07 06:48:56 +000082 /**
Mark Sleeb9ff32a2006-11-16 01:00:24 +000083 * Calls select on the socket to see if there is more data available.
84 */
85 bool peek();
86
87 /**
Mark Slee8d7e1f62006-06-07 06:48:56 +000088 * Creates and opens the UNIX socket.
89 *
90 * @throws TTransportException If the socket could not connect
91 */
jsobele02e4242007-05-08 17:51:49 +000092 virtual void open();
Mark Slee8d7e1f62006-06-07 06:48:56 +000093
94 /**
95 * Shuts down communications on the socket.
96 */
David Reiss450c2402010-03-09 05:20:26 +000097 virtual void close();
Mark Slee8d7e1f62006-06-07 06:48:56 +000098
99 /**
100 * Reads from the underlying socket.
101 */
102 uint32_t read(uint8_t* buf, uint32_t len);
103
104 /**
105 * Writes to the underlying socket.
106 */
107 void write(const uint8_t* buf, uint32_t len);
108
109 /**
dweatherford14b0ed62007-10-19 01:03:32 +0000110 * Get the host that the socket is connected to
111 *
112 * @return string host identifier
113 */
114 std::string getHost();
115
116 /**
117 * Get the port that the socket is connected to
118 *
119 * @return int port number
120 */
121 int getPort();
122
123 /**
Aditya Agarwalebc99e02007-01-15 23:14:58 +0000124 * Set the host that socket will connect to
125 *
126 * @param host host identifier
127 */
128 void setHost(std::string host);
129
130 /**
131 * Set the port that socket will connect to
132 *
133 * @param port port number
134 */
135 void setPort(int port);
136
137 /**
Mark Slee8d7e1f62006-06-07 06:48:56 +0000138 * Controls whether the linger option is set on the socket.
139 *
140 * @param on Whether SO_LINGER is on
141 * @param linger If linger is active, the number of seconds to linger for
142 */
143 void setLinger(bool on, int linger);
144
145 /**
146 * Whether to enable/disable Nagle's algorithm.
147 *
148 * @param noDelay Whether or not to disable the algorithm.
Mark Sleeb4552922007-11-28 00:12:11 +0000149 * @return
Mark Slee8d7e1f62006-06-07 06:48:56 +0000150 */
151 void setNoDelay(bool noDelay);
Mark Sleee8540632006-05-30 09:24:40 +0000152
Mark Slee29050782006-09-29 00:12:30 +0000153 /**
154 * Set the connect timeout
155 */
156 void setConnTimeout(int ms);
157
158 /**
159 * Set the receive timeout
160 */
161 void setRecvTimeout(int ms);
162
163 /**
164 * Set the send timeout
165 */
166 void setSendTimeout(int ms);
167
Aditya Agarwale04475b2007-05-23 02:14:58 +0000168 /**
169 * Set the max number of recv retries in case of an EAGAIN
170 * error
171 */
172 void setMaxRecvRetries(int maxRecvRetries);
173
Mark Sleeb4552922007-11-28 00:12:11 +0000174 /**
175 * Get socket information formated as a string <Host: x Port: x>
176 */
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000177 std::string getSocketInfo();
178
Mark Sleeb4552922007-11-28 00:12:11 +0000179 /**
180 * Returns the DNS name of the host to which the socket is connected
181 */
182 std::string getPeerHost();
183
184 /**
185 * Returns the address of the host to which the socket is connected
186 */
187 std::string getPeerAddress();
188
189 /**
190 * Returns the port of the host to which the socket is connected
191 **/
192 int getPeerPort();
193
David Reiss1c20c872010-03-09 05:20:14 +0000194 /**
195 * Sets whether to use a low minimum TCP retransmission timeout.
196 */
197 static void setUseLowMinRto(bool useLowMinRto);
198
199 /**
200 * Gets whether to use a low minimum TCP retransmission timeout.
201 */
202 static bool getUseLowMinRto();
Mark Sleeb4552922007-11-28 00:12:11 +0000203
Mark Slee8d7e1f62006-06-07 06:48:56 +0000204 /**
Bryan Duxbury010f1e02010-09-02 00:56:53 +0000205 * Constructor to create socket from raw UNIX handle.
Mark Slee8d7e1f62006-06-07 06:48:56 +0000206 */
Mark Sleee8540632006-05-30 09:24:40 +0000207 TSocket(int socket);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000208
Bryan Duxbury010f1e02010-09-02 00:56:53 +0000209 protected:
Mark Slee6d56eb92007-07-06 22:28:15 +0000210 /** connect, called by open */
211 void openConnection(struct addrinfo *res);
212
Mark Slee8d7e1f62006-06-07 06:48:56 +0000213 /** Host to connect to */
Mark Sleee8540632006-05-30 09:24:40 +0000214 std::string host_;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000215
Mark Sleeb4552922007-11-28 00:12:11 +0000216 /** Peer hostname */
217 std::string peerHost_;
218
219 /** Peer address */
220 std::string peerAddress_;
221
222 /** Peer port */
223 int peerPort_;
224
Mark Slee8d7e1f62006-06-07 06:48:56 +0000225 /** Port number to connect on */
Mark Sleee8540632006-05-30 09:24:40 +0000226 int port_;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000227
Bryan Duxburya18364a2010-09-28 14:36:07 +0000228 /** UNIX domain socket path */
229 std::string path_;
230
Mark Slee8d7e1f62006-06-07 06:48:56 +0000231 /** Underlying UNIX socket handle */
Mark Sleee8540632006-05-30 09:24:40 +0000232 int socket_;
Mark Slee29050782006-09-29 00:12:30 +0000233
234 /** Connect timeout in ms */
235 int connTimeout_;
236
237 /** Send timeout in ms */
238 int sendTimeout_;
239
240 /** Recv timeout in ms */
241 int recvTimeout_;
242
243 /** Linger on */
244 bool lingerOn_;
Mark Sleeb4552922007-11-28 00:12:11 +0000245
Mark Slee29050782006-09-29 00:12:30 +0000246 /** Linger val */
247 int lingerVal_;
248
249 /** Nodelay */
250 bool noDelay_;
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000251
Aditya Agarwale04475b2007-05-23 02:14:58 +0000252 /** Recv EGAIN retries */
253 int maxRecvRetries_;
254
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000255 /** Recv timeout timeval */
256 struct timeval recvTimeval_;
David Reiss1c20c872010-03-09 05:20:14 +0000257
258 /** Whether to use low minimum TCP retransmission timeout */
259 static bool useLowMinRto_;
Bryan Duxburya18364a2010-09-28 14:36:07 +0000260
261 private:
262 void unix_open();
263 void local_open();
Mark Sleee8540632006-05-30 09:24:40 +0000264};
265
T Jake Lucianib5e62212009-01-31 22:36:20 +0000266}}} // apache::thrift::transport
Mark Sleef5f2be42006-09-05 21:05:31 +0000267
268#endif // #ifndef _THRIFT_TRANSPORT_TSOCKET_H_
269