class Rsrb::Net::PacketBuilder

Constants

BIT_MASK_OUT

Bit masks for bit packing

Attributes

bit_position[R]

The current bit position

buffer[R]

The buffer for this packet

opcode[R]

The opcode for this packet

type[R]

The packet type. Fixed, Variable or Variable Short

Public Class Methods

new(opcode = -1, type = :FIXED) click to toggle source
# File lib/rsrb/net/packetbuilder.rb, line 18
def initialize(opcode = -1, type = :FIXED)
  @opcode = opcode
  @type = type
  @buffer = ""
end

Public Instance Methods

add_bits(num, val) click to toggle source

Adds a series of bits to the array.

# File lib/rsrb/net/packetbuilder.rb, line 120
def add_bits(num, val)
  byte_pos = @bit_position >> 3
  bit_offset = 8 - (@bit_position & 7)
  @bit_position += num
  
  while num > bit_offset
    @buffer[byte_pos] = [0].pack("c") if @buffer[byte_pos] == nil
    @buffer[byte_pos] = [(@buffer[byte_pos].unpack("c")[0] & ~BIT_MASK_OUT[bit_offset])].pack("c")
    @buffer[byte_pos] = [(@buffer[byte_pos].unpack("c")[0] | (val >> (num - bit_offset)) & BIT_MASK_OUT[bit_offset])].pack("c")
    byte_pos += 1
    num -= bit_offset
    bit_offset = 8
  end
  
  @buffer[byte_pos] = [0].pack("c") if @buffer[byte_pos] == nil
  
  if num == bit_offset
    @buffer[byte_pos] = [(@buffer[byte_pos].unpack("c")[0] & ~BIT_MASK_OUT[bit_offset])].pack("c")
    @buffer[byte_pos] = [(@buffer[byte_pos].unpack("c")[0] | (val & BIT_MASK_OUT[bit_offset]))].pack("c")
  else
    @buffer[byte_pos] = [(@buffer[byte_pos].unpack("c")[0] & ~(BIT_MASK_OUT[num] << (bit_offset - num)))].pack("c")
    @buffer[byte_pos] = [(@buffer[byte_pos].unpack("c")[0] | ((val & BIT_MASK_OUT[num]) << (bit_offset - num)))].pack("c")
  end
  
  self
end
add_byte(val) click to toggle source

Adds a byte to the array.

# File lib/rsrb/net/packetbuilder.rb, line 25
def add_byte(val)
  @buffer << [val].pack("C")
  self
end
add_byte_a(val) click to toggle source

Adds a byte type a to the array.

# File lib/rsrb/net/packetbuilder.rb, line 37
def add_byte_a(val)
  add_byte (val + 128)
  self
end
add_byte_c(val) click to toggle source

Adds a byte type c to the array.

# File lib/rsrb/net/packetbuilder.rb, line 43
def add_byte_c(val)
  add_byte -val
  self
end
add_byte_s(val) click to toggle source

Adds a byte type s to the array.

# File lib/rsrb/net/packetbuilder.rb, line 49
def add_byte_s(val)
  add_byte (128 - val)
  self
end
add_bytes(val) click to toggle source

Adds multiple bytes to the array.

# File lib/rsrb/net/packetbuilder.rb, line 31
def add_bytes(val)
  @buffer << val
  self
end
add_bytes_range(data, offset, length) click to toggle source

Adds a series of bytes into the array.

# File lib/rsrb/net/packetbuilder.rb, line 175
def add_bytes_range(data, offset, length)
  @buffer << data[offset...offset+length].pack("c" * length)
  self
end
add_int(val) click to toggle source

Adds an int to the array.

# File lib/rsrb/net/packetbuilder.rb, line 61
def add_int(val)
  add_byte (val >> 24).byte
  add_byte (val >> 16).byte
  add_byte (val >> 8).byte
  add_byte val.byte
  self
end
add_int1(val) click to toggle source

Adds an integer type 1 to the array.

# File lib/rsrb/net/packetbuilder.rb, line 148
def add_int1(val)
  add_byte val >> 8
  add_byte val
  add_byte val >> 24
  add_byte val >> 16
  self
end
add_int2(val) click to toggle source

