blob: 18abfa715c9ff9a4ad0b724bc94eb77c03c910d7 [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
6#include "transport/TTransport.h"
7#include "transport/TServerSocket.h"
8
Mark Sleee8540632006-05-30 09:24:40 +00009/**
10 * TCP Socket implementation of the TTransport interface.
11 *
12 * @author Mark Slee <mcslee@facebook.com>
13 */
14class TSocket : public TTransport {
Mark Slee8d7e1f62006-06-07 06:48:56 +000015 /**
16 * We allow the TServerSocket acceptImpl() method to access the private
17 * members of a socket so that it can access the TSocket(int socket)
18 * constructor which creates a socket object from the raw UNIX socket
19 * handle.
20 */
21 friend class TServerSocket;
Mark Sleee8540632006-05-30 09:24:40 +000022
23 public:
Mark Slee8d7e1f62006-06-07 06:48:56 +000024 /**
25 * Constructs a new socket. Note that this does NOT actually connect the
26 * socket.
27 *
28 * @param host An IP address or hostname to connect to
29 * @param port The port to connect on
30 */
Mark Sleee8540632006-05-30 09:24:40 +000031 TSocket(std::string host, int port);
Mark Slee8d7e1f62006-06-07 06:48:56 +000032
33 /**
34 * Destroyes the socket object, closing it if necessary.
35 */
Mark Sleee8540632006-05-30 09:24:40 +000036 ~TSocket();
37
Mark Slee8d7e1f62006-06-07 06:48:56 +000038 /**
39 * Whether the socket is alive.
40 *
41 * @return Is the socket alive?
42 */
43 bool isOpen();
Mark Sleee8540632006-05-30 09:24:40 +000044
Mark Slee8d7e1f62006-06-07 06:48:56 +000045 /**
46 * Creates and opens the UNIX socket.
47 *
48 * @throws TTransportException If the socket could not connect
49 */
50 void open();
51
52 /**
53 * Shuts down communications on the socket.
54 */
55 void close();
56
57 /**
58 * Reads from the underlying socket.
59 */
60 uint32_t read(uint8_t* buf, uint32_t len);
61
62 /**
63 * Writes to the underlying socket.
64 */
65 void write(const uint8_t* buf, uint32_t len);
66
67 /**
68 * Controls whether the linger option is set on the socket.
69 *
70 * @param on Whether SO_LINGER is on
71 * @param linger If linger is active, the number of seconds to linger for
72 */
73 void setLinger(bool on, int linger);
74
75 /**
76 * Whether to enable/disable Nagle's algorithm.
77 *
78 * @param noDelay Whether or not to disable the algorithm.
79 * @return
80 */
81 void setNoDelay(bool noDelay);
Mark Sleee8540632006-05-30 09:24:40 +000082
83 private:
Mark Slee8d7e1f62006-06-07 06:48:56 +000084 /**
85 * Constructor to create socket from raw UNIX handle. Never called directly
86 * but used by the TServerSocket class.
87 */
Mark Sleee8540632006-05-30 09:24:40 +000088 TSocket(int socket);
Mark Slee8d7e1f62006-06-07 06:48:56 +000089
90 /** Host to connect to */
Mark Sleee8540632006-05-30 09:24:40 +000091 std::string host_;
Mark Slee8d7e1f62006-06-07 06:48:56 +000092
93 /** Port number to connect on */
Mark Sleee8540632006-05-30 09:24:40 +000094 int port_;
Mark Slee8d7e1f62006-06-07 06:48:56 +000095
96 /** Underlying UNIX socket handle */
Mark Sleee8540632006-05-30 09:24:40 +000097 int socket_;
98};
99
100#endif