Exemple #1
0
 /**
  * Sets up the socket connection
  *
  * @throws \Exception
  */
 public function connect()
 {
     $this->sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
     list($sec, $uSec) = MiscHelper::splitSecondsMicroseconds($this->timeout);
     socket_set_option($this->sock, SOL_SOCKET, SO_RCVTIMEO, array('sec' => $sec, 'usec' => $uSec));
     socket_set_option($this->sock, SOL_SOCKET, SO_SNDTIMEO, array('sec' => $sec, 'usec' => $uSec));
     if (!socket_connect($this->sock, $this->host, $this->port)) {
         $errno = socket_last_error($this->sock);
         $errstr = socket_strerror($errno);
         throw new AMQPIOException(sprintf('Error Connecting to server (%s): %s', $errno, $errstr), $errno);
     }
     socket_set_block($this->sock);
     socket_set_option($this->sock, SOL_TCP, TCP_NODELAY, 1);
     if ($this->keepalive) {
         $this->enable_keepalive();
     }
 }
 /**
  * Setup the stream connection
  *
  * @throws \PhpAmqpLib\Exception\AMQPRuntimeException
  * @throws \Exception
  */
 public function connect()
 {
     $errstr = $errno = null;
     if ($this->context) {
         $remote = sprintf('ssl://%s:%s', $this->host, $this->port);
         $this->sock = @stream_socket_client($remote, $errno, $errstr, $this->connection_timeout, STREAM_CLIENT_CONNECT, $this->context);
     } else {
         $remote = sprintf('tcp://%s:%s', $this->host, $this->port);
         $this->sock = @stream_socket_client($remote, $errno, $errstr, $this->connection_timeout, STREAM_CLIENT_CONNECT);
     }
     if (!$this->sock) {
         throw new AMQPRuntimeException("Error Connecting to server({$errno}): {$errstr} ");
     }
     list($sec, $uSec) = MiscHelper::splitSecondsMicroseconds($this->read_write_timeout);
     if (!stream_set_timeout($this->sock, $sec, $uSec)) {
         throw new AMQPIOException("Timeout could not be set");
     }
     stream_set_blocking($this->sock, 1);
     if ($this->keepalive) {
         $this->enable_keepalive();
     }
 }
Exemple #3
0
 /**
  * Sets up the stream connection
  *
  * @throws \PhpAmqpLib\Exception\AMQPRuntimeException
  * @throws \Exception
  */
 public function connect()
 {
     $errstr = $errno = null;
     $remote = sprintf('%s://%s:%s', $this->protocol, $this->host, $this->port);
     set_error_handler(array($this, 'error_handler'));
     $this->sock = stream_socket_client($remote, $errno, $errstr, $this->connection_timeout, STREAM_CLIENT_CONNECT, $this->context);
     restore_error_handler();
     if (false === $this->sock) {
         throw new AMQPRuntimeException(sprintf('Error Connecting to server(%s): %s ', $errno, $errstr), $errno);
     }
     if (false === stream_socket_get_name($this->sock, true)) {
         throw new AMQPRuntimeException(sprintf('Connection refused: %s ', $remote));
     }
     list($sec, $uSec) = MiscHelper::splitSecondsMicroseconds($this->read_write_timeout);
     if (!stream_set_timeout($this->sock, $sec, $uSec)) {
         throw new AMQPIOException('Timeout could not be set');
     }
     // php cannot capture signals while streams are blocking
     if ($this->canDispatchPcntlSignal) {
         stream_set_blocking($this->sock, 0);
         stream_set_write_buffer($this->sock, 0);
         if (function_exists('stream_set_read_buffer')) {
             stream_set_read_buffer($this->sock, 0);
         }
     } else {
         stream_set_blocking($this->sock, 1);
     }
     if ($this->keepalive) {
         $this->enable_keepalive();
     }
 }
 /**
  * Wait until some data is retrieved from the socket.
  *
  * AMQPTimeoutException can be raised if the timeout is set
  *
  * @throws \PhpAmqpLib\Exception\AMQPTimeoutException
  */
 protected function wait()
 {
     if ($this->timeout == 0) {
         return;
     }
     // wait ..
     list($sec, $usec) = MiscHelper::splitSecondsMicroseconds($this->timeout);
     $result = $this->io->select($sec, $usec);
     if ($result === false) {
         throw new AMQPRuntimeException(sprintf("An error occurs", $this->timeout));
     }
     if ($result === 0) {
         throw new AMQPTimeoutException(sprintf("A timeout of %ds occurs while waiting for incoming data", $this->timeout));
     }
 }
Exemple #5
0
 /**
  * Waits until some data is retrieved from the socket.
  *
  * AMQPTimeoutException can be raised if the timeout is set
  *
  * @throws \PhpAmqpLib\Exception\AMQPTimeoutException
  */
 protected function wait()
 {
     if ($this->timeout == 0) {
         return;
     }
     // wait ..
     list($sec, $usec) = MiscHelper::splitSecondsMicroseconds($this->timeout);
     $result = $this->io->select($sec, $usec);
     if ($result === false) {
         throw new AMQPRuntimeException('A network error occured while awaiting for incoming data');
     }
     if ($result === 0) {
         throw new AMQPTimeoutException(sprintf('The connection timed out after %s sec while awaiting incoming data', $this->getTimeout()));
     }
 }
 /**
  * @dataProvider getInputOutputForSplitSecondsMicroseconds
  * @param mixed $input
  * @param array $expected
  */
 public function testSplitSecondsMicroseconds($input, $expected)
 {
     $this->assertEquals($expected, MiscHelper::splitSecondsMicroseconds($input));
 }