Module: Libuv::Stream

Included in:
Pipe, TCP, TTY
Defined in:
lib/libuv/mixins/stream.rb

Constant Summary collapse

BACKLOG_ERROR =
"backlog must be an Integer".freeze
WRITE_ERROR =
"data must be a String".freeze
STREAM_CLOSED_ERROR =
"unable to write to a closed stream".freeze
CLOSED_HANDLE_ERROR =
"handle closed before accept called".freeze

Instance Method Summary collapse

Instance Method Details

#listen(backlog) ⇒ Object



11
12
13
14
15
16
# File 'lib/libuv/mixins/stream.rb', line 11

def listen(backlog)
    return if @closed
    assert_type(Integer, backlog, BACKLOG_ERROR)
    error = check_result ::Libuv::Ext.listen(handle, Integer(backlog), callback(:on_listen))
    reject(error) if error
end

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



96
97
98
# File 'lib/libuv/mixins/stream.rb', line 96

def progress(callback = nil, &blk)
    @progress = callback || blk
end

#readable?Boolean

Returns:

  • (Boolean)


86
87
88
89
# File 'lib/libuv/mixins/stream.rb', line 86

def readable?
    return false if @closed
    ::Libuv::Ext.is_readable(handle) > 0
end

#shutdownObject

Shutsdown the writes on the handle waiting until the last write is complete before triggering the callback



33
34
35
36
37
# File 'lib/libuv/mixins/stream.rb', line 33

def shutdown
    return if @closed
    error = check_result ::Libuv::Ext.shutdown(::Libuv::Ext.create_request(:uv_shutdown), handle, callback(:on_shutdown))
    reject(error) if error
end

#start_readObject

Starts reading from the handle



19
20
21
22
23
# File 'lib/libuv/mixins/stream.rb', line 19

def start_read
    return if @closed
    error = check_result ::Libuv::Ext.read_start(handle, callback(:on_allocate), callback(:on_read))
    reject(error) if error
end

#stop_readObject

Stops reading from the handle



26
27
28
29
30
# File 'lib/libuv/mixins/stream.rb', line 26

def stop_read
    return if @closed
    error = check_result ::Libuv::Ext.read_stop(handle)
    reject(error) if error
end

#try_write(data) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/libuv/mixins/stream.rb', line 39

def try_write(data)
    assert_type(String, data, WRITE_ERROR)

    buffer1 = ::FFI::MemoryPointer.from_string(data)
    buffer  = ::Libuv::Ext.buf_init(buffer1, data.respond_to?(:bytesize) ? data.bytesize : data.size)

    result = ::Libuv::Ext.try_write(handle, buffer, 1)
    buffer1.free

    error = check_result result
    raise error if error
    return result
end

#writable?Boolean

Returns:

  • (Boolean)


91
92
93
94
# File 'lib/libuv/mixins/stream.rb', line 91

def writable?
    return false if @closed
    ::Libuv::Ext.is_writable(handle) > 0
end

#write(data) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/libuv/mixins/stream.rb', line 53

def write(data)
    # NOTE:: Similar to udp.rb -> send
    deferred = @loop.defer
    if !@closed
        begin
            assert_type(String, data, WRITE_ERROR)

            buffer1 = ::FFI::MemoryPointer.from_string(data)
            buffer  = ::Libuv::Ext.buf_init(buffer1, data.respond_to?(:bytesize) ? data.bytesize : data.size)

            # local as this variable will be available until the handle is closed
            @write_callbacks ||= {}
            req = ::Libuv::Ext.create_request(:uv_write)
            @write_callbacks[req.address] = [deferred, buffer1]
            error = check_result ::Libuv::Ext.write(req, handle, buffer, 1, callback(:write_complete))

            if error
                @write_callbacks.delete req.address
                ::Libuv::Ext.free(req)
                buffer1.free
                deferred.reject(error)

                reject(error)       # close the handle
            end
        rescue => e
            deferred.reject(e)  # this write exception may not be fatal
        end
    else
        deferred.reject(RuntimeError.new(STREAM_CLOSED_ERROR))
    end
    deferred.promise
end