diff --git a/src/API/PlayerAPI.php b/src/API/PlayerAPI.php index 29d1f05d..21859b06 100644 --- a/src/API/PlayerAPI.php +++ b/src/API/PlayerAPI.php @@ -148,9 +148,7 @@ class PlayerAPI{ if($additional){ $arqCnt = count($p->packetAlwaysRecoverQueue); $rqCnt = count($p->recoveryQueue); - $maxAck = $this->server->interface->maxACKsAllowed["{$p->ip}:{$p->port}"] ?? "???"; - $maxNack = $this->server->interface->maxNACKsAllowed["{$p->ip}:{$p->port}"] ?? "???"; - $additionalInfo = "ARQ/RQ/MACK/MNAK: $arqCnt/$rqCnt/$maxAck/$maxNack"; + $additionalInfo = "ARQ/RQ/EMPS: $arqCnt/$rqCnt"; } $chunksCnt = count($p->chunkDataSent); return "{$p->username}'s ping: {$ping}ms, packet loss {$pkLoss}%, $transfer KB/s. Chunks: $chunksCnt/256\n$additionalInfo"; diff --git a/src/Player.php b/src/Player.php index 6c90cb8d..626c7160 100755 --- a/src/Player.php +++ b/src/Player.php @@ -83,8 +83,8 @@ class Player{ private $timeout; private $connected = true; private $clientID; - public $ip; - public $port; + private $ip; + private $port; private $counter = [0, 0, 0, 0]; public $username; public $iusername; @@ -188,9 +188,6 @@ class Player{ */ public function __construct($clientID, $ip, $port, $MTU){ $this->bigCnt = 0; - //setting it above 1100 may cause issues for players on some servers - //(see 236e98be23c7f0d0654ae9af40703e4e4e89516a) - //(duplicated here cuz i wanted to remove it) $this->MTU = min($MTU, 1100); $this->server = ServerAPI::request(); $this->lastBreak = microtime(true); @@ -226,7 +223,7 @@ class Player{ $this->chatMessagesQueue->data = []; $this->server->schedule(20 * 60, [$this, "clearQueue"], [], true); - $this->server->interface->init($this->ip, $this->port); + $this->server->interface->maxACKsAllowed["{$this->ip}:{$this->port}"] = 0; $this->evid[] = $this->server->event("server.close", [$this, "close"]); console("[DEBUG] New Session started with $ip:$port. MTU {$this->MTU}, Client ID {$this->clientID}", true, true, 2); } @@ -488,7 +485,6 @@ class Player{ $pk = new RakNetPacket(RakNetInfo::DATA_PACKET_0); $pk->data[] = $packet; $pk->seqNumber = $this->counter[0]++; - //$this->counter[0] += 5000; $pk->sendtime = microtime(true); if($recover !== false){ $this->recoveryQueue[$pk->seqNumber] = $pk; @@ -534,6 +530,11 @@ class Player{ if(EventHandler::callEvent(new DataPacketSendEvent($this, $packet)) === BaseEvent::DENY) return false; if(!$this->convertToLocalEIDPacket($packet)) return false; + $pk = new RakNetPacket(RakNetInfo::DATA_PACKET_0); + $pk->data[] = $packet; + $pk->seqNumber = $this->counter[0]++; + $pk->sendtime = microtime(true); + $packet->encode(); $len = strlen($packet->buffer) + 1; $MTU = $this->MTU - 24; @@ -541,11 +542,6 @@ class Player{ return $this->sendBigPacketAlwaysRecover($packet); //TODO fix } - $pk = new RakNetPacket(RakNetInfo::DATA_PACKET_0); - $pk->data[] = $packet; - $pk->seqNumber = $this->counter[0]++; - $pk->sendtime = microtime(true); - $packet->messageIndex = $this->counter[3]++; $packet->reliability = RakNetInfo::RELIABILITY_RELIABLE; @@ -1578,13 +1574,8 @@ class Player{ } } } - + public function handlePacketQueues(){ - /*$acks = $this->server->interface->maxACKsAllowed["{$this->ip}:{$this->port}"] ?? 0; - $nacks = $this->server->interface->maxNACKsAllowed["{$this->ip}:{$this->port}"] ?? 0; - - console("acK: {$acks} nack: {$nacks}");*/ - if($this->server->ticks % 40 == 0){ //2s $this->sendPing(); } @@ -1603,7 +1594,7 @@ class Player{ $this->close("timeout"); return false; } - + if(($ackCnt = count($this->ackQueue)) > 0){ rsort($this->ackQueue); $safeCount = (int) (($this->MTU - 1) / 4); @@ -1714,7 +1705,7 @@ class Player{ } foreach($this->packetAlwaysRecoverQueue as $cnt => $data){ - $maxDelay = (isset($this->chunkCount[$cnt])) ? 2 : 5; + $maxDelay = (isset($this->chunkCount[$cnt])) ? 1 : 5; if($time - $data->sendtime >= $maxDelay){ $this->resendQueue[$cnt] = $data; } @@ -2032,8 +2023,7 @@ class Player{ } public function __destruct(){ //pwease dont leak memory >_< - $server = ServerAPI::request(); - $server->interface->clear($this->ip, $this->port); + unset($this->server->interface->maxACKsAllowed["{$this->ip}:{$this->port}"]); } public function save(){ @@ -2107,6 +2097,7 @@ class Player{ } public function entityTick(){ + if($this->isSleeping) ++$this->sleepingTime; if($this->server->difficulty == 0 && $this->entity->counter % (20 * 15) == 0){ if($this->entity->health < 20 && $this->entity->health > 0){ @@ -2202,7 +2193,6 @@ class Player{ $this->close("Incorrect protocol #" . $packet->protocol1, false); return; } - if(preg_match('#[^a-zA-Z0-9_]#', $this->username) > 0 || $this->username === "" || isset(Player::$blacklistedUsernames[$this->iusername]) || strlen($this->iusername) > 16){ $this->close("Bad username", false); return; @@ -3575,44 +3565,41 @@ class Player{ return true; } - public $maxRecvPacketSeq = 0; public function handlePacket(RakNetPacket $packet){ if($this->connected === true){ $time = microtime(true); $this->timeout = $time + 20; switch($packet->pid()){ case RakNetInfo::NACK: - foreach($packet->packets->sequences as $start => $end){ - for($count = $start; $count <= $end; ++$count){ - if(isset($this->packetAlwaysRecoverQueue[$count])){ - $this->resendQueue[$count] = $this->packetAlwaysRecoverQueue[$count]; - }else if(isset($this->recoveryQueue[$count])){ - $this->resendQueue[$count] = $this->recoveryQueue[$count]; - $this->lag[] = $time - $this->recoveryQueue[$count]->sendtime; - unset($this->recoveryQueue[$count]); - }else{ //lost and wont be recovered - ++$this->packetStats[1]; - } + foreach($packet->packets as $count){ + if(isset($this->packetAlwaysRecoverQueue[$count])){ + $this->resendQueue[$count] = $this->packetAlwaysRecoverQueue[$count]; + $this->resendQueue[$count]->incrementAllowedAck = false; + }else if(isset($this->recoveryQueue[$count])){ + $this->resendQueue[$count] =& $this->recoveryQueue[$count]; + $this->resendQueue[$count]->incrementAllowedAck = false; + $this->lag[] = $time - $this->recoveryQueue[$count]->sendtime; + unset($this->recoveryQueue[$count]); + }else{ //lost and wont be recovered + ++$this->packetStats[1]; } } break; case RakNetInfo::ACK: //if(mt_rand(0, 99) > 50) break; - foreach($packet->packets->sequences as $start => $end){ - for($count = $start; $count <= $end; ++$count){ - if(isset($this->packetAlwaysRecoverQueue[$count])){ - $this->lag[] = $time - $this->packetAlwaysRecoverQueue[$count]->sendtime; - }else if(isset($this->recoveryQueue[$count])){ - $this->lag[] = $time - $this->recoveryQueue[$count]->sendtime; - } - unset($this->recoveryQueue[$count]); + foreach($packet->packets as $count){ + if(isset($this->packetAlwaysRecoverQueue[$count])){ + $this->lag[] = $time - $this->packetAlwaysRecoverQueue[$count]->sendtime; + unset($this->packetAlwaysRecoverQueue[$count]); unset($this->chunkCount[$count]); - - unset($this->resendQueue[$count]); - ++$this->packetStats[0]; + }else if(isset($this->recoveryQueue[$count])){ + $this->lag[] = $time - $this->recoveryQueue[$count]->sendtime; + unset($this->recoveryQueue[$count]); } + unset($this->resendQueue[$count]); + ++$this->packetStats[0]; } break; @@ -3633,19 +3620,6 @@ class Player{ case RakNetInfo::DATA_PACKET_E: case RakNetInfo::DATA_PACKET_F: $this->ackQueue[] = $packet->seqNumber; - - if($packet->seqNumber > $this->maxRecvPacketSeq){ - /* - TODO send NACKs - $diff = $packet->seqNumber - $this->maxRecvPacketSeq; - if($diff > 1){ - console("diff is more than 1, sending nacks"); - $diff = $diff > 1000 ? 1000 : $diff; - $this->nackQueue[$packet->seqNumber] = $packet->seqNumber+$diff; - }*/ - $this->maxRecvPacketSeq = $packet->seqNumber; - } - $this->receiveQueue[$packet->seqNumber] = []; foreach($packet->data as $pk){ $this->receiveQueue[$packet->seqNumber][] = $pk; diff --git a/src/network/MinecraftInterface.php b/src/network/MinecraftInterface.php index a76ef623..087a0630 100644 --- a/src/network/MinecraftInterface.php +++ b/src/network/MinecraftInterface.php @@ -36,38 +36,13 @@ class MinecraftInterface{ ServerAPI::request()->api->dhandle("mcinterface.read", ["buffer" => $buf, "source" => $source, "port" => $port]); return $this->parsePacket($buf, $source, $port); } - public function clear($ip, $port){ - $id = "$ip:$port"; - unset($this->maxACKsAllowed[$id]); - unset($this->maxRecordedSeq[$id]); - unset($this->maxNACKsAllowed[$id]); - } - public function init($ip, $port){ - $id = "$ip:$port"; - $this->maxACKsAllowed[$id] = 0; - unset($this->maxRecordedSeq[$id]); - $this->maxNACKsAllowed[$id] = 0; - } - - - + /** - * Max ACK packets the client is allowed to send - * @var array - */ - public $maxACKsAllowed = []; - /** - * Max NACK packets the client is allowed to send - * @var array - */ - public $maxNACKsAllowed = []; - - /** - * Max packet seq number. Used to determine allowed nack cnt increment per packet + * Stores max ACK packets that the server is allowed to handle. * * @var array */ - public $maxRecordedSeq = []; + public $maxACKsAllowed = []; private function parsePacket($buffer, $source, $port){ $pid = ord($buffer[0]); @@ -75,18 +50,12 @@ class MinecraftInterface{ if(RakNetInfo::isValid($pid)){ $id = "$source:$port"; $ackCnt = 0; - $nackCnt = 0; $acks = $this->maxACKsAllowed[$id] ?? 0; - $nacks = $this->maxNACKsAllowed[$id] ?? 0; - $packet = RakNetParser::parse($buffer, $acks, $nacks, $ackCnt, $nackCnt); //TODO rewrite this mess later (hai "this code is still horribly ugly, messy, and unmaintainable") + + $packet = RakNetParser::parse($buffer, $acks, $ackCnt); if($packet !== false){ - if($ackCnt != 0){ - $pid = $packet->pid(); - if($pid == RakNetInfo::ACK && isset($this->maxACKsAllowed[$id])){ - $this->maxACKsAllowed[$id] = $acks - $ackCnt; - }else if($pid == RakNetInfo::NACK && isset($this->maxNACKsAllowed[$id])){ - $this->maxNACKsAllowed[$id] = $nacks - $ackCnt; - } + if($ackCnt != 0 && isset($this->maxACKsAllowed[$id]) && $packet->pid() != RakNetInfo::NACK){ + $this->maxACKsAllowed[$id] = $acks - $ackCnt; } $packet->ip = $source; $packet->port = $port; @@ -100,12 +69,7 @@ class MinecraftInterface{ $c = $server->clients[$CID] ?? false; if($c !== false){ $username = $c->iusername == "" ? $id : $c->iusername; - if($nackCnt > 0){ - ConsoleAPI::warn("Player '$username' sent too many NACK packets (allowed: {$nacks}, attempted {$ackCnt} or more)!"); - }else{ - ConsoleAPI::warn("Player '$username' sent too many ACK packets (allowed: {$acks}, attempted {$ackCnt} or more)!"); - } - + ConsoleAPI::warn("Player '$username' sent too many ACK packets (allowed: {$acks}, attempted {$ackCnt} or more)!"); $c->close("too many ack packets"); }else{ //ConsoleAPI::warn("$id sent too many ACK packets (allowed: {$acks}, attempted {$ackCnt} or more)!"); @@ -138,31 +102,8 @@ class MinecraftInterface{ RakNetCodec::encode($packet); if($packet->pid() >= RakNetInfo::DATA_PACKET_0 && $packet->pid() <= RakNetInfo::DATA_PACKET_F && $packet->incrementAllowedAck){ $id = "{$packet->ip}:{$packet->port}"; - $seq = $this->maxRecordedSeq[$id] ?? false; - $nackIncr = 0; - if($seq === false){ - $this->maxRecordedSeq[$id] = $packet->seqNumber; - $nackIncr = 1; - }else{ - if($packet->seqNumber > $seq){ - $nackIncr = $packet->seqNumber - $seq; - $this->maxRecordedSeq[$id] = $packet->seqNumber; - }else{ - $nackIncr = 0; - } - } - - //seems like real raknet would never attempt to send more than 1000 nack packets per single packet (CCRakNetSlidingWindow::OnGotPacket) - //raknet also does something if it received more than 50000 packets? - $nackIncr = $nackIncr > 1000 ? 1000 : $nackIncr; - if(isset($this->maxNACKsAllowed[$id])){ - $this->maxNACKsAllowed[$id] += $nackIncr; - }else{ - $this->maxNACKsAllowed[$id] = $nackIncr; - } - if(isset($this->maxACKsAllowed[$id])){ - $this->maxACKsAllowed[$id] += 1; + ++$this->maxACKsAllowed[$id]; }else{ $this->maxACKsAllowed[$id] = 1; } diff --git a/src/network/UDPSocket.php b/src/network/UDPSocket.php index 7029c40d..f53fc9a3 100644 --- a/src/network/UDPSocket.php +++ b/src/network/UDPSocket.php @@ -52,7 +52,7 @@ class UDPSocket{ $c = socket_select($read, $_, $_, 0, (int)($waitTime*1000000)); if($c === false || $c <= 0) return false; } - return @socket_recvfrom($this->sock, $buf, 2048, 0, $source, $port); //TODO make it read client's max mtu if exists? + return @socket_recvfrom($this->sock, $buf, 65535, 0, $source, $port); } public function write($data, $dest, $port){ diff --git a/src/network/raknet/RakNetParser.php b/src/network/raknet/RakNetParser.php index 82fc24e8..8ee4d362 100644 --- a/src/network/raknet/RakNetParser.php +++ b/src/network/raknet/RakNetParser.php @@ -1,13 +1,13 @@ buffer = &$buffer; $packet->length = strlen($buffer); - switch($pid = $packet->pid()){ + switch($packet->pid()){ case RakNetInfo::UNCONNECTED_PING: case RakNetInfo::UNCONNECTED_PING_OPEN_CONNECTIONS: $offset += 8; @@ -56,15 +56,10 @@ class RakNetParser{ } break; case RakNetInfo::NACK: - $maxRangeSize = 1000; - $maxACKAllowed = $maxNACKsAllowed; - goto handle_ack; case RakNetInfo::ACK: - $maxRangeSize = -1; - handle_ack: $offset += 2; $count = Utils::readShort(substr($buffer, $offset - 2, 2), false); - $packet->packets = new RangeList(); + $packet->packets = []; $acks = 0; for($i = 0; $i < $count && isset($buffer[$offset]); ++$i){ ++$offset; @@ -73,36 +68,34 @@ class RakNetParser{ $start = Utils::readTriad(strrev(substr($buffer, $offset - 3, 3))); $offset += 3; $end = Utils::readTriad(strrev(substr($buffer, $offset - 3, 3))); - $range = ($end - $start)+1; - if($range <= 0 || ($maxRangeSize >= 0 && $range > $maxRangeSize)){ + $acks += ($end - $start)+1; + if($acks <= 0 || $maxACKAllowed >= 0 && ($maxACKAllowed - $acks) < 0){ $packet = false; break; } - $acks += $range; - if($acks <= 0 || ($maxACKAllowed >= 0 && ($maxACKAllowed - $acks) < 0)){ - $packet = false; - break; + /*if(($end - $start) > 4096){ //not really needed? + $end = $start + 4096; + }*/ + for($c = $start; $c <= $end; ++$c){ + $packet->packets[] = $c; } - - $packet->packets->addRange($start, $end); }else{ $offset += 3; $acks += 1; - if(($maxACKAllowed >= 0 && ($maxACKAllowed - $acks) < 0)){ + if($maxACKAllowed >= 0 && ($maxACKAllowed - $acks) < 0){ $packet = false; break; } - $packet->packets->add(Utils::readTriad(strrev(substr($buffer, $offset - 3, 3)))); + $packet->packets[] = Utils::readTriad(strrev(substr($buffer, $offset - 3, 3))); } } + $ackCnt = $acks; - if($pid == RakNetInfo::NACK) $nackCnt = $acks; break; default: $packet = false; break; } - //console("recv $ackCnt n $nackCnt"); return $packet; } diff --git a/src/utils/RangeList.php b/src/utils/RangeList.php deleted file mode 100644 index 90e40bbe..00000000 --- a/src/utils/RangeList.php +++ /dev/null @@ -1,25 +0,0 @@ -sequences[$start] = $finish; - } - public function add($start){ - $this->sequences[$start] = $start; - } - - #[ReturnTypeWillChange] - public function getIterator(){ - return function(){ - foreach($this->sequences as $start => $end){ - for($s = $start; $s <= $end; ++$s){ - yield $s; - } - } - }; - } -} -