Class: GameMachine::MessageBuffer

Inherits:
Object
  • Object
show all
Defined in:
server/lib/game_machine/message_buffer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMessageBuffer


5
6
7
# File 'server/lib/game_machine/message_buffer.rb', line 5

def initialize
  reset
end

Instance Attribute Details

#bytesObject (readonly)

Returns the value of attribute bytes


4
5
6
# File 'server/lib/game_machine/message_buffer.rb', line 4

def bytes
  @bytes
end

#next_message_lengthObject (readonly)

Returns the value of attribute next_message_length


4
5
6
# File 'server/lib/game_machine/message_buffer.rb', line 4

def next_message_length
  @next_message_length
end

Instance Method Details

#add_bytes(bytes_to_add) ⇒ Object


33
34
35
36
37
38
39
40
41
42
# File 'server/lib/game_machine/message_buffer.rb', line 33

def add_bytes(bytes_to_add)
  if @bytes.nil?
    @bytes = bytes_to_add
  else
    new_bytes = Java::byte[bytes_to_add.length + @bytes.length].new
    java.lang.System.arraycopy(@bytes, 0, new_bytes, 0, @bytes.length)
    java.lang.System.arraycopy(bytes_to_add, 0, new_bytes, @bytes.length, bytes_to_add.length)
    @bytes = new_bytes
  end
end

#messagesObject


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'server/lib/game_machine/message_buffer.rb', line 9

def messages
  byte_messages = []
  return byte_messages if bytes.nil?
  stream = JavaLib::ByteArrayInputStream.new(bytes)
  if next_message_length == 0
    @next_message_length = read_message_length(stream)
  end
  while next_message_length >= 1 && stream.available >= next_message_length
    message_bytes = Java::byte[next_message_length].new
    stream.read(message_bytes,0,next_message_length)
    byte_messages << message_bytes
    if stream.available >= 1
      @next_message_length = read_message_length(stream)
      if next_message_length > stream.available
        @bytes = Java::byte[stream.available].new
        stream.read(@bytes,0,stream.available)
      end
    else
      reset
    end
  end
  byte_messages
end