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".freeze

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, #loop, #storage

Instance Method Summary collapse

Methods included from Stream

included, #listen, #progress, #readable?, #shutdown, #start_read, #stop_read, #try_write, #writable?, #write

Methods inherited from Handle

#active?, #close, #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

Constructor Details

#initialize(loop, ipc, acceptor = nil) ⇒ Pipe

Returns a new instance of Pipe.



13
14
15
16
17
18
19
20
21
# File 'lib/libuv/pipe.rb', line 13

def initialize(loop, ipc, acceptor = nil)
    @loop, @ipc = loop, ipc

    pipe_ptr = ::Libuv::Ext.allocate_handle_pipe
    error = check_result(::Libuv::Ext.pipe_init(loop.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

#accept(callback = nil, &blk) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/libuv/pipe.rb', line 33

def accept(callback = nil, &blk)
    pipe = nil
    begin
        raise RuntimeError, CLOSED_HANDLE_ERROR if @closed
        pipe = Pipe.new(loop, @ipc, handle)
    rescue Exception => e
        @loop.log :info, :pipe_accept_failed, e
    end
    if pipe
        begin
            (callback || blk).call(pipe)
        rescue Exception => e
            @loop.log :error, :pipe_accept_cb, e
        end
    end
    nil
end

#bind(name, callback = nil, &blk) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/libuv/pipe.rb', line 23

def bind(name, callback = nil, &blk)
    return if @closed
    @on_listen = callback || blk
    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
end

#check_pending(expecting = nil) ⇒ Object

Raises:

  • (TypeError)


121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/libuv/pipe.rb', line 121

def check_pending(expecting = nil)
    return nil if ::Libuv::Ext.pipe_pending_count(handle) <= 0

    pending = ::Libuv::Ext.pipe_pending_type(handle)
    raise TypeError, "IPC expecting #{expecting} and received #{pending}" if expecting && expecting != pending

    # Hide the accept logic
    remote = nil
    case pending
    when :tcp
        remote = TCP.new(loop, handle)
    when :pipe
        remote = Pipe.new(loop, @ipc, handle)
    else
        raise NotImplementedError, "IPC for handle #{pending} not supported"
    end
    remote
end

#connect(name, callback = nil, &blk) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/libuv/pipe.rb', line 69

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
end

#getsockname ⇒ Object



140
141
142
143
144
145
146
147
# File 'lib/libuv/pipe.rb', line 140

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



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/libuv/pipe.rb', line 51

def open(fileno, callback = nil, &blk)
    @callback = callback || blk
    assert_type(Integer, fileno, "io#fileno must return 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)
        rescue Exception => e
            @loop.log :error, :pipe_connect_cb, e
        end
    rescue Exception => e
        reject(e)
    end
end

#pending_instances=(count) ⇒ Object

Windows only



115
116
117
118
119
# File 'lib/libuv/pipe.rb', line 115

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

def write2(fd, data = ".")
    deferred = @loop.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
    deferred.promise
end