Exemplo n.º 1
0
 /**
  * Reads the given amount of data from the socket and wraps it into the
  * buffer
  *
  * @param int $bufferLength The data length to read from the socket
  * @throws SocketException if an error occurs while reading data
  * @throws TimeoutException if no packet is received on time
  * @return int The number of bytes that have been read from the socket
  * @see ByteBuffer
  */
 public function receivePacket($bufferLength = 0)
 {
     if (!$this->socket->select(self::$timeout)) {
         throw new TimeoutException();
     }
     if ($bufferLength == 0) {
         $this->buffer->clear();
     } else {
         $this->buffer = ByteBuffer::allocate($bufferLength);
     }
     try {
         $data = $this->socket->recv($this->buffer->remaining());
         $this->buffer->put($data);
     } catch (ConnectionResetException $e) {
         $this->socket->close();
         throw $e;
     }
     $bytesRead = $this->buffer->position();
     $this->buffer->rewind();
     $this->buffer->limit($bytesRead);
     return $bytesRead;
 }