class Estore::Connection::Buffer

Buffer receives data from the TCP connection, and parses the binary packages. Parsed packages are given back to the given handler as they are decoded.

Public Class Methods

new(&block) click to toggle source
# File lib/estore/connection/buffer.rb, line 7
def initialize(&block)
  @mutex = Mutex.new
  @buffer = ''.force_encoding('BINARY')
  @handler = block
end

Public Instance Methods

<<(bytes) click to toggle source
# File lib/estore/connection/buffer.rb, line 13
def <<(bytes)
  bytes = bytes.force_encoding('BINARY') if
    bytes.respond_to? :force_encoding

  @mutex.synchronize do
    @buffer << bytes
  end

  consume_packages
end
consume_packages() click to toggle source
# File lib/estore/connection/buffer.rb, line 24
def consume_packages
  while (pkg = read_package)
    handle(pkg)
    discard_bytes(pkg)
  end
end
discard_bytes(pkg) click to toggle source
# File lib/estore/connection/buffer.rb, line 38
def discard_bytes(pkg)
  @mutex.synchronize do
    @buffer = @buffer[(4 + pkg.bytesize)..-1]
  end
end
handle(pkg) click to toggle source
# File lib/estore/connection/buffer.rb, line 44
def handle(pkg)
  code, flags, uuid_bytes, package = parse(pkg)
  command = Estore::Connection.command_name(code)
  @handler.call(command, package, Package.parse_uuid(uuid_bytes), flags)
end
parse(pkg) click to toggle source
# File lib/estore/connection/buffer.rb, line 50
def parse(pkg)
  [
    pkg[0].unpack('C').first,
    pkg[1].unpack('C').first,
    pkg[2...(2 + 16)],
    pkg[18..-1]
  ]
end
read_package() click to toggle source
# File lib/estore/connection/buffer.rb, line 31
def read_package
  return nil if @buffer.length < 4
  package_length = @buffer[0...4].unpack('l<').first
  bytes = @buffer[4...(4 + package_length)].dup
  bytes if bytes.bytesize >= package_length
end