blob: fcaece77a8daec3144b6901c7589f949457516c6 [file] [log] [blame]
Mark Sleee8540632006-05-30 09:24:40 +00001#ifndef T_TRANSPORT_H
2#define T_TRANSPORT_H
3
4#include <string>
Mark Slee8d7e1f62006-06-07 06:48:56 +00005#include "transport/TTransportException.h"
Mark Sleee8540632006-05-30 09:24:40 +00006
7/**
Mark Slee8d7e1f62006-06-07 06:48:56 +00008 * Generic interface for a method of transporting data. A TTransport may be
9 * capable of either reading or writing, but not necessarily both.
Mark Sleee8540632006-05-30 09:24:40 +000010 *
11 * @author Mark Slee <mcslee@facebook.com>
12 */
13class TTransport {
14 public:
Mark Slee8d7e1f62006-06-07 06:48:56 +000015 /**
16 * Virtual deconstructor.
17 */
18 virtual ~TTransport() {}
Mark Sleee8540632006-05-30 09:24:40 +000019
Mark Slee8d7e1f62006-06-07 06:48:56 +000020 /**
21 * Whether this transport is open.
22 */
23 virtual bool isOpen() { return false; }
Mark Sleee8540632006-05-30 09:24:40 +000024
Mark Slee8d7e1f62006-06-07 06:48:56 +000025 /**
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 Sleee8540632006-05-30 09:24:40 +000089
90 protected:
Mark Slee8d7e1f62006-06-07 06:48:56 +000091 /**
92 * Simple constructor.
93 */
94 TTransport() {}
Mark Sleee8540632006-05-30 09:24:40 +000095};
96
97#endif