blob: 30a09ac9698a25120c418c117e58e8214b91faa9 [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
Marc Slemko6f038a72006-08-03 18:58:09 +000016namespace facebook { namespace thrift { namespace transport {
17
Mark Sleee8540632006-05-30 09:24:40 +000018/**
19 * TCP Socket implementation of the TTransport interface.
20 *
21 * @author Mark Slee <mcslee@facebook.com>
22 */
23class TSocket : public TTransport {
Mark Slee8d7e1f62006-06-07 06:48:56 +000024 /**
25 * We allow the TServerSocket acceptImpl() method to access the private
26 * members of a socket so that it can access the TSocket(int socket)
27 * constructor which creates a socket object from the raw UNIX socket
28 * handle.
29 */
30 friend class TServerSocket;
Mark Sleee8540632006-05-30 09:24:40 +000031
32 public:
Mark Slee8d7e1f62006-06-07 06:48:56 +000033 /**
34 * Constructs a new socket. Note that this does NOT actually connect the
35 * socket.
36 *
Aditya Agarwalebc99e02007-01-15 23:14:58 +000037 */
38 TSocket();
39
40 /**
41 * Constructs a new socket. Note that this does NOT actually connect the
42 * socket.
43 *
Mark Slee8d7e1f62006-06-07 06:48:56 +000044 * @param host An IP address or hostname to connect to
45 * @param port The port to connect on
46 */
Mark Sleee8540632006-05-30 09:24:40 +000047 TSocket(std::string host, int port);
Mark Slee8d7e1f62006-06-07 06:48:56 +000048
49 /**
50 * Destroyes the socket object, closing it if necessary.
51 */
Mark Slee8a98e1b2007-02-27 05:16:23 +000052 virtual ~TSocket();
Mark Sleee8540632006-05-30 09:24:40 +000053
Mark Slee8d7e1f62006-06-07 06:48:56 +000054 /**
55 * Whether the socket is alive.
56 *
57 * @return Is the socket alive?
58 */
59 bool isOpen();
Mark Sleee8540632006-05-30 09:24:40 +000060
Mark Slee8d7e1f62006-06-07 06:48:56 +000061 /**
Mark Sleeb9ff32a2006-11-16 01:00:24 +000062 * Calls select on the socket to see if there is more data available.
63 */
64 bool peek();
65
66 /**
Mark Slee8d7e1f62006-06-07 06:48:56 +000067 * Creates and opens the UNIX socket.
68 *
69 * @throws TTransportException If the socket could not connect
70 */
71 void open();
72
73 /**
74 * Shuts down communications on the socket.
75 */
76 void close();
77
78 /**
79 * Reads from the underlying socket.
80 */
81 uint32_t read(uint8_t* buf, uint32_t len);
82
83 /**
84 * Writes to the underlying socket.
85 */
86 void write(const uint8_t* buf, uint32_t len);
87
88 /**
Aditya Agarwalebc99e02007-01-15 23:14:58 +000089 * Set the host that socket will connect to
90 *
91 * @param host host identifier
92 */
93 void setHost(std::string host);
94
95 /**
96 * Set the port that socket will connect to
97 *
98 * @param port port number
99 */
100 void setPort(int port);
101
102 /**
Mark Slee8d7e1f62006-06-07 06:48:56 +0000103 * Controls whether the linger option is set on the socket.
104 *
105 * @param on Whether SO_LINGER is on
106 * @param linger If linger is active, the number of seconds to linger for
107 */
108 void setLinger(bool on, int linger);
109
110 /**
111 * Whether to enable/disable Nagle's algorithm.
112 *
113 * @param noDelay Whether or not to disable the algorithm.
114 * @return
115 */
116 void setNoDelay(bool noDelay);
Mark Sleee8540632006-05-30 09:24:40 +0000117
Mark Slee29050782006-09-29 00:12:30 +0000118 /**
119 * Set the connect timeout
120 */
121 void setConnTimeout(int ms);
122
123 /**
124 * Set the receive timeout
125 */
126 void setRecvTimeout(int ms);
127
128 /**
129 * Set the send timeout
130 */
131 void setSendTimeout(int ms);
132
Mark Slee8a98e1b2007-02-27 05:16:23 +0000133 protected:
Mark Slee8d7e1f62006-06-07 06:48:56 +0000134 /**
135 * Constructor to create socket from raw UNIX handle. Never called directly
136 * but used by the TServerSocket class.
137 */
Mark Sleee8540632006-05-30 09:24:40 +0000138 TSocket(int socket);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000139
140 /** Host to connect to */
Mark Sleee8540632006-05-30 09:24:40 +0000141 std::string host_;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000142
143 /** Port number to connect on */
Mark Sleee8540632006-05-30 09:24:40 +0000144 int port_;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000145
146 /** Underlying UNIX socket handle */
Mark Sleee8540632006-05-30 09:24:40 +0000147 int socket_;
Mark Slee29050782006-09-29 00:12:30 +0000148
149 /** Connect timeout in ms */
150 int connTimeout_;
151
152 /** Send timeout in ms */
153 int sendTimeout_;
154
155 /** Recv timeout in ms */
156 int recvTimeout_;
157
158 /** Linger on */
159 bool lingerOn_;
160
161 /** Linger val */
162 int lingerVal_;
163
164 /** Nodelay */
165 bool noDelay_;
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000166
167 /** Recv timeout timeval */
168 struct timeval recvTimeval_;
Mark Sleee8540632006-05-30 09:24:40 +0000169};
170
Marc Slemko6f038a72006-08-03 18:58:09 +0000171}}} // facebook::thrift::transport
Mark Sleef5f2be42006-09-05 21:05:31 +0000172
173#endif // #ifndef _THRIFT_TRANSPORT_TSOCKET_H_
174