blob: de9a836f1e8193d08f80188ec69d3e9796356b4a [file] [log] [blame]
Mark Sleef5f2be42006-09-05 21:05:31 +00001#ifndef _THRIFT_PROTOCOL_TBINARYPROTOCOL_H_
2#define _THRIFT_PROTOCOL_TBINARYPROTOCOL_H_ 1
Mark Sleee8540632006-05-30 09:24:40 +00003
Marc Slemkod42a2c22006-08-10 03:30:18 +00004#include "TProtocol.h"
Marc Slemko16698852006-08-04 03:16:10 +00005
6#include <boost/shared_ptr.hpp>
Mark Sleee8540632006-05-30 09:24:40 +00007
Marc Slemko6f038a72006-08-03 18:58:09 +00008namespace facebook { namespace thrift { namespace protocol {
9
Marc Slemko16698852006-08-04 03:16:10 +000010using namespace boost;
11
Mark Sleee8540632006-05-30 09:24:40 +000012/**
13 * The default binary protocol for thrift. Writes all data in a very basic
14 * binary format, essentially just spitting out the raw bytes.
15 *
16 * @author Mark Slee <mcslee@facebook.com>
17 */
Marc Slemko0b4ffa92006-08-11 02:49:29 +000018 class TBinaryProtocol : public TProtocol {
19 public:
Mark Slee4af6ed72006-10-25 19:02:49 +000020 TBinaryProtocol(shared_ptr<TTransport> in, shared_ptr<TTransport> out) :
21 TProtocol(in, out) {}
22
Mark Sleee8540632006-05-30 09:24:40 +000023 ~TBinaryProtocol() {}
24
Mark Slee8d7e1f62006-06-07 06:48:56 +000025 /**
26 * Writing functions.
27 */
Mark Sleee8540632006-05-30 09:24:40 +000028
Mark Slee4af6ed72006-10-25 19:02:49 +000029 virtual uint32_t writeMessageBegin(const std::string name,
Marc Slemko16698852006-08-04 03:16:10 +000030 const TMessageType messageType,
Mark Slee4af6ed72006-10-25 19:02:49 +000031 const int32_t seqid);
Marc Slemko16698852006-08-04 03:16:10 +000032
Mark Slee4af6ed72006-10-25 19:02:49 +000033 virtual uint32_t writeMessageEnd();
Marc Slemko16698852006-08-04 03:16:10 +000034
35
Mark Slee4af6ed72006-10-25 19:02:49 +000036 uint32_t writeStructBegin(const std::string& name);
Mark Sleee8540632006-05-30 09:24:40 +000037
Mark Slee4af6ed72006-10-25 19:02:49 +000038 uint32_t writeStructEnd();
Mark Sleee8540632006-05-30 09:24:40 +000039
Mark Slee4af6ed72006-10-25 19:02:49 +000040 uint32_t writeFieldBegin(const std::string& name,
41 const TType fieldType,
42 const int16_t fieldId);
Mark Slee8d7e1f62006-06-07 06:48:56 +000043
Mark Slee4af6ed72006-10-25 19:02:49 +000044 uint32_t writeFieldEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +000045
Mark Slee4af6ed72006-10-25 19:02:49 +000046 uint32_t writeFieldStop();
Mark Slee8d7e1f62006-06-07 06:48:56 +000047
Mark Slee4af6ed72006-10-25 19:02:49 +000048 uint32_t writeMapBegin(const TType keyType,
49 const TType valType,
50 const uint32_t size);
Mark Slee8d7e1f62006-06-07 06:48:56 +000051
Mark Slee4af6ed72006-10-25 19:02:49 +000052 uint32_t writeMapEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +000053
Mark Slee4af6ed72006-10-25 19:02:49 +000054 uint32_t writeListBegin(const TType elemType,
55 const uint32_t size);
Mark Slee8d7e1f62006-06-07 06:48:56 +000056
Mark Slee4af6ed72006-10-25 19:02:49 +000057 uint32_t writeListEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +000058
Mark Slee4af6ed72006-10-25 19:02:49 +000059 uint32_t writeSetBegin(const TType elemType,
60 const uint32_t size);
Mark Slee8d7e1f62006-06-07 06:48:56 +000061
Mark Slee4af6ed72006-10-25 19:02:49 +000062 uint32_t writeSetEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +000063
Mark Slee4af6ed72006-10-25 19:02:49 +000064 uint32_t writeBool(const bool value);
Mark Slee8d7e1f62006-06-07 06:48:56 +000065
Mark Slee4af6ed72006-10-25 19:02:49 +000066 uint32_t writeByte(const int8_t byte);
Mark Slee8d7e1f62006-06-07 06:48:56 +000067
Mark Slee4af6ed72006-10-25 19:02:49 +000068 uint32_t writeI16(const int16_t i16);
Mark Slee8d7e1f62006-06-07 06:48:56 +000069
Mark Slee4af6ed72006-10-25 19:02:49 +000070 uint32_t writeI32(const int32_t i32);
Marc Slemko0b4ffa92006-08-11 02:49:29 +000071
Mark Slee4af6ed72006-10-25 19:02:49 +000072 uint32_t writeI64(const int64_t i64);
Marc Slemko0b4ffa92006-08-11 02:49:29 +000073
Mark Slee4af6ed72006-10-25 19:02:49 +000074 uint32_t writeDouble(const double dub);
Mark Sleec98d0502006-09-06 02:42:25 +000075
76
Mark Slee4af6ed72006-10-25 19:02:49 +000077 uint32_t writeString(const std::string& str);
Mark Slee8d7e1f62006-06-07 06:48:56 +000078
79 /**
80 * Reading functions
81 */
82
Marc Slemko16698852006-08-04 03:16:10 +000083
Mark Slee4af6ed72006-10-25 19:02:49 +000084 uint32_t readMessageBegin(std::string& name,
Marc Slemkoe6889de2006-08-12 00:32:53 +000085 TMessageType& messageType,
Mark Slee4af6ed72006-10-25 19:02:49 +000086 int32_t& seqid);
Marc Slemko16698852006-08-04 03:16:10 +000087
Mark Slee4af6ed72006-10-25 19:02:49 +000088 uint32_t readMessageEnd();
Marc Slemko16698852006-08-04 03:16:10 +000089
Mark Slee4af6ed72006-10-25 19:02:49 +000090 uint32_t readStructBegin(std::string& name);
Mark Slee8d7e1f62006-06-07 06:48:56 +000091
Mark Slee4af6ed72006-10-25 19:02:49 +000092 uint32_t readStructEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +000093
Mark Slee4af6ed72006-10-25 19:02:49 +000094 uint32_t readFieldBegin(std::string& name,
95 TType& fieldType,
96 int16_t& fieldId);
Mark Slee8d7e1f62006-06-07 06:48:56 +000097
Mark Slee4af6ed72006-10-25 19:02:49 +000098 uint32_t readFieldEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +000099
Mark Slee4af6ed72006-10-25 19:02:49 +0000100 uint32_t readMapBegin(TType& keyType,
101 TType& valType,
102 uint32_t& size);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000103
Mark Slee4af6ed72006-10-25 19:02:49 +0000104 uint32_t readMapEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000105
Mark Slee4af6ed72006-10-25 19:02:49 +0000106 uint32_t readListBegin(TType& elemType,
107 uint32_t& size);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000108
Mark Slee4af6ed72006-10-25 19:02:49 +0000109 uint32_t readListEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000110
Mark Slee4af6ed72006-10-25 19:02:49 +0000111 uint32_t readSetBegin(TType& elemType,
112 uint32_t& size);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000113
Mark Slee4af6ed72006-10-25 19:02:49 +0000114 uint32_t readSetEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000115
Mark Slee4af6ed72006-10-25 19:02:49 +0000116 uint32_t readBool(bool& value);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000117
Mark Slee4af6ed72006-10-25 19:02:49 +0000118 uint32_t readByte(int8_t& byte);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000119
Mark Slee4af6ed72006-10-25 19:02:49 +0000120 uint32_t readI16(int16_t& i16);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000121
Mark Slee4af6ed72006-10-25 19:02:49 +0000122 uint32_t readI32(int32_t& i32);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000123
Mark Slee4af6ed72006-10-25 19:02:49 +0000124 uint32_t readI64(int64_t& i64);
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000125
Mark Slee4af6ed72006-10-25 19:02:49 +0000126 uint32_t readDouble(double& dub);
Mark Sleec98d0502006-09-06 02:42:25 +0000127
Mark Slee4af6ed72006-10-25 19:02:49 +0000128 uint32_t readString(std::string& str);
129};
130
131/**
132 * Constructs binary protocol handlers
133 */
134class TBinaryProtocolFactory : public TProtocolFactory {
135 public:
136 TBinaryProtocolFactory() {}
137
138 virtual ~TBinaryProtocolFactory() {}
139
140 std::pair<boost::shared_ptr<TProtocol>, boost::shared_ptr<TProtocol> > getIOProtocols(boost::shared_ptr<TTransport> in, boost::shared_ptr<TTransport> out) {
141 boost::shared_ptr<TProtocol> prot(new TBinaryProtocol(in, out));
142 return std::make_pair(prot, prot);
143 }
Mark Sleee8540632006-05-30 09:24:40 +0000144};
145
Marc Slemko6f038a72006-08-03 18:58:09 +0000146}}} // facebook::thrift::protocol
147
Mark Sleef5f2be42006-09-05 21:05:31 +0000148#endif // #ifndef _THRIFT_PROTOCOL_TBINARYPROTOCOL_H_
149
Marc Slemko6f038a72006-08-03 18:58:09 +0000150