PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/src/synapse/network/synlib/SynapseClient.php

https://gitlab.com/kamata4649/genisys
PHP | 220 lines | 173 code | 40 blank | 7 comment | 10 complexity | c788a3d316a3afda7ace41cad15de7d1 MD5 | raw file
  1. <?php
  2. namespace synapse\network\synlib;
  3. use pocketmine\Thread;
  4. class SynapseClient extends Thread{
  5. const VERSION = "0.1.1";
  6. /** @var \ThreadedLogger */
  7. private $logger;
  8. /** @var string */
  9. private $interface;
  10. /** @var int */
  11. private $port;
  12. private $shutdown = true;
  13. /** @var \Threaded */
  14. private $externalQueue, $internalQueue;
  15. private $mainPath;
  16. private $needAuth = false;
  17. private $connected = true;
  18. public $needReconnect = false;
  19. public function __construct(\ThreadedLogger $logger, \ClassLoader $loader, $port, $interface = "127.0.0.1"){
  20. $this->logger = $logger;
  21. $this->interface = $interface;
  22. $this->port = (int) $port;
  23. if($port < 1 or $port > 65536){
  24. throw new \Exception("Invalid port range");
  25. }
  26. $this->setClassLoader($loader);
  27. $this->shutdown = false;
  28. $this->externalQueue = new \Threaded;
  29. $this->internalQueue = new \Threaded;
  30. if(\Phar::running(true) !== ""){
  31. $this->mainPath = \Phar::running(true);
  32. }else{
  33. $this->mainPath = \getcwd() . DIRECTORY_SEPARATOR;
  34. }
  35. $this->start();
  36. }
  37. public function reconnect(){
  38. $this->needReconnect = true;
  39. }
  40. public function isNeedAuth() : bool{
  41. return $this->needAuth;
  42. }
  43. public function setNeedAuth(bool $need){
  44. $this->needAuth = $need;
  45. }
  46. public function isConnected() : bool{
  47. return $this->connected;
  48. }
  49. public function setConnected(bool $con){
  50. $this->connected = $con;
  51. }
  52. public function quit(){
  53. $this->shutdown();
  54. parent::quit();
  55. }
  56. public function run(){
  57. $this->registerClassLoader();
  58. gc_enable();
  59. error_reporting(-1);
  60. ini_set("display_errors", 1);
  61. ini_set("display_startup_errors", 1);
  62. set_error_handler([$this, "errorHandler"], E_ALL);
  63. register_shutdown_function([$this, "shutdownHandler"]);
  64. try{
  65. $socket = new SynapseSocket($this->getLogger(), $this->port, $this->interface);
  66. new ServerConnection($this, $socket);
  67. }catch(\Throwable $e){
  68. $this->logger->logException($e);
  69. }
  70. }
  71. public function shutdownHandler(){
  72. if($this->shutdown !== true){
  73. $this->getLogger()->emergency("SynLib crashed!");
  74. }
  75. }
  76. public function errorHandler($errno, $errstr, $errfile, $errline, $context, $trace = null){
  77. if(error_reporting() === 0){
  78. return false;
  79. }
  80. $errorConversion = [
  81. E_ERROR => "E_ERROR",
  82. E_WARNING => "E_WARNING",
  83. E_PARSE => "E_PARSE",
  84. E_NOTICE => "E_NOTICE",
  85. E_CORE_ERROR => "E_CORE_ERROR",
  86. E_CORE_WARNING => "E_CORE_WARNING",
  87. E_COMPILE_ERROR => "E_COMPILE_ERROR",
  88. E_COMPILE_WARNING => "E_COMPILE_WARNING",
  89. E_USER_ERROR => "E_USER_ERROR",
  90. E_USER_WARNING => "E_USER_WARNING",
  91. E_USER_NOTICE => "E_USER_NOTICE",
  92. E_STRICT => "E_STRICT",
  93. E_RECOVERABLE_ERROR => "E_RECOVERABLE_ERROR",
  94. E_DEPRECATED => "E_DEPRECATED",
  95. E_USER_DEPRECATED => "E_USER_DEPRECATED",
  96. ];
  97. $errno = isset($errorConversion[$errno]) ? $errorConversion[$errno] : $errno;
  98. if(($pos = strpos($errstr, "\n")) !== false){
  99. $errstr = substr($errstr, 0, $pos);
  100. }
  101. $errfile = $this->cleanPath($errfile);
  102. $this->getLogger()->debug("An $errno error happened: \"$errstr\" in \"$errfile\" at line $errline");
  103. foreach(($trace = $this->getTrace($trace === null ? 3 : 0, $trace)) as $i => $line){
  104. $this->getLogger()->debug($line);
  105. }
  106. return true;
  107. }
  108. public function getTrace($start = 1, $trace = null){
  109. if($trace === null){
  110. if(function_exists("xdebug_get_function_stack")){
  111. $trace = array_reverse(xdebug_get_function_stack());
  112. }else{
  113. $e = new \Exception();
  114. $trace = $e->getTrace();
  115. }
  116. }
  117. $messages = [];
  118. $j = 0;
  119. for($i = (int) $start; isset($trace[$i]); ++$i, ++$j){
  120. $params = "";
  121. if(isset($trace[$i]["args"]) or isset($trace[$i]["params"])){
  122. if(isset($trace[$i]["args"])){
  123. $args = $trace[$i]["args"];
  124. }else{
  125. $args = $trace[$i]["params"];
  126. }
  127. foreach($args as $name => $value){
  128. $params .= (is_object($value) ? get_class($value) . " " . (method_exists($value, "__toString") ? $value->__toString() : "object") : gettype($value) . " " . @strval($value)) . ", ";
  129. }
  130. }
  131. $messages[] = "#$j " . (isset($trace[$i]["file"]) ? $this->cleanPath($trace[$i]["file"]) : "") . "(" . (isset($trace[$i]["line"]) ? $trace[$i]["line"] : "") . "): " . (isset($trace[$i]["class"]) ? $trace[$i]["class"] . (($trace[$i]["type"] === "dynamic" or $trace[$i]["type"] === "->") ? "->" : "::") : "") . $trace[$i]["function"] . "(" . substr($params, 0, -2) . ")";
  132. }
  133. return $messages;
  134. }
  135. public function cleanPath($path){
  136. return rtrim(str_replace(["\\", ".php", "phar://", rtrim(str_replace(["\\", "phar://"], ["/", ""], $this->mainPath), "/")], ["/", "", "", ""], $path), "/");
  137. }
  138. public function getExternalQueue(){
  139. return $this->externalQueue;
  140. }
  141. public function getInternalQueue(){
  142. return $this->internalQueue;
  143. }
  144. public function pushMainToThreadPacket($str){
  145. $this->internalQueue[] = $str;
  146. }
  147. public function readMainToThreadPacket(){
  148. return $this->internalQueue->shift();
  149. }
  150. public function pushThreadToMainPacket($str){
  151. $this->externalQueue[] = $str;
  152. }
  153. public function readThreadToMainPacket(){
  154. return $this->externalQueue->shift();
  155. }
  156. public function isShutdown(){
  157. return $this->shutdown === true;
  158. }
  159. public function shutdown(){
  160. $this->shutdown = true;
  161. }
  162. public function getPort(){
  163. return $this->port;
  164. }
  165. public function getInterface(){
  166. return $this->interface;
  167. }
  168. /**
  169. * @return \ThreadedLogger
  170. */
  171. public function getLogger(){
  172. return $this->logger;
  173. }
  174. public function isGarbage() : bool{
  175. parent::isGarbage();
  176. }
  177. public function getThreadName(){
  178. return "SynapseClient";
  179. }
  180. }