Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 1 | #ifndef T_TRANSPORT_H |
| 2 | #define T_TRANSPORT_H |
| 3 | |
| 4 | #include <string> |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 5 | #include "transport/TTransportException.h" |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 6 | |
| 7 | /** |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 8 | * Generic interface for a method of transporting data. A TTransport may be |
| 9 | * capable of either reading or writing, but not necessarily both. |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 10 | * |
| 11 | * @author Mark Slee <mcslee@facebook.com> |
| 12 | */ |
| 13 | class TTransport { |
| 14 | public: |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 15 | /** |
| 16 | * Virtual deconstructor. |
| 17 | */ |
| 18 | virtual ~TTransport() {} |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 19 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 20 | /** |
| 21 | * Whether this transport is open. |
| 22 | */ |
| 23 | virtual bool isOpen() { return false; } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 24 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 25 | /** |
| 26 | * Opens the transport for communications. |
| 27 | * |
| 28 | * @return bool Whether the transport was successfully opened |
| 29 | * @throws TTransportException if opening failed |
| 30 | */ |
| 31 | virtual void open() { |
| 32 | throw TTransportException(TTX_NOT_OPEN, "Cannot open base TTransport."); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Closes the transport. |
| 37 | */ |
| 38 | virtual void close() { |
| 39 | throw TTransportException(TTX_NOT_OPEN, "Cannot close base TTransport."); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Attempt to read up to the specified number of bytes into the string. |
| 44 | * |
| 45 | * @param s Reference to the location to append the read data |
| 46 | * @param len How many bytes to read |
| 47 | * @return How many bytes were actually read |
| 48 | * @throws TTransportException If an error occurs |
| 49 | */ |
| 50 | virtual uint32_t read(uint8_t* buf, uint32_t len) { |
| 51 | throw TTransportException(TTX_NOT_OPEN, "Base TTransport cannot read."); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Reads the given amount of data in its entirety no matter what. |
| 56 | * |
| 57 | * @param s Reference to location for read data |
| 58 | * @param len How many bytes to read |
| 59 | * @return How many bytes read, which must be equal to size |
| 60 | * @throws TTransportException If insufficient data was read |
| 61 | */ |
| 62 | virtual uint32_t readAll(uint8_t* buf, uint32_t len) { |
| 63 | uint32_t have = 0; |
| 64 | |
| 65 | while (have < len) { |
| 66 | have += read(buf+have, len-have); |
| 67 | } |
| 68 | |
| 69 | return have; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Writes the string in its entirety to the buffer. |
| 74 | * |
| 75 | * @param s The string to write out |
| 76 | * @throws TTransportException if an error occurs |
| 77 | */ |
| 78 | virtual void write(const uint8_t* buf, uint32_t len) { |
| 79 | throw TTransportException(TTX_NOT_OPEN, "Base TTransport cannot write."); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Flushes any pending data to be written. Typically used with buffered |
| 84 | * transport mechanisms. |
| 85 | * |
| 86 | * @throws TTransportException if an error occurs |
| 87 | */ |
| 88 | virtual void flush() {} |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 89 | |
| 90 | protected: |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 91 | /** |
| 92 | * Simple constructor. |
| 93 | */ |
| 94 | TTransport() {} |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | #endif |