blob: b0f445aa372caaf451072c48db208f8d63458e67 [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>
Mark Sleee8540632006-05-30 09:24:40 +000025
Marc Slemkod42a2c22006-08-10 03:30:18 +000026#include "TTransport.h"
27#include "TServerSocket.h"
Mark Sleee8540632006-05-30 09:24:40 +000028
T Jake Lucianib5e62212009-01-31 22:36:20 +000029namespace apache { namespace thrift { namespace transport {
Marc Slemko6f038a72006-08-03 18:58:09 +000030
Mark Sleee8540632006-05-30 09:24:40 +000031/**
32 * TCP Socket implementation of the TTransport interface.
33 *
Mark Sleee8540632006-05-30 09:24:40 +000034 */
35class TSocket : public TTransport {
Mark Slee8d7e1f62006-06-07 06:48:56 +000036 /**
37 * We allow the TServerSocket acceptImpl() method to access the private
38 * members of a socket so that it can access the TSocket(int socket)
39 * constructor which creates a socket object from the raw UNIX socket
40 * handle.
41 */
42 friend class TServerSocket;
Mark Sleee8540632006-05-30 09:24:40 +000043
44 public:
Mark Slee8d7e1f62006-06-07 06:48:56 +000045 /**
46 * Constructs a new socket. Note that this does NOT actually connect the
47 * socket.
48 *
Aditya Agarwalebc99e02007-01-15 23:14:58 +000049 */
50 TSocket();
Mark Sleeb4552922007-11-28 00:12:11 +000051
Aditya Agarwalebc99e02007-01-15 23:14:58 +000052 /**
53 * Constructs a new socket. Note that this does NOT actually connect the
54 * socket.
55 *
Mark Slee8d7e1f62006-06-07 06:48:56 +000056 * @param host An IP address or hostname to connect to
57 * @param port The port to connect on
58 */
Mark Sleee8540632006-05-30 09:24:40 +000059 TSocket(std::string host, int port);
Mark Slee8d7e1f62006-06-07 06:48:56 +000060
61 /**
62 * Destroyes the socket object, closing it if necessary.
63 */
Mark Slee8a98e1b2007-02-27 05:16:23 +000064 virtual ~TSocket();
Mark Sleee8540632006-05-30 09:24:40 +000065
Mark Slee8d7e1f62006-06-07 06:48:56 +000066 /**
67 * Whether the socket is alive.
68 *
69 * @return Is the socket alive?
70 */
71 bool isOpen();
Mark Sleee8540632006-05-30 09:24:40 +000072
Mark Slee8d7e1f62006-06-07 06:48:56 +000073 /**
Mark Sleeb9ff32a2006-11-16 01:00:24 +000074 * Calls select on the socket to see if there is more data available.
75 */
76 bool peek();
77
78 /**
Mark Slee8d7e1f62006-06-07 06:48:56 +000079 * Creates and opens the UNIX socket.
80 *
81 * @throws TTransportException If the socket could not connect
82 */
jsobele02e4242007-05-08 17:51:49 +000083 virtual void open();
Mark Slee8d7e1f62006-06-07 06:48:56 +000084
85 /**
86 * Shuts down communications on the socket.
87 */
88 void close();
89
90 /**
91 * Reads from the underlying socket.
92 */
93 uint32_t read(uint8_t* buf, uint32_t len);
94
95 /**
96 * Writes to the underlying socket.
97 */
98 void write(const uint8_t* buf, uint32_t len);
99
100 /**
dweatherford14b0ed62007-10-19 01:03:32 +0000101 * Get the host that the socket is connected to
102 *
103 * @return string host identifier
104 */
105 std::string getHost();
106
107 /**
108 * Get the port that the socket is connected to
109 *
110 * @return int port number
111 */
112 int getPort();
113
114 /**
Aditya Agarwalebc99e02007-01-15 23:14:58 +0000115 * Set the host that socket will connect to
116 *
117 * @param host host identifier
118 */
119 void setHost(std::string host);
120
121 /**
122 * Set the port that socket will connect to
123 *
124 * @param port port number
125 */
126 void setPort(int port);
127
128 /**
Mark Slee8d7e1f62006-06-07 06:48:56 +0000129 * Controls whether the linger option is set on the socket.
130 *
131 * @param on Whether SO_LINGER is on
132 * @param linger If linger is active, the number of seconds to linger for
133 */
134 void setLinger(bool on, int linger);
135
136 /**
137 * Whether to enable/disable Nagle's algorithm.
138 *
139 * @param noDelay Whether or not to disable the algorithm.
Mark Sleeb4552922007-11-28 00:12:11 +0000140 * @return
Mark Slee8d7e1f62006-06-07 06:48:56 +0000141 */
142 void setNoDelay(bool noDelay);
Mark Sleee8540632006-05-30 09:24:40 +0000143
Mark Slee29050782006-09-29 00:12:30 +0000144 /**
145 * Set the connect timeout
146 */
147 void setConnTimeout(int ms);
148
149 /**
150 * Set the receive timeout
151 */
152 void setRecvTimeout(int ms);
153
154 /**
155 * Set the send timeout
156 */
157 void setSendTimeout(int ms);
158
Aditya Agarwale04475b2007-05-23 02:14:58 +0000159 /**
160 * Set the max number of recv retries in case of an EAGAIN
161 * error
162 */
163 void setMaxRecvRetries(int maxRecvRetries);
164
Mark Sleeb4552922007-11-28 00:12:11 +0000165 /**
166 * Get socket information formated as a string <Host: x Port: x>
167 */
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000168 std::string getSocketInfo();
169
Mark Sleeb4552922007-11-28 00:12:11 +0000170 /**
171 * Returns the DNS name of the host to which the socket is connected
172 */
173 std::string getPeerHost();
174
175 /**
176 * Returns the address of the host to which the socket is connected
177 */
178 std::string getPeerAddress();
179
180 /**
181 * Returns the port of the host to which the socket is connected
182 **/
183 int getPeerPort();
184
185
Mark Slee8a98e1b2007-02-27 05:16:23 +0000186 protected:
Mark Slee8d7e1f62006-06-07 06:48:56 +0000187 /**
188 * Constructor to create socket from raw UNIX handle. Never called directly
189 * but used by the TServerSocket class.
190 */
Mark Sleee8540632006-05-30 09:24:40 +0000191 TSocket(int socket);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000192
Mark Slee6d56eb92007-07-06 22:28:15 +0000193 /** connect, called by open */
194 void openConnection(struct addrinfo *res);
195
Mark Slee8d7e1f62006-06-07 06:48:56 +0000196 /** Host to connect to */
Mark Sleee8540632006-05-30 09:24:40 +0000197 std::string host_;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000198
Mark Sleeb4552922007-11-28 00:12:11 +0000199 /** Peer hostname */
200 std::string peerHost_;
201
202 /** Peer address */
203 std::string peerAddress_;
204
205 /** Peer port */
206 int peerPort_;
207
Mark Slee8d7e1f62006-06-07 06:48:56 +0000208 /** Port number to connect on */
Mark Sleee8540632006-05-30 09:24:40 +0000209 int port_;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000210
211 /** Underlying UNIX socket handle */
Mark Sleee8540632006-05-30 09:24:40 +0000212 int socket_;
Mark Slee29050782006-09-29 00:12:30 +0000213
214 /** Connect timeout in ms */
215 int connTimeout_;
216
217 /** Send timeout in ms */
218 int sendTimeout_;
219
220 /** Recv timeout in ms */
221 int recvTimeout_;
222
223 /** Linger on */
224 bool lingerOn_;
Mark Sleeb4552922007-11-28 00:12:11 +0000225
Mark Slee29050782006-09-29 00:12:30 +0000226 /** Linger val */
227 int lingerVal_;
228
229 /** Nodelay */
230 bool noDelay_;
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000231
Aditya Agarwale04475b2007-05-23 02:14:58 +0000232 /** Recv EGAIN retries */
233 int maxRecvRetries_;
234
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000235 /** Recv timeout timeval */
236 struct timeval recvTimeval_;
Mark Sleee8540632006-05-30 09:24:40 +0000237};
238
T Jake Lucianib5e62212009-01-31 22:36:20 +0000239}}} // apache::thrift::transport
Mark Sleef5f2be42006-09-05 21:05:31 +0000240
241#endif // #ifndef _THRIFT_TRANSPORT_TSOCKET_H_
242