This is automatic generated document for things in Swow\Socket class

Class Socket

Constants

public BIND_FLAG_IPV6ONLY = 1 public BIND_FLAG_NONE = 0 public BIND_FLAG_REUSEADDR = 2 public BIND_FLAG_REUSEPORT = 4 public DEFAULT_BACKLOG = 511 public INVALID_FD = -1 public IO_FLAG_ACCEPT = 11 public IO_FLAG_BIND = 7 public IO_FLAG_CONNECT = 19 public IO_FLAG_NONE = 0 public IO_FLAG_RDWR = 3 public IO_FLAG_READ = 1 public IO_FLAG_WRITE = 2 public TYPE_ANY = 0 public TYPE_FLAG_DGRAM = 2 public TYPE_FLAG_INET = 16 public TYPE_FLAG_IPC = 1024 public TYPE_FLAG_IPV4 = 32 public TYPE_FLAG_IPV6 = 64 public TYPE_FLAG_LOCAL = 128 public TYPE_FLAG_STDERR = 4096 public TYPE_FLAG_STDIN = 1024 public TYPE_FLAG_STDOUT = 2048 public TYPE_FLAG_STREAM = 1 public TYPE_IPCC = 33555585 public TYPE_PIPE = 33554561 public TYPE_STDERR = 67112961 public TYPE_STDIN = 67109889 public TYPE_STDOUT = 67110913 public TYPE_TCP = 16777233 public TYPE_TCP4 = 16777265 public TYPE_TCP6 = 16777297 public TYPE_TTY = 67108865 public TYPE_UDG = 268435586

UNIX datagram socket type, this constant is only avaliable at unix-like os.

public TYPE_UDP = 134217746 public TYPE_UDP4 = 134217778 public TYPE_UDP6 = 134217810 public TYPE_UNIX = 33554561

UNIX domain socket type, this constant is only available at unix-like os.

Methods

Static method getGlobalAcceptTimeout

public static function getGlobalAcceptTimeout ( ) : int

Static method getGlobalConnectTimeout

public static function getGlobalConnectTimeout ( ) : int

Static method getGlobalDnsTimeout

public static function getGlobalDnsTimeout ( ) : int

Static method getGlobalHandshakeTimeout

public static function getGlobalHandshakeTimeout ( ) : int

Static method getGlobalReadTimeout

public static function getGlobalReadTimeout ( ) : int

Static method getGlobalWriteTimeout

public static function getGlobalWriteTimeout ( ) : int

Static method setGlobalAcceptTimeout

public static function setGlobalAcceptTimeout ( int $timeout ) : void

Static method setGlobalConnectTimeout

public static function setGlobalConnectTimeout ( int $timeout ) : void

Static method setGlobalDnsTimeout

public static function setGlobalDnsTimeout ( int $timeout ) : void

Static method setGlobalHandshakeTimeout

public static function setGlobalHandshakeTimeout ( int $timeout ) : void

Static method setGlobalReadTimeout

public static function setGlobalReadTimeout ( int $timeout ) : void

Static method setGlobalTimeout

public static function setGlobalTimeout ( int $timeout ) : void

Static method setGlobalWriteTimeout

public static function setGlobalWriteTimeout ( int $timeout ) : void

Static method typeName

public static function typeName ( int $type ) : string

Static method typeSimplify

public static function typeSimplify ( int $type ) : int

Method __construct

public function __construct ( int $type )

Method __debugInfo

public function __debugInfo ( ) : array<string, mixed>

Returns array<string, mixed> debug information for var_dump

Method accept

public function accept ( int $timeout ) : static

Param int $timeout [optional] = $this->getAcceptTimeout()

Returns static Notice: it returns a new un-constructed connection instead of returning itself

Method acceptTo

public function acceptTo ( T $connection , int $timeout ) : T

Template T of static

Param int $timeout [optional] = $this->getAcceptTimeout()

Returns T Notice: it does not return the connection, but returns itself

Method bind

public function bind ( string $name , int $port , int $flags ) : static

Param int $flags self::BIND_FLAG_* constants combinations

Method checkLiveness

public function checkLiveness ( ) : static

Throws SocketException when the socket connection is broken

Method close

