Properties

$description

$description : string

Object description

Type

string

$connection

$connection : resource|null

Reference to socket connection resource

Type

resource|null

$connected

$connected : boolean

This boolean contains the current state of the Socket class

Type

boolean

$lastError

$lastError : array

This variable contains an array with the last error number (num) and string (str)

Type

array

$encrypted

$encrypted : boolean

True if the socket stream is encrypted after a Cake\Network\Socket::enableCrypto() call

Type

boolean

$_config

$_config : array

Runtime config

Type

array

$_configInitialized

$_configInitialized : boolean

Whether the config property has already been configured with defaults

Type

boolean

$_defaultConfig

$_defaultConfig : array

Default configuration settings for the socket connection

Type

array

$_encryptMethods

$_encryptMethods : array

Contains all the encryption methods available

SSLv2 and SSLv3 are deprecated, and should not be used as they have several published vulnerablilities.

Type

array

$_connectionErrors

$_connectionErrors : array

Used to capture connection warnings which can happen when there are SSL errors for example.

Type

array

Methods

setConfig()

setConfig(string|array  $key, mixed|null  $value = null, boolean  $merge = true) : $this

Sets the config.

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

string|array $key

The key to set, or a complete array of configs.

mixed|null $value

The value to set.

boolean $merge

Whether to recursively merge or overwrite existing config, defaults to true.

Throws

\Cake\Core\Exception\Exception

When trying to set a key that is invalid.

Returns

$this

getConfig()

getConfig(string|null  $key = null, mixed  $default = null) : mixed

Returns the config.

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

string|null $key

The key to get or null for the whole config.

mixed $default

The return value when the key does not exist.

Returns

mixed —

Config value being read.

config()

config(string|array|null  $key = null, mixed|null  $value = null, boolean  $merge = true) : mixed

Gets/Sets the config.

Usage

Reading the whole config:

$this->config();

Reading a specific value:

$this->config('key');

Reading a nested value:

$this->config('some.nested.key');

Setting a specific value:

$this->config('key', $value);

Setting a nested value:

$this->config('some.nested.key', $value);

Updating multiple config settings at the same time:

$this->config(['one' => 'value', 'another' => 'value']);

Parameters

string|array|null $key

The key to get/set, or a complete array of configs.

mixed|null $value

The value to set.

boolean $merge

Whether to recursively merge or overwrite existing config, defaults to true.

Throws

\Cake\Core\Exception\Exception

When trying to set a key that is invalid.

Returns

mixed —

Config value being read, or the object itself on write operations.

configShallow()

configShallow(string|array  $key, mixed|null  $value = null) : $this

Merge provided config with existing config. Unlike `config()` which does a recursive merge for nested keys, this method does a simple merge.

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

string|array $key

The key to set, or a complete array of configs.

mixed|null $value

The value to set.

Returns

$this

__construct()

__construct(array  $config = array()) 

Constructor.

Parameters

array $config

Socket configuration, which will be merged with the base configuration

connect()

connect() : boolean

Connect the socket to the given host and port.

Throws

\Cake\Network\Exception\SocketException

Returns

boolean —

Success

context()

context() : null|array

Get the connection context.

Returns

null|array —

Null when there is no connection, an array when there is.

host()

host() : string

Get the host name of the current connection.

Returns

string —

Host name

address()

address() : string

Get the IP address of the current connection.

Returns

string —

IP address

addresses()

addresses() : array

Get all IP addresses associated with the current connection.

Returns

array —

IP addresses

lastError()

lastError() : string|null

Get the last error as a string.

Returns

string|null —

Last error

setLastError()

setLastError(integer  $errNum, string  $errStr) : void

Set the last error.

Parameters

integer $errNum

Error code

string $errStr

Error string

write()

write(string  $data) : integer|false

Write data to the socket.

The bool false return value is deprecated and will be int 0 in the next major. Please code respectively to be future proof.

Parameters

string $data

The data to write to the socket.

Returns

integer|false —

Bytes written.

read()

read(integer  $length = 1024) : mixed

Read data from the socket. Returns false if no data is available or no connection could be established.

The bool false return value is deprecated and will be null in the next major. Please code respectively to be future proof.

Parameters

integer $length

Optional buffer length to read; defaults to 1024

Returns

mixed —

Socket data

disconnect()

disconnect() : boolean

Disconnect the socket from the current connection.

Returns

boolean —

Success

__destruct()

__destruct() 

Destructor, used to disconnect from current connection.

reset()

reset(array|null  $state = null) : boolean

Resets the state of this Socket instance to it's initial state (before Object::__construct got executed)

Parameters

array|null $state

Array with key and values to reset

Returns

boolean —

True on success

enableCrypto()

enableCrypto(string  $type, string  $clientOrServer = 'client', boolean  $enable = true) : boolean

Encrypts current stream socket, using one of the defined encryption methods

Parameters

string $type

can be one of 'ssl2', 'ssl3', 'ssl23' or 'tls'

string $clientOrServer

can be one of 'client', 'server'. Default is 'client'

boolean $enable

enable or disable encryption. Default is true (enable)

Throws

\InvalidArgumentException

When an invalid encryption scheme is chosen.

\Cake\Network\Exception\SocketException

When attempting to enable SSL/TLS fails

Returns

boolean —

True on success

_configRead()

_configRead(string|null  $key) : mixed

Reads a config key.

Parameters

string|null $key

Key to read.

Returns

mixed

_configWrite()

_configWrite(string|array  $key, mixed  $value, boolean|string  $merge = false) : void

Writes a config key.

Parameters

string|array $key

Key to write to.

mixed $value

Value to write.

boolean|string $merge

True to merge recursively, 'shallow' for simple merge, false to overwrite, defaults to false.

Throws

\Cake\Core\Exception\Exception

if attempting to clobber existing config

_configDelete()

_configDelete(string  $key) : void

Deletes a single config key.

Parameters

string $key

Key to delete.

Throws

\Cake\Core\Exception\Exception

if attempting to clobber existing config

_setSslContext()

_setSslContext(string  $host) : void

Configure the SSL context options.

Parameters

string $host

The host name being connected to.

_connectionErrorHandler()

_connectionErrorHandler(integer  $code, string  $message) : void

socket_stream_client() does not populate errNum, or $errStr when there are connection errors, as in the case of SSL verification failure.

Instead we need to handle those errors manually.

Parameters

integer $code

Code number.

string $message

Message.