blob: bf90b63a47592bd2168fb1fcbe0cb9899258d038 [file] [log] [blame]
Mark Sleee8540632006-05-30 09:24:40 +00001#ifndef T_SOCKET_H
2#define T_SOCKET_H
3
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
85 private:
Mark Slee8d7e1f62006-06-07 06:48:56 +000086 /**
87 * Constructor to create socket from raw UNIX handle. Never called directly
88 * but used by the TServerSocket class.
89 */
Mark Sleee8540632006-05-30 09:24:40 +000090 TSocket(int socket);
Mark Slee8d7e1f62006-06-07 06:48:56 +000091
92 /** Host to connect to */
Mark Sleee8540632006-05-30 09:24:40 +000093 std::string host_;
Mark Slee8d7e1f62006-06-07 06:48:56 +000094
95 /** Port number to connect on */
Mark Sleee8540632006-05-30 09:24:40 +000096 int port_;
Mark Slee8d7e1f62006-06-07 06:48:56 +000097
98 /** Underlying UNIX socket handle */
Mark Sleee8540632006-05-30 09:24:40 +000099 int socket_;
100};
101
Marc Slemko6f038a72006-08-03 18:58:09 +0000102}}} // facebook::thrift::transport
Mark Sleee8540632006-05-30 09:24:40 +0000103#endif