public function close ( ) : bool

Method connect

public function connect ( string $name , int $port , int $timeout ) : static

Param int $timeout [optional] = $this->getConnectTimeout()

Method enableCrypto

public function enableCrypto ( ?array $options ) : static

Method getAcceptTimeout

public function getAcceptTimeout ( ) : int

Method getConnectTimeout

public function getConnectTimeout ( ) : int

Method getConnectionError

public function getConnectionError ( ) : int

Returns int return Errno constants if the socket is broken, zero otherwise, it’s a silent version of {@see Socket::checkLiveness()}

Method getDnsTimeout

public function getDnsTimeout ( ) : int

Method getFd

public function getFd ( ) : int

Returns int real file descriptor number

Method getHandshakeTimeout

public function getHandshakeTimeout ( ) : int

Method getId

public function getId ( ) : int

Fixme may cause error on 32-bit env

Returns int 64-bit auto-incrementing ID, never repeat in the same process

Method getIoState

public function getIoState ( ) : int

Returns int self::IO_FLAG_* constants combinations

Method getIoStateName

public function getIoStateName ( ) : string

Method getIoStateNaming

public function getIoStateNaming ( ) : string

Method getPeerAddress

public function getPeerAddress ( ) : string

Method getPeerPort

public function getPeerPort ( ) : int

Method getReadTimeout

public function getReadTimeout ( ) : int

Method getRecvBufferSize

public function getRecvBufferSize ( ) : int

Method getSendBufferSize

public function getSendBufferSize ( ) : int

Method getSimpleType

public function getSimpleType ( ) : int

Method getSimpleTypeName

public function getSimpleTypeName ( ) : string

Method getSockAddress

public function getSockAddress ( ) : string

Method getSockPort

public function getSockPort ( ) : int

Method getType

public function getType ( ) : int

Method getTypeName

public function getTypeName ( ) : string

Method getWriteTimeout

public function getWriteTimeout ( ) : int

Method isAvailable

public function isAvailable ( ) : bool

Returns bool Whether the socket has been constructed and has not been closed

Method isClient

public function isClient ( ) : bool

Method isEstablished

public function isEstablished ( ) : bool

this method may return true even if the connection is broken, use {@see Socket::checkLiveness()} to check if the connection is broken

Returns bool Whether the socket has connected (including SSL handshake if it’s on SSL)

Method isOpen

public function isOpen ( ) : bool

Returns bool Whether the socket has been opened with exact type and file descriptor is valid

Method isServer

public function isServer ( ) : bool

Method isServerConnection

public function isServerConnection ( ) : bool

Method listen

public function listen ( int $backlog ) : static

Method open

public function open ( mixed $socket ) : static

Method peek

public function peek ( Buffer $buffer , int $offset , int $size , ?int $timeout ) : int

read at max $size bytes data into buffer from socket without removing the read data from socket, only works on some type of socket, returns immediately.

Param Buffer $buffer buffer to write in, data will be written from offset to offset + length Param int $offset offset is the start position to write in buffer Param int $size -1 meaning not limited, otherwise buffer size in bytes.

Returns int bytes received

Throws SocketException when socket read failed

Method peekFrom

public function peekFrom ( \Swow\Buffer $buffer , int $offset , int $size , & $address , (int<0, 65535> | null) & $port , ?int $timeout ) : int

read at max $size bytes data into buffer from socket without removing the read data from socket, only works on some type of socket, peer info is stored into $address and $port if applicable, returns immediately.

Returns int bytes received

Throws SocketException when socket read failed

Method peekString

public function peekString ( int $size , ?int $timeout ) : string

read at max $size bytes data from socket as string without removing the read data from socket, only works on some type of socket, returns immediately.

Param int $size -1 meaning not limited, otherwise buffer size in bytes.

Returns string data received in string

Throws SocketException when socket read failed

Method peekStringFrom

public function peekStringFrom ( int $size , & $address , (int<0, 65535> | null) & $port , ?int $timeout ) : string

read at max $size bytes data from socket as string without removing the read data from socket, only works on some type of socket, peer info is stored into $address and $port if applicable, returns immediately.

Returns string data received in string

Throws SocketException when socket read failed

Method read

