blob: 0184362c14c3191e09c8b25994d24d9356a00ee6 [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 /**
63 * Destroyes the socket object, closing it if necessary.
64 */
Mark Slee8a98e1b2007-02-27 05:16:23 +000065 virtual ~TSocket();
Mark Sleee8540632006-05-30 09:24:40 +000066
Mark Slee8d7e1f62006-06-07 06:48:56 +000067 /**
68 * Whether the socket is alive.
69 *
70 * @return Is the socket alive?
71 */
72 bool isOpen();
Mark Sleee8540632006-05-30 09:24:40 +000073
Mark Slee8d7e1f62006-06-07 06:48:56 +000074 /**
Mark Sleeb9ff32a2006-11-16 01:00:24 +000075 * Calls select on the socket to see if there is more data available.
76 */
77 bool peek();
78
79 /**
Mark Slee8d7e1f62006-06-07 06:48:56 +000080 * Creates and opens the UNIX socket.
81 *
82 * @throws TTransportException If the socket could not connect
83 */
jsobele02e4242007-05-08 17:51:49 +000084 virtual void open();
Mark Slee8d7e1f62006-06-07 06:48:56 +000085
86 /**
87 * Shuts down communications on the socket.
88 */
David Reiss450c2402010-03-09 05:20:26 +000089 virtual void close();
Mark Slee8d7e1f62006-06-07 06:48:56 +000090
91 /**
92 * Reads from the underlying socket.
93 */
94 uint32_t read(uint8_t* buf, uint32_t len);
95
96 /**
97 * Writes to the underlying socket.
98 */
99 void write(const uint8_t* buf, uint32_t len);
100
101 /**
dweatherford14b0ed62007-10-19 01:03:32 +0000102 * Get the host that the socket is connected to
103 *
104 * @return string host identifier
105 */
106 std::string getHost();
107
108 /**
109 * Get the port that the socket is connected to
110 *
111 * @return int port number
112 */
113 int getPort();
114
115 /**
Aditya Agarwalebc99e02007-01-15 23:14:58 +0000116 * Set the host that socket will connect to
117 *
118 * @param host host identifier
119 */
120 void setHost(std::string host);
121
122 /**
123 * Set the port that socket will connect to
124 *
125 * @param port port number
126 */
127 void setPort(int port);
128
129 /**
Mark Slee8d7e1f62006-06-07 06:48:56 +0000130 * Controls whether the linger option is set on the socket.
131 *
132 * @param on Whether SO_LINGER is on
133 * @param linger If linger is active, the number of seconds to linger for
134 */
135 void setLinger(bool on, int linger);
136
137 /**
138 * Whether to enable/disable Nagle's algorithm.
139 *
140 * @param noDelay Whether or not to disable the algorithm.
Mark Sleeb4552922007-11-28 00:12:11 +0000141 * @return
Mark Slee8d7e1f62006-06-07 06:48:56 +0000142 */
143 void setNoDelay(bool noDelay);
Mark Sleee8540632006-05-30 09:24:40 +0000144
Mark Slee29050782006-09-29 00:12:30 +0000145 /**
146 * Set the connect timeout
147 */
148 void setConnTimeout(int ms);
149
150 /**
151 * Set the receive timeout
152 */
153 void setRecvTimeout(int ms);
154
155 /**
156 * Set the send timeout
157 */
158 void setSendTimeout(int ms);
159
Aditya Agarwale04475b2007-05-23 02:14:58 +0000160 /**
161 * Set the max number of recv retries in case of an EAGAIN
162 * error
163 */
164 void setMaxRecvRetries(int maxRecvRetries);
165
Mark Sleeb4552922007-11-28 00:12:11 +0000166 /**
167 * Get socket information formated as a string <Host: x Port: x>
168 */
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000169 std::string getSocketInfo();
170
Mark Sleeb4552922007-11-28 00:12:11 +0000171 /**
172 * Returns the DNS name of the host to which the socket is connected
173 */
174 std::string getPeerHost();
175
176 /**
177 * Returns the address of the host to which the socket is connected
178 */
179 std::string getPeerAddress();
180
181 /**
182 * Returns the port of the host to which the socket is connected
183 **/
184 int getPeerPort();
185
David Reiss1c20c872010-03-09 05:20:14 +0000186 /**
187 * Sets whether to use a low minimum TCP retransmission timeout.
188 */
189 static void setUseLowMinRto(bool useLowMinRto);
190
191 /**
192 * Gets whether to use a low minimum TCP retransmission timeout.
193 */
194 static bool getUseLowMinRto();
Mark Sleeb4552922007-11-28 00:12:11 +0000195
Mark Slee8d7e1f62006-06-07 06:48:56 +0000196 /**
Bryan Duxbury010f1e02010-09-02 00:56:53 +0000197 * Constructor to create socket from raw UNIX handle.
Mark Slee8d7e1f62006-06-07 06:48:56 +0000198 */
Mark Sleee8540632006-05-30 09:24:40 +0000199 TSocket(int socket);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000200
Bryan Duxbury010f1e02010-09-02 00:56:53 +0000201 protected:
Mark Slee6d56eb92007-07-06 22:28:15 +0000202 /** connect, called by open */
203 void openConnection(struct addrinfo *res);
204
Mark Slee8d7e1f62006-06-07 06:48:56 +0000205 /** Host to connect to */
Mark Sleee8540632006-05-30 09:24:40 +0000206 std::string host_;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000207
Mark Sleeb4552922007-11-28 00:12:11 +0000208 /** Peer hostname */
209 std::string peerHost_;
210
211 /** Peer address */
212 std::string peerAddress_;
213
214 /** Peer port */
215 int peerPort_;
216
Mark Slee8d7e1f62006-06-07 06:48:56 +0000217 /** Port number to connect on */
Mark Sleee8540632006-05-30 09:24:40 +0000218 int port_;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000219
220 /** Underlying UNIX socket handle */
Mark Sleee8540632006-05-30 09:24:40 +0000221 int socket_;
Mark Slee29050782006-09-29 00:12:30 +0000222
223 /** Connect timeout in ms */
224 int connTimeout_;
225
226 /** Send timeout in ms */
227 int sendTimeout_;
228
229 /** Recv timeout in ms */
230 int recvTimeout_;
231
232 /** Linger on */
233 bool lingerOn_;
Mark Sleeb4552922007-11-28 00:12:11 +0000234
Mark Slee29050782006-09-29 00:12:30 +0000235 /** Linger val */
236 int lingerVal_;
237
238 /** Nodelay */
239 bool noDelay_;
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000240
Aditya Agarwale04475b2007-05-23 02:14:58 +0000241 /** Recv EGAIN retries */
242 int maxRecvRetries_;
243
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000244 /** Recv timeout timeval */
245 struct timeval recvTimeval_;
David Reiss1c20c872010-03-09 05:20:14 +0000246
247 /** Whether to use low minimum TCP retransmission timeout */
248 static bool useLowMinRto_;
Mark Sleee8540632006-05-30 09:24:40 +0000249};
250
T Jake Lucianib5e62212009-01-31 22:36:20 +0000251}}} // apache::thrift::transport
Mark Sleef5f2be42006-09-05 21:05:31 +0000252
253#endif // #ifndef _THRIFT_TRANSPORT_TSOCKET_H_
254