mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-06-22 19:34:02 +00:00
Changes to Threads
This commit is contained in:
parent
539c8046d8
commit
20694f2c77
@ -186,8 +186,9 @@ class PocketMinecraftServer{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function setType($type = "normal"){
|
public function setType($type = "normal"){
|
||||||
switch($type){
|
switch(trim(strtolower($type))){
|
||||||
case "normal":
|
case "normal":
|
||||||
|
case "demo":
|
||||||
$this->serverType = "MCCPP;Demo;";
|
$this->serverType = "MCCPP;Demo;";
|
||||||
break;
|
break;
|
||||||
case "minecon":
|
case "minecon":
|
||||||
|
111
src/network/ThreadedUDPSocket.php
Normal file
111
src/network/ThreadedUDPSocket.php
Normal file
@ -0,0 +1,111 @@
|
|||||||
|
<?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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -28,43 +28,27 @@ the Free Software Foundation, either version 3 of the License, or
|
|||||||
|
|
||||||
|
|
||||||
class UDPSocket{
|
class UDPSocket{
|
||||||
private $encrypt;
|
public $connected, $sock, $server;
|
||||||
var $buffer, $connected, $errors, $sock, $server;
|
|
||||||
function __construct($server, $port, $listen = false, $serverip = "0.0.0.0"){
|
function __construct($server, $port, $listen = false, $serverip = "0.0.0.0"){
|
||||||
$this->errors = array_fill(88,(125 - 88) + 1, true);
|
|
||||||
$this->server = $server;
|
$this->server = $server;
|
||||||
$this->port = $port;
|
$this->port = $port;
|
||||||
$this->sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
|
$this->sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
|
||||||
socket_set_option($this->sock, SOL_SOCKET, SO_BROADCAST, 1); //Allow sending broadcast messages
|
socket_set_option($this->sock, SOL_SOCKET, SO_BROADCAST, 1); //Allow sending broadcast messages
|
||||||
if($listen !== true){
|
if($listen !== true){
|
||||||
$this->connected = true;
|
$this->connected = true;
|
||||||
$this->buffer = array();
|
|
||||||
$this->unblock();
|
$this->unblock();
|
||||||
}else{
|
}else{
|
||||||
if(socket_bind($this->sock, $serverip, $port) === true){
|
if(socket_bind($this->sock, $serverip, $port) === true){
|
||||||
$this->unblock();
|
$this->unblock();
|
||||||
|
$this->connected = true;
|
||||||
}else{
|
}else{
|
||||||
console("[ERROR] Couldn't bind to $serverip:".$port, true, true, 0);
|
$this->connected = false;
|
||||||
die();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function listenSocket(){
|
|
||||||
$sock = @socket_accept($this->sock);
|
|
||||||
if($sock !== false){
|
|
||||||
$sock = new Socket(false, false, false, $sock);
|
|
||||||
$sock->unblock();
|
|
||||||
return $sock;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function close($error = 125){
|
public function close($error = 125){
|
||||||
$this->connected = false;
|
$this->connected = false;
|
||||||
if($error !== false){
|
|
||||||
console("[ERROR] [Socket] Socket closed, Error $error: ".socket_strerror($error));
|
|
||||||
}
|
|
||||||
return @socket_close($this->sock);
|
return @socket_close($this->sock);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -80,8 +64,7 @@ class UDPSocket{
|
|||||||
if($this->connected === false){
|
if($this->connected === false){
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
$len = @socket_recvfrom($this->sock, $buf, 65535, 0, $source, $port);
|
return @socket_recvfrom($this->sock, $buf, 65535, 0, $source, $port);
|
||||||
return $len;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function write($data, $dest = false, $port = false){
|
public function write($data, $dest = false, $port = false){
|
||||||
|
@ -1,49 +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 TickLoop extends Thread{
|
|
||||||
public $tick, $stop, $lastTic;
|
|
||||||
private $server;
|
|
||||||
public function __construct(){
|
|
||||||
$this->tick = false;
|
|
||||||
$this->lastTick = 0;
|
|
||||||
$this->server = ServerAPI::request();
|
|
||||||
}
|
|
||||||
public function run(){
|
|
||||||
while($this->stop !== true){
|
|
||||||
usleep(1);
|
|
||||||
$time = microtime(true);
|
|
||||||
if($this->lastTick <= ($time - 0.05)){
|
|
||||||
$this->lastTick = $time;
|
|
||||||
$this->tick = true;
|
|
||||||
$this->wait();
|
|
||||||
$this->tick = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
exit(0);
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user