public function read ( Buffer $buffer , int $offset , int $length , (int | null) $timeout ) : int

read $length bytes data into buffer from socket, keep reading until $length bytes data received.

context switching may happen here

Param Buffer $buffer buffer to write in, data will be written from offset to offset + length Param int $offset offset is the start position to write in buffer Param int $length -1 meaning not limited, receive until eof, otherwise length in bytes Param (int | null) $timeout timeout in microseconds or null for using {@see Socket::getReadTimeout()} value

Returns int bytes received

Throws SocketException when got eof without enough data received Throws SocketException when timed out Throws SocketException when socket read failed

Method readString

public function readString ( int $length , (int | null) $timeout ) : string

read $length bytes data from socket as string, keep reading until $length bytes data received.

context switching may happen here

Param int $length -1 meaning not limited, receive until eof, otherwise length in bytes Param (int | null) $timeout timeout in microseconds or null for using {@see Socket::getReadTimeout()} value

Returns string data received in string

Throws SocketException when got eof without enough data received Throws SocketException when timed out Throws SocketException when socket read failed

Method recv

public function recv ( Buffer $buffer , int $offset , int $size , (int | null) $timeout ) : int

read at max $size bytes data into buffer from socket, returns when any data received or eof.

context switching may happen here

Param Buffer $buffer buffer to write in, data will be written from offset to offset + length Param int $offset offset is the start position to write in buffer Param int $size -1 meaning not limited, otherwise buffer size in bytes. only $size bytes data will be read from socket if there are more data than $size bytes, extra data will be kept in socket for further reading options Param (int | null) $timeout timeout in microseconds or null for using {@see Socket::getReadTimeout()} value

Returns int bytes received

Throws SocketException when timed out Throws SocketException when socket read failed

Method recvData

public function recvData ( Buffer $buffer , int $offset , int $size , (int | null) $timeout ) : int

read at max $size bytes data into buffer from socket, return when any data received.

context switching may happen here

Param Buffer $buffer buffer to write in, data will be written from offset to offset + length Param int $offset offset is the start position to write in buffer Param int $size -1 meaning not limited, otherwise buffer size in bytes. only $size bytes data will be read from socket if there are more data than $size bytes, extra data will be kept in socket for further reading options Param (int | null) $timeout timeout in microseconds or null for using {@see Socket::getReadTimeout()} value

Throws SocketException when no data received Throws SocketException when timed out Throws SocketException when socket read failed

Method recvDataFrom

public function recvDataFrom ( \Swow\Buffer $buffer , int $offset , int $size , & $address , (int<0, 65535> | null) & $port , ?int $timeout ) : int

read at max $size bytes data into buffer from socket, peer info is stored into $address and $port if applicable, return when any data received.

context switching may happen here

Returns int bytes received

Throws SocketException when no data received Throws SocketException when timed out Throws SocketException when socket read failed

Method recvFrom

public function recvFrom ( \Swow\Buffer $buffer , int $offset , int $size , & $address , (int<0, 65535> | null) & $port , ?int $timeout ) : int

read at max $size bytes data into buffer from socket, peer info is stored into $address and $port if applicable, return when any data received or eof.

context switching may happen here

Returns int bytes received

Throws SocketException when timed out Throws SocketException when socket read failed

Method recvString

public function recvString ( int $size , (int | null) $timeout ) : string

read at max $size bytes data from socket as string, returns when any data received or eof.

context switching may happen here

Param int $size -1 meaning not limited, otherwise buffer size in bytes. only $size bytes data will be read from socket if there are more data than $size bytes, extra data will be kept in socket for further reading options Param (int | null) $timeout timeout in microseconds or null for using {@see Socket::getReadTimeout()} value

Returns string data received in string

Throws SocketException when timed out Throws SocketException when socket read failed

Method recvStringData

public function recvStringData ( int $size , (int | null) $timeout ) : string

read at max $size bytes data from socket as string, return when any data received.

context switching may happen here

Param int $size -1 meaning not limited, otherwise buffer size in bytes. only $size bytes data will be read from socket if there are more data than $size bytes, extra data will be kept in socket for further reading options Param (int | null) $timeout timeout in microseconds or null for using {@see Socket::getReadTimeout()} value

