blob: 9d8f082ff31647f1fa5eb42a9d943119dd96fe44 [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>
Aditya Agarwale04475b2007-05-23 02:14:58 +000022 * @author Aditya Agarwal <aditya@facebook.com>
Mark Sleee8540632006-05-30 09:24:40 +000023 */
24class TSocket : public TTransport {
Mark Slee8d7e1f62006-06-07 06:48:56 +000025 /**
26 * We allow the TServerSocket acceptImpl() method to access the private
27 * members of a socket so that it can access the TSocket(int socket)
28 * constructor which creates a socket object from the raw UNIX socket
29 * handle.
30 */
31 friend class TServerSocket;
Mark Sleee8540632006-05-30 09:24:40 +000032
33 public:
Mark Slee8d7e1f62006-06-07 06:48:56 +000034 /**
35 * Constructs a new socket. Note that this does NOT actually connect the
36 * socket.
37 *
Aditya Agarwalebc99e02007-01-15 23:14:58 +000038 */
39 TSocket();
40
41 /**
42 * Constructs a new socket. Note that this does NOT actually connect the
43 * socket.
44 *
Mark Slee8d7e1f62006-06-07 06:48:56 +000045 * @param host An IP address or hostname to connect to
46 * @param port The port to connect on
47 */
Mark Sleee8540632006-05-30 09:24:40 +000048 TSocket(std::string host, int port);
Mark Slee8d7e1f62006-06-07 06:48:56 +000049
50 /**
51 * Destroyes the socket object, closing it if necessary.
52 */
Mark Slee8a98e1b2007-02-27 05:16:23 +000053 virtual ~TSocket();
Mark Sleee8540632006-05-30 09:24:40 +000054
Mark Slee8d7e1f62006-06-07 06:48:56 +000055 /**
56 * Whether the socket is alive.
57 *
58 * @return Is the socket alive?
59 */
60 bool isOpen();
Mark Sleee8540632006-05-30 09:24:40 +000061
Mark Slee8d7e1f62006-06-07 06:48:56 +000062 /**
Mark Sleeb9ff32a2006-11-16 01:00:24 +000063 * Calls select on the socket to see if there is more data available.
64 */
65 bool peek();
66
67 /**
Mark Slee8d7e1f62006-06-07 06:48:56 +000068 * Creates and opens the UNIX socket.
69 *
70 * @throws TTransportException If the socket could not connect
71 */
jsobele02e4242007-05-08 17:51:49 +000072 virtual void open();
Mark Slee8d7e1f62006-06-07 06:48:56 +000073
74 /**
75 * Shuts down communications on the socket.
76 */
77 void close();
78
79 /**
80 * Reads from the underlying socket.
81 */
82 uint32_t read(uint8_t* buf, uint32_t len);
83
84 /**
85 * Writes to the underlying socket.
86 */
87 void write(const uint8_t* buf, uint32_t len);
88
89 /**
Aditya Agarwalebc99e02007-01-15 23:14:58 +000090 * Set the host that socket will connect to
91 *
92 * @param host host identifier
93 */
94 void setHost(std::string host);
95
96 /**
97 * Set the port that socket will connect to
98 *
99 * @param port port number
100 */
101 void setPort(int port);
102
103 /**
Mark Slee8d7e1f62006-06-07 06:48:56 +0000104 * Controls whether the linger option is set on the socket.
105 *
106 * @param on Whether SO_LINGER is on
107 * @param linger If linger is active, the number of seconds to linger for
108 */
109 void setLinger(bool on, int linger);
110
111 /**
112 * Whether to enable/disable Nagle's algorithm.
113 *
114 * @param noDelay Whether or not to disable the algorithm.
115 * @return
116 */
117 void setNoDelay(bool noDelay);
Mark Sleee8540632006-05-30 09:24:40 +0000118
Mark Slee29050782006-09-29 00:12:30 +0000119 /**
120 * Set the connect timeout
121 */
122 void setConnTimeout(int ms);
123
124 /**
125 * Set the receive timeout
126 */
127 void setRecvTimeout(int ms);
128
129 /**
130 * Set the send timeout
131 */
132 void setSendTimeout(int ms);
133
Aditya Agarwale04475b2007-05-23 02:14:58 +0000134 /**
135 * Set the max number of recv retries in case of an EAGAIN
136 * error
137 */
138 void setMaxRecvRetries(int maxRecvRetries);
139
Mark Slee8a98e1b2007-02-27 05:16:23 +0000140 protected:
Mark Slee8d7e1f62006-06-07 06:48:56 +0000141 /**
142 * Constructor to create socket from raw UNIX handle. Never called directly
143 * but used by the TServerSocket class.
144 */
Mark Sleee8540632006-05-30 09:24:40 +0000145 TSocket(int socket);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000146
Mark Slee6d56eb92007-07-06 22:28:15 +0000147 /** connect, called by open */
148 void openConnection(struct addrinfo *res);
149
Mark Slee8d7e1f62006-06-07 06:48:56 +0000150 /** Host to connect to */
Mark Sleee8540632006-05-30 09:24:40 +0000151 std::string host_;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000152
153 /** Port number to connect on */
Mark Sleee8540632006-05-30 09:24:40 +0000154 int port_;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000155
156 /** Underlying UNIX socket handle */
Mark Sleee8540632006-05-30 09:24:40 +0000157 int socket_;
Mark Slee29050782006-09-29 00:12:30 +0000158
159 /** Connect timeout in ms */
160 int connTimeout_;
161
162 /** Send timeout in ms */
163 int sendTimeout_;
164
165 /** Recv timeout in ms */
166 int recvTimeout_;
167
168 /** Linger on */
169 bool lingerOn_;
170
171 /** Linger val */
172 int lingerVal_;
173
174 /** Nodelay */
175 bool noDelay_;
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000176
Aditya Agarwale04475b2007-05-23 02:14:58 +0000177 /** Recv EGAIN retries */
178 int maxRecvRetries_;
179
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000180 /** Recv timeout timeval */
181 struct timeval recvTimeval_;
Mark Sleee8540632006-05-30 09:24:40 +0000182};
183
Marc Slemko6f038a72006-08-03 18:58:09 +0000184}}} // facebook::thrift::transport
Mark Sleef5f2be42006-09-05 21:05:31 +0000185
186#endif // #ifndef _THRIFT_TRANSPORT_TSOCKET_H_
187