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