Name

SPVM::IO::Socket - Sockets

Description

IO::Socket class in SPVM represents a socket.

Usage

use IO::Socket;

Details

IO::Socket is an abstract class.

See "Well Known Child Classes" about child classes of this class.

Socket Constant Values

See Sys::Socket::Constant about constant values for sockets.

Goroutine

Connect, accept, read, and write operations in IO::Socket class work with goroutines.

Non-blocking mode of a IO::Socket object is enabled.

If an IO wait occurs, control is transferred to another goroutine.

This allows you to write connect, accept, read, and write operations as if they were synchronous.

Well Known Child Classes

Super Class

IO::Handle

Fields

Domain

has Domain : protected int;

A protocol family, such as AF_INET, AF_INET6, AF_UNIX.

Type

has Type : protected int;

A socket type, such as SOCK_STREAM, SOCK_DGRAM, SOCK_RAW.

Proto

has Proto : protected ro int;

A socket protocol, such as IPPROTO_TCP, IPPROTO_UDP.

Timeout

has Timeout : protected double;

A timeout seconds for read, write, connect, accept operations.

Listen

has Listen : protected int;

The number of listen backlog.

Instance Methods

init

protected method init : void ($options : object[] = undef);

Options:

The following options are available adding to the options for IO::Handle#init method.

[Name][Type][Default Value]

  • Domain : Int = 0

  • Type : Int = 0

  • Proto : Int = 0

  • Timeout : Double = 0.0

  • Listen : Int = 0

The blocking mode of the socket is set to non-blocking mode.

DESTROY

method DESTROY : void ();

Closes the socket by "close" method if the socket is opened.

sockdomain

method sockdomain : int ();

Returns the value of "Domain" field.

socktype

method socktype : int ();

Returns the value of "Type" field.

protocol

method protocol : int ();

Returns the value of "Proto" field.

timeout

method timeout : double ();

Returns the value of "Timeout" field.

set_timeout

method set_timeout : void ($timeout : double);

Sets "Timeout" field to $timeout.

set_blocking

method set_blocking : void ($blocking : int);

This method is the same as IO::Handle#set_blocking method, but if a true value is given to $blocking, an exception is thrown.

Exceptions:

Calling set_blocking method given a true value on an IO::Socket object is forbidden.

socket

protected method socket : void ();

Opens a socket using "Domain" field, "Type" field, and "Protocal" field.

IO::Handle#FD field is set to the file descriptor of the socket.

This method calls Sys#socket method.

Exceptions:

Exceptions thrown by Sys#socket method could be thrown.

connect

protected method connect : void ($sockaddr : Sys::Socket::Sockaddr);

Performs connect operation.

This method calls Sys#connect method given the value of IO::Handle#FD field and $sockaddr.

If connect operation need to be performed again for IO wait, Go#gosched_io_write method is called given the value of IO::Handle#FD field and the value of "Timeout" field.

And when the current goroutine is returned, this method retries connect operation.

If timeout occurs, an exception is thrown set eval_error_id to the basic type ID of the Go::Error::IOTimeout class.

Exceptions:

Exceptions thrown by Sys#connect method could be thrown.

Exceptions thrown by Go#gosched_io_write method could be thrown.

bind

protected method bind : void ($sockaddr : Sys::Socket::Sockaddr);

Perform bind operation.

This method calls Sys#bind method given the value of IO::Handle#FD field and $sockaddr.

Exceptions:

Exceptions thrown by Sys#bind method could be thrown.

listen

protected method listen : void ();

Does the same thing that listen system call does given the file descriptor IO::Handle#FD field.

This method calls Sys#listen.

Exceptions:

Exceptions thrown by Sys#listen method could be thrown.

accept

method accept : IO::Socket ($peer_ref : Sys::Socket::Sockaddr[] = undef);

Performs accept operation and returns a client socket object.

The type of the returned object is the type of this instance.

This method calls Sys#accept method given the value of IO::Handle#FD field and $peer_ref.

If accept operation need to be performed again for IO wait, Go#gosched_io_read method is called given the value of IO::Handle#FD field and the value of "Timeout" field.

And when the current goroutine is returned, this method retries accept operation.

If timeout occurs, an exception is thrown set eval_error_id to the basic type ID of the Go::Error::IOTimeout class.

$peer_ref at index 0 is set to a client socket address if specified.

Exceptions:

Exceptions thrown by Sys#accept method could be thrown.

Exceptions thrown by Go#gosched_io_read method could be thrown.

shutdown

method shutdown : void ($how : int);

Performs shutdown operation given the way $how.

This method calls Sys#shutdown method.

See Sys::Socket::Constant about constant values given to $how.

  • SHUT_RD

  • SHUT_WR

  • SHUT_RDWR

Exceptions:

Exceptions thrown by Sys#shutdown method could be thrown.

close

method close : void ();

Performs close operation.

