Class: Thrift::BinaryProtocol

Inherits:
BaseProtocol show all
Defined in:
lib/thrift/protocol/binary_protocol.rb

Constant Summary collapse

VERSION_MASK =
0xffff0000
VERSION_1 =
0x80010000
TYPE_MASK =
0x000000ff

Instance Attribute Summary collapse

Attributes inherited from BaseProtocol

#trans

Instance Method Summary collapse

Methods inherited from BaseProtocol

#native?, #read_field_end, #read_list_end, #read_map_end, #read_message_end, #read_set_end, #read_struct_end, #read_type, #skip, #write_field, #write_field_end, #write_list_end, #write_map_end, #write_message_end, #write_set_end, #write_struct_end, #write_type

Constructor Details

#initialize(trans, strict_read = true, strict_write = true) ⇒ BinaryProtocol

Returns a new instance of BinaryProtocol.



28
29
30
31
32
33
34
35
36
# File 'lib/thrift/protocol/binary_protocol.rb', line 28

def initialize(trans, strict_read=true, strict_write=true)
  super(trans)
  @strict_read = strict_read
  @strict_write = strict_write

  # Pre-allocated read buffer for fixed-size read methods. Needs to be at least 8 bytes long for
  # read_i64() and read_double().
  @rbuf = Bytes.empty_byte_buffer(8)
end

Instance Attribute Details

#strict_readObject (readonly)

Returns the value of attribute strict_read.



26
27
28
# File 'lib/thrift/protocol/binary_protocol.rb', line 26

def strict_read
  @strict_read
end

#strict_writeObject (readonly)

Returns the value of attribute strict_write.



26
27
28
# File 'lib/thrift/protocol/binary_protocol.rb', line 26

def strict_write
  @strict_write
end

Instance Method Details

#read_binaryObject



225
226
227
228
# File 'lib/thrift/protocol/binary_protocol.rb', line 225

def read_binary
  size = read_i32
  trans.read_all(size)
end

#read_boolObject



171
172
173
174
# File 'lib/thrift/protocol/binary_protocol.rb', line 171

def read_bool
  byte = read_byte
  byte != 0
end

#read_byteObject



176
177
178
179
180
181
182
# File 'lib/thrift/protocol/binary_protocol.rb', line 176

def read_byte
  val = trans.read_byte
  if (val > 0x7f)
    val = 0 - ((val - 1) ^ 0xff)
  end
  val
end

#read_doubleObject



214
215
216
217
218
# File 'lib/thrift/protocol/binary_protocol.rb', line 214

def read_double
  trans.read_into_buffer(@rbuf, 8)
  val = @rbuf.unpack('G').first
  val
end

#read_field_beginObject



142
143
144
145
146
147
148
149
150
# File 'lib/thrift/protocol/binary_protocol.rb', line 142

def read_field_begin
  type = read_byte
  if (type == Types::STOP)
    [nil, type, 0]
  else
    id = read_i16
    [nil, type, id]
  end
end

#read_i16Object



184
185
186
187
188
189
190
191
# File 'lib/thrift/protocol/binary_protocol.rb', line 184

def read_i16
  trans.read_into_buffer(@rbuf, 2)
  val, = @rbuf.unpack('n')
  if (val > 0x7fff)
    val = 0 - ((val - 1) ^ 0xffff)
  end
  val
end

#read_i32Object



193
194
195
196
197
198
199
200
# File 'lib/thrift/protocol/binary_protocol.rb', line 193

def read_i32
  trans.read_into_buffer(@rbuf, 4)
  val, = @rbuf.unpack('N')
  if (val > 0x7fffffff)
    val = 0 - ((val - 1) ^ 0xffffffff)
  end
  val
end

#read_i64Object



202
203
204
205
206
207
208
209
210
211
212
# File 'lib/thrift/protocol/binary_protocol.rb', line 202

def read_i64
  trans.read_into_buffer(@rbuf, 8)
  hi, lo = @rbuf.unpack('N2')
  if (hi > 0x7fffffff)
    hi ^= 0xffffffff
    lo ^= 0xffffffff
    0 - (hi << 32) - lo - 1
  else
    (hi << 32) + lo
  end
end

#read_list_beginObject



159
160
161
162
163
# File 'lib/thrift/protocol/binary_protocol.rb', line 159

def read_list_begin
  etype = read_byte
  size = read_i32
  [etype, size]
end

#read_map_beginObject



152
153
154
155
156
157
# File 'lib/thrift/protocol/binary_protocol.rb', line 152

def read_map_begin
  ktype = read_byte
  vtype = read_byte
  size = read_i32
  [ktype, vtype, size]
end

#read_message_beginObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/thrift/protocol/binary_protocol.rb', line 119

