Module: Libuv::Stream
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
- #listen(backlog) ⇒ Object
- #progress(callback = nil, &blk) ⇒ Object
- #readable? ⇒ Boolean
-
#shutdown ⇒ Object
Shutsdown the writes on the handle waiting until the last write is complete before triggering the callback.
-
#start_read ⇒ Object
Starts reading from the handle.
-
#stop_read ⇒ Object
Stops reading from the handle.
- #writable? ⇒ Boolean
- #write(data) ⇒ Object
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
84 85 86 |
# File 'lib/libuv/mixins/stream.rb', line 84 def progress(callback = nil, &blk) @progress = callback || blk end |
#readable? ⇒ Boolean
74 75 76 77 |
# File 'lib/libuv/mixins/stream.rb', line 74 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
79 80 81 82 |
# File 'lib/libuv/mixins/stream.rb', line 79 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 |
# File 'lib/libuv/mixins/stream.rb', line 39 def write(data) # NOTE:: Similar to udp.rb -> send deferred = @loop.defer if !@closed begin assert_type(String, data, WRITE_ERROR) size = data.respond_to?(:bytesize) ? data.bytesize : data.size buffer1 = ::Libuv::Ext.malloc size buffer1.write_string data buffer = ::Libuv::Ext.buf_init(buffer1, 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) ::Libuv::Ext.free(buffer1) 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 |