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:

$this->sock

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

$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
$this->sock[''] = $data;

// read a line of data
$line = $this->sock[''];

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

3.2General methods

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

3.3Line-by-line data management

// write a ligne of data
// a CRLF sequence ("\r\n") is added at the end of the line
$this->sock->set('', $data);

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

// read one line of data
$line = $this->sock->get('');
// read one line of data in non-blocking mode
$line = $this->sock->get('', null, false);
// read one line of data with a 3 seconds timeout
$line = $this->sock->get('', null, false);
// read one line of data in non-blocking mode, with a 2 seconds timeout
$line = $this->sock->get('', null, [
    'blocking' => false,
    'timeout'  => 2,
]);

3.4Raw data management

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

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

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

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

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