0.0
No commit activity in last 3 years
No release in over 3 years
Cleans up the string mess when packing items (in Array#pack) and unpacking items (in String#unpack).
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

PackedStruct Build Status

PackedStruct is a way to define packing strings (see Array#pack). It was created after @charliesome suggested a format for defining these strings, but never finished it.

The basic way of defining a packed struct is such:

class RconPacket
  include PackedStruct
  struct_layout :packet do
    little_endian signed size[32] # defaults to a number of size 32.
                                  # also the same as: `little_endian signed long size`
    little_endian signed id[32]
    little_endian signed type[32]
    string body[size]
    null
  end
end

This can be accessed as:

RconPacket.structs[:packet].pack(size: 11, id: 1, type: 0, body: "hello world")
# => "\v\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00hello world\x00"

You can also unpack strings.

RconPacket.structs[:packet].unpack("\v\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00hello world\x00")
# => {:size => 11, :id => 1, :type => 0, :body => "hello world"}

From sockets, too. Anything that responds to #read.

file = File.open("/path/to/some/file", "r")

RconPacket.structs[:packet].unpack_from_socket(file)
# => ...

Bitdeli Badge