blob: 3decb2cf65b6383b0ddd78d166d27ebcb59285b3 [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
Aditya Agarwalebc99e02007-01-15 23:14:58 +000043
Mark Slee8d7e1f62006-06-07 06:48:56 +000044 /**
45 * Destroyes the socket object, closing it if necessary.
46 */
Mark Sleee8540632006-05-30 09:24:40 +000047 ~TSocket();
48
Mark Slee8d7e1f62006-06-07 06:48:56 +000049 /**
50 * Whether the socket is alive.
51 *
52 * @return Is the socket alive?
53 */
54 bool isOpen();
Mark Sleee8540632006-05-30 09:24:40 +000055
Mark Slee8d7e1f62006-06-07 06:48:56 +000056 /**
Mark Sleeb9ff32a2006-11-16 01:00:24 +000057 * Calls select on the socket to see if there is more data available.
58 */
59 bool peek();
60
61 /**
Mark Slee8d7e1f62006-06-07 06:48:56 +000062 * Creates and opens the UNIX socket.
63 *
64 * @throws TTransportException If the socket could not connect
65 */
66 void open();
67
68 /**
69 * Shuts down communications on the socket.
70 */
71 void close();
72
73 /**
74 * Reads from the underlying socket.
75 */
76 uint32_t read(uint8_t* buf, uint32_t len);
77
78 /**
79 * Writes to the underlying socket.
80 */
81 void write(const uint8_t* buf, uint32_t len);
82
83 /**
Aditya Agarwalebc99e02007-01-15 23:14:58 +000084 * Set the host that socket will connect to
85 *
86 * @param host host identifier
87 */
88 void setHost(std::string host);
89
90 /**
91 * Set the port that socket will connect to
92 *
93 * @param port port number
94 */
95 void setPort(int port);
96
97 /**
Mark Slee8d7e1f62006-06-07 06:48:56 +000098 * Controls whether the linger option is set on the socket.
99 *
100 * @param on Whether SO_LINGER is on
101 * @param linger If linger is active, the number of seconds to linger for
102 */
103 void setLinger(bool on, int linger);
104
105 /**
106 * Whether to enable/disable Nagle's algorithm.
107 *
108 * @param noDelay Whether or not to disable the algorithm.
109 * @return
110 */
111 void setNoDelay(bool noDelay);
Mark Sleee8540632006-05-30 09:24:40 +0000112
Mark Slee29050782006-09-29 00:12:30 +0000113 /**
114 * Set the connect timeout
115 */
116 void setConnTimeout(int ms);
117
118 /**
119 * Set the receive timeout
120 */
121 void setRecvTimeout(int ms);
122
123 /**
124 * Set the send timeout
125 */
126 void setSendTimeout(int ms);
127
128
Mark Sleee8540632006-05-30 09:24:40 +0000129 private:
Mark Slee8d7e1f62006-06-07 06:48:56 +0000130 /**
131 * Constructor to create socket from raw UNIX handle. Never called directly
132 * but used by the TServerSocket class.
133 */
Mark Sleee8540632006-05-30 09:24:40 +0000134 TSocket(int socket);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000135
136 /** Host to connect to */
Mark Sleee8540632006-05-30 09:24:40 +0000137 std::string host_;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000138
139 /** Port number to connect on */
Mark Sleee8540632006-05-30 09:24:40 +0000140 int port_;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000141
142 /** Underlying UNIX socket handle */
Mark Sleee8540632006-05-30 09:24:40 +0000143 int socket_;
Mark Slee29050782006-09-29 00:12:30 +0000144
145 /** Connect timeout in ms */
146 int connTimeout_;
147
148 /** Send timeout in ms */
149 int sendTimeout_;
150
151 /** Recv timeout in ms */
152 int recvTimeout_;
153
154 /** Linger on */
155 bool lingerOn_;
156
157 /** Linger val */
158 int lingerVal_;
159
160 /** Nodelay */
161 bool noDelay_;
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000162
163 /** Recv timeout timeval */
164 struct timeval recvTimeval_;
Mark Sleee8540632006-05-30 09:24:40 +0000165};
166
Marc Slemko6f038a72006-08-03 18:58:09 +0000167}}} // facebook::thrift::transport
Mark Sleef5f2be42006-09-05 21:05:31 +0000168
169#endif // #ifndef _THRIFT_TRANSPORT_TSOCKET_H_
170