Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 1 | #ifndef _THRIFT_TRANSPORT_TSOCKET_H_ |
| 2 | #define _THRIFT_TRANSPORT_TSOCKET_H_ 1 |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 3 | |
| 4 | #include <string> |
| 5 | |
Marc Slemko | d42a2c2 | 2006-08-10 03:30:18 +0000 | [diff] [blame] | 6 | #include "TTransport.h" |
| 7 | #include "TServerSocket.h" |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 8 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 9 | namespace facebook { namespace thrift { namespace transport { |
| 10 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 11 | /** |
| 12 | * TCP Socket implementation of the TTransport interface. |
| 13 | * |
| 14 | * @author Mark Slee <mcslee@facebook.com> |
| 15 | */ |
| 16 | class TSocket : public TTransport { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 17 | /** |
| 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 Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 24 | |
| 25 | public: |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 26 | /** |
| 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 Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 33 | TSocket(std::string host, int port); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * Destroyes the socket object, closing it if necessary. |
| 37 | */ |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 38 | ~TSocket(); |
| 39 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 40 | /** |
| 41 | * Whether the socket is alive. |
| 42 | * |
| 43 | * @return Is the socket alive? |
| 44 | */ |
| 45 | bool isOpen(); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 46 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 47 | /** |
| 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 Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 84 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 85 | /** |
| 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 Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 101 | private: |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 102 | /** |
| 103 | * Constructor to create socket from raw UNIX handle. Never called directly |
| 104 | * but used by the TServerSocket class. |
| 105 | */ |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 106 | TSocket(int socket); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 107 | |
| 108 | /** Host to connect to */ |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 109 | std::string host_; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 110 | |
| 111 | /** Port number to connect on */ |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 112 | int port_; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 113 | |
| 114 | /** Underlying UNIX socket handle */ |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 115 | int socket_; |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 116 | |
| 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 Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 134 | }; |
| 135 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 136 | }}} // facebook::thrift::transport |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 137 | |
| 138 | #endif // #ifndef _THRIFT_TRANSPORT_TSOCKET_H_ |
| 139 | |