Module: Binary::Protocol::ClassMethods

Defined in:
lib/binary/protocol.rb

Overview

Provides a DSL for defining struct-like fields for building binary messages.

Examples:

class Command
  include Binary::Protocol

  int32 :length
end

Command.fields # => [:length]
command = Command.new
command.length = 12
command.serialize_length("") # => "\f\x00\x00\x00"

Instance Method Summary collapse

Instance Method Details

#deserialize(buffer = nil, &block) ⇒ Object



214
215
216
217
218
219
220
221
222
# File 'lib/binary/protocol.rb', line 214

def deserialize(buffer = nil, &block)
  if block_given?
    re_define_method(:deserialize, &block)
  else
    message = allocate
    message.deserialize(buffer)
    message
  end
end

#fieldsArray

Returns the fields defined for this message.

Returns:

  • (Array)

    the fields defined for this message



77
78
79
# File 'lib/binary/protocol.rb', line 77

def fields
  @fields ||= []
end

#finalizeObject

Declares the protocol class as complete, and defines its serialization method from the declared fields.



204
205
206
207
208
209
210
211
212
# File 'lib/binary/protocol.rb', line 204

def finalize
  class_eval <<-RUBY, __FILE__, __LINE__ + 1
    def serialize(buffer = "")
      #{serialization.map { |command| "#{command}(buffer)" }.join("\n")}
      buffer
    end
    alias to_s serialize
  RUBY
end

#serializationArray

Returns the methods to run in order for serialiation.

Returns:

  • (Array)

    the methods to run in order for serialiation



72
73
74
# File 'lib/binary/protocol.rb', line 72

def serialization
  @serialization ||= []
end

#string(name, options = {}) ⇒ Object

Declare a string field.

Examples:

class Message
  include Binary::Protocol
  string :collection
end

Parameters:

  • name (String)

    the name of this field



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/binary/protocol.rb', line 138

def string(name, options = {})
  if options.key?(:always)
    __define_always__(name, options[:always])
  else
    if options.key?(:default)
      __define_default__(name, options[:default])
    else
      attr_accessor name
    end

    class_eval <<-RUBY, __FILE__, __LINE__ + 1
      def deserialize_#{name}(buffer)
        raise NotImplementedError
      end
    RUBY
  end

  class_eval <<-RUBY, __FILE__, __LINE__ + 1
    def serialize_#{name}(buffer)
      buffer << #{name}
    end
  RUBY

  serialization << :"serialize_#{name}"
  fields << name
end

#stringz(name, options = {}) ⇒ Object

Declare a null terminated string field.

Examples:

class Message
  include Binary::Protocol
  stringz :collection
end

Parameters:

  • name (String)

    the name of this field



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/binary/protocol.rb', line 174

def stringz(name, options = {})
  if options.key?(:always)
    __define_always__(name, options[:always])
  else
    if options.key?(:default)
      __define_default__(name, options[:default])
    else
      attr_accessor name
    end

    class_eval <<-RUBY, __FILE__, __LINE__ + 1
      def deserialize_#{name}(buffer)
        raise NotImplementedError
      end
    RUBY
  end

  class_eval <<-RUBY, __FILE__, __LINE__ + 1
    def serialize_#{name}(buffer)
      buffer << #{name}
      buffer << 0
    end
  RUBY

  serialization << :"serialize_#{name}"
  fields << name
end