/usr/share/php/PhpAmqpLib/Wire/IO/SocketIO.php is in php-amqplib 2.4.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | <?php
namespace PhpAmqpLib\Wire\IO;
use PhpAmqpLib\Exception\AMQPIOException;
use PhpAmqpLib\Exception\AMQPRuntimeException;
class SocketIO extends AbstractIO
{
/**
* @var string
*/
protected $host;
/**
* @var int
*/
protected $port;
/**
* @var int
*/
protected $timeout;
/**
* @var resource
*/
private $sock;
public function __construct($host, $port, $timeout, $keepalive = false)
{
$this->host = $host;
$this->port = $port;
$this->timeout = $timeout;
$this->keepalive = $keepalive;
}
/**
* Setup the socket connection
*
* @throws \Exception
*/
public function connect()
{
$this->sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($this->sock, SOL_SOCKET, SO_RCVTIMEO, array('sec' => $this->timeout, 'usec' => 0));
socket_set_option($this->sock, SOL_SOCKET, SO_SNDTIMEO, array('sec' => $this->timeout, 'usec' => 0));
if (!socket_connect($this->sock, $this->host, $this->port)) {
$errno = socket_last_error($this->sock);
$errstr = socket_strerror($errno);
throw new AMQPIOException("Error Connecting to server($errno): $errstr ", $errno);
}
socket_set_block($this->sock);
socket_set_option($this->sock, SOL_TCP, TCP_NODELAY, 1);
if ($this->keepalive) {
$this->enable_keepalive();
}
}
/**
* Reconnect the socket
*/
public function reconnect()
{
$this->close();
$this->connect();
}
public function read($n)
{
$res = '';
$read = 0;
$buf = socket_read($this->sock, $n);
while ($read < $n && $buf !== '' && $buf !== false) {
// Null sockets are invalid, throw exception
if (is_null($this->sock)) {
throw new AMQPRuntimeException("Socket was null! Last SocketError was: "
. socket_strerror(socket_last_error()));
}
$read += mb_strlen($buf, 'ASCII');
$res .= $buf;
$buf = socket_read($this->sock, $n - $read);
}
if (mb_strlen($res, 'ASCII') != $n) {
throw new AMQPIOException("Error reading data. Received " .
mb_strlen($res, 'ASCII') . " instead of expected $n bytes");
}
return $res;
}
public function write($data)
{
$len = mb_strlen($data, 'ASCII');
while (true) {
// Null sockets are invalid, throw exception
if (is_null($this->sock)) {
throw new AMQPRuntimeException("Socket was null! Last SocketError was: "
. socket_strerror(socket_last_error()));
}
$sent = socket_write($this->sock, $data, $len);
if ($sent === false) {
throw new AMQPIOException ("Error sending data. Last SocketError: "
. socket_strerror(socket_last_error()));
}
// Check if the entire message has been sent
if ($sent < $len) {
// If not sent the entire message.
// Get the part of the message that has not yet been sent as message
$data = mb_substr($data, $sent, mb_strlen($data, 'ASCII') - $sent, 'ASCII');
// Get the length of the not sent part
$len -= $sent;
} else {
break;
}
}
}
public function close()
{
if (is_resource($this->sock)) {
socket_close($this->sock);
}
$this->sock = null;
}
public function select($sec, $usec)
{
$read = array($this->sock);
$write = null;
$except = null;
return socket_select($read, $write, $except, $sec, $usec);
}
protected function enable_keepalive()
{
if (!defined('SOL_SOCKET') || !defined('SO_KEEPALIVE')) {
throw new AMQPIOException("Can not enable keepalive: SOL_SOCKET or SO_KEEPALIVE is not defined");
}
socket_set_option($this->sock, SOL_SOCKET, SO_KEEPALIVE, 1);
}
}
|