Class: Protobuf::Rpc::Buffer

Inherits:
Object
  • Object
show all
Defined in:
lib/protobuf/rpc/buffer.rb

Constant Summary collapse

MODES =
[:read, :write]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mode = :read, data = '') ⇒ Buffer

Returns a new instance of Buffer.



10
11
12
13
14
# File 'lib/protobuf/rpc/buffer.rb', line 10

def initialize(mode=:read, data='')
  @data = data.is_a?(Protobuf::Message) ? data.serialize_to_string : data.to_s
  @flush = false
  self.mode = mode
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



6
7
8
# File 'lib/protobuf/rpc/buffer.rb', line 6

def data
  @data
end

#modeObject

Returns the value of attribute mode.



5
6
7
# File 'lib/protobuf/rpc/buffer.rb', line 5

def mode
  @mode
end

#sizeObject (readonly)

Returns the value of attribute size.



6
7
8
# File 'lib/protobuf/rpc/buffer.rb', line 6

def size
  @size
end

Instance Method Details

#<<(data) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/protobuf/rpc/buffer.rb', line 35

def <<(data)
  @data << data
  if reading?
    get_data_size
    check_for_flush
  end
end

#flushed?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/protobuf/rpc/buffer.rb', line 51

def flushed?
  @flush
end

#reading?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/protobuf/rpc/buffer.rb', line 43

def reading?
  mode == :read
end

#write(force_mode = true) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/protobuf/rpc/buffer.rb', line 24

def write(force_mode=true)
  if force_mode and reading?
    mode = :write
  elsif not force_mode and reading?
    raise = 'You chose to write the buffer when in read mode'
  end
  
  @size = @data.length
  '%d-%s' % [@size, @data]
end

#writing?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/protobuf/rpc/buffer.rb', line 47

def writing?
  mode == :write
end