class XChan::UNIXSocket
Constants
- NULL_BYTE
@api private
Attributes
bytes_read[R]
@return [Integer]
Returns the total number of bytes read from the channel
bytes_written[R]
@return [Integer]
Returns the total number of bytes written to the channel
Public Class Methods
new(serializer)
click to toggle source
@example
ch = XChan::UNIXSocket.new(Marshal) ch.send [1,2,3] ch.recv.pop # => 3 ch.close
@param [#dump, load] serializer
A serializer (eg Marshal, JSON, YAML)
@return [XChan::UNIXSocket]
# File lib/xchan/unix_socket.rb, line 28 def initialize(serializer) @serializer = serializer @reader, @writer = ::UNIXSocket.pair :STREAM @bytes_written = 0 @bytes_read = 0 end
Public Instance Methods
close()
click to toggle source
@raise [IOError]
Raised when the channel is already closed.
@return [Boolean]
Returns true when the channel is closed.
# File lib/xchan/unix_socket.rb, line 48 def close if closed? raise IOError, "Channel is already closed" else @reader.close @writer.close true end end
closed?()
click to toggle source
@return [Boolean]
Returns true when the channel is closed.
# File lib/xchan/unix_socket.rb, line 39 def closed? @reader.closed? and @writer.closed? end
readable?()
click to toggle source
@return [Boolean]
Returns true when the channel is ready to be read
# File lib/xchan/unix_socket.rb, line 155 def readable? if closed? false else readable, _ = IO.select [@reader], nil, nil, 0 !!readable end end
recv()
click to toggle source
Performs a read that blocks until the underlying IO is readable.
@raise (see timed_recv
)
@return [Object]
An object from the channel.
# File lib/xchan/unix_socket.rb, line 108 def recv timed_recv(nil) end
Also aliased as: read
recv_last()
click to toggle source
@example
ch = xchan ch.send 1 ch.send 2 ch.send 3 ch.recv_last # => 3
@return [Object, nil]
Returns the last object written to the channel or "nil" if the underlying IO is not readable.
# File lib/xchan/unix_socket.rb, line 146 def recv_last last = nil last = recv while readable? last end
Also aliased as: read_last
send(object)
click to toggle source
Performs a write that blocks until the underlying IO is writable.
@raise (see timed_send
)
@param [Object] object
The object to send to the channel.
@return (see timed_send
)
# File lib/xchan/unix_socket.rb, line 66 def send(object) timed_send(object, nil) end
Also aliased as: write
timed_recv(timeout = 0.1)
click to toggle source
Performs a read with a time out.
@param [Float, Integer] timeout
The amount of time to wait for the underlying IO to be readable.
@raise [IOError]
Raised when the channel is closed.
@return [Object, nil]
An object from the channel, or `nil` if the read times out.
# File lib/xchan/unix_socket.rb, line 123 def timed_recv(timeout = 0.1) if @reader.closed? raise IOError, "closed channel" end readable, = IO.select [@reader], nil, nil, timeout if readable base64_str = readable[0].readline(NULL_BYTE) @bytes_read += base64_str.bytesize @serializer.load strict_decode64(base64_str.chomp(NULL_BYTE)) end end
Also aliased as: timed_read
timed_send(object, timeout = 0.1)
click to toggle source
Performs a write with a time out.
@param [Object] object
The object to write to the channel.
@param [Float, Integer] timeout
The amount of time to wait for the underlying IO to be writable
@raise [IOError]
Raised when the channel is closed.
@raise [XChan::NilError]
Raised when trying to write `nil` or `false` to the channel.
@return [Integer, nil]
The number of bytes written to the channel, or `nil` if the write times out.
# File lib/xchan/unix_socket.rb, line 89 def timed_send(object, timeout = 0.1) raise IOError, "closed channel" if @writer.closed? raise XChan::NilError, "false and nil can't be written directly to a channel" if [nil, false].include?(object) _, writable, _ = IO.select nil, [@writer], nil, timeout if writable msg = @serializer.dump(object) syswrite_count = writable[0].syswrite "#{strict_encode64(msg)}#{NULL_BYTE}" @bytes_written += syswrite_count syswrite_count end end
Also aliased as: timed_write