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      = ''
  @wbuf      = ''
  @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.



65
66
67
68
69
70
71
72
73
# File 'lib/thrift/transport/framed_transport.rb', line 65

def flush
  return @transport.flush unless @write

  out = [@wbuf.length].pack('N')
  out << @wbuf
  @transport.write(out)
  @transport.flush
  @wbuf = ''
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 '' if sz <= 0

  read_frame if @index >= @rbuf.length

  @index += sz
  @rbuf.slice(@index - sz, sz) || ''
end

#write(buf, sz = nil) ⇒ Object



55
56
57
58
59
# File 'lib/thrift/transport/framed_transport.rb', line 55

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

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