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.



24
25
26
27
28
29
30
31
# File 'lib/thrift/transport/framed_transport.rb', line 24

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



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

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.



92
93
94
95
96
97
98
99
100
101
# File 'lib/thrift/transport/framed_transport.rb', line 92

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



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

def open
  @transport.open
end

#open?Boolean

Returns:

  • (Boolean)


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

def open?
  @transport.open?
end

#read(sz) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/thrift/transport/framed_transport.rb', line 45

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



56
57
58
59
60
61
62
63
64
65
# File 'lib/thrift/transport/framed_transport.rb', line 56

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



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/thrift/transport/framed_transport.rb', line 67

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



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

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

#write(buf, sz = nil) ⇒ Object



81
82
83
84
85
86
# File 'lib/thrift/transport/framed_transport.rb', line 81

def write(buf, sz = nil)
  return @transport.write(buf) unless @write

  buf = Bytes.force_binary_encoding(buf)
  @wbuf << (sz ? buf[0...sz] : buf)
end