Class CakeSocket

CakePHP network socket connection class.

Core base class for network communication.

Direct Subclasses

Package: Cake\Network
Copyright: Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
License: MIT License
Location: Cake/Network/CakeSocket.php

Properties summary

  • $_baseConfig protected
    array
    Base configuration settings for the socket connection
  • $_connectionErrors protected
    array

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

  • $_encryptMethods protected
    array
    Contains all the encryption methods available
  • $config public
    array
    Configuration settings for the socket connection
  • $connected public
    boolean
    This boolean contains the current state of the CakeSocket class
  • $connection public
    resource
    Reference to socket connection resource
  • $description public
    string
    CakeSocket description
  • $encrypted public
    boolean
    True if the socket stream is encrypted after a CakeSocket::enableCrypto() call
  • $lastError public
    array
    This variable contains an array with the last error number (num) and string (str)

Method Summary

  • __construct() public
    Constructor.
  • __destruct() public
    Destructor, used to disconnect from current connection.
  • _addTlsVersions() protected
    Add TLS versions that are dependent on specific PHP versions.
  • socket_stream_client() does not populate errNum, or $errStr when there are connection errors, as in the case of SSL verification failure.

  • _setSslContext() protected
    Configure the SSL context options.
  • address() public
    Gets the IP address of the current connection.
  • addresses() public
    Gets all IP addresses associated with the current connection.
  • connect() public
    Connects the socket to the given host and port.
  • context() public
    Gets the connection context.
  • disconnect() public
    Disconnects the socket from the current connection.
  • Encrypts current stream socket, using one of the defined encryption methods.
  • host() public
    Gets the host name of the current connection.
  • lastError() public
    Gets the last error as a string.
  • read() public

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

  • reset() public
    Resets the state of this Socket instance to it's initial state (before CakeObject::__construct got executed)
  • Sets the last error.
  • write() public
    Writes data to the socket.

Method Detail

__construct()source public

__construct( array $config array() )

Constructor.

Parameters

array $config optional array()
Socket configuration, which will be merged with the base configuration

See

CakeSocket::$_baseConfig

__destruct()source public

__destruct( )

Destructor, used to disconnect from current connection.

_addTlsVersions()source protected

_addTlsVersions( )

Add TLS versions that are dependent on specific PHP versions.

These TLS versions are not supported by older PHP versions, so we have to conditionally set them if they are supported.

As of PHP5.6.6, STREAM_CRYPTO_METHOD_TLS_CLIENT does not include TLS1.1 or 1.2. If we have TLS1.2 support we need to update the method map.

See

https://bugs.php.net/bug.php?id=69195
https://github.com/php/php-src/commit/10bc5fd4c4c8e1dd57bd911b086e9872a56300a0

_connectionErrorHandler()source protected

_connectionErrorHandler( integer $code , string $message )

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.
string $message
Message.

_setSslContext()source protected

_setSslContext( string $host )

Configure the SSL context options.

Parameters

string $host
The host name being connected to.

address()source public

address( )

Gets the IP address of the current connection.

Returns

string
IP address

addresses()source public

addresses( )

Gets all IP addresses associated with the current connection.

Returns

array
IP addresses

connect()source public

connect( )

Connects the socket to the given host and port.

Returns

boolean
Success

Throws

SocketException

context()source public

context( )

Gets the connection context.

Returns

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

disconnect()source public

disconnect( )

Disconnects the socket from the current connection.

Returns

boolean
Success

enableCrypto()source public

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

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

Parameters

string $type
Type which can be one of 'sslv2', 'sslv3', 'sslv23', 'tls', 'tlsv1_1' or 'tlsv1_2'.
string $clientOrServer optional 'client'
Can be one of 'client', 'server'. Default is 'client'.
boolean $enable optional true
Enable or disable encryption. Default is true (enable)

Returns

boolean
True on success

Throws

InvalidArgumentException
When an invalid encryption scheme is chosen.
SocketException
When attempting to enable SSL/TLS fails.

See

stream_socket_enable_crypto

host()source public

host( )

Gets the host name of the current connection.

Returns

string
Host name

lastError()source public

lastError( )

Gets the last error as a string.

Returns

string|null
Last error

read()source public

read( integer $length 1024 )

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

Parameters

integer $length optional 1024
Optional buffer length to read; defaults to 1024

Returns

mixed
Socket data

reset()source public

reset( array $state null )

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

Parameters

array $state optional null
Array with key and values to reset

Returns

boolean
True on success

setLastError()source public

setLastError( integer $errNum , string $errStr )

Sets the last error.

Parameters

integer $errNum
Error code
string $errStr
Error string

write()source public

write( string $data )

Writes data to the socket.

Parameters

string $data
The data to write to the socket

Returns

boolean
Success

Properties detail

$_baseConfigsource

protected array

Base configuration settings for the socket connection

array(
    'persistent' => false,
    'host' => 'localhost',
    'protocol' => 'tcp',
    'port' => 80,
    'timeout' => 30,
    'cryptoType' => 'tls',
)

$_connectionErrorssource

protected array

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

array()

$_encryptMethodssource

protected array

Contains all the encryption methods available

array(
    // @codingStandardsIgnoreStart
    'sslv2_client' => STREAM_CRYPTO_METHOD_SSLv2_CLIENT,
    'sslv3_client' => STREAM_CRYPTO_METHOD_SSLv3_CLIENT,
    'sslv23_client' => STREAM_CRYPTO_METHOD_SSLv23_CLIENT,
    'tls_client' => STREAM_CRYPTO_METHOD_TLS_CLIENT,
    'sslv2_server' => STREAM_CRYPTO_METHOD_SSLv2_SERVER,
    'sslv3_server' => STREAM_CRYPTO_METHOD_SSLv3_SERVER,
    'sslv23_server' => STREAM_CRYPTO_METHOD_SSLv23_SERVER,
    'tls_server' => STREAM_CRYPTO_METHOD_TLS_SERVER,
    // @codingStandardsIgnoreEnd
)

$configsource

public array

Configuration settings for the socket connection

array()

$connectedsource

public boolean

This boolean contains the current state of the CakeSocket class

false

$connectionsource

public resource

Reference to socket connection resource

null

$descriptionsource

public string

CakeSocket description

'Remote DataSource Network Socket Interface'

$encryptedsource

public boolean

True if the socket stream is encrypted after a CakeSocket::enableCrypto() call

false

$lastErrorsource

public array

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

array()

© 2005–2017 The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
https://api.cakephp.org/2.10/class-CakeSocket.html