function it_sends_a_packet(Socket $client, Packet $packet) { $rawPacket = pack('VV', 1, Packet::SERVERDATA_EXECCOMMAND) . '/command' . pack('HH', 0x0, 0x0); $packet->convertToRcon()->willReturn($rawPacket); $rawPacket = pack('V', mb_strlen($rawPacket)) . $rawPacket; $client->write($rawPacket)->shouldBeCalled(); $rawPacket = pack('VV', 1, Packet::SERVERDATA_RESPONSE_VALUE) . 'Command result' . pack('HH', 0x0, 0x0); $packetSize = pack('V', mb_strlen($rawPacket)); $client->read(4)->willReturn($packetSize); $client->read(mb_strlen($rawPacket))->willReturn($rawPacket); $response = $this->sendPacket($packet); $response->shouldHaveType('Minecraft\\Rcon\\Packet'); $response->getId()->shouldReturn(1); $response->getType()->shouldReturn(Packet::SERVERDATA_RESPONSE_VALUE); $response->getBody()->shouldReturn('Command result'); }
/** * Sends a packet through the connection * * @param Packet $packet * * @return Packet */ public function sendPacket(Packet $packet) { // Create a raw packet $rawPacket = $packet->convertToRcon(); // Attach size $rawPacket = pack('V', mb_strlen($rawPacket)) . $rawPacket; // Write to the socket $this->socket->write($rawPacket); // Read the packet size $packetSize = $this->socket->read(4); $packetSize = unpack('V', $packetSize); $packetSize = reset($packetSize); // TODO: multi-packet responses // Read the packet $rawPacket = $this->socket->read($packetSize); // Parse and return the packet return Packet::createFromRcon($rawPacket); }