Class: Libuv::Pipe

Inherits:
Handle show all
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

Assertions::MSG_NO_PROC

Constants inherited from Q::Promise

Q::Promise::MAKE_PROMISE

Instance Attribute Summary

Attributes inherited from Handle

#closed, #reactor, #storage

Attributes inherited from Q::Promise

#trace

Instance Method Summary collapse

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

#resolved?, #then

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 = nil, &blk) ⇒ 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 = nil, &blk)
    return if @closed
    @on_accept = callback || blk
    @on_listen = method(: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

Raises:

  • (TypeError)


127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/libuv/pipe.rb', line 127

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, callback = nil, &blk) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/libuv/pipe.rb', line 62

def connect(name, callback = nil, &blk)
    return if @closed
    @callback = callback || blk
    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 @callback.nil?
        @coroutine = @reactor.defer
        co @coroutine.promise
    end

    self
end

#getsocknameObject



146
147
148
149
150
151
152
153
# File 'lib/libuv/pipe.rb', line 146

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, callback = nil, &blk) ⇒ 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, callback = nil, &blk)
    @callback = callback || blk
    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
        begin
            @callback.call(self) if @callback
        rescue Exception => e
            @reactor.log e, 'performing pipe connect callback'
            raise e unless @callback
        end
    rescue Exception => e
        reject(e)
        raise e unless @callback
    end

    self
end

#pending_instances=(count) ⇒ Object

Windows only



121
122
123
124
125
# File 'lib/libuv/pipe.rb', line 121

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



82
83
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
# File 'lib/libuv/pipe.rb', line 82

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
        co deferred.promise
    end

    self
end