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