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      = ''
26   @wbuf      = ''
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   out << @wbuf
96   @transport.write(out)
97   @transport.flush
98   @wbuf = ''
99 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 '' if sz <= 0
48 
49   read_frame if @index >= @rbuf.length
50 
51   @index += sz
52   @rbuf.slice(@index - sz, sz) || ''
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 ::Thrift::TransportUtils.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 = ::Thrift::TransportUtils.get_string_byte(@rbuf, @index)
73     ::Thrift::TransportUtils.set_string_byte(buffer, i, byte)
74     @index += 1
75     i += 1
76   end
77   i
78 end
write(buf,sz=nil) click to toggle source
   # File lib/thrift/transport/framed_transport.rb
81 def write(buf,sz=nil)
82   return @transport.write(buf) unless @write
83 
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
103 def read_frame
104   sz = @transport.read_all(4).unpack('N').first
105 
106   @index = 0
107   @rbuf = @transport.read_all(sz)
108 end