Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 1 | #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 Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 9 | /** |
| 10 | * TCP Socket implementation of the TTransport interface. |
| 11 | * |
| 12 | * @author Mark Slee <mcslee@facebook.com> |
| 13 | */ |
| 14 | class TSocket : public TTransport { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 15 | /** |
| 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 Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 22 | |
| 23 | public: |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 24 | /** |
| 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 Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 31 | TSocket(std::string host, int port); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Destroyes the socket object, closing it if necessary. |
| 35 | */ |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 36 | ~TSocket(); |
| 37 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 38 | /** |
| 39 | * Whether the socket is alive. |
| 40 | * |
| 41 | * @return Is the socket alive? |
| 42 | */ |
| 43 | bool isOpen(); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 44 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 45 | /** |
| 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 Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 82 | |
| 83 | private: |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 84 | /** |
| 85 | * Constructor to create socket from raw UNIX handle. Never called directly |
| 86 | * but used by the TServerSocket class. |
| 87 | */ |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 88 | TSocket(int socket); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 89 | |
| 90 | /** Host to connect to */ |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 91 | std::string host_; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 92 | |
| 93 | /** Port number to connect on */ |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 94 | int port_; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 95 | |
| 96 | /** Underlying UNIX socket handle */ |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 97 | int socket_; |
| 98 | }; |
| 99 | |
| 100 | #endif |