blob: 8137984aae1a03133fb2d2312da538ffd665c879 [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 *
31 * @param host An IP address or hostname to connect to
32 * @param port The port to connect on
33 */
Mark Sleee8540632006-05-30 09:24:40 +000034 TSocket(std::string host, int port);
Mark Slee8d7e1f62006-06-07 06:48:56 +000035
36 /**
37 * Destroyes the socket object, closing it if necessary.
38 */
Mark Sleee8540632006-05-30 09:24:40 +000039 ~TSocket();
40
Mark Slee8d7e1f62006-06-07 06:48:56 +000041 /**
42 * Whether the socket is alive.
43 *
44 * @return Is the socket alive?
45 */
46 bool isOpen();
Mark Sleee8540632006-05-30 09:24:40 +000047
Mark Slee8d7e1f62006-06-07 06:48:56 +000048 /**
Mark Sleeb9ff32a2006-11-16 01:00:24 +000049 * Calls select on the socket to see if there is more data available.
50 */
51 bool peek();
52
53 /**
Mark Slee8d7e1f62006-06-07 06:48:56 +000054 * Creates and opens the UNIX socket.
55 *
56 * @throws TTransportException If the socket could not connect
57 */
58 void open();
59
60 /**
61 * Shuts down communications on the socket.
62 */
63 void close();
64
65 /**
66 * Reads from the underlying socket.
67 */
68 uint32_t read(uint8_t* buf, uint32_t len);
69
70 /**
71 * Writes to the underlying socket.
72 */
73 void write(const uint8_t* buf, uint32_t len);
74
75 /**
76 * Controls whether the linger option is set on the socket.
77 *
78 * @param on Whether SO_LINGER is on
79 * @param linger If linger is active, the number of seconds to linger for
80 */
81 void setLinger(bool on, int linger);
82
83 /**
84 * Whether to enable/disable Nagle's algorithm.
85 *
86 * @param noDelay Whether or not to disable the algorithm.
87 * @return
88 */
89 void setNoDelay(bool noDelay);
Mark Sleee8540632006-05-30 09:24:40 +000090
Mark Slee29050782006-09-29 00:12:30 +000091 /**
92 * Set the connect timeout
93 */
94 void setConnTimeout(int ms);
95
96 /**
97 * Set the receive timeout
98 */
99 void setRecvTimeout(int ms);
100
101 /**
102 * Set the send timeout
103 */
104 void setSendTimeout(int ms);
105
106
Mark Sleee8540632006-05-30 09:24:40 +0000107 private:
Mark Slee8d7e1f62006-06-07 06:48:56 +0000108 /**
109 * Constructor to create socket from raw UNIX handle. Never called directly
110 * but used by the TServerSocket class.
111 */
Mark Sleee8540632006-05-30 09:24:40 +0000112 TSocket(int socket);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000113
114 /** Host to connect to */
Mark Sleee8540632006-05-30 09:24:40 +0000115 std::string host_;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000116
117 /** Port number to connect on */
Mark Sleee8540632006-05-30 09:24:40 +0000118 int port_;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000119
120 /** Underlying UNIX socket handle */
Mark Sleee8540632006-05-30 09:24:40 +0000121 int socket_;
Mark Slee29050782006-09-29 00:12:30 +0000122
123 /** Connect timeout in ms */
124 int connTimeout_;
125
126 /** Send timeout in ms */
127 int sendTimeout_;
128
129 /** Recv timeout in ms */
130 int recvTimeout_;
131
132 /** Linger on */
133 bool lingerOn_;
134
135 /** Linger val */
136 int lingerVal_;
137
138 /** Nodelay */
139 bool noDelay_;
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000140
141 /** Recv timeout timeval */
142 struct timeval recvTimeval_;
Mark Sleee8540632006-05-30 09:24:40 +0000143};
144
Marc Slemko6f038a72006-08-03 18:58:09 +0000145}}} // facebook::thrift::transport
Mark Sleef5f2be42006-09-05 21:05:31 +0000146
147#endif // #ifndef _THRIFT_TRANSPORT_TSOCKET_H_
148