class Thrift::FramedTransport

Public Class Methods

new(transport, read=true, write=true) click to toggle source
   # File lib/thrift/transport/framed_transport.rb
23 def initialize(transport, read=true, write=true)
24   @transport = transport
25   @rbuf      = Bytes.empty_byte_buffer
26   @wbuf      = Bytes.empty_byte_buffer
27   @read      = read
28   @write     = write
29   @index      = 0
30 end

Public Instance Methods

close() click to toggle source
   # File lib/thrift/transport/framed_transport.rb
40 def close
41   @transport.close
42 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
 91 def flush
 92   return @transport.flush unless @write
 93 
 94   out = [@wbuf.length].pack('N')
 95   # Array#pack should return a BINARY encoded String, so it shouldn't be necessary to force encoding
 96   out << @wbuf
 97   @transport.write(out)
 98   @transport.flush
 99   @wbuf = Bytes.empty_byte_buffer
100 end
open() click to toggle source
   # File lib/thrift/transport/framed_transport.rb
36 def open
37   @transport.open
38 end
open?() click to toggle source
   # File lib/thrift/transport/framed_transport.rb
32 def open?
33   @transport.open?
34 end
read(sz) click to toggle source
   # File lib/thrift/transport/framed_transport.rb
44 def read(sz)
45   return @transport.read(sz) unless @read
46 
47   return Bytes.empty_byte_buffer if sz <= 0
48 
49   read_frame if @index >= @rbuf.length
50 
51   @index += sz
52   @rbuf.slice(@index - sz, sz) || Bytes.empty_byte_buffer
53 end
read_byte() click to toggle source
   # File lib/thrift/transport/framed_transport.rb
55 def read_byte
56   return @transport.read_byte() unless @read
57 
58   read_frame if @index >= @rbuf.length
59 
60   # The read buffer has some data now, read a single byte. Using get_string_byte() avoids
61   # allocating a temp string of size 1 unnecessarily.
62   @index += 1
63   return Bytes.get_string_byte(@rbuf, @index - 1)
64 end
read_into_buffer(buffer, size) click to toggle source
   # File lib/thrift/transport/framed_transport.rb
66 def read_into_buffer(buffer, size)
67   i = 0
68   while i < size
69     read_frame if @index >= @rbuf.length
70 
71     # The read buffer has some data now, so copy bytes over to the output buffer.
72     byte = Bytes.get_string_byte(@rbuf, @index)
73     Bytes.set_string_byte(buffer, i, byte)
74     @index += 1
75     i += 1
76   end
77   i
78 end
to_s() click to toggle source
    # File lib/thrift/transport/framed_transport.rb
102 def to_s
103   "framed(#{@transport.to_s})"
104 end
write(buf, sz=nil) click to toggle source
   # File lib/thrift/transport/framed_transport.rb
80 def write(buf, sz=nil)
81   return @transport.write(buf) unless @write
82 
83   buf = Bytes.force_binary_encoding(buf)
84   @wbuf << (sz ? buf[0...sz] : buf)
85 end

Private Instance Methods

read_frame() click to toggle source
    # File lib/thrift/transport/framed_transport.rb
108 def read_frame
109   sz = @transport.read_all(4).unpack('N').first
110 
111   @index = 0
112   @rbuf = @transport.read_all(sz)
113 end