Class: Ccrpc::RpcConnection
- Inherits:
-
Object
- Object
- Ccrpc::RpcConnection
- Defined in:
- lib/ccrpc/rpc_connection.rb
Defined Under Namespace
Classes: Call, CallAlreadyReturned, CallbackReceiver, ConnectionDetached, DoubleResultError, InvalidResponse, NoCallbackDefined, ReceiverAlreadyDefined
Instance Attribute Summary collapse
-
#read_io ⇒ Object
Returns the value of attribute read_io.
-
#write_io ⇒ Object
Returns the value of attribute write_io.
Instance Method Summary collapse
-
#call(func = nil, params = {}) {|block| ... } ⇒ Hash, ...
Do a RPC call and/or wait for a RPC call from the other side.
-
#detach ⇒ Object
Disable reception of data from the read_io object.
-
#initialize(read_io, write_io, lazy_answers: false) ⇒ RpcConnection
constructor
Create a RPC connection.
Constructor Details
#initialize(read_io, write_io, lazy_answers: false) ⇒ RpcConnection
Create a RPC connection
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/ccrpc/rpc_connection.rb', line 91 def initialize(read_io, write_io, lazy_answers: false) super() @read_io = read_io @write_io = write_io if lazy_answers require 'ccrpc/lazy' alias maybe_lazy do_lazy else alias maybe_lazy dont_lazy end if @write_io.respond_to?(:setsockopt) @write_io.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, true) end # A random number as start call ID is not technically required, but makes transferred data more readable. @id = rand(1000) @id_mutex = Mutex.new @read_mutex = Mutex.new @write_mutex = Mutex.new @answers = {} @receivers = {} @answers_mutex = Mutex.new @new_answer = ConditionVariable.new @read_enum = Enumerator.new do |y| begin while @read_enum l = @read_io.gets&.force_encoding(Encoding::BINARY) break if l.nil? y << l end rescue => err y << err end end end |
Instance Attribute Details
#read_io ⇒ Object
Returns the value of attribute read_io.
80 81 82 |
# File 'lib/ccrpc/rpc_connection.rb', line 80 def read_io @read_io end |
#write_io ⇒ Object
Returns the value of attribute write_io.
81 82 83 |
# File 'lib/ccrpc/rpc_connection.rb', line 81 def write_io @write_io end |
Instance Method Details
#call(func = nil, params = {}) {|block| ... } ⇒ Hash, ...
Do a RPC call and/or wait for a RPC call from the other side.
#call must be called with either a function name (and optional parameters) or with a block or with both. If #call is called with a function name, the block on the other side of the RPC connection is called with that function name. If #call is called with a block only, than it receives these kind of calls, which are called anonymous callbacks. If #call is called with a function name and a block, then the RPC function on the other side is called and it is possible to call back to this dedicated block by invoking Ccrpc::RpcConnection::Call#call_back .
172 173 174 |
# File 'lib/ccrpc/rpc_connection.rb', line 172 def call(func=nil, params={}, &block) call_intern(func, params, &block) end |
#detach ⇒ Object
Disable reception of data from the read_io object.
This function doesn’t close the IO objects. A waiting reception is not aborted by this call. It can be aborted by calling IO#close on the underlying read_io and write_io objects.
142 143 144 |
# File 'lib/ccrpc/rpc_connection.rb', line 142 def detach @read_enum = nil end |