def read_message_begin
  version = read_i32
  if version < 0
    if (version & VERSION_MASK != VERSION_1)
      raise ProtocolException.new(ProtocolException::BAD_VERSION, 'Missing version identifier')
    end
    type = version & TYPE_MASK
    name = read_string
    seqid = read_i32
    [name, type, seqid]
  else
    if strict_read
      raise ProtocolException.new(ProtocolException::BAD_VERSION, 'No version identifier, old protocol client?')
    end
    name = trans.read_all(version)
    type = read_byte
    seqid = read_i32
    [name, type, seqid]
  end
end

#read_set_beginObject



165
166
167
168
169
# File 'lib/thrift/protocol/binary_protocol.rb', line 165

def read_set_begin
  etype = read_byte
  size = read_i32
  [etype, size]
end

#read_stringObject



220
221
222
223
# File 'lib/thrift/protocol/binary_protocol.rb', line 220

def read_string
  buffer = read_binary
  Bytes.convert_to_string(buffer)
end

#read_struct_beginObject



140
# File 'lib/thrift/protocol/binary_protocol.rb', line 140

def read_struct_begin; nil; end

#to_sObject



230
231
232
# File 'lib/thrift/protocol/binary_protocol.rb', line 230

def to_s
  "binary(#{super.to_s})"
end

#write_binary(buf) ⇒ Object



114
115
116
117
# File 'lib/thrift/protocol/binary_protocol.rb', line 114

def write_binary(buf)
  write_i32(buf.bytesize)
  trans.write(buf)
end

#write_bool(bool) ⇒ Object



80
81
82
# File 'lib/thrift/protocol/binary_protocol.rb', line 80

def write_bool(bool)
  write_byte(bool ? 1 : 0)
end

#write_byte(byte) ⇒ Object

Raises:

  • (RangeError)


84
85
86
87
# File 'lib/thrift/protocol/binary_protocol.rb', line 84

def write_byte(byte)
  raise RangeError if byte < -2**31 || byte >= 2**32
  trans.write([byte].pack('c'))
end

#write_double(dub) ⇒ Object



105
106
107
# File 'lib/thrift/protocol/binary_protocol.rb', line 105

def write_double(dub)
  trans.write([dub].pack('G'))
end

#write_field_begin(name, type, id) ⇒ Object



55
56
57
58
# File 'lib/thrift/protocol/binary_protocol.rb', line 55

def write_field_begin(name, type, id)
  write_byte(type)
  write_i16(id)
end

#write_field_stopObject



60
61
62
# File 'lib/thrift/protocol/binary_protocol.rb', line 60

def write_field_stop
  write_byte(Thrift::Types::STOP)
end

#write_i16(i16) ⇒ Object



89
90
91
# File 'lib/thrift/protocol/binary_protocol.rb', line 89

def write_i16(i16)
  trans.write([i16].pack('n'))
end

#write_i32(i32) ⇒ Object

Raises:

  • (RangeError)


93
94
95
96
# File 'lib/thrift/protocol/binary_protocol.rb', line 93

def write_i32(i32)
  raise RangeError if i32 < -2**31 || i32 >= 2**31
  trans.write([i32].pack('N'))
end

#write_i64(i64) ⇒ Object

Raises:

  • (RangeError)


98
99
100
101
102
103
# File 'lib/thrift/protocol/binary_protocol.rb', line 98

def write_i64(i64)
  raise RangeError if i64 < -2**63 || i64 >= 2**64
  hi = i64 >> 32
  lo = i64 & 0xffffffff
  trans.write([hi, lo].pack('N2'))
end

#write_list_begin(etype, size) ⇒ Object



70
71
72
73
# File 'lib/thrift/protocol/binary_protocol.rb', line 70

def write_list_begin(etype, size)
  write_byte(etype)
  write_i32(size)
end

#write_map_begin(ktype, vtype, size) ⇒ Object



64
65
66
67
68
# File 'lib/thrift/protocol/binary_protocol.rb', line 64

def write_map_begin(ktype, vtype, size)
  write_byte(ktype)
  write_byte(vtype)
  write_i32(size)
end

#write_message_begin(name, type, seqid) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/thrift/protocol/binary_protocol.rb', line 38

def write_message_begin(name, type, seqid)
  # this is necessary because we added (needed) bounds checking to 
  # write_i32, and 0x80010000 is too big for that.
  if strict_write
    write_i16(VERSION_1 >> 16)
    write_i16(type)
    write_string(name)
    write_i32(seqid)
  else
    write_string(name)
    write_byte(type)
    write_i32(seqid)
  end
end

#write_set_begin(etype, size) ⇒ Object



75
76
77
78
# File 'lib/thrift/protocol/binary_protocol.rb', line 75

def write_set_begin(etype, size)
  write_byte(etype)
  write_i32(size)
end

#write_string(str) ⇒ Object



109
110
111
112
# File 'lib/thrift/protocol/binary_protocol.rb', line 109

def write_string(str)
  buf = Bytes.convert_to_utf8_byte_buffer(str)
  write_binary(buf)
end

#write_struct_begin(name) ⇒ Object



53
# File 'lib/thrift/protocol/binary_protocol.rb', line 53

def write_struct_begin(name); nil; end