Skip to content

Instantly share code, notes, and snippets.

@laktek
Created July 6, 2009 06:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laktek/141291 to your computer and use it in GitHub Desktop.
Save laktek/141291 to your computer and use it in GitHub Desktop.
class Message
attr_accessor :body
def initialize
@api_version = 1
@api_format = 1
@message_type = 1
@source_id = 64071
@destination_id = 64072
@checksum = 1
end
def serialize
output = ""
output << [@api_version].pack('c')
output << [@api_format].pack('c')
output << [@message_type].pack('c')
output << [].pack('x')
output << [@source_id].pack('n')
output << [@destination_id].pack('n')
output << [@body.length].pack('n')
output << [].pack('x2')
output << [].pack('x3')
output << [@checksum].pack('c')
output << @body
end
def deserialize(message)
@api_version = message[0]
@api_format = message[1]
@message_type = message[2]
@source_id = message[4..5].unpack('n')[0]
@destination_id = message[6..7].unpack('n')[0]
@message_length = message[8..9].unpack('n')[0]
@checksum = message[15]
@body = message[16..(@message_length + 15)]
self
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment