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> |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame^] | 5 | #include <sys/time.h> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 6 | |
Marc Slemko | d42a2c2 | 2006-08-10 03:30:18 +0000 | [diff] [blame] | 7 | #include "TTransport.h" |
| 8 | #include "TServerSocket.h" |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 9 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 10 | namespace facebook { namespace thrift { namespace transport { |
| 11 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 12 | /** |
| 13 | * TCP Socket implementation of the TTransport interface. |
| 14 | * |
| 15 | * @author Mark Slee <mcslee@facebook.com> |
| 16 | */ |
| 17 | class TSocket : public TTransport { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 18 | /** |
| 19 | * We allow the TServerSocket acceptImpl() method to access the private |
| 20 | * members of a socket so that it can access the TSocket(int socket) |
| 21 | * constructor which creates a socket object from the raw UNIX socket |
| 22 | * handle. |
| 23 | */ |
| 24 | friend class TServerSocket; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 25 | |
| 26 | public: |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 27 | /** |
| 28 | * Constructs a new socket. Note that this does NOT actually connect the |
| 29 | * socket. |
| 30 | * |
| 31 | * @param host An IP address or hostname to connect to |
| 32 | * @param port The port to connect on |
| 33 | */ |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 34 | TSocket(std::string host, int port); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * Destroyes the socket object, closing it if necessary. |
| 38 | */ |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 39 | ~TSocket(); |
| 40 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 41 | /** |
| 42 | * Whether the socket is alive. |
| 43 | * |
| 44 | * @return Is the socket alive? |
| 45 | */ |
| 46 | bool isOpen(); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 47 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 48 | /** |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame^] | 49 | * Calls select on the socket to see if there is more data available. |
| 50 | */ |
| 51 | bool peek(); |
| 52 | |
| 53 | /** |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 54 | * Creates and opens the UNIX socket. |
| 55 | * |
| 56 | * @throws TTransportException If the socket could not connect |
| 57 | */ |
| 58 | void open(); |
| 59 | |
| 60 | /** |
| 61 | * Shuts down communications on the socket. |
| 62 | */ |
| 63 | void close(); |
| 64 | |
| 65 | /** |
| 66 | * Reads from the underlying socket. |
| 67 | */ |
| 68 | uint32_t read(uint8_t* buf, uint32_t len); |
| 69 | |
| 70 | /** |
| 71 | * Writes to the underlying socket. |
| 72 | */ |
| 73 | void write(const uint8_t* buf, uint32_t len); |
| 74 | |
| 75 | /** |
| 76 | * Controls whether the linger option is set on the socket. |
| 77 | * |
| 78 | * @param on Whether SO_LINGER is on |
| 79 | * @param linger If linger is active, the number of seconds to linger for |
| 80 | */ |
| 81 | void setLinger(bool on, int linger); |
| 82 | |
| 83 | /** |
| 84 | * Whether to enable/disable Nagle's algorithm. |
| 85 | * |
| 86 | * @param noDelay Whether or not to disable the algorithm. |
| 87 | * @return |
| 88 | */ |
| 89 | void setNoDelay(bool noDelay); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 90 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 91 | /** |
| 92 | * Set the connect timeout |
| 93 | */ |
| 94 | void setConnTimeout(int ms); |
| 95 | |
| 96 | /** |
| 97 | * Set the receive timeout |
| 98 | */ |
| 99 | void setRecvTimeout(int ms); |
| 100 | |
| 101 | /** |
| 102 | * Set the send timeout |
| 103 | */ |
| 104 | void setSendTimeout(int ms); |
| 105 | |
| 106 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 107 | private: |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 108 | /** |
| 109 | * Constructor to create socket from raw UNIX handle. Never called directly |
| 110 | * but used by the TServerSocket class. |
| 111 | */ |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 112 | TSocket(int socket); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 113 | |
| 114 | /** Host to connect to */ |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 115 | std::string host_; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 116 | |
| 117 | /** Port number to connect on */ |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 118 | int port_; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 119 | |
| 120 | /** Underlying UNIX socket handle */ |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 121 | int socket_; |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 122 | |
| 123 | /** Connect timeout in ms */ |
| 124 | int connTimeout_; |
| 125 | |
| 126 | /** Send timeout in ms */ |
| 127 | int sendTimeout_; |
| 128 | |
| 129 | /** Recv timeout in ms */ |
| 130 | int recvTimeout_; |
| 131 | |
| 132 | /** Linger on */ |
| 133 | bool lingerOn_; |
| 134 | |
| 135 | /** Linger val */ |
| 136 | int lingerVal_; |
| 137 | |
| 138 | /** Nodelay */ |
| 139 | bool noDelay_; |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame^] | 140 | |
| 141 | /** Recv timeout timeval */ |
| 142 | struct timeval recvTimeval_; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 143 | }; |
| 144 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 145 | }}} // facebook::thrift::transport |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 146 | |
| 147 | #endif // #ifndef _THRIFT_TRANSPORT_TSOCKET_H_ |
| 148 | |