/** * 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; }