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)


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

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)


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

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)


29
30
31
32
33
# File 'lib/dizby/stream/readable.rb', line 29

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

#load_size (private)


23
24
25
26
27
# File 'lib/dizby/stream/readable.rb', line 23

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

#read


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

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