diff --git a/src/Player.php b/src/Player.php index 98580b407..757bdfb41 100644 --- a/src/Player.php +++ b/src/Player.php @@ -78,6 +78,7 @@ class Player{ public $toCraft = array(); public $lastCraft = 0; private $chunkCount = array(); + private $received = array(); public function __get($name){ if(isset($this->{$name})){ @@ -103,7 +104,8 @@ class Player{ $this->level = $this->server->api->level->getDefault(); $this->slot = 0; $this->packetStats = array(0,0); - $this->server->schedule(1, array($this, "handlePacketQueues"), array(), true); + $this->server->schedule(2, array($this, "handlePacketQueues"), array(), true); + $this->server->schedule(20 * 60, array($this, "clearQueue"), array(), true); $this->evid[] = $this->server->event("server.close", array($this, "close")); console("[DEBUG] New Session started with ".$ip.":".$port.". MTU ".$this->MTU.", Client ID ".$this->clientID, true, true, 2); } @@ -824,6 +826,19 @@ class Player{ return array_sum($this->bandwidthStats) / max(1, count($this->bandwidthStats)); } + public function clearQueue(){ + ksort($this->received); + if(($cnt = count($this->received)) > PLAYER_MAX_QUEUE){ + foreach($this->received as $c => $t){ + unset($this->received[$c]); + --$cnt; + if($cnt <= PLAYER_MAX_QUEUE){ + break; + } + } + } + } + public function handlePacketQueues(){ if($this->connected === false){ return false; @@ -860,7 +875,7 @@ class Player{ if($p["counter"] > $this->receiveCount){ $this->receiveCount = $p["counter"]; }elseif($p["counter"] !== 0){ - if(($p["counter"] - $this->receiveCount) > 16){ + if(isset($this->received[$p["counter"]])){ continue; } switch($p["id"]){ @@ -874,6 +889,7 @@ class Player{ continue; } } + $this->received[$p["counter"]] = true; } $this->handleDataPacket($p["id"], $p); } diff --git a/src/constants/GeneralConstants.php b/src/constants/GeneralConstants.php index 4b7592341..14c1e9f6a 100644 --- a/src/constants/GeneralConstants.php +++ b/src/constants/GeneralConstants.php @@ -38,7 +38,7 @@ define("VIEWER", 3); //Players define("MAX_CHUNK_RATE", 20 / arg("max-chunks-per-second", 3.5)); //Default rate ~172 kB/s -define("PLAYER_MAX_RECOVERY_BUFFER", 1024); +define("PLAYER_MAX_QUEUE", 1024); define("PLAYER_SURVIVAL_SLOTS", 36); define("PLAYER_CREATIVE_SLOTS", 111); diff --git a/src/network/ThreadedUDPSocket.php b/src/network/ThreadedUDPSocket.php deleted file mode 100644 index 9191c97fd..000000000 --- a/src/network/ThreadedUDPSocket.php +++ /dev/null @@ -1,111 +0,0 @@ -server = $server; - $this->port = (int) $port; - $this->d = array($listen, $serverip); - } - - public function isConnected(){ - return (bool) $this->connected; - } - - public function run(){ - $this->socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); - socket_set_option($this->socket, SOL_SOCKET, SO_BROADCAST, 1); //Allow sending broadcast messages - if($this->d[0] !== true){ - $this->connected = true; - $this->unblock(); - }else{ - if(socket_bind($this->socket, $this->d[1], $this->port) === true){ - $this->unblock(); - $this->connected = true; - }else{ - $this->connected = false; - } - } - $this->sendQueue = array(); - $this->receiveQueue = array(); - $this->wait(); - while($this->connected === true){ - if(count($this->receiveQueue) < 1024){ - $buf = ""; - $source = false; - $port = 1; - $len = @socket_recvfrom($this->socket, $buf, 65535, 0, $source, $port); - if($len !== false){ - $this->receiveQueue[] = array($buf, $source, $port, $len); - } - } - if(count($this->sendQueue) > 0){ - $item = array_shift($this->sendQueue); - @socket_sendto($this->socket, $item[0], strlen($item[0]), 0, ($item[1] === false ? $this->server:$item[1]), ($item[2] === false ? $this->port:$item[2])); - } - usleep(1); - } - exit(0); - } - - public function close($error = 125){ - $this->connected = false; - return @socket_close($this->socket); - } - - public function block(){ - socket_set_block($this->socket); - } - - public function unblock(){ - socket_set_nonblock($this->socket); - } - - public function read(){ - if($this->connected === false or count($this->receiveQueue) <= 0){ - return false; - } - return array_shift($this->receiveQueue); - } - - public function write($data, $dest = false, $port = false){ - if($this->connected === false){ - return false; - } - $this->sendQueue[] = array($data, $dest, $port); - return true; - } - -} \ No newline at end of file