Class: DRb::DRbMessage

Inherits:
Object
  • Object
show all
Defined in:
lib/opal/drb/drb_message.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ DRbMessage

Returns a new instance of DRbMessage.



3
4
5
6
7
# File 'lib/opal/drb/drb_message.rb', line 3

def initialize(config)
  config = DRb.default_config.merge(config || {})
  @load_limit = config[:load_limit]
  @argc_limit = config[:argc_limit]
end

Instance Method Details

#dump(obj, error = false) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/opal/drb/drb_message.rb', line 9

def dump(obj, error=false)
  obj = make_proxy(obj, error) if obj.kind_of? DRbUndumped
  begin
    str = Marshal::dump(obj)
  rescue
    str = Marshal::dump(make_proxy(obj, error))
  end
  pack_n(str.size) + str
end

#load(soc) ⇒ Object

Raises:



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/opal/drb/drb_message.rb', line 19

def load(soc)
  begin
    sz = soc.read(4)
  rescue
    raise(DRbConnError, $!.message, $!.backtrace)
  end

  raise(DRbConnError, 'connection closed') if sz.nil?
  raise(DRbConnError, 'premature header') if sz.size < 4

  sz = unpack_n(sz)[0]

  raise(DRbConnError, "too large packet #{sz}") if @load_limit < sz
  begin
    str = soc.read(sz)
  rescue
    raise(DRbConnError, $!.message, $!.backtrace)
  end

  raise(DRbConnError, 'connection closed') if str.nil?
  raise(DRbConnError, 'premature marshal format(can\'t read)') if str.size < sz

  Marshal::load(str)
end

#recv_reply(stream) ⇒ Object



78
79
80
81
82
# File 'lib/opal/drb/drb_message.rb', line 78

def recv_reply(stream)
  succ = load(stream)
  result = load(stream)
  [succ, result]
end

#recv_request(stream) ⇒ Object

Raises:



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/opal/drb/drb_message.rb', line 58

def recv_request(stream)
  ref = load(stream)
  ro = DRb.to_obj(ref)
  msg = load(stream)
  argc = load(stream)
  raise(DRbConnError, "too many arguments") if @argc_limit < argc
  argv = Array.new(argc, nil)
  argc.times do |n|
    argv[n] = load(stream)
  end
  block = load(stream)
  return ro, msg, argv, block
end

#send_reply(stream, succ, result) ⇒ Object



72
73
74
75
76
# File 'lib/opal/drb/drb_message.rb', line 72

def send_reply(stream, succ, result)
  stream.write(dump(succ) + dump(result, !succ))
rescue
  raise(DRbConnError, $!.message, $!.backtrace)
end

#send_request(stream, ref, msg_id, arg, b) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/opal/drb/drb_message.rb', line 44

def send_request(stream, ref, msg_id, arg, b)
  ary = []
  ary.push(dump(ref.__drbref))
  ary.push(dump(msg_id))
  ary.push(dump(arg.length))
  arg.each do |e|
    ary.push(dump(e))
  end
  ary.push(dump(b))
  stream.write(ary.join(''))
rescue
  raise(DRbConnError, $!.message, $!.backtrace)
end