class Thrift::FramedTransport
Public Class Methods
new(transport, read=true, write=true)
click to toggle source
# File lib/thrift/transport/framed_transport.rb, line 23 def initialize(transport, read=true, write=true) @transport = transport @rbuf = Bytes.empty_byte_buffer @wbuf = Bytes.empty_byte_buffer @read = read @write = write @index = 0 end
Public Instance Methods
close()
click to toggle source
# File lib/thrift/transport/framed_transport.rb, line 40 def close @transport.close end
flush()
click to toggle source
Writes the output buffer to the stream in the format of a 4-byte length followed by the actual data.
# File lib/thrift/transport/framed_transport.rb, line 91 def flush return @transport.flush unless @write out = [@wbuf.length].pack('N') # Array#pack should return a BINARY encoded String, so it shouldn't be necessary to force encoding out << @wbuf @transport.write(out) @transport.flush @wbuf = Bytes.empty_byte_buffer end
open()
click to toggle source
# File lib/thrift/transport/framed_transport.rb, line 36 def open @transport.open end
open?()
click to toggle source
# File lib/thrift/transport/framed_transport.rb, line 32 def open? @transport.open? end
read(sz)
click to toggle source
# File lib/thrift/transport/framed_transport.rb, line 44 def read(sz) return @transport.read(sz) unless @read return Bytes.empty_byte_buffer if sz <= 0 read_frame if @index >= @rbuf.length @index += sz @rbuf.slice(@index - sz, sz) || Bytes.empty_byte_buffer end
read_byte()
click to toggle source
# File lib/thrift/transport/framed_transport.rb, line 55 def read_byte return @transport.read_byte() unless @read read_frame if @index >= @rbuf.length # The read buffer has some data now, read a single byte. Using get_string_byte() avoids # allocating a temp string of size 1 unnecessarily. @index += 1 return Bytes.get_string_byte(@rbuf, @index - 1) end
read_into_buffer(buffer, size)
click to toggle source
# File lib/thrift/transport/framed_transport.rb, line 66 def read_into_buffer(buffer, size) i = 0 while i < size read_frame if @index >= @rbuf.length # The read buffer has some data now, so copy bytes over to the output buffer. byte = Bytes.get_string_byte(@rbuf, @index) Bytes.set_string_byte(buffer, i, byte) @index += 1 i += 1 end i end
write(buf, sz=nil)
click to toggle source
# File lib/thrift/transport/framed_transport.rb, line 80 def write(buf, sz=nil) return @transport.write(buf) unless @write buf = Bytes.force_binary_encoding(buf) @wbuf << (sz ? buf[0...sz] : buf) end
Private Instance Methods
read_frame()
click to toggle source
# File lib/thrift/transport/framed_transport.rb, line 104 def read_frame sz = @transport.read_all(4).unpack('N').first @index = 0 @rbuf = @transport.read_all(sz) end