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

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



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

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
end

#check_pending(expecting = nil) ⇒ Object

Raises:

  • (TypeError)


106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/libuv/pipe.rb', line 106

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(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



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

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

#getsocknameObject



125
126
127
128
129
130
131
132
# File 'lib/libuv/pipe.rb', line 125

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



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

def open(fileno, callback = nil, &blk)
    @callback = callback || blk
    assert_type(Integer, fileno, 'fileno must be an integer file descriptor'.freeze)

    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
            @loop.log :error, :pipe_connect_cb, e
        end
    rescue Exception => e
        reject(e)
    end
end

#pending_instances=(count) ⇒ Object

Windows only



100
101
102
103
104
# File 'lib/libuv/pipe.rb', line 100

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



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/libuv/pipe.rb', line 67

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