Class: EaseEngine::Packet

Inherits:
Object
  • Object
show all
Defined in:
lib/ease_engine/packet.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePacket

Returns a new instance of Packet.



80
81
82
83
# File 'lib/ease_engine/packet.rb', line 80

def initialize
  @packers = []
  @packet_name = self.class.name.gsub( /::/, "." )
end

Instance Attribute Details

#packet_nameObject (readonly)

Returns the value of attribute packet_name.



78
79
80
# File 'lib/ease_engine/packet.rb', line 78

def packet_name
  @packet_name
end

Class Method Details

.create(socket) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/ease_engine/packet.rb', line 53

def self.create( socket )
  # 受信したデータから動的にパケットクラスを生成
  begin
    size = self.unpack_uint32( socket.read_buf.value.slice( 0, 4 ) )
    break if ! socket.err.nil?
    break if 0 == size
    break if socket.read_buf.size < 4 + size
    
    socket.read_buf >> 4
    buf = socket.read_buf >> size
    packet_name = self.unpack_str( buf )
    buf.slice!( 0, packet_name.length + 1 )
    
    packet = const_get( packet_name.gsub( /\./, "::" ) ).new
    packet.unpack( buf )
  rescue => err
    EaseEngine::Log.err( "#{packet_name}: #{err}" )
    socket.err = err
    socket.is_disable = true
    packet = nil
  end while false
  
  packet
end

.creates(socket) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ease_engine/packet.rb', line 38

def self.creates( socket )
  packets = []
  
  if ! socket.recv( socket.read_max_size ).empty?
    while true
      packet = self.create( socket )
      break if packet.nil?
      
      packets.push packet
    end
  end
  
  packets
end

.pack_str(value) ⇒ Object



18
19
20
# File 'lib/ease_engine/packet.rb', line 18

def self.pack_str( value )
  [ value ].pack( "Z*" )
end

.pack_uint16(value) ⇒ Object



10
11
12
# File 'lib/ease_engine/packet.rb', line 10

def self.pack_uint16( value )
  [ value ].pack( "v" )
end

.pack_uint32(value) ⇒ Object



14
15
16
# File 'lib/ease_engine/packet.rb', line 14

def self.pack_uint32( value )
  [ value ].pack( "V" )
end

.pack_uint8(value) ⇒ Object



6
7
8
# File 'lib/ease_engine/packet.rb', line 6

def self.pack_uint8( value )
  [ value ].pack( "C" )
end

.unpack_str(buf) ⇒ Object



34
35
36
# File 'lib/ease_engine/packet.rb', line 34

def self.unpack_str( buf )
  buf.unpack( "Z*" )[ 0 ]
end

.unpack_uint16(buf) ⇒ Object



26
27
28
# File 'lib/ease_engine/packet.rb', line 26

def self.unpack_uint16( buf )
  ( 2 != buf.length ) ? 0 : buf.unpack( "v" )[ 0 ]
end

.unpack_uint32(buf) ⇒ Object



30
31
32
# File 'lib/ease_engine/packet.rb', line 30

def self.unpack_uint32( buf )
  ( 4 != buf.length ) ? 0 : buf.unpack( "V" )[ 0 ]
end

.unpack_uint8(buf) ⇒ Object



22
23
24
# File 'lib/ease_engine/packet.rb', line 22

def self.unpack_uint8( buf )
  ( 1 != buf.length ) ? 0 : buf.unpack( "C" )[ 0 ]
end

Instance Method Details

#packObject



101
102
103
104
105
106
107
108
109
110
# File 'lib/ease_engine/packet.rb', line 101

def pack
  bufs = []
  bufs.push Packet.pack_str( @packet_name )
  hash = {}
  @packers.each{|name|
    hash[ name.to_s ] = instance_variable_get( "@#{name}" )
  }
  bufs.push MessagePack.pack( hash )
  bufs.join
end

#packer(*args) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/ease_engine/packet.rb', line 85

def packer( *args )
  args.each{|name|
    self.class.class_eval <<-EOS
      def #{name}
        @#{name}
      end
      
      def #{name}=( value )
        @#{name} = value
      end
    EOS
    
    @packers.push name
  }
end

#unpack(buf) ⇒ Object



112
113
114
115
116
# File 'lib/ease_engine/packet.rb', line 112

def unpack( buf )
  MessagePack.unpack( buf ).each{|name, value|
    instance_variable_set( "@#{name}", value ) if @packers.include?( name.to_sym )
  }
end

#write(socket, flags, *args) ⇒ Object



118
119
120
121
122
# File 'lib/ease_engine/packet.rb', line 118

def write( socket, flags, *args )
  packed_packet = pack
  size = Packet.pack_uint32( packed_packet.size )
  socket.send( "#{size}#{packed_packet}", flags, *args )
end