Class: UNIXSocket
- Inherits:
-
BasicSocket
- Object
- IO
- BasicSocket
- UNIXSocket
- Defined in:
- lib/polyphony/extensions/socket.rb
Overview
UNIXSocket extensions
Direct Known Subclasses
Instance Method Summary collapse
-
#<<(mesg) ⇒ Integer
Sends the given message on the socket.
-
#feed_loop(receiver, method = :call, &block) ⇒ Socket
Receives data from the socket in an infinite loop, passing the data to the given receiver using the given method.
-
#read(len = nil, buf = nil, buffer_pos = 0) ⇒ String
Reads from the socket.
-
#read_nonblock(maxlen, buf = nil, exception: true) ⇒ String, :wait_readable
Performs a non-blocking read from the socket of up to
maxlen
bytes. -
#readpartial(maxlen, buf = +'',, buffer_pos = 0, raise_on_eof = true) ⇒ String?
Reads up to
maxlen
from the socket. -
#recv(maxlen, flags = 0, outbuf = nil) ⇒ String
Receives up to
maxlen
bytes from the socket. -
#recv_loop(maxlen = 8192) {|String| ... } ⇒ Socket
(also: #read_loop)
Receives up to
maxlen
bytes at a time in an infinite loop. -
#send(mesg, flags) ⇒ Integer
Sends the given message on the socket.
-
#write(*args) ⇒ Integer
Sends one or more strings on the socket.
-
#write_nonblock(buf, exception: true) ⇒ Integer, :wait_readable
Performs a non-blocking to the socket.
Methods inherited from BasicSocket
#__read_method__, #__write_method__
Methods inherited from IO
#close, copy_stream, deflate, double_splice, #double_splice, gunzip, gzip, http1_splice_chunked, inflate, orig_readlines, readlines, splice, #splice_from, tee, #tee_from, #wait_readable, #wait_writable
Instance Method Details
#<<(mesg) ⇒ Integer
Sends the given message on the socket.
607 608 609 |
# File 'lib/polyphony/extensions/socket.rb', line 607 def <<(mesg) Polyphony.backend_send(self, mesg, 0) end |
#feed_loop(receiver, method = :call, &block) ⇒ Socket
Receives data from the socket in an infinite loop, passing the data to the given receiver using the given method. If a block is given, the result of the method call to the receiver is passed to the block.
This method can be used to feed data into parser objects. The following example shows how to feed data from a socket directly into a MessagePack unpacker:
unpacker = MessagePack::Unpacker.new conn.feed_loop(unpacker, :feed_each) { |msg| handle_msg(msg) }
581 582 583 |
# File 'lib/polyphony/extensions/socket.rb', line 581 def feed_loop(receiver, method = :call, &block) Polyphony.backend_recv_feed_loop(self, receiver, method, &block) end |
#read(len = nil, buf = nil, buffer_pos = 0) ⇒ String
Reads from the socket. If maxlen
is given, reads up to maxlen
bytes from
the socket, otherwise reads to EOF
. If buf
is given, it is used as the
buffer to read into, otherwise a new string is allocated. If buffer_pos
is
given, reads into the given offset (in bytes) in the given buffer. If the
given buffer offset is negative, it is calculated from the current end of
the buffer (-1
means the read data will be appended to the end of the
buffer).
If no bytes are available and EOF
is not hit, this method will block until
the socket is ready to read from.
528 529 530 531 532 533 534 535 536 537 538 539 |
# File 'lib/polyphony/extensions/socket.rb', line 528 def read(len = nil, buf = nil, buffer_pos = 0) return '' if len == 0 return Polyphony.backend_read(self, buf, len, true, buffer_pos) if buf @read_buffer ||= +'' result = Polyphony.backend_read(self, @read_buffer, len, true, -1) return nil unless result already_read = @read_buffer @read_buffer = +'' already_read end |
#read_nonblock(maxlen, buf = nil, exception: true) ⇒ String, :wait_readable
Performs a non-blocking read from the socket of up to maxlen
bytes. If
buf
is given, it is used as the read buffer, otherwise a new string will
be allocated. If the socket is not ready for reading and exception
is
true, an IO::WaitReadable
will be raised. If the socket is not ready for
reading and exception
is false, :wait_readable
is returned.
644 645 646 |
# File 'lib/polyphony/extensions/socket.rb', line 644 def read_nonblock(maxlen, buf = nil, exception: true) @io.read_nonblock(maxlen, buf, exception:) end |
#readpartial(maxlen, buf = +'',, buffer_pos = 0, raise_on_eof = true) ⇒ String?
Reads up to maxlen
from the socket. If buf
is given, it is used as the
buffer to read into, otherwise a new string is allocated. If buffer_pos
is
given, reads into the given offset (in bytes) in the given buffer. If the
given buffer offset is negative, it is calculated from the current end of
the buffer (-1
means the read data will be appended to the end of the
buffer). If raise_on_eof
is true
(the default,) an EOFError
will be
raised on EOF
, otherwise nil
will be returned.
If no bytes are available and EOF
is not hit, this method will block until
the socket is ready to read from.
627 628 629 630 631 632 |
# File 'lib/polyphony/extensions/socket.rb', line 627 def readpartial(maxlen, buf = +'', buffer_pos = 0, raise_on_eof = true) result = Polyphony.backend_recv(self, buf, maxlen, buffer_pos) raise EOFError if !result && raise_on_eof result end |
#recv(maxlen, flags = 0, outbuf = nil) ⇒ String
Receives up to maxlen
bytes from the socket. If outbuf
is given, it is
used as the buffer to receive into, otherwise a new string is allocated and
used as buffer.
If no bytes are available, this method will block until the socket is ready to receive from.
552 553 554 |
# File 'lib/polyphony/extensions/socket.rb', line 552 def recv(maxlen, flags = 0, outbuf = nil) Polyphony.backend_recv(self, outbuf || +'', maxlen, 0) end |
#recv_loop(maxlen = 8192) {|String| ... } ⇒ Socket Also known as: read_loop
Receives up to maxlen
bytes at a time in an infinite loop. Read buffers
will be passed to the given block.
562 563 564 |
# File 'lib/polyphony/extensions/socket.rb', line 562 def recv_loop(maxlen = 8192, &block) Polyphony.backend_recv_loop(self, maxlen, &block) end |
#send(mesg, flags) ⇒ Integer
Sends the given message on the socket.
590 591 592 |
# File 'lib/polyphony/extensions/socket.rb', line 590 def send(mesg, flags) Polyphony.backend_send(self, mesg, flags) end |
#write(*args) ⇒ Integer
Sends one or more strings on the socket. The strings are guaranteed to be written as a single blocking operation.
599 600 601 |
# File 'lib/polyphony/extensions/socket.rb', line 599 def write(*args) Polyphony.backend_sendv(self, args, 0) end |
#write_nonblock(buf, exception: true) ⇒ Integer, :wait_readable
Performs a non-blocking to the socket. If the socket is not ready for
writing and exception
is true, an IO::WaitWritable
will be raised. If
the socket is not ready for writing and exception
is false,
:wait_writable
is returned.
656 657 658 |
# File 'lib/polyphony/extensions/socket.rb', line 656 def write_nonblock(buf, exception: true) @io.write_nonblock(buf, exception:) end |