Adds an integer type 2 to the array.

# File lib/rsrb/net/packetbuilder.rb, line 157
def add_int2(val)
  add_byte val >> 16
  add_byte val >> 24
  add_byte val
  add_byte val >> 8
  self
end
add_leint(val) click to toggle source

Adds a little-endian integer to the array.

# File lib/rsrb/net/packetbuilder.rb, line 166
def add_leint(val)
  add_byte val
  add_byte val >> 8
  add_byte val >> 16
  add_byte val >> 24
  self
end
add_leshort(val) click to toggle source

Adds a little-endian short to the array.

# File lib/rsrb/net/packetbuilder.rb, line 96
def add_leshort(val)
  add_byte val
  add_byte (val >> 8)
  self
end
add_leshort_a(val) click to toggle source

Adds a little-endian short type a to the array.

# File lib/rsrb/net/packetbuilder.rb, line 103
def add_leshort_a(val)
  add_byte (val + 128)
  add_byte (val >> 8)
  self
end
add_long(val) click to toggle source

Adds a long to the array.

# File lib/rsrb/net/packetbuilder.rb, line 70
def add_long(val)
  add_byte (val >> 56).int
                    add_byte (val >> 48).int
                    add_byte (val >> 40).int
                    add_byte (val >> 32).int
                    add_byte (val >> 24).int
                    add_byte (val >> 16).int
                    add_byte (val >> 8).int
                    add_byte val.int
  self
end
add_reverse(is, offset, length) click to toggle source

Adds a series of reversed bytes into the array.

# File lib/rsrb/net/packetbuilder.rb, line 188
def add_reverse(is, offset, length)
  @buffer << data[offset...offset+length].reverse
  self
end
add_reverse_a(data, offset, length) click to toggle source

Adds a series of type a bytes into the array.

# File lib/rsrb/net/packetbuilder.rb, line 181
def add_reverse_a(data, offset, length)
  bytes = data[offset...offset+length].reverse.unpack("c" * length)
  bytes.each {|e| add_byte_a(e) }
  self
end
add_short(val) click to toggle source

Adds a short to the array.

# File lib/rsrb/net/packetbuilder.rb, line 55
def add_short(val)
  @buffer << [val].pack("n")
  self
end
add_short_a(val) click to toggle source

Adds a short type a to the array.

# File lib/rsrb/net/packetbuilder.rb, line 89
def add_short_a(val)
  add_byte (val >> 8)
  add_byte (val + 128)
  self
end
add_smart(val) click to toggle source

Adds an smart into the array.

# File lib/rsrb/net/packetbuilder.rb, line 202
def add_smart(val)
  val >= 128 ? add_short(val + 32768) : add_byte(val)
  self
end
add_str(val) click to toggle source

Adds a string to the array.

# File lib/rsrb/net/packetbuilder.rb, line 83
def add_str(val)
  @buffer << (val + "\n")
  self
end
add_tribyte(val) click to toggle source

Adds a three byte integer into the array.

# File lib/rsrb/net/packetbuilder.rb, line 194
def add_tribyte(val)
  add_byte val >> 16
  add_byte val >> 8
  add_byte val
  self
end
add_usmart(val) click to toggle source

Adds an unsigned smart into the array.

# File lib/rsrb/net/packetbuilder.rb, line 208
def add_usmart(val)
  if val >= 128 
    add_short(val + 49152)
  else 
    add_byte(val + 64)
  end
  
  self
end
empty?() click to toggle source

Checks whether or not the buffer has data in it.

# File lib/rsrb/net/packetbuilder.rb, line 219
def empty?
  @buffer.empty?
end
finish_bit_access() click to toggle source
# File lib/rsrb/net/packetbuilder.rb, line 114
def finish_bit_access
  @bit_position = (@bit_position + 7) / 8
  self
end
start_bit_access() click to toggle source
# File lib/rsrb/net/packetbuilder.rb, line 109
def start_bit_access
  @bit_position = @buffer.size * 8
  self
end
to_packet() click to toggle source

Creates the packet.

# File lib/rsrb/net/packetbuilder.rb, line 224
def to_packet
  Packet.new @opcode, @type, @buffer
end