Thrift TBinaryProtocol change

Summary: New Thrift TBinaryProtocol with a version identifier

Reviewed By: aditya, eugene

Test Plan: Modify your services to have strictRead_ and strictWrite_ both set to FALSE. Then redeploy your services and test running clients against them. Once you have clients and servers running stably on this new code, you should redploy versions with strictWrite_ set to TRUE. Once that's all good, we can set strictRead_ to TRUE as well, and eventually deprecate the old protocol code entirely.


git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665138 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/cpp/src/protocol/TBinaryProtocol.h b/lib/cpp/src/protocol/TBinaryProtocol.h
index 89220e2..6cf2c4b 100644
--- a/lib/cpp/src/protocol/TBinaryProtocol.h
+++ b/lib/cpp/src/protocol/TBinaryProtocol.h
@@ -20,20 +20,30 @@
  * @author Mark Slee <mcslee@facebook.com>
  */
 class TBinaryProtocol : public TProtocol {
+ protected:
+  static const int32_t VERSION_MASK = 0xffff0000;
+  static const int32_t VERSION_1 = 0x80010000;
+
  public:
   TBinaryProtocol(boost::shared_ptr<TTransport> trans) :
     TProtocol(trans),
     string_limit_(0),
     container_limit_(0),
+    strict_read_(false),
+    strict_write_(true),
     string_buf_(NULL),
     string_buf_size_(0) {}
 
   TBinaryProtocol(boost::shared_ptr<TTransport> trans,
                   int32_t string_limit,
-                  int32_t container_limit) :
+                  int32_t container_limit,
+                  bool strict_read,
+                  bool strict_write) :
     TProtocol(trans),
     string_limit_(string_limit),
     container_limit_(container_limit),
+    strict_read_(strict_read),
+    strict_write_(strict_write),
     string_buf_(NULL),
     string_buf_size_(0) {}
 
@@ -52,6 +62,11 @@
     container_limit_ = container_limit;
   }
 
+  void setStrict(bool strict_read, bool strict_write) {
+    strict_read_ = strict_read;
+    strict_write_ = strict_write;
+  }
+
   /**
    * Writing functions.
    */
@@ -157,10 +172,17 @@
 
   uint32_t readString(std::string& str);
 
+ protected:
+  uint32_t readStringBody(std::string& str, int32_t sz);
+
  private:
   int32_t string_limit_;
   int32_t container_limit_;
 
+  // Enforce presence of version identifier
+  bool strict_read_;
+  bool strict_write_;
+
   // Buffer for reading strings, save for the lifetime of the protocol to
   // avoid memory churn allocating memory on every string read
   uint8_t* string_buf_;
@@ -175,11 +197,15 @@
  public:
   TBinaryProtocolFactory() :
     string_limit_(0),
-    container_limit_(0) {}
+    container_limit_(0),
+    strict_read_(false),
+    strict_write_(true) {}
 
-  TBinaryProtocolFactory(int32_t string_limit, int32_t container_limit) :
+  TBinaryProtocolFactory(int32_t string_limit, int32_t container_limit, bool strict_read, bool strict_write) :
     string_limit_(string_limit),
-    container_limit_(container_limit) {}
+    container_limit_(container_limit),
+    strict_read_(strict_read),
+    strict_write_(strict_write) {}
 
   virtual ~TBinaryProtocolFactory() {}
 
@@ -191,13 +217,20 @@
     container_limit_ = container_limit;
   }
 
+  void setStrict(bool strict_read, bool strict_write) {
+    strict_read_ = strict_read;
+    strict_write_ = strict_write;
+  }
+
   boost::shared_ptr<TProtocol> getProtocol(boost::shared_ptr<TTransport> trans) {
-    return boost::shared_ptr<TProtocol>(new TBinaryProtocol(trans, string_limit_, container_limit_));
+    return boost::shared_ptr<TProtocol>(new TBinaryProtocol(trans, string_limit_, container_limit_, strict_read_, strict_write_));
   }
 
  private:
   int32_t string_limit_;
   int32_t container_limit_;
+  bool strict_read_;
+  bool strict_write_;
 
 };