Returns string data received in string

Throws SocketException when no data received Throws SocketException when timed out Throws SocketException when socket read failed

Method recvStringDataFrom

public function recvStringDataFrom ( int $size , & $address , (int<0, 65535> | null) & $port , ?int $timeout ) : string

read at max $size bytes data from socket as string, peer info is stored into $address and $port if applicable, return when any data received.

context switching may happen here

Returns string data received in string

Throws SocketException when no data received Throws SocketException when timed out Throws SocketException when socket read failed

Method recvStringFrom

public function recvStringFrom ( int $size , & $address , (int<0, 65535> | null) & $port , ?int $timeout ) : string

read at max $size bytes data from socket as string, peer info is stored into $address and $port if applicable, return when any data received or eof.

context switching may happen here

Returns string data received in string

Throws SocketException when timed out Throws SocketException when socket read faile

Method send

public function send ( \Stringable|string $data , int<0, max> $start , int<-1, max> $length , ?int $timeout ) : static

write data into socket from $start of data with $length bytes

Throws SocketException when timed out Throws SocketException when write failed

Method sendFile

public function sendFile ( string $filename , int $offset , int $length , int $timeout ) : int

Send file without memory allocation or memory copy

Param int $offset [optional] = 0 Param int $timeout [optional] = $this->getWriteTimeout()

Method sendHandle

public function sendHandle ( self $handle , int $timeout ) : static

Send a socket handle to peer via pipe socket

Param int $timeout [optional] = $this->getWriteTimeout()

Method sendTo

public function sendTo ( \Stringable|string $data , int<0, max> $start , int<-1, max> $length , ?string $address , (int<0, 65535> | null) $port , ?int $timeout ) : static

write data to specified address (and port if protocol needs port) from $start of data with $length bytes

Throws SocketException when timed out Throws SocketException when write failed

Method setAcceptTimeout

public function setAcceptTimeout ( int $timeout ) : static

Method setConnectTimeout

public function setConnectTimeout ( int $timeout ) : static

Method setDnsTimeout

public function setDnsTimeout ( int $timeout ) : static

Method setHandshakeTimeout

public function setHandshakeTimeout ( int $timeout ) : static

Method setReadTimeout

public function setReadTimeout ( int $timeout ) : static

Method setRecvBufferSize

public function setRecvBufferSize ( int $size ) : static

Method setSendBufferSize

public function setSendBufferSize ( int $size ) : static

Method setTcpKeepAlive

public function setTcpKeepAlive ( bool $enable , int $delay ) : static

Method setTcpNodelay

public function setTcpNodelay ( bool $enable ) : static

Method setTimeout

public function setTimeout ( int $timeout ) : static

Method setWriteTimeout

public function setWriteTimeout ( int $timeout ) : static

Method write

public function write ( non-empty-array<(string | \Stringable | Buffer | array{0: (string | \Stringable | Buffer), 1?: int, 2?: int} | null)> $vector , ?int $timeout ) : static

write io vector to socket

vector is an array for all contents to write, elements in it maybe

  • Buffer: write full text from start $start
  • string|Stringable: write full text from start $start
  • array[Buffer $content, int $start, int $length]: write it from start $start, with length $length
  • array[string|Stringable $content, int $start, int $offset, ?int $length]: write the string from start $start, with length $length, and offset $offset in the string

for code

$s = new Socket(Socket::TYPE_STDOUT);
$s->write([
    (new Buffer(0))->write('1234567890', start: 6),
    'abc',
    ['1234567890', 1, 2],
    [(new Buffer(0))->write('1234567890', start: 4), 2],
]);

string "7890abc2356" will be written to the socket.

context switching may happen here

Throws SocketException when timed out Throws SocketException when write failed

Method writeTo

public function writeTo ( non-empty-array<(string | \Stringable | Buffer | array{0: (string | \Stringable | Buffer), 1?: int, 2?: int} | null)> $vector , ?string $address , (int<0, 65535> | null) $port , ?int $timeout ) : static

write io vector to socket with peer information

See Socket::write() for io vector format

context switching may happen here

Throws SocketException when timed out Throws SocketException when write failed


Copyright 2022 the Swow contributors