Class: Libuv::Pipe
- Inherits:
-
Handle
- Object
- Q::Promise
- Q::DeferredPromise
- Handle
- Libuv::Pipe
- Includes:
- Stream
- Defined in:
- lib/libuv/pipe.rb
Constant Summary collapse
- WRITE2_ERROR =
"data must be a String"
Constants included from Stream
Stream::BACKLOG_ERROR, Stream::CLOSED_HANDLE_ERROR, Stream::STREAM_CLOSED_ERROR, Stream::WRITE_ERROR
Constants included from Assertions
Instance Attribute Summary
Attributes inherited from Handle
Attributes inherited from Q::Promise
Instance Method Summary collapse
- #bind(name, &callback) ⇒ Object
- #check_pending(expecting = nil) ⇒ Object
- #connect(name) ⇒ Object
- #getsockname ⇒ Object
-
#initialize(reactor, ipc, acceptor = nil) ⇒ Pipe
constructor
A new instance of Pipe.
- #open(fileno) ⇒ Object
-
#pending_instances=(count) ⇒ Object
Windows only.
- #write2(fd, data = ".", wait: false) ⇒ Object
Methods included from Stream
#close_write, #flush, included, #listen, #progress, #read, #readable?, #shutdown, #start_read, #stop_read, #try_write, #writable?, #write
Methods inherited from Handle
#active?, #close, #closed?, #closing?, #ref, #unref
Methods included from Assertions
#assert_block, #assert_boolean, #assert_type
Methods included from Resource
#check_result, #check_result!, #resolve, #to_ptr
Methods inherited from Q::DeferredPromise
Methods inherited from Q::Promise
#catch, #finally, #progress, #ruby_catch, #value
Constructor Details
#initialize(reactor, ipc, acceptor = nil) ⇒ Pipe
Returns a new instance of Pipe.
15 16 17 18 19 20 21 22 23 |
# File 'lib/libuv/pipe.rb', line 15 def initialize(reactor, ipc, acceptor = nil) @reactor, @ipc = reactor, ipc pipe_ptr = ::Libuv::Ext.allocate_handle_pipe error = check_result(::Libuv::Ext.pipe_init(reactor.handle, pipe_ptr, ipc ? 1 : 0)) error = check_result(::Libuv::Ext.accept(acceptor, pipe_ptr)) if acceptor && error.nil? super(pipe_ptr, error) end |
Instance Method Details
#bind(name, &callback) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/libuv/pipe.rb', line 25 def bind(name, &callback) return if @closed @on_accept = callback @on_listen = proc { accept } assert_type(String, name, "name must be a String") name = windows_path name if FFI::Platform.windows? error = check_result ::Libuv::Ext.pipe_bind(handle, name) reject(error) if error self end |
#check_pending(expecting = nil) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/libuv/pipe.rb', line 129 def check_pending(expecting = nil) return nil if ::Libuv::Ext.pipe_pending_count(handle) <= 0 pending = ::Libuv::Ext.pipe_pending_type(handle).to_sym raise TypeError, "IPC expecting #{expecting} and received #{pending}" if expecting && expecting.to_sym != pending # Hide the accept logic remote = nil case pending when :tcp remote = TCP.new(reactor, handle) when :pipe remote = Pipe.new(reactor, @ipc, handle) else raise NotImplementedError, "IPC for handle #{pending} not supported" end remote end |
#connect(name) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/libuv/pipe.rb', line 62 def connect(name) return if @closed assert_type(String, name, "name must be a String") begin name = windows_path name if FFI::Platform.windows? req = ::Libuv::Ext.allocate_request_connect ::Libuv::Ext.pipe_connect(req, handle, name, callback(:on_connect, req.address)) rescue Exception => e reject(e) end if block_given? @callback = Proc.new else @coroutine = @reactor.defer @coroutine.promise.value end self end |
#getsockname ⇒ Object
148 149 150 151 152 153 154 155 |
# File 'lib/libuv/pipe.rb', line 148 def getsockname size = 256 len = FFI::MemoryPointer.new(:size_t) len.put_int(0, size) buffer = FFI::MemoryPointer.new(size) check_result! ::Libuv::Ext.pipe_getsockname(handle, buffer, len) buffer.read_string end |
#open(fileno) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/libuv/pipe.rb', line 39 def open(fileno) assert_type(Integer, fileno, 'fileno must be an integer file descriptor') begin raise RuntimeError, CLOSED_HANDLE_ERROR if @closed check_result! ::Libuv::Ext.pipe_open(handle, fileno) # Emulate on_connect behavior if block_given? begin yield(self) rescue Exception => e @reactor.log e, 'performing pipe connect callback' end end rescue Exception => e reject(e) raise e unless block_given? end self end |
#pending_instances=(count) ⇒ Object
Windows only
123 124 125 126 127 |
# File 'lib/libuv/pipe.rb', line 123 def pending_instances=(count) return 0 if @closed assert_type(Integer, count, "count must be an Integer") ::Libuv::Ext.pipe_pending_instances(handle, count) end |
#write2(fd, data = ".", wait: false) ⇒ Object
84 85 86 87 88 89 90 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 |
# File 'lib/libuv/pipe.rb', line 84 def write2(fd, data = ".", wait: false) deferred = @reactor.defer if @ipc && !@closed begin assert_type(String, data, WRITE_ERROR) assert_type(Handle, fd, WRITE2_ERROR) size = data.respond_to?(:bytesize) ? data.bytesize : data.size buffer = ::Libuv::Ext.buf_init(FFI::MemoryPointer.from_string(data), size) # local as this variable will be avaliable until the handle is closed req = ::Libuv::Ext.allocate_request_write @write_callbacks ||= {} @write_callbacks[req.address] = deferred error = check_result ::Libuv::Ext.write2(req, handle, buffer, 1, fd.handle, callback(:write2_complete, req.address)) if error @write_callbacks.delete(req.address) ::Libuv::Ext.free(req) deferred.reject(error) reject(error) # close the handle end rescue Exception => e deferred.reject(e) # this write exception may not be fatal end else deferred.reject(TypeError.new('pipe not initialized for interprocess communication')) end if wait return deferred.promise if wait == :promise deferred.promise.value end self end |