blob: 8013582c27534a3fb821450ea2c7e70a2a6f5e4c [file] [log] [blame]
Mark Slee9f0c6512007-02-28 23:58:26 +00001// Copyright (c) 2006- Facebook
2// Distributed under the Thrift Software License
3//
4// See accompanying file LICENSE or visit the Thrift site at:
5// http://developers.facebook.com/thrift/
6
Mark Sleef5f2be42006-09-05 21:05:31 +00007#ifndef _THRIFT_TRANSPORT_TSOCKET_H_
8#define _THRIFT_TRANSPORT_TSOCKET_H_ 1
Mark Sleee8540632006-05-30 09:24:40 +00009
10#include <string>
Mark Sleeb9ff32a2006-11-16 01:00:24 +000011#include <sys/time.h>
Mark Sleee8540632006-05-30 09:24:40 +000012
Marc Slemkod42a2c22006-08-10 03:30:18 +000013#include "TTransport.h"
14#include "TServerSocket.h"
Mark Sleee8540632006-05-30 09:24:40 +000015
T Jake Lucianib5e62212009-01-31 22:36:20 +000016namespace apache { namespace thrift { namespace transport {
Marc Slemko6f038a72006-08-03 18:58:09 +000017
Mark Sleee8540632006-05-30 09:24:40 +000018/**
19 * TCP Socket implementation of the TTransport interface.
20 *
Mark Sleee8540632006-05-30 09:24:40 +000021 */
22class TSocket : public TTransport {
Mark Slee8d7e1f62006-06-07 06:48:56 +000023 /**
24 * We allow the TServerSocket acceptImpl() method to access the private
25 * members of a socket so that it can access the TSocket(int socket)
26 * constructor which creates a socket object from the raw UNIX socket
27 * handle.
28 */
29 friend class TServerSocket;
Mark Sleee8540632006-05-30 09:24:40 +000030
31 public:
Mark Slee8d7e1f62006-06-07 06:48:56 +000032 /**
33 * Constructs a new socket. Note that this does NOT actually connect the
34 * socket.
35 *
Aditya Agarwalebc99e02007-01-15 23:14:58 +000036 */
37 TSocket();
Mark Sleeb4552922007-11-28 00:12:11 +000038
Aditya Agarwalebc99e02007-01-15 23:14:58 +000039 /**
40 * Constructs a new socket. Note that this does NOT actually connect the
41 * socket.
42 *
Mark Slee8d7e1f62006-06-07 06:48:56 +000043 * @param host An IP address or hostname to connect to
44 * @param port The port to connect on
45 */
Mark Sleee8540632006-05-30 09:24:40 +000046 TSocket(std::string host, int port);
Mark Slee8d7e1f62006-06-07 06:48:56 +000047
48 /**
49 * Destroyes the socket object, closing it if necessary.
50 */
Mark Slee8a98e1b2007-02-27 05:16:23 +000051 virtual ~TSocket();
Mark Sleee8540632006-05-30 09:24:40 +000052
Mark Slee8d7e1f62006-06-07 06:48:56 +000053 /**
54 * Whether the socket is alive.
55 *
56 * @return Is the socket alive?
57 */
58 bool isOpen();
Mark Sleee8540632006-05-30 09:24:40 +000059
Mark Slee8d7e1f62006-06-07 06:48:56 +000060 /**
Mark Sleeb9ff32a2006-11-16 01:00:24 +000061 * Calls select on the socket to see if there is more data available.
62 */
63 bool peek();
64
65 /**
Mark Slee8d7e1f62006-06-07 06:48:56 +000066 * Creates and opens the UNIX socket.
67 *
68 * @throws TTransportException If the socket could not connect
69 */
jsobele02e4242007-05-08 17:51:49 +000070 virtual void open();
Mark Slee8d7e1f62006-06-07 06:48:56 +000071
72 /**
73 * Shuts down communications on the socket.
74 */
75 void close();
76
77 /**
78 * Reads from the underlying socket.
79 */
80 uint32_t read(uint8_t* buf, uint32_t len);
81
82 /**
83 * Writes to the underlying socket.
84 */
85 void write(const uint8_t* buf, uint32_t len);
86
87 /**
dweatherford14b0ed62007-10-19 01:03:32 +000088 * Get the host that the socket is connected to
89 *
90 * @return string host identifier
91 */
92 std::string getHost();
93
94 /**
95 * Get the port that the socket is connected to
96 *
97 * @return int port number
98 */
99 int getPort();
100
101 /**
Aditya Agarwalebc99e02007-01-15 23:14:58 +0000102 * Set the host that socket will connect to
103 *
104 * @param host host identifier
105 */
106 void setHost(std::string host);
107
108 /**
109 * Set the port that socket will connect to
110 *
111 * @param port port number
112 */
113 void setPort(int port);
114
115 /**
Mark Slee8d7e1f62006-06-07 06:48:56 +0000116 * Controls whether the linger option is set on the socket.
117 *
118 * @param on Whether SO_LINGER is on
119 * @param linger If linger is active, the number of seconds to linger for
120 */
121 void setLinger(bool on, int linger);
122
123 /**
124 * Whether to enable/disable Nagle's algorithm.
125 *
126 * @param noDelay Whether or not to disable the algorithm.
Mark Sleeb4552922007-11-28 00:12:11 +0000127 * @return
Mark Slee8d7e1f62006-06-07 06:48:56 +0000128 */
129 void setNoDelay(bool noDelay);
Mark Sleee8540632006-05-30 09:24:40 +0000130
Mark Slee29050782006-09-29 00:12:30 +0000131 /**
132 * Set the connect timeout
133 */
134 void setConnTimeout(int ms);
135
136 /**
137 * Set the receive timeout
138 */
139 void setRecvTimeout(int ms);
140
141 /**
142 * Set the send timeout
143 */
144 void setSendTimeout(int ms);
145
Aditya Agarwale04475b2007-05-23 02:14:58 +0000146 /**
147 * Set the max number of recv retries in case of an EAGAIN
148 * error
149 */
150 void setMaxRecvRetries(int maxRecvRetries);
151
Mark Sleeb4552922007-11-28 00:12:11 +0000152 /**
153 * Get socket information formated as a string <Host: x Port: x>
154 */
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000155 std::string getSocketInfo();
156
Mark Sleeb4552922007-11-28 00:12:11 +0000157 /**
158 * Returns the DNS name of the host to which the socket is connected
159 */
160 std::string getPeerHost();
161
162 /**
163 * Returns the address of the host to which the socket is connected
164 */
165 std::string getPeerAddress();
166
167 /**
168 * Returns the port of the host to which the socket is connected
169 **/
170 int getPeerPort();
171
172
Mark Slee8a98e1b2007-02-27 05:16:23 +0000173 protected:
Mark Slee8d7e1f62006-06-07 06:48:56 +0000174 /**
175 * Constructor to create socket from raw UNIX handle. Never called directly
176 * but used by the TServerSocket class.
177 */
Mark Sleee8540632006-05-30 09:24:40 +0000178 TSocket(int socket);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000179
Mark Slee6d56eb92007-07-06 22:28:15 +0000180 /** connect, called by open */
181 void openConnection(struct addrinfo *res);
182
Mark Slee8d7e1f62006-06-07 06:48:56 +0000183 /** Host to connect to */
Mark Sleee8540632006-05-30 09:24:40 +0000184 std::string host_;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000185
Mark Sleeb4552922007-11-28 00:12:11 +0000186 /** Peer hostname */
187 std::string peerHost_;
188
189 /** Peer address */
190 std::string peerAddress_;
191
192 /** Peer port */
193 int peerPort_;
194
Mark Slee8d7e1f62006-06-07 06:48:56 +0000195 /** Port number to connect on */
Mark Sleee8540632006-05-30 09:24:40 +0000196 int port_;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000197
198 /** Underlying UNIX socket handle */
Mark Sleee8540632006-05-30 09:24:40 +0000199 int socket_;
Mark Slee29050782006-09-29 00:12:30 +0000200
201 /** Connect timeout in ms */
202 int connTimeout_;
203
204 /** Send timeout in ms */
205 int sendTimeout_;
206
207 /** Recv timeout in ms */
208 int recvTimeout_;
209
210 /** Linger on */
211 bool lingerOn_;
Mark Sleeb4552922007-11-28 00:12:11 +0000212
Mark Slee29050782006-09-29 00:12:30 +0000213 /** Linger val */
214 int lingerVal_;
215
216 /** Nodelay */
217 bool noDelay_;
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000218
Aditya Agarwale04475b2007-05-23 02:14:58 +0000219 /** Recv EGAIN retries */
220 int maxRecvRetries_;
221
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000222 /** Recv timeout timeval */
223 struct timeval recvTimeval_;
Mark Sleee8540632006-05-30 09:24:40 +0000224};
225
T Jake Lucianib5e62212009-01-31 22:36:20 +0000226}}} // apache::thrift::transport
Mark Sleef5f2be42006-09-05 21:05:31 +0000227
228#endif // #ifndef _THRIFT_TRANSPORT_TSOCKET_H_
229