blob: b946b6ae16b9fe511acf0b9c4e34245a87ef947f [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>
5
Marc Slemkod42a2c22006-08-10 03:30:18 +00006#include "TTransport.h"
7#include "TServerSocket.h"
Mark Sleee8540632006-05-30 09:24:40 +00008
Marc Slemko6f038a72006-08-03 18:58:09 +00009namespace facebook { namespace thrift { namespace transport {
10
Mark Sleee8540632006-05-30 09:24:40 +000011/**
12 * TCP Socket implementation of the TTransport interface.
13 *
14 * @author Mark Slee <mcslee@facebook.com>
15 */
16class TSocket : public TTransport {
Mark Slee8d7e1f62006-06-07 06:48:56 +000017 /**
18 * We allow the TServerSocket acceptImpl() method to access the private
19 * members of a socket so that it can access the TSocket(int socket)
20 * constructor which creates a socket object from the raw UNIX socket
21 * handle.
22 */
23 friend class TServerSocket;
Mark Sleee8540632006-05-30 09:24:40 +000024
25 public:
Mark Slee8d7e1f62006-06-07 06:48:56 +000026 /**
27 * Constructs a new socket. Note that this does NOT actually connect the
28 * socket.
29 *
30 * @param host An IP address or hostname to connect to
31 * @param port The port to connect on
32 */
Mark Sleee8540632006-05-30 09:24:40 +000033 TSocket(std::string host, int port);
Mark Slee8d7e1f62006-06-07 06:48:56 +000034
35 /**
36 * Destroyes the socket object, closing it if necessary.
37 */
Mark Sleee8540632006-05-30 09:24:40 +000038 ~TSocket();
39
Mark Slee8d7e1f62006-06-07 06:48:56 +000040 /**
41 * Whether the socket is alive.
42 *
43 * @return Is the socket alive?
44 */
45 bool isOpen();
Mark Sleee8540632006-05-30 09:24:40 +000046
Mark Slee8d7e1f62006-06-07 06:48:56 +000047 /**
48 * Creates and opens the UNIX socket.
49 *
50 * @throws TTransportException If the socket could not connect
51 */
52 void open();
53
54 /**
55 * Shuts down communications on the socket.
56 */
57 void close();
58
59 /**
60 * Reads from the underlying socket.
61 */
62 uint32_t read(uint8_t* buf, uint32_t len);
63
64 /**
65 * Writes to the underlying socket.
66 */
67 void write(const uint8_t* buf, uint32_t len);
68
69 /**
70 * Controls whether the linger option is set on the socket.
71 *
72 * @param on Whether SO_LINGER is on
73 * @param linger If linger is active, the number of seconds to linger for
74 */
75 void setLinger(bool on, int linger);
76
77 /**
78 * Whether to enable/disable Nagle's algorithm.
79 *
80 * @param noDelay Whether or not to disable the algorithm.
81 * @return
82 */
83 void setNoDelay(bool noDelay);
Mark Sleee8540632006-05-30 09:24:40 +000084
Mark Slee29050782006-09-29 00:12:30 +000085 /**
86 * Set the connect timeout
87 */
88 void setConnTimeout(int ms);
89
90 /**
91 * Set the receive timeout
92 */
93 void setRecvTimeout(int ms);
94
95 /**
96 * Set the send timeout
97 */
98 void setSendTimeout(int ms);
99
100
Mark Sleee8540632006-05-30 09:24:40 +0000101 private:
Mark Slee8d7e1f62006-06-07 06:48:56 +0000102 /**
103 * Constructor to create socket from raw UNIX handle. Never called directly
104 * but used by the TServerSocket class.
105 */
Mark Sleee8540632006-05-30 09:24:40 +0000106 TSocket(int socket);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000107
108 /** Host to connect to */
Mark Sleee8540632006-05-30 09:24:40 +0000109 std::string host_;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000110
111 /** Port number to connect on */
Mark Sleee8540632006-05-30 09:24:40 +0000112 int port_;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000113
114 /** Underlying UNIX socket handle */
Mark Sleee8540632006-05-30 09:24:40 +0000115 int socket_;
Mark Slee29050782006-09-29 00:12:30 +0000116
117 /** Connect timeout in ms */
118 int connTimeout_;
119
120 /** Send timeout in ms */
121 int sendTimeout_;
122
123 /** Recv timeout in ms */
124 int recvTimeout_;
125
126 /** Linger on */
127 bool lingerOn_;
128
129 /** Linger val */
130 int lingerVal_;
131
132 /** Nodelay */
133 bool noDelay_;
Mark Sleee8540632006-05-30 09:24:40 +0000134};
135
Marc Slemko6f038a72006-08-03 18:58:09 +0000136}}} // facebook::thrift::transport
Mark Sleef5f2be42006-09-05 21:05:31 +0000137
138#endif // #ifndef _THRIFT_TRANSPORT_TSOCKET_H_
139