Data source: Socket


1Presentation

This data source can be used to connect to a server (local or remote) using a socket (network or Unix), TCP/IP, UDP/IP or TCP/IP+SSL.

It can be used to connect to HTTP(S), (x)inetd, etc. servers.

If you have correctly configured the socket connection parameters, Temma automatically creates an object of type \Temma\Datasources\Socket. By convention, we'll assume that you've named this connection sock in the etc/temma.php file (see configuration documentation).

The connection is then available in the controller by writing:

$socket = $this->sock;

In other objects managed by the dependency injection component, the socket is accessible by writing:

$socket = $loader->dataSources->sock;
$socket = $loader->dataSources['sock']

2Configuration

In the etc/temma.php file (see configuration documentation), you declare the DSN (Data Source Name) used to open a socket.

The DSN used to create the socket is written as: PROTOCOL://SERVER:PORT[#TIMEOUT]
In the case of a Unix socket, the DSN is written as follows: unix:///path/to/file.sock

  • PROTOCOL: is the protocol to be used.
    • tcp: to open a TCP socket.
    • udp: to open a UDP socket.
    • ssl: to open a TCP+SSL socket. The object will attempt to negotiate an SSLv2 or SSLv3 connection, depending on the server.
    • sslv2: to open a TCP+SSLv2 socket.
    • sslv3: to open a TCP+SSLv3 socket.
    • tls: to open a TCP+TLS socket.
    • unix: to open a Unix socket.
  • SERVER: is the name of the server to connect to, or its IP address. IPv6 addresses must be enclosed in square brackets.
  • PORT: is the connection port number.
  • TIMEOUT: (optional) is the timeout delay for the connection, in seconds. This parameter only applies to network sockets, not Unix sockets.

Examples:

  • udp://localhost:7
  • tcp://www.google.com:80
  • ssl://www.google.com:443#3
  • tls://[fe80::1]:443
  • unix:///var/run/myapp/myapp.sock

3Unified calls

3.1Array-like access

// send a line of data in the socket
// if the data is not a string, it is serialized to JSON
// if it is a string, “\r” and “\n” are removed at the end
// a CRLF sequence (“\r\n”) is added at the end of the line
$socket[''] = $data;

// read a line of data
// the characters “\r” and “\n” are removed at the end of the string
$line = $socket[''];

// close the connexion
// (the socket is automatically reopened when a new writing is made)
unset($socket['']);

3.2General methods

// close the connexion
// (the socket is automatically reopened when a new writing is made)
$socket->remove('');

3.3Line-by-line data management

// write a ligne of data
// if the data is not a string, it is serialized to JSON
// if it is a string, “\r” and “\n” are removed at the end
// a CRLF sequence ("\r\n") is added at the end of the line
$socket->set('', $data);

// multi-line writing
// a CRLF sequence ("\r\n") is added at the end of each line
$socket->mSet([
    'GET / HTTP/1.1',
    'Host: www.myhost.com',
    'Connection: Close',
    '',
]);

// read one line of data
// the characters “\r” and “\n” are removed at the end of the string
$line = $socket->get('');
// read one line of data in non-blocking mode
$line = $socket->get('', null, false);
// read one line of data with a 3 seconds timeout
$line = $socket->get('', null, false);
// read one line of data in non-blocking mode, with a 2 seconds timeout
$line = $socket->get('', null, [
    'blocking' => false,
    'timeout'  => 2,
]);

3.4Raw data management

// read (raw) data
$html = $socket->read('');
// read (raw) data in non-blocking mode
$html = $socket->read('', null, false);
// read (raw) data with a 3 seconds timeout
$html = $socket->read('', null, 3);
// read (raw) data in non-blocking mode, with a 2 seconds timeout
$html = $socket->read('', null, [
    'blocking' => false,
    'timeout'  => 2,
]);

// write (raw) data
$socket->write('', $msgData);

// write multiple (raw) data
$socket->mWrite({
    $user1data,
    $user2data,
    $user3data,
]);

// write (raw) data from a local file
$socket->copyTo('', '/path/to/file');

// write multiple (raw) data from local files
$socket->mCopyTo([
    '/path/to/file1',
    '/path/to/file2',
    '/path/to/file3',
]);