/**
  * Creates a new packet object based on the header byte of the given raw
  * data
  *
  * @param string $rawData The raw data of the packet
  * @return RCONPacket The packet object generated from the packet data
  * @throws PacketFormatException if the packet header is not recognized
  */
 public static function getPacketFromData($rawData)
 {
     $byteBuffer = new ByteBuffer($rawData);
     $requestId = $byteBuffer->getLong();
     $header = $byteBuffer->getLong();
     $data = $byteBuffer->getString();
     switch ($header) {
         case RCONPacket::SERVERDATA_AUTH_RESPONSE:
             return new RCONAuthResponse($requestId);
         case RCONPacket::SERVERDATA_RESPONSE_VALUE:
             return new RCONExecResponse($requestId, $data);
         default:
             throw new PacketFormatException('Unknown packet with header ' . dechex($header) . ' received.');
     }
 }
 /**
  * Creates a new TCP socket to communicate with the server on the given IP
  * address and port
  *
  * @param string $ipAddress Either the IP address or the DNS name of the
  *        server
  * @param int $portNumber The port the server is listening on
  */
 public function __construct($ipAddress, $portNumber)
 {
     $this->logger = \SteamCondenser\getLogger(get_class($this));
     $this->buffer = ByteBuffer::allocate(1400);
     $this->ipAddress = $ipAddress;
     $this->portNumber = $portNumber;
 }
 /**
  * Creates a new TCP socket to communicate with the server on the given IP
  * address and port
  *
  * @param string $ipAddress Either the IP address or the DNS name of the
  *        server
  * @param int $portNumber The port the server is listening on
  */
 public function __construct($ipAddress, $portNumber)
 {
     if (!isset(self::$log)) {
         self::$log = new Logger('\\SteamCondenser\\Servers\\Sockets\\RCONSocket');
     }
     $this->buffer = ByteBuffer::allocate(1400);
     $this->ipAddress = $ipAddress;
     $this->portNumber = $portNumber;
 }
 public function testReceiveIntoExistingBuffer()
 {
     $this->socket->buffer = \SteamCondenser\ByteBuffer::allocate(10);
     $this->udpSocket->expects($this->once())->method('select')->will($this->returnValue(true));
     $this->udpSocket->expects($this->once())->method('recv')->with(4)->will($this->returnValue('test'));
     $this->assertEquals(4, $this->socket->receivePacket(4));
     $buffer = $this->socket->buffer;
     $this->assertEquals(0, $buffer->position());
     $this->assertEquals(4, $buffer->remaining());
     $this->assertEquals('test', $buffer->_array());
 }
 /**
  * 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;
 }
 /**
  * Creates a new packet object based on the given data
  *
  * @param int $headerData The packet header
  * @param string $contentData The raw data of the packet
  */
 public function __construct($headerData, $contentData = null)
 {
     $this->headerData = $headerData;
     $this->contentData = ByteBuffer::wrap($contentData);
 }