blob: 991b50c80c93cf2f03d91bc75d2fd4659b92e506 [file] [log] [blame]
Mark Slee8d7e1f62006-06-07 06:48:56 +00001#ifndef T_BUFFERED_TRANSPORT_H
2#define T_BUFFERED_TRANSPORT_H
3
4#include "transport/TTransport.h"
5#include <string>
6
7/**
8 * Buffered transport. For reads it will read more data than is requested
9 * and will serve future data out of a local buffer. For writes, data is
10 * stored to an in memory buffer before being written out.
11 *
12 * @author Mark Slee <mcslee@facebook.com>
13 */
14class TBufferedTransport : public TTransport {
15 public:
16 TBufferedTransport(TTransport* transport) :
17 transport_(transport),
18 rBufSize_(512), rPos_(0), rLen_(0),
19 wBufSize_(512), wLen_(0) {
20 rBuf_ = new uint8_t[rBufSize_];
21 wBuf_ = new uint8_t[wBufSize_];
22 }
23
24 TBufferedTransport(TTransport* transport, uint32_t sz) :
25 transport_(transport),
26 rBufSize_(sz), rPos_(0), rLen_(0),
27 wBufSize_(sz), wLen_(0) {
28 rBuf_ = new uint8_t[rBufSize_];
29 wBuf_ = new uint8_t[wBufSize_];
30 }
31
32 TBufferedTransport(TTransport* transport, uint32_t rsz, uint32_t wsz) :
33 transport_(transport),
34 rBufSize_(rsz), rPos_(0), rLen_(0),
35 wBufSize_(wsz), wLen_(0) {
36 rBuf_ = new uint8_t[rBufSize_];
37 wBuf_ = new uint8_t[wBufSize_];
38 }
39
40 ~TBufferedTransport() {
41 delete [] rBuf_;
42 delete [] wBuf_;
43 }
44
45 bool isOpen() {
46 return transport_->isOpen();
47 }
48
49 void open() {
50 transport_->open();
51 }
52
53 void close() {
54 transport_->close();
55 }
56
57 uint32_t read(uint8_t* buf, uint32_t len);
58
59 void write(const uint8_t* buf, uint32_t len);
60
61 void flush();
62
63 protected:
64 TTransport* transport_;
65 uint8_t* rBuf_;
66 uint32_t rBufSize_;
67 uint32_t rPos_;
68 uint32_t rLen_;
69
70 uint8_t* wBuf_;
71 uint32_t wBufSize_;
72 uint32_t wLen_;
73};
74
75#endif