blob: 8d4a5a77f94820e29eee380ee69829617cb99952 [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
Mark Sleeb4552922007-11-28 00:12:11 +000016namespace facebook { 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 *
21 * @author Mark Slee <mcslee@facebook.com>
Aditya Agarwale04475b2007-05-23 02:14:58 +000022 * @author Aditya Agarwal <aditya@facebook.com>
Mark Sleee8540632006-05-30 09:24:40 +000023 */
24class TSocket : public TTransport {
Mark Slee8d7e1f62006-06-07 06:48:56 +000025 /**
26 * We allow the TServerSocket acceptImpl() method to access the private
27 * members of a socket so that it can access the TSocket(int socket)
28 * constructor which creates a socket object from the raw UNIX socket
29 * handle.
30 */
31 friend class TServerSocket;
Mark Sleee8540632006-05-30 09:24:40 +000032
33 public:
Mark Slee8d7e1f62006-06-07 06:48:56 +000034 /**
35 * Constructs a new socket. Note that this does NOT actually connect the
36 * socket.
37 *
Aditya Agarwalebc99e02007-01-15 23:14:58 +000038 */
39 TSocket();
Mark Sleeb4552922007-11-28 00:12:11 +000040
Aditya Agarwalebc99e02007-01-15 23:14:58 +000041 /**
42 * Constructs a new socket. Note that this does NOT actually connect the
43 * socket.
44 *
Mark Slee8d7e1f62006-06-07 06:48:56 +000045 * @param host An IP address or hostname to connect to
46 * @param port The port to connect on
47 */
Mark Sleee8540632006-05-30 09:24:40 +000048 TSocket(std::string host, int port);
Mark Slee8d7e1f62006-06-07 06:48:56 +000049
50 /**
51 * Destroyes the socket object, closing it if necessary.
52 */
Mark Slee8a98e1b2007-02-27 05:16:23 +000053 virtual ~TSocket();
Mark Sleee8540632006-05-30 09:24:40 +000054
Mark Slee8d7e1f62006-06-07 06:48:56 +000055 /**
56 * Whether the socket is alive.
57 *
58 * @return Is the socket alive?
59 */
60 bool isOpen();
Mark Sleee8540632006-05-30 09:24:40 +000061
Mark Slee8d7e1f62006-06-07 06:48:56 +000062 /**
Mark Sleeb9ff32a2006-11-16 01:00:24 +000063 * Calls select on the socket to see if there is more data available.
64 */
65 bool peek();
66
67 /**
Mark Slee8d7e1f62006-06-07 06:48:56 +000068 * Creates and opens the UNIX socket.
69 *
70 * @throws TTransportException If the socket could not connect
71 */
jsobele02e4242007-05-08 17:51:49 +000072 virtual void open();
Mark Slee8d7e1f62006-06-07 06:48:56 +000073
74 /**
75 * Shuts down communications on the socket.
76 */
77 void close();
78
79 /**
80 * Reads from the underlying socket.
81 */
82 uint32_t read(uint8_t* buf, uint32_t len);
83
84 /**
85 * Writes to the underlying socket.
86 */
87 void write(const uint8_t* buf, uint32_t len);
88
89 /**
dweatherford14b0ed62007-10-19 01:03:32 +000090 * Get the host that the socket is connected to
91 *
92 * @return string host identifier
93 */
94 std::string getHost();
95
96 /**
97 * Get the port that the socket is connected to
98 *
99 * @return int port number
100 */
101 int getPort();
102
103 /**
Aditya Agarwalebc99e02007-01-15 23:14:58 +0000104 * Set the host that socket will connect to
105 *
106 * @param host host identifier
107 */
108 void setHost(std::string host);
109
110 /**
111 * Set the port that socket will connect to
112 *
113 * @param port port number
114 */
115 void setPort(int port);
116
117 /**
Mark Slee8d7e1f62006-06-07 06:48:56 +0000118 * Controls whether the linger option is set on the socket.
119 *
120 * @param on Whether SO_LINGER is on
121 * @param linger If linger is active, the number of seconds to linger for
122 */
123 void setLinger(bool on, int linger);
124
125 /**
126 * Whether to enable/disable Nagle's algorithm.
127 *
128 * @param noDelay Whether or not to disable the algorithm.
Mark Sleeb4552922007-11-28 00:12:11 +0000129 * @return
Mark Slee8d7e1f62006-06-07 06:48:56 +0000130 */
131 void setNoDelay(bool noDelay);
Mark Sleee8540632006-05-30 09:24:40 +0000132
Mark Slee29050782006-09-29 00:12:30 +0000133 /**
134 * Set the connect timeout
135 */
136 void setConnTimeout(int ms);
137
138 /**
139 * Set the receive timeout
140 */
141 void setRecvTimeout(int ms);
142
143 /**
144 * Set the send timeout
145 */
146 void setSendTimeout(int ms);
147
Aditya Agarwale04475b2007-05-23 02:14:58 +0000148 /**
149 * Set the max number of recv retries in case of an EAGAIN
150 * error
151 */
152 void setMaxRecvRetries(int maxRecvRetries);
153
Mark Sleeb4552922007-11-28 00:12:11 +0000154 /**
155 * Get socket information formated as a string <Host: x Port: x>
156 */
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000157 std::string getSocketInfo();
158
Mark Sleeb4552922007-11-28 00:12:11 +0000159 /**
160 * Returns the DNS name of the host to which the socket is connected
161 */
162 std::string getPeerHost();
163
164 /**
165 * Returns the address of the host to which the socket is connected
166 */
167 std::string getPeerAddress();
168
169 /**
170 * Returns the port of the host to which the socket is connected
171 **/
172 int getPeerPort();
173
174
Mark Slee8a98e1b2007-02-27 05:16:23 +0000175 protected:
Mark Slee8d7e1f62006-06-07 06:48:56 +0000176 /**
177 * Constructor to create socket from raw UNIX handle. Never called directly
178 * but used by the TServerSocket class.
179 */
Mark Sleee8540632006-05-30 09:24:40 +0000180 TSocket(int socket);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000181
Mark Slee6d56eb92007-07-06 22:28:15 +0000182 /** connect, called by open */
183 void openConnection(struct addrinfo *res);
184
Mark Slee8d7e1f62006-06-07 06:48:56 +0000185 /** Host to connect to */
Mark Sleee8540632006-05-30 09:24:40 +0000186 std::string host_;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000187
Mark Sleeb4552922007-11-28 00:12:11 +0000188 /** Peer hostname */
189 std::string peerHost_;
190
191 /** Peer address */
192 std::string peerAddress_;
193
194 /** Peer port */
195 int peerPort_;
196
Mark Slee8d7e1f62006-06-07 06:48:56 +0000197 /** Port number to connect on */
Mark Sleee8540632006-05-30 09:24:40 +0000198 int port_;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000199
200 /** Underlying UNIX socket handle */
Mark Sleee8540632006-05-30 09:24:40 +0000201 int socket_;
Mark Slee29050782006-09-29 00:12:30 +0000202
203 /** Connect timeout in ms */
204 int connTimeout_;
205
206 /** Send timeout in ms */
207 int sendTimeout_;
208
209 /** Recv timeout in ms */
210 int recvTimeout_;
211
212 /** Linger on */
213 bool lingerOn_;
Mark Sleeb4552922007-11-28 00:12:11 +0000214
Mark Slee29050782006-09-29 00:12:30 +0000215 /** Linger val */
216 int lingerVal_;
217
218 /** Nodelay */
219 bool noDelay_;
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000220
Aditya Agarwale04475b2007-05-23 02:14:58 +0000221 /** Recv EGAIN retries */
222 int maxRecvRetries_;
223
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000224 /** Recv timeout timeval */
225 struct timeval recvTimeval_;
Mark Sleee8540632006-05-30 09:24:40 +0000226};
227
Marc Slemko6f038a72006-08-03 18:58:09 +0000228}}} // facebook::thrift::transport
Mark Sleef5f2be42006-09-05 21:05:31 +0000229
230#endif // #ifndef _THRIFT_TRANSPORT_TSOCKET_H_
231