class Thrift::FramedTransport
Constants
- DEFAULT_MAX_FRAME_SIZE
-
The largest frame accepted by default. The value the Java, netstd, C++ and Python framed transports use.
- HARD_MAX_FRAME_SIZE
-
The frame length field is 30 bits, so no frame can usefully be larger than this whatever a caller configures.
Attributes
Public Class Methods
Source
# File lib/thrift/transport/framed_transport.rb 32 def initialize(transport, read = true, write = true, max_frame_size: DEFAULT_MAX_FRAME_SIZE) 33 unless max_frame_size > 0 && max_frame_size <= HARD_MAX_FRAME_SIZE 34 raise ArgumentError, "max_frame_size must be > 0 and <= #{HARD_MAX_FRAME_SIZE}" 35 end 36 37 @transport = transport 38 @rbuf = Bytes.empty_byte_buffer 39 @wbuf = Bytes.empty_byte_buffer 40 @read = read 41 @write = write 42 @index = 0 43 @max_frame_size = max_frame_size 44 end
Public Instance Methods
Source
# File lib/thrift/transport/framed_transport.rb 56 def close 57 @transport.close 58 end
Source
# File lib/thrift/transport/framed_transport.rb 107 def flush 108 return @transport.flush unless @write 109 110 out = [@wbuf.length].pack("N") 111 # Array#pack should return a BINARY encoded String, so it shouldn't be necessary to force encoding 112 out << @wbuf 113 @transport.write(out) 114 @transport.flush 115 @wbuf = Bytes.empty_byte_buffer 116 end
Writes the output buffer to the stream in the format of a 4-byte length followed by the actual data.
Source
# File lib/thrift/transport/framed_transport.rb 52 def open 53 @transport.open 54 end
Source
# File lib/thrift/transport/framed_transport.rb 48 def open? 49 @transport.open? 50 end
Source
# File lib/thrift/transport/framed_transport.rb 60 def read(sz) 61 return @transport.read(sz) unless @read 62 63 return Bytes.empty_byte_buffer if sz <= 0 64 65 read_frame if @index >= @rbuf.length 66 67 @index += sz 68 @rbuf.slice(@index - sz, sz) || Bytes.empty_byte_buffer 69 end
Source
# File lib/thrift/transport/framed_transport.rb 71 def read_byte 72 return @transport.read_byte unless @read 73 74 read_frame if @index >= @rbuf.length 75 76 # The read buffer has some data now, read a single byte. Using get_string_byte() avoids 77 # allocating a temp string of size 1 unnecessarily. 78 @index += 1 79 Bytes.get_string_byte(@rbuf, @index - 1) 80 end
Source
# File lib/thrift/transport/framed_transport.rb 82 def read_into_buffer(buffer, size) 83 i = 0 84 while i < size 85 read_frame if @index >= @rbuf.length 86 87 # The read buffer has some data now, so copy bytes over to the output buffer. 88 byte = Bytes.get_string_byte(@rbuf, @index) 89 Bytes.set_string_byte(buffer, i, byte) 90 @index += 1 91 i += 1 92 end 93 i 94 end
Source
# File lib/thrift/transport/framed_transport.rb 118 def to_s 119 "framed(#{@transport})" 120 end
Source
# File lib/thrift/transport/framed_transport.rb 96 def write(buf, sz = nil) 97 return @transport.write(buf) unless @write 98 99 buf = Bytes.force_binary_encoding(buf) 100 @wbuf << (sz ? buf[0...sz] : buf) 101 end
Private Instance Methods
Source
# File lib/thrift/transport/framed_transport.rb 124 def read_frame 125 sz = @transport.read_all(4).unpack1("N") 126 raise TransportException.new(TransportException::END_OF_FILE, "Cannot read from a zero-length frame") if sz == 0 127 128 # Checked before the read, not after: read_all accumulates whatever the 129 # four bytes declare while the peer decides how much of it to send. 130 if sz > @max_frame_size 131 raise TransportException.new( 132 TransportException::UNKNOWN, 133 "Frame size #{sz} exceeds maximum #{@max_frame_size}", 134 ) 135 end 136 137 @index = 0 138 @rbuf = @transport.read_all(sz) 139 end