blob: 1259598cce89eba9d11f0636c8a2908627b60cdf [file] [log] [blame]
David Reisse0e3d1b2008-04-08 05:06:45 +00001// Copyright (c) 2006- Facebook
2// Distributed under the Thrift Software License
3//
4// See accompanying file LICENSE or visit the Thrift site at:
5// http://developers.facebook.com/thrift/
6
7#ifndef _THRIFT_PROTOCOL_TPROTOCOLTAP_H_
8#define _THRIFT_PROTOCOL_TPROTOCOLTAP_H_ 1
9
10#include <protocol/TOneWayProtocol.h>
11
12namespace facebook { namespace thrift { namespace protocol {
13
14using facebook::thrift::transport::TTransport;
15
16/**
17 * Puts a wiretap on a protocol object. Any reads to this class are passed
18 * through to an enclosed protocol object, but also mirrored as write to a
19 * second protocol object.
20 *
21 * @author David Reiss <dreiss@facebook.com>
22 */
23class TProtocolTap : public TReadOnlyProtocol {
24 public:
25 TProtocolTap(boost::shared_ptr<TProtocol> source,
26 boost::shared_ptr<TProtocol> sink)
27 : TReadOnlyProtocol(source->getTransport(), "TProtocolTap")
28 , source_(source)
29 , sink_(sink)
30 {}
31
32 virtual uint32_t readMessageBegin(std::string& name,
33 TMessageType& messageType,
34 int32_t& seqid) {
35 uint32_t rv = source_->readMessageBegin(name, messageType, seqid);
36 sink_->writeMessageBegin(name, messageType, seqid);
37 return rv;
38 }
39
40 virtual uint32_t readMessageEnd() {
41 uint32_t rv = source_->readMessageEnd();
42 sink_->writeMessageEnd();
43 return rv;
44 }
45
46 virtual uint32_t readStructBegin(std::string& name) {
47 uint32_t rv = source_->readStructBegin(name);
48 sink_->writeStructBegin(name);
49 return rv;
50 }
51
52 virtual uint32_t readStructEnd() {
53 uint32_t rv = source_->readStructEnd();
54 sink_->writeStructEnd();
55 return rv;
56 }
57
58 virtual uint32_t readFieldBegin(std::string& name,
59 TType& fieldType,
60 int16_t& fieldId) {
61 uint32_t rv = source_->readFieldBegin(name, fieldType, fieldId);
62 if (fieldType == T_STOP) {
63 sink_->writeFieldStop();
64 } else {
65 sink_->writeFieldBegin(name, fieldType, fieldId);
66 }
67 return rv;
68 }
69
70
71 virtual uint32_t readFieldEnd() {
72 uint32_t rv = source_->readFieldEnd();
73 sink_->writeFieldEnd();
74 return rv;
75 }
76
77 virtual uint32_t readMapBegin(TType& keyType,
78 TType& valType,
79 uint32_t& size) {
80 uint32_t rv = source_->readMapBegin(keyType, valType, size);
81 sink_->writeMapBegin(keyType, valType, size);
82 return rv;
83 }
84
85
86 virtual uint32_t readMapEnd() {
87 uint32_t rv = source_->readMapEnd();
88 sink_->writeMapEnd();
89 return rv;
90 }
91
92 virtual uint32_t readListBegin(TType& elemType,
93 uint32_t& size) {
94 uint32_t rv = source_->readListBegin(elemType, size);
95 sink_->writeListBegin(elemType, size);
96 return rv;
97 }
98
99
100 virtual uint32_t readListEnd() {
101 uint32_t rv = source_->readListEnd();
102 sink_->writeListEnd();
103 return rv;
104 }
105
106 virtual uint32_t readSetBegin(TType& elemType,
107 uint32_t& size) {
108 uint32_t rv = source_->readSetBegin(elemType, size);
109 sink_->writeSetBegin(elemType, size);
110 return rv;
111 }
112
113
114 virtual uint32_t readSetEnd() {
115 uint32_t rv = source_->readSetEnd();
116 sink_->writeSetEnd();
117 return rv;
118 }
119
120 virtual uint32_t readBool(bool& value) {
121 uint32_t rv = source_->readBool(value);
122 sink_->writeBool(value);
123 return rv;
124 }
125
126 virtual uint32_t readByte(int8_t& byte) {
127 uint32_t rv = source_->readByte(byte);
128 sink_->writeByte(byte);
129 return rv;
130 }
131
132 virtual uint32_t readI16(int16_t& i16) {
133 uint32_t rv = source_->readI16(i16);
134 sink_->writeI16(i16);
135 return rv;
136 }
137
138 virtual uint32_t readI32(int32_t& i32) {
139 uint32_t rv = source_->readI32(i32);
140 sink_->writeI32(i32);
141 return rv;
142 }
143
144 virtual uint32_t readI64(int64_t& i64) {
145 uint32_t rv = source_->readI64(i64);
146 sink_->writeI64(i64);
147 return rv;
148 }
149
150 virtual uint32_t readDouble(double& dub) {
151 uint32_t rv = source_->readDouble(dub);
152 sink_->writeDouble(dub);
153 return rv;
154 }
155
156 virtual uint32_t readString(std::string& str) {
157 uint32_t rv = source_->readString(str);
158 sink_->writeString(str);
159 return rv;
160 }
161
162 virtual uint32_t readBinary(std::string& str) {
163 uint32_t rv = source_->readBinary(str);
164 sink_->writeBinary(str);
165 return rv;
166 }
167
168 private:
169 boost::shared_ptr<TProtocol> source_;
170 boost::shared_ptr<TProtocol> sink_;
171};
172
173}}} // facebook::thrift::protocol
174
175#endif // #define _THRIFT_PROTOCOL_TPROTOCOLTAP_H_ 1