From 83303e1cf7157e5c9862feacf3994782c1540eff Mon Sep 17 00:00:00 2001 From: Mark Slee Date: Wed, 25 Oct 2006 01:29:37 +0000 Subject: [PATCH] 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 --- lib/cpp/src/transport/TFramedTransport.cc | 15 ++++++++++ lib/cpp/src/transport/TFramedTransport.h | 34 +++++++++++++++++------ 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/lib/cpp/src/transport/TFramedTransport.cc b/lib/cpp/src/transport/TFramedTransport.cc index 6ab9dcc5..a05fd27e 100644 --- a/lib/cpp/src/transport/TFramedTransport.cc +++ b/lib/cpp/src/transport/TFramedTransport.cc @@ -6,6 +6,10 @@ using std::string; 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 @@ void TFramedTransport::write(const uint8_t* buf, uint32_t len) { 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::write(const uint8_t* buf, uint32_t len) { } 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 f0234cea..37b82b42 100644 --- a/lib/cpp/src/transport/TFramedTransport.h +++ b/lib/cpp/src/transport/TFramedTransport.h @@ -21,16 +21,24 @@ class TFramedTransport : public TTransport { public: TFramedTransport(shared_ptr 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 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 @@ class TFramedTransport : public TTransport { } } - 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 @@ class TFramedTransport : public TTransport { 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. -- 2.17.1