Class: UNIXSocket

Inherits:
BasicSocket show all
Defined in:
lib/polyphony/extensions/socket.rb

Overview

UNIXSocket extensions

Direct Known Subclasses

UNIXServer

Instance Method Summary collapse

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.

Parameters:

  • mesg (String)

    data to send

Returns:

  • (Integer)

    number of bytes sent



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) }

Parameters:

  • receiver (any)

    receiver object

  • method (Symbol) (defaults to: :call)

    method to call

Returns:



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.

Parameters:

  • len (Integer, nil) (defaults to: nil)

    maximum bytes to read from socket

  • buf (String, nil) (defaults to: nil)

    buffer to read into

  • buffer_pos (Number) (defaults to: 0)

    buffer position to read into

Returns:

  • (String)

    buffer used for reading



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.

Parameters:

  • maxlen (Integer)

    maximum bytes to read

  • buf (String, nil) (defaults to: nil)

    read buffer

  • exception (bool) (defaults to: true)

    whether to raise an exception if not ready for reading

Returns:

  • (String, :wait_readable)

    read buffer



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.

Parameters:

  • maxlen (Integer, nil)

    maximum bytes to read from socket

  • buf (String, nil) (defaults to: +'',)

    buffer to read into

  • buffer_pos (Number) (defaults to: 0)

    buffer position to read into

  • raise_on_eof (bool) (defaults to: true)

    whether to raise an exception on EOF

Returns:

  • (String, nil)

    buffer used for reading or nil on EOF

Raises:

  • (EOFError)


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.

Parameters:

  • maxlen (Integer)

    maximum bytes to receive

  • flags (Integer) (defaults to: 0)

    receive flags

  • outbuf (String, nil) (defaults to: nil)

    buffer for reading or nil to allocate new string

Returns:

  • (String)

    receive buffer



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.

Parameters:

  • maxlen (Integer) (defaults to: 8192)

    maximum bytes to receive

Yields:

  • (String)

    received data

Returns:



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.

Parameters:

  • mesg (String)

    data to send

  • flags (Integer)

    send flags

Returns:

  • (Integer)

    number of bytes sent



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.

Parameters:

  • args (Array<String>)

    string buffers to write

Returns:

  • (Integer)

    number of bytes written



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.

Parameters:

  • buf (String, nil)

    write buffer

  • exception (bool) (defaults to: true)

    whether to raise an exception if not ready for reading

Returns:

  • (Integer, :wait_readable)

    number of bytes written



656
657
658
# File 'lib/polyphony/extensions/socket.rb', line 656

def write_nonblock(buf, exception: true)
  @io.write_nonblock(buf, exception:)
end