]>
git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/lib/cpp/src/thrift/transport/TSocket.h
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 <thrift/transport/TTransport.h>
26 #include <thrift/transport/TVirtualTransport.h>
27 #include <thrift/transport/TServerSocket.h>
28 #include <thrift/transport/PlatformSocket.h>
30 #ifdef HAVE_ARPA_INET_H
31 #include <arpa/inet.h>
33 #ifdef HAVE_SYS_TIME_H
45 * TCP Socket implementation of the TTransport interface.
48 class TSocket
: public TVirtualTransport
<TSocket
> {
51 * Constructs a new socket. Note that this does NOT actually connect the
58 * Constructs a new socket. Note that this does NOT actually connect the
61 * @param host An IP address or hostname to connect to
62 * @param port The port to connect on
64 TSocket(const std::string
& host
, int port
);
67 * Constructs a new Unix domain socket.
68 * Note that this does NOT actually connect the socket.
70 * @param path The Unix domain socket e.g. "/tmp/ThriftTest.binary.thrift"
72 TSocket(const std::string
& path
);
75 * Destroyes the socket object, closing it if necessary.
80 * Whether the socket is alive.
82 * @return Is the socket alive?
84 bool isOpen() const override
;
87 * Checks whether there is more data available in the socket to read.
89 * This call blocks until at least one byte is available or the socket is closed.
94 * Creates and opens the UNIX socket.
96 * @throws TTransportException If the socket could not connect
101 * Shuts down communications on the socket.
103 void close() override
;
106 * Determines whether there is pending data to read or not.
108 * This call does not block.
109 * \throws TTransportException of types:
110 * NOT_OPEN means the socket has been closed
111 * UNKNOWN means something unexpected happened
112 * \returns true if there is pending data to read, false otherwise
114 virtual bool hasPendingDataToRead();
117 * Reads from the underlying socket.
118 * \returns the number of bytes read or 0 indicates EOF
119 * \throws TTransportException of types:
120 * INTERRUPTED means the socket was interrupted
121 * out of a blocking call
122 * NOT_OPEN means the socket has been closed
123 * TIMED_OUT means the receive timeout expired
124 * UNKNOWN means something unexpected happened
126 virtual uint32_t read(uint8_t* buf
, uint32_t len
);
129 * Writes to the underlying socket. Loops until done or fail.
131 virtual void write(const uint8_t* buf
, uint32_t len
);
134 * Writes to the underlying socket. Does single send() and returns result.
136 virtual uint32_t write_partial(const uint8_t* buf
, uint32_t len
);
139 * Get the host that the socket is connected to
141 * @return string host identifier
143 std::string
getHost();
146 * Get the port that the socket is connected to
148 * @return int port number
153 * Set the host that socket will connect to
155 * @param host host identifier
157 void setHost(std::string host
);
160 * Set the port that socket will connect to
162 * @param port port number
164 void setPort(int port
);
167 * Controls whether the linger option is set on the socket.
169 * @param on Whether SO_LINGER is on
170 * @param linger If linger is active, the number of seconds to linger for
172 void setLinger(bool on
, int linger
);
175 * Whether to enable/disable Nagle's algorithm.
177 * @param noDelay Whether or not to disable the algorithm.
180 void setNoDelay(bool noDelay
);
183 * Set the connect timeout
185 void setConnTimeout(int ms
);
188 * Set the receive timeout
190 void setRecvTimeout(int ms
);
193 * Set the send timeout
195 void setSendTimeout(int ms
);
198 * Set the max number of recv retries in case of an THRIFT_EAGAIN
201 void setMaxRecvRetries(int maxRecvRetries
);
206 void setKeepAlive(bool keepAlive
);
209 * Get socket information formatted as a string <Host: x Port: x>
211 std::string
getSocketInfo() const;
214 * Returns the DNS name of the host to which the socket is connected
216 std::string
getPeerHost() const;
219 * Returns the address of the host to which the socket is connected
221 std::string
getPeerAddress() const;
224 * Returns the port of the host to which the socket is connected
226 int getPeerPort() const;
229 * Returns the underlying socket file descriptor.
231 THRIFT_SOCKET
getSocketFD() { return socket_
; }
234 * (Re-)initialize a TSocket for the supplied descriptor. This is only
235 * intended for use by TNonblockingServer -- other use may result in
236 * unfortunate surprises.
238 * @param fd the descriptor for an already-connected socket
240 void setSocketFD(THRIFT_SOCKET fd
);
243 * Returns a cached copy of the peer address.
245 sockaddr
* getCachedAddress(socklen_t
* len
) const;
248 * Sets whether to use a low minimum TCP retransmission timeout.
250 static void setUseLowMinRto(bool useLowMinRto
);
253 * Gets whether to use a low minimum TCP retransmission timeout.
255 static bool getUseLowMinRto();
258 * Get the origin the socket is connected to
260 * @return string peer host identifier and port
262 const std::string
getOrigin() const override
;
265 * Constructor to create socket from file descriptor.
267 TSocket(THRIFT_SOCKET socket
);
270 * Constructor to create socket from file descriptor that
271 * can be interrupted safely.
273 TSocket(THRIFT_SOCKET socket
, std::shared_ptr
<THRIFT_SOCKET
> interruptListener
);
276 * Set a cache of the peer address (used when trivially available: e.g.
277 * accept() or connect()). Only caches IPV4 and IPV6; unset for others.
279 void setCachedAddress(const sockaddr
* addr
, socklen_t len
);
282 /** connect, called by open */
283 void openConnection(struct addrinfo
* res
);
285 /** Host to connect to */
288 /** Port number to connect on */
291 /** UNIX domain socket path */
294 /** Underlying socket handle */
295 THRIFT_SOCKET socket_
;
298 mutable std::string peerHost_
;
301 mutable std::string peerAddress_
;
304 mutable int peerPort_
;
307 * A shared socket pointer that will interrupt a blocking read if data
308 * becomes available on it
310 std::shared_ptr
<THRIFT_SOCKET
> interruptListener_
;
312 /** Connect timeout in ms */
315 /** Send timeout in ms */
318 /** Recv timeout in ms */
333 /** Recv EGAIN retries */
336 /** Cached peer address */
342 /** Whether to use low minimum TCP retransmission timeout */
343 static bool useLowMinRto_
;
351 } // apache::thrift::transport
353 #endif // #ifndef _THRIFT_TRANSPORT_TSOCKET_H_