Added Packet Count check

This commit is contained in:
Shoghi Cervantes 2013-06-14 00:21:43 +02:00
parent f51630c572
commit 18562317c3
3 changed files with 19 additions and 114 deletions

View File

@ -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);
}

View File

@ -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);

View File

@ -1,111 +0,0 @@
<?php
/*
-
/ \
/ \
/ PocketMine \
/ MP \
|\ @shoghicp /|
|. \ / .|
| .. \ / .. |
| .. | .. |
| .. | .. |
\ | /
\ | /
\ | /
\ | /
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
*/
class ThreadedUDPSocket extends Thread{
private $socket;
private $sendQueue;
private $receiveQueue;
public $connected;
public $d;
public $port;
public $server;
public function __construct($server, $port, $listen = false, $serverip = "0.0.0.0"){
$this->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;
}
}