Class: DRb::DRbMessage

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

Overview

Handler for sending and receiving drb messages.

This takes care of the low-level marshalling and unmarshalling of drb requests and responses sent over the wire between server and client. This relieves the implementor of a new drb protocol layer with having to deal with these details.

The user does not have to directly deal with this object in normal use.

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ DRbMessage

:nodoc:


556
557
558
559
# File 'lib/drb/drb.rb', line 556

def initialize(config) # :nodoc:
  @load_limit = config[:load_limit]
  @argc_limit = config[:argc_limit]
end

Instance Method Details

#dump(obj, error = false) ⇒ Object

:nodoc:


561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
# File 'lib/drb/drb.rb', line 561

def dump(obj, error=false)  # :nodoc:
  case obj
  when DRbUndumped
    obj = make_proxy(obj, error)
  when Object
    # nothing
  else
    obj = make_proxy(obj, error)
  end
  begin
    str = Marshal::dump(obj)
  rescue
    str = Marshal::dump(make_proxy(obj, error))
  end
  [str.size].pack('N') + str
end

#load(soc) ⇒ Object

:nodoc:

Raises:


578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
# File 'lib/drb/drb.rb', line 578

def load(soc)  # :nodoc:
  begin
    sz = soc.read(4)        # sizeof (N)
  rescue
    raise(DRbConnError, $!.message, $!.backtrace)
  end
  raise(DRbConnError, 'connection closed') if sz.nil?
  raise(DRbConnError, 'premature header') if sz.size < 4
  sz = sz.unpack('N')[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
  DRb.mutex.synchronize do
    begin
      Marshal::load(str)
    rescue NameError, ArgumentError
      DRbUnknown.new($!, str)
    end
  end
end

#recv_reply(stream) ⇒ Object

:nodoc:


638
639
640
641
642
# File 'lib/drb/drb.rb', line 638

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

#recv_request(stream) ⇒ Object

:nodoc:

Raises:


618
619
620
621
622
623
624
625
626
627
628
629
630
# File 'lib/drb/drb.rb', line 618

def recv_request(stream) # :nodoc:
  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

:nodoc:


632
633
634
635
636
# File 'lib/drb/drb.rb', line 632

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

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

:nodoc:


604
605
606
607
608
609
610
611
612
613
614
615
616
# File 'lib/drb/drb.rb', line 604

def send_request(stream, ref, msg_id, arg, b) # :nodoc:
  ary = []
  ary.push(dump(ref.__drbref))
  ary.push(dump(msg_id.id2name))
  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