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
10
* http://www.apache.org/licenses/LICENSE-2.0
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
20
#ifndef _THRIFT_TRANSPORT_TSOCKET_H_
21
#define _THRIFT_TRANSPORT_TSOCKET_H_ 1
25
#include "TTransport.h"
26
#include "TVirtualTransport.h"
27
#include "TServerSocket.h"
29
#ifdef HAVE_SYS_TIME_H
36
namespace apache { namespace thrift { namespace transport {
39
* TCP Socket implementation of the TTransport interface.
42
class TSocket : public TVirtualTransport<TSocket> {
45
* Constructs a new socket. Note that this does NOT actually connect the
52
* Constructs a new socket. Note that this does NOT actually connect the
55
* @param host An IP address or hostname to connect to
56
* @param port The port to connect on
58
TSocket(std::string host, int port);
61
* Constructs a new Unix domain socket.
62
* Note that this does NOT actually connect the socket.
64
* @param path The Unix domain socket e.g. "/tmp/ThriftTest.binary.thrift"
66
TSocket(std::string path);
69
* Destroyes the socket object, closing it if necessary.
74
* Whether the socket is alive.
76
* @return Is the socket alive?
78
virtual bool isOpen();
81
* Calls select on the socket to see if there is more data available.
86
* Creates and opens the UNIX socket.
88
* @throws TTransportException If the socket could not connect
93
* Shuts down communications on the socket.
98
* Reads from the underlying socket.
100
virtual uint32_t read(uint8_t* buf, uint32_t len);
103
* Writes to the underlying socket. Loops until done or fail.
105
virtual void write(const uint8_t* buf, uint32_t len);
108
* Writes to the underlying socket. Does single send() and returns result.
110
uint32_t write_partial(const uint8_t* buf, uint32_t len);
113
* Get the host that the socket is connected to
115
* @return string host identifier
117
std::string getHost();
120
* Get the port that the socket is connected to
122
* @return int port number
127
* Set the host that socket will connect to
129
* @param host host identifier
131
void setHost(std::string host);
134
* Set the port that socket will connect to
136
* @param port port number
138
void setPort(int port);
141
* Controls whether the linger option is set on the socket.
143
* @param on Whether SO_LINGER is on
144
* @param linger If linger is active, the number of seconds to linger for
146
void setLinger(bool on, int linger);
149
* Whether to enable/disable Nagle's algorithm.
151
* @param noDelay Whether or not to disable the algorithm.
154
void setNoDelay(bool noDelay);
157
* Set the connect timeout
159
void setConnTimeout(int ms);
162
* Set the receive timeout
164
void setRecvTimeout(int ms);
167
* Set the send timeout
169
void setSendTimeout(int ms);
172
* Set the max number of recv retries in case of an EAGAIN
175
void setMaxRecvRetries(int maxRecvRetries);
178
* Get socket information formated as a string <Host: x Port: x>
180
std::string getSocketInfo();
183
* Returns the DNS name of the host to which the socket is connected
185
std::string getPeerHost();
188
* Returns the address of the host to which the socket is connected
190
std::string getPeerAddress();
193
* Returns the port of the host to which the socket is connected
198
* Returns the underlying socket file descriptor.
205
* (Re-)initialize a TSocket for the supplied descriptor. This is only
206
* intended for use by TNonblockingServer -- other use may result in
207
* unfortunate surprises.
209
* @param fd the descriptor for an already-connected socket
211
void setSocketFD(int fd);
214
* Returns a cached copy of the peer address.
216
sockaddr* getCachedAddress(socklen_t* len) const;
219
* Sets whether to use a low minimum TCP retransmission timeout.
221
static void setUseLowMinRto(bool useLowMinRto);
224
* Gets whether to use a low minimum TCP retransmission timeout.
226
static bool getUseLowMinRto();
229
* Constructor to create socket from raw UNIX handle.
234
* Set a cache of the peer address (used when trivially available: e.g.
235
* accept() or connect()). Only caches IPV4 and IPV6; unset for others.
237
void setCachedAddress(const sockaddr* addr, socklen_t len);
240
/** connect, called by open */
241
void openConnection(struct addrinfo *res);
243
/** Host to connect to */
247
std::string peerHost_;
250
std::string peerAddress_;
255
/** Port number to connect on */
258
/** UNIX domain socket path */
261
/** Underlying UNIX socket handle */
264
/** Connect timeout in ms */
267
/** Send timeout in ms */
270
/** Recv timeout in ms */
282
/** Recv EGAIN retries */
285
/** Recv timeout timeval */
286
struct timeval recvTimeval_;
288
/** Cached peer address */
294
/** Connection start time */
297
/** Whether to use low minimum TCP retransmission timeout */
298
static bool useLowMinRto_;
305
}}} // apache::thrift::transport
307
#endif // #ifndef _THRIFT_TRANSPORT_TSOCKET_H_