Class: Thrift::FramedTransport

Inherits:
BaseTransport show all
Defined in:
lib/thrift/transport/framed_transport.rb

Instance Method Summary collapse

Methods inherited from BaseTransport

#read_all

Constructor Details

#initialize(transport, read = true, write = true) ⇒ FramedTransport

Returns a new instance of FramedTransport.



23
24
25
26
27
28
29
30
# 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

Instance Method Details

#closeObject



40
41
42
# File 'lib/thrift/transport/framed_transport.rb', line 40

def close
  @transport.close
end

#flushObject

Writes the output buffer to the stream in the format of a 4-byte length followed by the actual data.



91
92
93
94
95
96
97
98
99
100
# 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

#openObject



36
37
38
# File 'lib/thrift/transport/framed_transport.rb', line 36

def open
  @transport.open
end

#open?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/thrift/transport/framed_transport.rb', line 32

def open?
  @transport.open?
end

#read(sz) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# 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_byteObject



55
56
57
58
59
60
61
62
63
64
# 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) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
# 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

#to_sObject



102
103
104
# File 'lib/thrift/transport/framed_transport.rb', line 102

def to_s
  "framed(#{@transport.to_s})"
end

#write(buf, sz = nil) ⇒ Object



80
81
82
83
84
85
# 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