class EaseEngine::Packet
Attributes
packet_name[R]
Public Class Methods
create( socket )
click to toggle source
# 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 )
click to toggle source
# 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
new()
click to toggle source
# File lib/ease_engine/packet.rb, line 80 def initialize @packers = [] @packet_name = self.class.name.gsub( /::/, "." ) end
pack_str( value )
click to toggle source
# File lib/ease_engine/packet.rb, line 18 def self.pack_str( value ) [ value ].pack( "Z*" ) end
pack_uint16( value )
click to toggle source
# File lib/ease_engine/packet.rb, line 10 def self.pack_uint16( value ) [ value ].pack( "v" ) end
pack_uint32( value )
click to toggle source
# File lib/ease_engine/packet.rb, line 14 def self.pack_uint32( value ) [ value ].pack( "V" ) end
pack_uint8( value )
click to toggle source
# File lib/ease_engine/packet.rb, line 6 def self.pack_uint8( value ) [ value ].pack( "C" ) end
unpack_str( buf )
click to toggle source
# File lib/ease_engine/packet.rb, line 34 def self.unpack_str( buf ) buf.unpack( "Z*" )[ 0 ] end
unpack_uint16( buf )
click to toggle source
# 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 )
click to toggle source
# 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 )
click to toggle source
# File lib/ease_engine/packet.rb, line 22 def self.unpack_uint8( buf ) ( 1 != buf.length ) ? 0 : buf.unpack( "C" )[ 0 ] end
Public Instance Methods
pack()
click to toggle source
# 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 )
click to toggle source
# 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 )
click to toggle source
# 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 )
click to toggle source
# 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