Allow optional framing of input/output in FramedTransport
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@664836 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/cpp/src/transport/TFramedTransport.cc b/lib/cpp/src/transport/TFramedTransport.cc
index 6ab9dcc..a05fd27 100644
--- a/lib/cpp/src/transport/TFramedTransport.cc
+++ b/lib/cpp/src/transport/TFramedTransport.cc
@@ -6,6 +6,10 @@
namespace facebook { namespace thrift { namespace transport {
uint32_t TFramedTransport::read(uint8_t* buf, uint32_t len) {
+ if (!read_) {
+ return transport_->read(buf, len);
+ }
+
uint32_t need = len;
// We don't have enough data yet
@@ -60,6 +64,12 @@
return;
}
+ // Shortcut out if not write mode
+ if (!write_) {
+ transport_->write(buf, len);
+ return;
+ }
+
// Need to grow the buffer
if (len + wLen_ >= wBufSize_) {
@@ -85,6 +95,11 @@
}
void TFramedTransport::flush() {
+ if (!write_) {
+ transport_->flush();
+ return;
+ }
+
// Write frame size
int32_t sz = wLen_;
sz = (int32_t)htonl(sz);
diff --git a/lib/cpp/src/transport/TFramedTransport.h b/lib/cpp/src/transport/TFramedTransport.h
index f0234ce..37b82b4 100644
--- a/lib/cpp/src/transport/TFramedTransport.h
+++ b/lib/cpp/src/transport/TFramedTransport.h
@@ -21,16 +21,24 @@
public:
TFramedTransport(shared_ptr<TTransport> transport) :
transport_(transport),
- rPos_(0), rLen_(0),
- wBufSize_(512), wLen_(0) {
+ rPos_(0),
+ rLen_(0),
+ read_(true),
+ wBufSize_(512),
+ wLen_(0),
+ write_(true) {
rBuf_ = NULL;
wBuf_ = new uint8_t[wBufSize_];
}
TFramedTransport(shared_ptr<TTransport> transport, uint32_t sz) :
transport_(transport),
- rPos_(0), rLen_(0),
- wBufSize_(sz), wLen_(0) {
+ rPos_(0),
+ rLen_(0),
+ read_(true),
+ wBufSize_(sz),
+ wLen_(0),
+ write_(true) {
rBuf_ = NULL;
wBuf_ = new uint8_t[wBufSize_];
}
@@ -44,18 +52,26 @@
}
}
- bool isOpen() {
- return transport_->isOpen();
+ void setRead(bool read) {
+ read_ = read;
}
-
+
+ void setWrite(bool write) {
+ write_ = write;
+ }
+
void open() {
transport_->open();
}
+ bool isOpen() {
+ return transport_->isOpen();
+ }
+
void close() {
transport_->close();
}
-
+
uint32_t read(uint8_t* buf, uint32_t len);
void write(const uint8_t* buf, uint32_t len);
@@ -67,10 +83,12 @@
uint8_t* rBuf_;
uint32_t rPos_;
uint32_t rLen_;
+ bool read_;
uint8_t* wBuf_;
uint32_t wBufSize_;
uint32_t wLen_;
+ bool write_;
/**
* Reads a frame of input from the underlying stream.