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



94
95
96
# File 'lib/libuv/mixins/stream.rb', line 94

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

#readable? ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
87
# File 'lib/libuv/mixins/stream.rb', line 84

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

#shutdown ⇒ Object

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_read ⇒ Object

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_read ⇒ Object

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

#writable? ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
92
# File 'lib/libuv/mixins/stream.rb', line 89

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

#write(data) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
# File 'lib/libuv/mixins/stream.rb', line 39

def write(data)
    # NOTE:: Similar to udp.rb -> write
    deferred = @loop.defer
    if !@closed
        begin
            assert_type(String, data, WRITE_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
            @write_callbacks ||= []

            #
            # create the curried callback
            #
            callback = FFI::Function.new(:void, [:pointer, :int]) do |req, status|
                ::Libuv::Ext.free(req)
                # remove the callback from the array
                # assumes writes are done in order
                promise = @write_callbacks.shift[0]
                resolve promise, status
            end


            @write_callbacks << [deferred, callback]
            req = ::Libuv::Ext.create_request(:uv_write)
            error = check_result ::Libuv::Ext.write(req, handle, buffer, 1, callback)

            if error
                @write_callbacks.pop
                ::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(RuntimeError.new(STREAM_CLOSED_ERROR))
    end
    deferred.promise
end