Socket
in package
uses
InstanceConfigTrait
CakePHP network socket connection class.
Core base class for network communication.
Table of Contents
Properties
- $_config : array<string, mixed>
- Runtime config
- $_configInitialized : bool
- Whether the config property has already been configured with defaults
- $_connectionErrors : array<string|int, string>
- Used to capture connection warnings which can happen when there are SSL errors for example.
- $_defaultConfig : array<string, mixed>
- Default configuration settings for the socket connection
- $_encryptMethods : array<string, int>
- Contains all the encryption methods available
- $connected : bool
- This boolean contains the current state of the Socket class
- $connection : resource|null
- Reference to socket connection resource
- $encrypted : bool
- True if the socket stream is encrypted after a {@link \Cake\Network\Socket::enableCrypto()} call
- $lastError : array<string, mixed>
- This variable contains an array with the last error number (num) and string (str)
Methods
- __construct() : mixed
- Constructor.
- __destruct() : mixed
- Destructor, used to disconnect from current connection.
- __get() : mixed
- Temporary magic method to allow accessing protected properties.
- address() : string
- Get the IP address of the current connection.
- addresses() : array<string|int, mixed>
- Get all IP addresses associated with the current connection.
- configShallow() : $this
- Merge provided config with existing config. Unlike `config()` which does a recursive merge for nested keys, this method does a simple merge.
- connect() : bool
- Connect the socket to the given host and port.
- context() : array<string|int, mixed>|null
- Get the connection context.
- disconnect() : bool
- Disconnect the socket from the current connection.
- enableCrypto() : void
- Encrypts current stream socket, using one of the defined encryption methods
- getConfig() : mixed
- Returns the config.
- getConfigOrFail() : mixed
- Returns the config for this specific key.
- host() : string
- Get the host name of the current connection.
- isConnected() : bool
- Check the connection status after calling `connect()`.
- isEncrypted() : bool
- Check the encryption status after calling `enableCrypto()`.
- lastError() : string|null
- Get the last error as a string.
- read() : string|null
- Read data from the socket. Returns null if no data is available or no connection could be established.
- reset() : void
- Resets the state of this Socket instance to it's initial state (before Object::__construct got executed)
- setConfig() : $this
- Sets the config.
- setLastError() : void
- Set the last error.
- write() : int
- Write data to the socket.
- _configDelete() : void
- Deletes a single config key.
- _configRead() : mixed
- Reads a config key.
- _configWrite() : void
- Writes a config key.
- _connectionErrorHandler() : void
- socket_stream_client() does not populate errNum, or $errStr when there are connection errors, as in the case of SSL verification failure.
- _getStreamSocketClient() : resource|null
- Create a stream socket client. Mock utility.
- _setSslContext() : void
- Configure the SSL context options.
Properties
$_config
Runtime config
protected
array<string, mixed>
$_config
= []
$_configInitialized
Whether the config property has already been configured with defaults
protected
bool
$_configInitialized
= false
$_connectionErrors
Used to capture connection warnings which can happen when there are SSL errors for example.
protected
array<string|int, string>
$_connectionErrors
= []
$_defaultConfig
Default configuration settings for the socket connection
protected
array<string, mixed>
$_defaultConfig
= ['persistent' => false, 'host' => 'localhost', 'protocol' => 'tcp', 'port' => 80, 'timeout' => 30]
$_encryptMethods
Contains all the encryption methods available
protected
array<string, int>
$_encryptMethods
= ['sslv23_client' => STREAM_CRYPTO_METHOD_SSLv23_CLIENT, 'tls_client' => STREAM_CRYPTO_METHOD_TLS_CLIENT, 'tlsv10_client' => STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT, 'tlsv11_client' => STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT, 'tlsv12_client' => STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT, 'sslv23_server' => STREAM_CRYPTO_METHOD_SSLv23_SERVER, 'tls_server' => STREAM_CRYPTO_METHOD_TLS_SERVER, 'tlsv10_server' => STREAM_CRYPTO_METHOD_TLSv1_0_SERVER, 'tlsv11_server' => STREAM_CRYPTO_METHOD_TLSv1_1_SERVER, 'tlsv12_server' => STREAM_CRYPTO_METHOD_TLSv1_2_SERVER]
$connected
This boolean contains the current state of the Socket class
protected
bool
$connected
= false
$connection
Reference to socket connection resource
protected
resource|null
$connection
$encrypted
True if the socket stream is encrypted after a {@link \Cake\Network\Socket::enableCrypto()} call
protected
bool
$encrypted
= false
$lastError
This variable contains an array with the last error number (num) and string (str)
protected
array<string, mixed>
$lastError
= []
Methods
__construct()
Constructor.
public
__construct([array<string, mixed> $config = [] ]) : mixed
Parameters
- $config : array<string, mixed> = []
-
Socket configuration, which will be merged with the base configuration
Tags
__destruct()
Destructor, used to disconnect from current connection.
public
__destruct() : mixed
__get()
Temporary magic method to allow accessing protected properties.
public
__get(string $name) : mixed
Will be removed in 5.0.
Parameters
- $name : string
-
Property name.
address()
Get the IP address of the current connection.
public
address() : string
Return values
string —IP address
addresses()
Get all IP addresses associated with the current connection.
public
addresses() : array<string|int, mixed>
Return values
array<string|int, mixed> —IP addresses
configShallow()
Merge provided config with existing config. Unlike `config()` which does a recursive merge for nested keys, this method does a simple merge.
public
configShallow(array<string, mixed>|string $key[, mixed|null $value = null ]) : $this
Setting a specific value:
$this->configShallow('key', $value);
Setting a nested value:
$this->configShallow('some.nested.key', $value);
Updating multiple config settings at the same time:
$this->configShallow(['one' => 'value', 'another' => 'value']);
Parameters
- $key : array<string, mixed>|string
-
The key to set, or a complete array of configs.
- $value : mixed|null = null
-
The value to set.
Return values
$thisconnect()
Connect the socket to the given host and port.
public
connect() : bool
Tags
Return values
bool —Success
context()
Get the connection context.
public
context() : array<string|int, mixed>|null
Return values
array<string|int, mixed>|null —Null when there is no connection, an array when there is.
disconnect()
Disconnect the socket from the current connection.
public
disconnect() : bool
Return values
bool —Success
enableCrypto()
Encrypts current stream socket, using one of the defined encryption methods
public
enableCrypto(string $type[, string $clientOrServer = 'client' ][, bool $enable = true ]) : void
Parameters
- $type : string
-
can be one of 'ssl2', 'ssl3', 'ssl23' or 'tls'
- $clientOrServer : string = 'client'
-
can be one of 'client', 'server'. Default is 'client'
- $enable : bool = true
-
enable or disable encryption. Default is true (enable)
Tags
getConfig()
Returns the config.
public
getConfig([string|null $key = null ][, mixed $default = null ]) : mixed
Usage
Reading the whole config:
$this->getConfig();
Reading a specific value:
$this->getConfig('key');
Reading a nested value:
$this->getConfig('some.nested.key');
Reading with default value:
$this->getConfig('some-key', 'default-value');
Parameters
- $key : string|null = null
-
The key to get or null for the whole config.
- $default : mixed = null
-
The return value when the key does not exist.
Return values
mixed —Configuration data at the named key or null if the key does not exist.
getConfigOrFail()
Returns the config for this specific key.
public
getConfigOrFail(string $key) : mixed
The config value for this key must exist, it can never be null.
Parameters
- $key : string
-
The key to get.
Tags
Return values
mixed —Configuration data at the named key
host()
Get the host name of the current connection.
public
host() : string
Return values
string —Host name
isConnected()
Check the connection status after calling `connect()`.
public
isConnected() : bool
Return values
boolisEncrypted()
Check the encryption status after calling `enableCrypto()`.
public
isEncrypted() : bool
Return values
boollastError()
Get the last error as a string.
public
lastError() : string|null
Return values
string|null —Last error
read()
Read data from the socket. Returns null if no data is available or no connection could be established.
public
read([int $length = 1024 ]) : string|null
Parameters
- $length : int = 1024
-
Optional buffer length to read; defaults to 1024
Return values
string|null —Socket data
reset()
Resets the state of this Socket instance to it's initial state (before Object::__construct got executed)
public
reset([array<string|int, mixed>|null $state = null ]) : void
Parameters
- $state : array<string|int, mixed>|null = null
-
Array with key and values to reset
setConfig()
Sets the config.
public
setConfig(array<string, mixed>|string $key[, mixed|null $value = null ][, bool $merge = true ]) : $this
Usage
Setting a specific value:
$this->setConfig('key', $value);
Setting a nested value:
$this->setConfig('some.nested.key', $value);
Updating multiple config settings at the same time:
$this->setConfig(['one' => 'value', 'another' => 'value']);
Parameters
- $key : array<string, mixed>|string
-
The key to set, or a complete array of configs.
- $value : mixed|null = null
-
The value to set.
- $merge : bool = true
-
Whether to recursively merge or overwrite existing config, defaults to true.
Tags
Return values
$thissetLastError()
Set the last error.
public
setLastError(int|null $errNum, string $errStr) : void
Parameters
- $errNum : int|null
-
Error code
- $errStr : string
-
Error string
write()
Write data to the socket.
public
write(string $data) : int
Parameters
- $data : string
-
The data to write to the socket.
Return values
int —Bytes written.
_configDelete()
Deletes a single config key.
protected
_configDelete(string $key) : void
Parameters
- $key : string
-
Key to delete.
Tags
_configRead()
Reads a config key.
protected
_configRead(string|null $key) : mixed
Parameters
- $key : string|null
-
Key to read.
_configWrite()
Writes a config key.
protected
_configWrite(array<string, mixed>|string $key, mixed $value[, string|bool $merge = false ]) : void
Parameters
- $key : array<string, mixed>|string
-
Key to write to.
- $value : mixed
-
Value to write.
- $merge : string|bool = false
-
True to merge recursively, 'shallow' for simple merge, false to overwrite, defaults to false.
Tags
_connectionErrorHandler()
socket_stream_client() does not populate errNum, or $errStr when there are connection errors, as in the case of SSL verification failure.
protected
_connectionErrorHandler(int $code, string $message) : void
Instead we need to handle those errors manually.
Parameters
- $code : int
-
Code number.
- $message : string
-
Message.
_getStreamSocketClient()
Create a stream socket client. Mock utility.
protected
_getStreamSocketClient(string $remoteSocketTarget, int &$errNum, string &$errStr, int $timeout, int $connectAs, resource $context) : resource|null
Parameters
- $remoteSocketTarget : string
-
remote socket
- $errNum : int
-
error number
- $errStr : string
-
error string
- $timeout : int
-
timeout
- $connectAs : int
-
flags
- $context : resource
-
context
Return values
resource|null_setSslContext()
Configure the SSL context options.
protected
_setSslContext(string $host) : void
Parameters
- $host : string
-
The host name being connected to.