class Thrift::HeaderProtocol
HeaderProtocol is a protocol that wraps HeaderTransport and delegates to either BinaryProtocol or CompactProtocol based on auto-detection.
It provides access to header management (get_headers, set_header, etc.) through the underlying HeaderTransport.
Example usage:
socket = Thrift::Socket.new('localhost', 9090) protocol = Thrift::HeaderProtocol.new(socket) client = MyService::Client.new(protocol) protocol.trans.open client.some_method() protocol.trans.close
Public Class Methods
Source
# File lib/thrift/protocol/header_protocol.rb 44 def initialize(transport, allowed_client_types = nil, default_protocol = HeaderSubprotocolID::COMPACT) 45 # Wrap transport in HeaderTransport if not already wrapped 46 if transport.is_a?(HeaderTransport) 47 @header_transport = transport 48 else 49 @header_transport = HeaderTransport.new(transport, allowed_client_types, default_protocol) 50 end 51 52 @default_protocol = default_protocol 53 @current_protocol_id = default_protocol 54 55 # Create initial protocol 56 @protocol = create_protocol(@current_protocol_id) 57 end
Creates a new HeaderProtocol.
@param transport [BaseTransport, HeaderTransport] The transport to wrap.
If not already a HeaderTransport, it will be wrapped in one.
@param allowed_client_types [Array<Integer>] Allowed client types for auto-detection @param default_protocol [Integer] Default protocol ID (BINARY or COMPACT)
Public Instance Methods
Source
# File lib/thrift/protocol/header_protocol.rb 80 def add_transform(transform_id) 81 @header_transport.add_transform(transform_id) 82 end
Adds a transform (e.g., ZLIB compression)
Source
# File lib/thrift/protocol/header_protocol.rb 75 def clear_headers 76 @header_transport.clear_headers 77 end
Clears all write headers
Source
# File lib/thrift/protocol/header_protocol.rb 65 def get_headers 66 @header_transport.get_headers 67 end
Returns headers read from the last message
Source
# File lib/thrift/protocol/header_protocol.rb 264 def read_binary 265 @protocol.read_binary 266 end
Source
# File lib/thrift/protocol/header_protocol.rb 236 def read_bool 237 @protocol.read_bool 238 end
Source
# File lib/thrift/protocol/header_protocol.rb 240 def read_byte 241 @protocol.read_byte 242 end
Source
# File lib/thrift/protocol/header_protocol.rb 256 def read_double 257 @protocol.read_double 258 end
Source
# File lib/thrift/protocol/header_protocol.rb 204 def read_field_begin 205 @protocol.read_field_begin 206 end
Source
# File lib/thrift/protocol/header_protocol.rb 208 def read_field_end 209 @protocol.read_field_end 210 end
Source
# File lib/thrift/protocol/header_protocol.rb 244 def read_i16 245 @protocol.read_i16 246 end
Source
# File lib/thrift/protocol/header_protocol.rb 248 def read_i32 249 @protocol.read_i32 250 end
Source
# File lib/thrift/protocol/header_protocol.rb 252 def read_i64 253 @protocol.read_i64 254 end
Source
# File lib/thrift/protocol/header_protocol.rb 220 def read_list_begin 221 @protocol.read_list_begin 222 end
Source
# File lib/thrift/protocol/header_protocol.rb 224 def read_list_end 225 @protocol.read_list_end 226 end
Source
# File lib/thrift/protocol/header_protocol.rb 212 def read_map_begin 213 @protocol.read_map_begin 214 end
Source
# File lib/thrift/protocol/header_protocol.rb 216 def read_map_end 217 @protocol.read_map_end 218 end
Source
# File lib/thrift/protocol/header_protocol.rb 176 def read_message_begin 177 begin 178 @header_transport.reset_protocol 179 reset_protocol_if_needed 180 rescue ProtocolException => ex 181 use_default_protocol 182 app_ex = ApplicationException.new(ApplicationException::INVALID_PROTOCOL, ex.message) 183 write_message_begin("", MessageTypes::EXCEPTION, @header_transport.sequence_id) 184 app_ex.write(self) 185 write_message_end 186 @header_transport.flush 187 raise ex 188 end 189 @protocol.read_message_begin 190 end
Read methods - delegate to underlying protocol read_message_begin handles protocol switching after detection
Source
# File lib/thrift/protocol/header_protocol.rb 192 def read_message_end 193 @protocol.read_message_end 194 end
Source
# File lib/thrift/protocol/header_protocol.rb 228 def read_set_begin 229 @protocol.read_set_begin 230 end
Source
# File lib/thrift/protocol/header_protocol.rb 232 def read_set_end 233 @protocol.read_set_end 234 end
Source
# File lib/thrift/protocol/header_protocol.rb 260 def read_string 261 @protocol.read_string 262 end
Source
# File lib/thrift/protocol/header_protocol.rb 196 def read_struct_begin 197 @protocol.read_struct_begin 198 end
Source
# File lib/thrift/protocol/header_protocol.rb 200 def read_struct_end 201 @protocol.read_struct_end 202 end
Source
# File lib/thrift/protocol/header_protocol.rb 272 def read_uuid 273 @protocol.read_uuid 274 end
Source
# File lib/thrift/protocol/header_protocol.rb 70 def set_header(key, value) 71 @header_transport.set_header(key, value) 72 end
Sets a header to be sent with the next message
Source
# File lib/thrift/protocol/header_protocol.rb 268 def skip_string 269 @protocol.skip_string 270 end
Source
# File lib/thrift/protocol/header_protocol.rb 276 def to_s 277 "header(#{@protocol})" 278 end
Source
# File lib/thrift/protocol/header_protocol.rb 60 def trans 61 @header_transport 62 end
Returns the HeaderTransport
Source
# File lib/thrift/protocol/header_protocol.rb 166 def write_binary(buf) 167 @protocol.write_binary(buf) 168 end
Source
# File lib/thrift/protocol/header_protocol.rb 138 def write_bool(bool) 139 @protocol.write_bool(bool) 140 end
Source
# File lib/thrift/protocol/header_protocol.rb 142 def write_byte(byte) 143 @protocol.write_byte(byte) 144 end
Source
# File lib/thrift/protocol/header_protocol.rb 158 def write_double(dub) 159 @protocol.write_double(dub) 160 end
Source
# File lib/thrift/protocol/header_protocol.rb 102 def write_field_begin(name, type, id) 103 @protocol.write_field_begin(name, type, id) 104 end
Source
# File lib/thrift/protocol/header_protocol.rb 106 def write_field_end 107 @protocol.write_field_end 108 end
Source
# File lib/thrift/protocol/header_protocol.rb 110 def write_field_stop 111 @protocol.write_field_stop 112 end
Source
# File lib/thrift/protocol/header_protocol.rb 146 def write_i16(i16) 147 @protocol.write_i16(i16) 148 end
Source
# File lib/thrift/protocol/header_protocol.rb 150 def write_i32(i32) 151 @protocol.write_i32(i32) 152 end
Source
# File lib/thrift/protocol/header_protocol.rb 154 def write_i64(i64) 155 @protocol.write_i64(i64) 156 end
Source
# File lib/thrift/protocol/header_protocol.rb 122 def write_list_begin(etype, size) 123 @protocol.write_list_begin(etype, size) 124 end
Source
# File lib/thrift/protocol/header_protocol.rb 126 def write_list_end 127 @protocol.write_list_end 128 end
Source
# File lib/thrift/protocol/header_protocol.rb 114 def write_map_begin(ktype, vtype, size) 115 @protocol.write_map_begin(ktype, vtype, size) 116 end
Source
# File lib/thrift/protocol/header_protocol.rb 118 def write_map_end 119 @protocol.write_map_end 120 end
Source
# File lib/thrift/protocol/header_protocol.rb 85 def write_message_begin(name, type, seqid) 86 @header_transport.sequence_id = seqid 87 @protocol.write_message_begin(name, type, seqid) 88 end
Write methods - delegate to underlying protocol
Source
# File lib/thrift/protocol/header_protocol.rb 90 def write_message_end 91 @protocol.write_message_end 92 end
Source
# File lib/thrift/protocol/header_protocol.rb 130 def write_set_begin(etype, size) 131 @protocol.write_set_begin(etype, size) 132 end
Source
# File lib/thrift/protocol/header_protocol.rb 134 def write_set_end 135 @protocol.write_set_end 136 end
Source
# File lib/thrift/protocol/header_protocol.rb 162 def write_string(str) 163 @protocol.write_string(str) 164 end
Source
# File lib/thrift/protocol/header_protocol.rb 94 def write_struct_begin(name) 95 @protocol.write_struct_begin(name) 96 end
Source
# File lib/thrift/protocol/header_protocol.rb 98 def write_struct_end 99 @protocol.write_struct_end 100 end
Source
# File lib/thrift/protocol/header_protocol.rb 170 def write_uuid(uuid) 171 @protocol.write_uuid(uuid) 172 end
Private Instance Methods
Source
# File lib/thrift/protocol/header_protocol.rb 298 def create_protocol(protocol_id) 299 case protocol_id 300 when HeaderSubprotocolID::BINARY 301 BinaryProtocol.new(@header_transport) 302 when HeaderSubprotocolID::COMPACT 303 CompactProtocol.new(@header_transport) 304 else 305 raise ProtocolException.new( 306 ProtocolException::INVALID_DATA, 307 "Unknown protocol ID: #{protocol_id}", 308 ) 309 end 310 end
Creates a protocol instance based on protocol ID
Source
# File lib/thrift/protocol/header_protocol.rb 283 def reset_protocol_if_needed 284 new_protocol_id = @header_transport.protocol_id 285 if new_protocol_id != @current_protocol_id 286 @protocol = create_protocol(new_protocol_id) 287 @current_protocol_id = new_protocol_id 288 end 289 end
Checks if the protocol needs to be switched after reading
Source
# File lib/thrift/protocol/header_protocol.rb 291 def use_default_protocol 292 @protocol = create_protocol(@default_protocol) 293 @current_protocol_id = @default_protocol 294 @header_transport.protocol_id = @default_protocol 295 end