blob: 9fbdb20d8e6c6adfe6cb9498e59b8a20f6576d5c [file] [log] [blame]
Mark Sleef5f2be42006-09-05 21:05:31 +00001#ifndef _THRIFT_TRANSPORT_TSOCKET_H_
2#define _THRIFT_TRANSPORT_TSOCKET_H_ 1
Mark Sleee8540632006-05-30 09:24:40 +00003
4#include <string>
Mark Sleeb9ff32a2006-11-16 01:00:24 +00005#include <sys/time.h>
Mark Sleee8540632006-05-30 09:24:40 +00006
Marc Slemkod42a2c22006-08-10 03:30:18 +00007#include "TTransport.h"
8#include "TServerSocket.h"
Mark Sleee8540632006-05-30 09:24:40 +00009
Marc Slemko6f038a72006-08-03 18:58:09 +000010namespace facebook { namespace thrift { namespace transport {
11
Mark Sleee8540632006-05-30 09:24:40 +000012/**
13 * TCP Socket implementation of the TTransport interface.
14 *
15 * @author Mark Slee <mcslee@facebook.com>
16 */
17class TSocket : public TTransport {
Mark Slee8d7e1f62006-06-07 06:48:56 +000018 /**
19 * We allow the TServerSocket acceptImpl() method to access the private
20 * members of a socket so that it can access the TSocket(int socket)
21 * constructor which creates a socket object from the raw UNIX socket
22 * handle.
23 */
24 friend class TServerSocket;
Mark Sleee8540632006-05-30 09:24:40 +000025
26 public:
Mark Slee8d7e1f62006-06-07 06:48:56 +000027 /**
28 * Constructs a new socket. Note that this does NOT actually connect the
29 * socket.
30 *
Aditya Agarwalebc99e02007-01-15 23:14:58 +000031 */
32 TSocket();
33
34 /**
35 * Constructs a new socket. Note that this does NOT actually connect the
36 * socket.
37 *
Mark Slee8d7e1f62006-06-07 06:48:56 +000038 * @param host An IP address or hostname to connect to
39 * @param port The port to connect on
40 */
Mark Sleee8540632006-05-30 09:24:40 +000041 TSocket(std::string host, int port);
Mark Slee8d7e1f62006-06-07 06:48:56 +000042
43 /**
44 * Destroyes the socket object, closing it if necessary.
45 */
Mark Slee8a98e1b2007-02-27 05:16:23 +000046 virtual ~TSocket();
Mark Sleee8540632006-05-30 09:24:40 +000047
Mark Slee8d7e1f62006-06-07 06:48:56 +000048 /**
49 * Whether the socket is alive.
50 *
51 * @return Is the socket alive?
52 */
53 bool isOpen();
Mark Sleee8540632006-05-30 09:24:40 +000054
Mark Slee8d7e1f62006-06-07 06:48:56 +000055 /**
Mark Sleeb9ff32a2006-11-16 01:00:24 +000056 * Calls select on the socket to see if there is more data available.
57 */
58 bool peek();
59
60 /**
Mark Slee8d7e1f62006-06-07 06:48:56 +000061 * Creates and opens the UNIX socket.
62 *
63 * @throws TTransportException If the socket could not connect
64 */
65 void open();
66
67 /**
68 * Shuts down communications on the socket.
69 */
70 void close();
71
72 /**
73 * Reads from the underlying socket.
74 */
75 uint32_t read(uint8_t* buf, uint32_t len);
76
77 /**
78 * Writes to the underlying socket.
79 */
80 void write(const uint8_t* buf, uint32_t len);
81
82 /**
Aditya Agarwalebc99e02007-01-15 23:14:58 +000083 * Set the host that socket will connect to
84 *
85 * @param host host identifier
86 */
87 void setHost(std::string host);
88
89 /**
90 * Set the port that socket will connect to
91 *
92 * @param port port number
93 */
94 void setPort(int port);
95
96 /**
Mark Slee8d7e1f62006-06-07 06:48:56 +000097 * Controls whether the linger option is set on the socket.
98 *
99 * @param on Whether SO_LINGER is on
100 * @param linger If linger is active, the number of seconds to linger for
101 */
102 void setLinger(bool on, int linger);
103
104 /**
105 * Whether to enable/disable Nagle's algorithm.
106 *
107 * @param noDelay Whether or not to disable the algorithm.
108 * @return
109 */
110 void setNoDelay(bool noDelay);
Mark Sleee8540632006-05-30 09:24:40 +0000111
Mark Slee29050782006-09-29 00:12:30 +0000112 /**
113 * Set the connect timeout
114 */
115 void setConnTimeout(int ms);
116
117 /**
118 * Set the receive timeout
119 */
120 void setRecvTimeout(int ms);
121
122 /**
123 * Set the send timeout
124 */
125 void setSendTimeout(int ms);
126
Mark Slee8a98e1b2007-02-27 05:16:23 +0000127 protected:
Mark Slee8d7e1f62006-06-07 06:48:56 +0000128 /**
129 * Constructor to create socket from raw UNIX handle. Never called directly
130 * but used by the TServerSocket class.
131 */
Mark Sleee8540632006-05-30 09:24:40 +0000132 TSocket(int socket);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000133
134 /** Host to connect to */
Mark Sleee8540632006-05-30 09:24:40 +0000135 std::string host_;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000136
137 /** Port number to connect on */
Mark Sleee8540632006-05-30 09:24:40 +0000138 int port_;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000139
140 /** Underlying UNIX socket handle */
Mark Sleee8540632006-05-30 09:24:40 +0000141 int socket_;
Mark Slee29050782006-09-29 00:12:30 +0000142
143 /** Connect timeout in ms */
144 int connTimeout_;
145
146 /** Send timeout in ms */
147 int sendTimeout_;
148
149 /** Recv timeout in ms */
150 int recvTimeout_;
151
152 /** Linger on */
153 bool lingerOn_;
154
155 /** Linger val */
156 int lingerVal_;
157
158 /** Nodelay */
159 bool noDelay_;
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000160
161 /** Recv timeout timeval */
162 struct timeval recvTimeval_;
Mark Sleee8540632006-05-30 09:24:40 +0000163};
164
Marc Slemko6f038a72006-08-03 18:58:09 +0000165}}} // facebook::thrift::transport
Mark Sleef5f2be42006-09-05 21:05:31 +0000166
167#endif // #ifndef _THRIFT_TRANSPORT_TSOCKET_H_
168