This method calls Sys::Socket#close method

Exceptions:

If this socket is not opened or already closed, an excetpion is thrown.

recvfrom

method recvfrom : int ($buffer : mutable string, $length : int, $flags : int, $from_ref : Sys::Socket::Sockaddr[], $offset : int = 0)

Performs recvfrom operation and returns read length.

This method calls Sys#recvfrom method given the value of IO::Handle#FD field, $buffer, $length, $flags, $from_ref, $offset, and returns its return value.

If recvfrom operation need to be performed again for IO wait, Go#gosched_io_read method is called given the value of IO::Handle#FD field and the value of "Timeout" field.

And when the current goroutine is returned, this method retries recvfrom operation.

If timeout occurs, an exception is thrown set eval_error_id to the basic type ID of the Go::Error::IOTimeout class.

Exceptions:

Exceptions thrown by Sys#recvfrom method could be thrown.

Exceptions thrown by Go#gosched_io_read method could be thrown.

sendto

method sendto : int ($buffer : string, $flags : int, $to : Sys::Socket::Sockaddr, $length : int = -1, $offset : int = 0);

Performs sendto operation and returns write length.

This method calls Sys#sendto method given the value of IO::Handle#FD field, $buffer, $flags, $to, $length, $offset, and returns its return value.

If sendto operation need to be performed again for IO wait, Go#gosched_io_write method is called given the value of IO::Handle#FD field and the value of "Timeout" field.

And when the current goroutine is returned, this method retries sendto operation.

If timeout occurs, an exception is thrown set eval_error_id to the basic type ID of the Go::Error::IOTimeout class.

Exceptions:

Exceptions thrown by Sys#sendto method could be thrown.

Exceptions thrown by Go#gosched_io_write method could be thrown.

recv

method recv : int ($buffer : mutable string, $length : int = -1, $flags : int = 0, $offset : int = 0);

Performs recv operation.

Thie method just calls "recvfrom" method given $buffer, $length, $flags, $from_ref set to undef, $offset, and returns its return value.

Exceptions:

Exceptions thrown by "recvfrom" method could be thrown.

send

method send : int ($buffer : string, $flags : int = 0, $length : int = -1, $offset : int = 0);

Performs send operation.

This method just calls "sendto" method given $buffer, $flags, $to set to 0, $length, $offset, and returns its return value.

Exceptions:

Exceptions thrown by "sendto" method could be thrown.

read

method read : int ($buffer : mutable string, $length : int = -1, $offset : int = 0);

Perform read operation.

This method just calls "recv" method given $buffer, $length, $flags set to 0, $offset, and returns its return values.

Exceptions:

Exceptions thrown by "recv" method could be thrown.

write

method write : int ($buffer : string, $length : int = -1, $offset : int = 0);

Perform write operation.

This method just calls "send" method given $buffer, $flags set to 0, $length, $offset, and returns its return values.

Exceptions:

Exceptions thrown by "send" method could be thrown.

sockname

method sockname : Sys::Socket::Sockaddr ();

Returns a Sys::Socket::Sockaddr for a local address.

This method calls Sys#getsockname method given the vlaue of IO::Handle#FD field.

Exceptions:

Exceptions thrown by Sys#getsockname method could be thrown.

peername

method peername : Sys::Socket::Sockaddr ();

Returns a Sys::Socket::Sockaddr for a remote address.

This method calls Sys#getpeername method given the vlaue of IO::Handle#FD field.

Exceptions:

Exceptions thrown by Sys#getpeername method could be thrown.

connected

method connected : Sys::Socket::Sockaddr ();

Checks if the socket is connected.

If "peername" method does not throw an exception, returns a Sys::Socket::Sockaddr object, otherwise returns undef.

atmark

method atmark : int ();

Perform atmark operation and returns the result.

This method just calls Sys::Socket#sockatmark method given the vlaue of IO::Handle#FD field.

Exceptions:

Exceptions thrown by Sys::Socket#sockatmark method could be thrown.

sockopt

method sockopt : int ($level : int, $option_name : int);

Perform sockopt operation and returns the result.

Gets a socket option of the file descriptor stored in IO::Handle#FD field given the socket level $level and the option name $option_name.

This method just calls Sys#getsockopt method given the vlaue of IO::Handle#FD field, $level, $opton_name, and its return value.

Exceptions:

Exceptions thrown by Sys#getsockopt method could be thrown.

setsockopt

method setsockopt : void ($level : int, $option_name : int, $option_value : object of string|Int);

Perform setsockopt operation.

This method just calls Sys#setsockopt method given the arguments given the vlaue of IO::Handle#FD field, $level, $option_name.

Exceptions:

Exceptions thrown by Sys#setsockopt method could be thrown.

See Also

Porting

This class is a Perl's IO::Socket porting to SPVM.

Copyright & License

Copyright (c) 2023 Yuki Kimoto

MIT License