Module: Dizby::ReadableStream

Included in:
Messenger
Defined in:
lib/dizby/stream/readable.rb

Instance Method Summary collapse

Instance Method Details

#check_packet_size(sz) (private)


52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/dizby/stream/readable.rb', line 52

def check_packet_size(sz)
  fail RemoteServerShutdown unless sz
  fail ConnectionError, 'premature header' if sz.size < 4

  sz = sz.unpack('N')[0]

  # load_limit must be greater than the size of the packet
  # or the load_limit can be 0 or less to be considered "infinite"
  if @server.load_limit.between?(0, sz)
    fail ConnectionError, "too large packet for #{sz}"
  end

  sz
end

#load_obj(marshalled_str) (private)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/dizby/stream/readable.rb', line 37

def load_obj(marshalled_str)
  @server.log.debug("loading data: #{marshalled_str.inspect}")
  obj = Marshal.load(marshalled_str)
  @server.log.debug("loaded: #{obj.inspect}")

  # get a local object or create the proxy using the current server
  # done here since marshalling doesn't know about the current server
  obj = obj.evaluate(@server) if obj.is_a?(SemiObjectProxy)

  obj
rescue NameError, ArgumentError
  @server.log.debug("unknown: #{$!.inspect} #{$!.backtrace}")
  UnknownObject.new($!, marshalled_str)
end

#load_packet(sz) (private)


31
32
33
34
35
# File 'lib/dizby/stream/readable.rb', line 31

def load_packet(sz)
  @stream.read(sz)
rescue
  raise ConnectionError, $!.message, $!.backtrace
end

#load_size (private)


25
26
27
28
29
# File 'lib/dizby/stream/readable.rb', line 25

def load_size
  @stream.read(4)
rescue
  raise ConnectionError, $!.message, $!.backtrace
end

#read


8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/dizby/stream/readable.rb', line 8

def read
  sz = check_packet_size(load_size)
  str = load_packet(sz)

  fail ConnectionError, 'connection closed' unless str

  if str.size < sz
    fail ConnectionError, 'premature marshal format(can\'t read)'
  end

  load_obj(str)

  # TODO: un-tainting???
end