AbstractSocket
+ Inheritance diagram for AbstractSocket:

Description

Abstract parent to all sockets.

Protected Properties

resource $resource
 The underlying PHP resource. More...
 

Public Static Methods

static self create (int $domain=AF_INET,... $extra)
 Creates an instance of the called class. More...
 
static int getType ()
 The SOCK_* type constant for the class. More...
 

Public Methods

 __construct ($resource)
 Validates and sets the underlying socket resource. More...
 
 __destruct ()
 Closes the socket if it's open. More...
 
$this await (int $channel)
 Blocks until the socket becomes available on a given channel. More...
 
$this awaitOutOfBand ()
 
$this awaitReadable ()
 
$this awaitWritable ()
 
$this close ()
 Closes the underlying resource if it's open. More...
 
int getDomain ()
 The AF_* address family constant. More...
 
int getId ()
 
mixed getOption (int $option)
 Retrieves an option value. More...
 
resource getResource ()
 
array getSockName ()
 The local address and port, or Unix file path and port 0. More...
 
bool isOpen ()
 
bool isOutOfBand ()
 Polls for whether the socket can perform a non-blocking out-of-band read. More...
 
bool isReadable ()
 Polls for whether the socket can perform a non-blocking read. More...
 
bool isReady (int $channel, ?float $timeout=0)
 Selects for channel availability. More...
 
bool isWritable ()
 Polls for whether the socket can perform a non-blocking write. More...
 
$this setBlocking (bool $blocking)
 Enables or disables blocking. More...
 
$this setOption (int $option, $value)
 Sets an option on the underlying resource. More...
 
$this setTimeout (float $timeout)
 Sets the I/O timeout length in seconds. More...
 
$this shutdown (int $channel)
 Shuts down I/O for a single channel. More...
 

Additional Inherited Members

- Public Properties inherited from SocketInterface
const CH_EXCEPT = 2
 Out-of-band channel. More...
 
const CH_READ = 0
 Read channel. More...
 
const CH_WRITE = 1
 Write channel. More...
 

Properties

◆ $resource

resource $resource
protected

The underlying PHP resource.

Constructor

◆ __construct()

__construct (   $resource)

Validates and sets the underlying socket resource.

The resource must be open, of the correct type, and have no pending errors.

Parameters
resource$resourcePHP socket resource.
Exceptions
InvalidArgumentExceptionNot a socket resource, or the socket is of the wrong type.
SocketErrorSlippage of an existing error on the resource.

◆ __destruct()

__destruct ( )

Closes the socket if it's open.

See also
close()

Methods

◆ await()

$this await ( int  $channel)

Blocks until the socket becomes available on a given channel.

Throws if the underlying resource has a pending error (non-blocking sockets only).

See also
isReady()
Parameters
int$channelSocketInterface channel constant.
Returns
$this
Exceptions
SocketError

◆ awaitOutOfBand()

$this awaitOutOfBand ( )
final
See also
await()
Returns
$this

◆ awaitReadable()

$this awaitReadable ( )
final
See also
await()
Returns
$this

◆ awaitWritable()

$this awaitWritable ( )
final
See also
await()
Returns
$this

◆ close()

$this close ( )

Closes the underlying resource if it's open.

See also
https://php.net/socket_close
Returns
$this

◆ create()

static self create ( int  $domain = AF_INET,
  $extra 
)
static

Creates an instance of the called class.

See also
https://php.net/socket_create
Parameters
int$domainAF_* constant.
array$extraVariadic constructor arguments.
Returns
self
Exceptions
SocketError

◆ getDomain()

int getDomain ( )
final

The AF_* address family constant.

Returns
int

◆ getId()

int getId ( )
final
Returns
int

Implements SocketInterface.

◆ getOption()

mixed getOption ( int  $option)

Retrieves an option value.

See also
https://php.net/socket_get_option
Parameters
int$optionSO_* option constant.
Returns
mixed The option's value. This is never false.
Exceptions
SocketError

◆ getResource()

resource getResource ( )
final
Returns
resource

Implements SocketInterface.

◆ getSockName()

array getSockName ( )

The local address and port, or Unix file path and port 0.

See also
https://php.net/socket_getsockname
Returns
array [ 0 => address, 1 => port ]
Exceptions
SocketError

◆ getType()

static int getType ( )
staticabstract

The SOCK_* type constant for the class.

Returns
int

Reimplemented in DatagramClient, DatagramServer, StreamClient, and StreamServer.

◆ isOpen()

bool isOpen ( )
Returns
bool

Implements SocketInterface.

◆ isOutOfBand()

bool isOutOfBand ( )
final

Polls for whether the socket can perform a non-blocking out-of-band read.

See also
isReady()
Returns
bool

◆ isReadable()

bool isReadable ( )
final

Polls for whether the socket can perform a non-blocking read.

See also
isReady()
Returns
bool

◆ isReady()

bool isReady ( int  $channel,
?float  $timeout = 0 
)

Selects for channel availability.

See also
await()
Parameters
int$channelSocketInterface channel constant.
float | null$timeoutMaximum seconds to block. NULL blocks forever. Defaults to a poll.
Returns
bool
Exceptions
SocketError

◆ isWritable()

bool isWritable ( )
final

Polls for whether the socket can perform a non-blocking write.

See also
isReady()
Returns
bool

◆ setBlocking()

$this setBlocking ( bool  $blocking)

Enables or disables blocking.

Note: PHP provides no way to retrieve a socket's blocking mode.

Sockets are always created in blocking mode.

If you're using a reactor, keep the sockets in blocking mode.

Non-blocking errors are thrown when performing ordinary operations, even if unrelated to those operations. This is known as error slippage. To test for and clear an existing non-blocking error, use ->getOption(SO_ERROR).

See also
https://php.net/socket_set_block
https://php.net/socket_set_nonblock
Parameters
bool$blockingWhether the socket should block.
Returns
$this
Exceptions
SocketError

◆ setOption()

$this setOption ( int  $option,
  $value 
)

Sets an option on the underlying resource.

See also
https://php.net/socket_set_option
Parameters
int$optionSO_* constant.
mixed$value
Returns
$this
Exceptions
SocketError

◆ setTimeout()

$this setTimeout ( float  $timeout)

Sets the I/O timeout length in seconds.

Parameters
float$timeoutZero means "no timeout" (block forever).
Returns
$this

◆ shutdown()

$this shutdown ( int  $channel)

Shuts down I/O for a single channel.

Shutting down is a formal handshake two peers can do before actually closing. It's not required, but it can help assert I/O access logic.

If writing is shut down, trying to send will throw SOCKET_EPIPE, and the remote peer will read an empty string after receiving all pending data.

If reading is shut down, trying to receive will return an empty string, and the remote peer will get an EPIPE error if they try to send.

Writing should be shut down first between two connected sockets.

Selection always returns positive for a shut down channel, and the remote peer will similarly have positive selection for the opposite channel.

See also
https://php.net/socket_shutdown
Parameters
int$channelCH_READ or CH_WRITE
Returns
$this
Exceptions
SocketError