From 9e7734dada3606d54b9ca520fac4f1f93e4ffd83 Mon Sep 17 00:00:00 2001 From: Mark Slee Date: Fri, 8 Sep 2006 03:51:34 +0000 Subject: [PATCH] Thrift: Rename chunked to framed transports git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@664796 13f79535-47bb-0310-9956-ffa450edef68 --- lib/cpp/Makefile.am | 4 +-- ...hunkedTransport.cc => TFramedTransport.cc} | 27 +++++++++++-------- ...TChunkedTransport.h => TFramedTransport.h} | 20 +++++++------- lib/php/Makefile.am | 2 +- ...nkedTransport.php => TFramedTransport.php} | 8 +++--- 5 files changed, 33 insertions(+), 28 deletions(-) rename lib/cpp/src/transport/{TChunkedTransport.cc => TFramedTransport.cc} (75%) rename lib/cpp/src/transport/{TChunkedTransport.h => TFramedTransport.h} (72%) rename lib/php/src/transport/{TChunkedTransport.php => TFramedTransport.php} (91%) diff --git a/lib/cpp/Makefile.am b/lib/cpp/Makefile.am index 554cb90f..4de829f1 100644 --- a/lib/cpp/Makefile.am +++ b/lib/cpp/Makefile.am @@ -11,7 +11,7 @@ libthrift_sources = src/concurrency/Monitor.cc \ src/concurrency/TimerManager.cc \ src/protocol/TBinaryProtocol.cc \ src/transport/TBufferedTransport.cc \ - src/transport/TChunkedTransport.cc \ + src/transport/TFramedTransport.cc \ src/transport/TSocket.cc \ src/transport/TServerSocket.cc \ src/server/TSimpleServer.cc \ @@ -48,7 +48,7 @@ include_protocol_HEADERS = \ include_transportdir = $(include_thriftdir)/transport include_transport_HEADERS = \ src/transport/TBufferedTransport.h \ - src/transport/TChunkedTransport.h \ + src/transport/TFramedTransport.h \ src/transport/TNullTransport.h \ src/transport/TServerSocket.h \ src/transport/TServerTransport.h \ diff --git a/lib/cpp/src/transport/TChunkedTransport.cc b/lib/cpp/src/transport/TFramedTransport.cc similarity index 75% rename from lib/cpp/src/transport/TChunkedTransport.cc rename to lib/cpp/src/transport/TFramedTransport.cc index bb42e380..6ab9dcc5 100644 --- a/lib/cpp/src/transport/TChunkedTransport.cc +++ b/lib/cpp/src/transport/TFramedTransport.cc @@ -1,9 +1,11 @@ -#include "TChunkedTransport.h" +#include +#include + using std::string; namespace facebook { namespace thrift { namespace transport { -uint32_t TChunkedTransport::read(uint8_t* buf, uint32_t len) { +uint32_t TFramedTransport::read(uint8_t* buf, uint32_t len) { uint32_t need = len; // We don't have enough data yet @@ -16,7 +18,7 @@ uint32_t TChunkedTransport::read(uint8_t* buf, uint32_t len) { } // Read another chunk - readChunk(); + readFrame(); } // Hand over whatever we have @@ -30,8 +32,8 @@ uint32_t TChunkedTransport::read(uint8_t* buf, uint32_t len) { return (len - need); } -void TChunkedTransport::readChunk() { - // Get rid of the old chunk +void TFramedTransport::readFrame() { + // Get rid of the old frame if (rBuf_ != NULL) { delete [] rBuf_; rBuf_ = NULL; @@ -40,19 +42,20 @@ void TChunkedTransport::readChunk() { // Read in the next chunk size int32_t sz; transport_->readAll((uint8_t*)&sz, 4); + sz = (int32_t)ntohl(sz); if (sz < 0) { - throw new TTransportException("Next chunk has negative size"); + throw new TTransportException("Frame size has negative value"); } - // Read the chunk payload, reset markers + // Read the frame payload, reset markers rBuf_ = new uint8_t[sz]; transport_->readAll(rBuf_, sz); rPos_ = 0; rLen_ = sz; } -void TChunkedTransport::write(const uint8_t* buf, uint32_t len) { +void TFramedTransport::write(const uint8_t* buf, uint32_t len) { if (len == 0) { return; } @@ -81,12 +84,14 @@ void TChunkedTransport::write(const uint8_t* buf, uint32_t len) { wLen_ += len; } -void TChunkedTransport::flush() { - // Write chunk size +void TFramedTransport::flush() { + // Write frame size int32_t sz = wLen_; + sz = (int32_t)htonl(sz); + transport_->write((const uint8_t*)&sz, 4); - // Write chunk body + // Write frame body if (sz > 0) { transport_->write(wBuf_, wLen_); } diff --git a/lib/cpp/src/transport/TChunkedTransport.h b/lib/cpp/src/transport/TFramedTransport.h similarity index 72% rename from lib/cpp/src/transport/TChunkedTransport.h rename to lib/cpp/src/transport/TFramedTransport.h index ebc27801..f0234cea 100644 --- a/lib/cpp/src/transport/TChunkedTransport.h +++ b/lib/cpp/src/transport/TFramedTransport.h @@ -1,5 +1,5 @@ -#ifndef _THRIFT_TRANSPORT_TCHUNKEDTRANSPORT_H_ -#define _THRIFT_TRANSPORT_TCHUNKEDTRANSPORT_H_ 1 +#ifndef _THRIFT_TRANSPORT_TFRAMEDTRANSPORT_H_ +#define _THRIFT_TRANSPORT_TFRAMEDTRANSPORT_H_ 1 #include "TTransport.h" #include @@ -10,16 +10,16 @@ namespace facebook { namespace thrift { namespace transport { using namespace boost; /** - * Chunked transport. All writes go into an in-memory buffer until flush is + * Framed transport. All writes go into an in-memory buffer until flush is * called, at which point the transport writes the length of the entire * binary chunk followed by the data payload. This allows the receiver on the * other end to always do fixed-length reads. * * @author Mark Slee */ -class TChunkedTransport : public TTransport { +class TFramedTransport : public TTransport { public: - TChunkedTransport(shared_ptr transport) : + TFramedTransport(shared_ptr transport) : transport_(transport), rPos_(0), rLen_(0), wBufSize_(512), wLen_(0) { @@ -27,7 +27,7 @@ class TChunkedTransport : public TTransport { wBuf_ = new uint8_t[wBufSize_]; } - TChunkedTransport(shared_ptr transport, uint32_t sz) : + TFramedTransport(shared_ptr transport, uint32_t sz) : transport_(transport), rPos_(0), rLen_(0), wBufSize_(sz), wLen_(0) { @@ -35,7 +35,7 @@ class TChunkedTransport : public TTransport { wBuf_ = new uint8_t[wBufSize_]; } - ~TChunkedTransport() { + ~TFramedTransport() { if (rBuf_ != NULL) { delete [] rBuf_; } @@ -73,11 +73,11 @@ class TChunkedTransport : public TTransport { uint32_t wLen_; /** - * Reads a chunk of input from the underlying stream. + * Reads a frame of input from the underlying stream. */ - void readChunk(); + void readFrame(); }; }}} // facebook::thrift::transport -#endif // #ifndef _THRIFT_TRANSPORT_TCHUNKEDTRANSPORT_H_ +#endif // #ifndef _THRIFT_TRANSPORT_TFRAMEDTRANSPORT_H_ diff --git a/lib/php/Makefile.am b/lib/php/Makefile.am index c7cd1069..0521c777 100644 --- a/lib/php/Makefile.am +++ b/lib/php/Makefile.am @@ -6,7 +6,7 @@ protocol_SCRIPTS = src/protocol/TProtocol.php \ transport_SCRIPTS = src/transport/TTransport.php \ src/transport/TBufferedTransport.php \ - src/transport/TChunkedTransport.php \ + src/transport/TFramedTransport.php \ src/transport/TSocket.php \ src/transport/TSocketPool.php diff --git a/lib/php/src/transport/TChunkedTransport.php b/lib/php/src/transport/TFramedTransport.php similarity index 91% rename from lib/php/src/transport/TChunkedTransport.php rename to lib/php/src/transport/TFramedTransport.php index 04fba1ff..2d41b400 100644 --- a/lib/php/src/transport/TChunkedTransport.php +++ b/lib/php/src/transport/TFramedTransport.php @@ -1,13 +1,13 @@ */ -class TChunkedTransport extends TTransport { +class TFramedTransport extends TTransport { /** * Underlying transport object. @@ -52,7 +52,7 @@ class TChunkedTransport extends TTransport { if ($need > $have) { $out = $this->rBuf_; $need -= $have; - $this->readChunk(); + $this->readFrame(); } $give = $need; @@ -70,7 +70,7 @@ class TChunkedTransport extends TTransport { /** * Reads a chunk of data into the internal read buffer. */ - private function readChunk() { + private function readFrame() { $buf = $this->transport_->readAll(4); $val = unpack('N', $buf); $sz = $val[1]; -- 2.17.1