PocketMine-MP 5.23.3 git-976fc63567edab7a6fb6aeae739f43cf9fe57de4
Loading...
Searching...
No Matches
RakLibServer.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\network\mcpe\raklib;
25
26use pmmp\thread\Thread as NativeThread;
27use pmmp\thread\ThreadSafeArray;
41use function gc_disable;
42use function ini_set;
43
44class RakLibServer extends Thread{
45 protected bool $ready = false;
46 protected string $mainPath;
49
54 public function __construct(
55 protected ThreadSafeLogger $logger,
56 protected ThreadSafeArray $mainToThreadBuffer,
57 protected ThreadSafeArray $threadToMainBuffer,
59 protected int $serverId,
60 protected int $maxMtuSize,
61 protected int $protocolVersion,
62 protected SleeperHandlerEntry $sleeperEntry
63 ){
64 $this->mainPath = \pocketmine\PATH;
65 $this->address = new NonThreadSafeValue($address);
66 }
67
68 public function startAndWait(int $options = NativeThread::INHERIT_NONE) : void{
69 $this->start($options);
70 $this->synchronized(function() : void{
71 while(!$this->ready && $this->getCrashInfo() === null){
72 $this->wait();
73 }
74 $crashInfo = $this->getCrashInfo();
75 if($crashInfo !== null){
76 if($crashInfo->getType() === SocketException::class){
77 throw new SocketException($crashInfo->getMessage());
78 }
79 throw new ThreadCrashException("RakLib failed to start", $crashInfo);
80 }
81 });
82 }
83
84 protected function onRun() : void{
85 //RakLib has cycles (e.g. ServerSession <-> Server) but these cycles are explicitly cleaned up anyway, and are
86 //very few, so it's pointless to waste CPU time on GC
87 gc_disable();
88
89 ini_set("display_errors", '1');
90 ini_set("display_startup_errors", '1');
91 \GlobalLogger::set($this->logger);
92
93 $socket = new ServerSocket($this->address->deserialize());
94 $manager = new Server(
95 $this->serverId,
96 $this->logger,
97 $socket,
98 $this->maxMtuSize,
99 new SimpleProtocolAcceptor($this->protocolVersion),
100 new UserToRakLibThreadMessageReceiver(new PthreadsChannelReader($this->mainToThreadBuffer)),
101 new RakLibToUserThreadMessageSender(new SnoozeAwarePthreadsChannelWriter($this->threadToMainBuffer, $this->sleeperEntry->createNotifier())),
102 new ExceptionTraceCleaner($this->mainPath),
103 recvMaxSplitParts: 512
104 );
105 $this->synchronized(function() : void{
106 $this->ready = true;
107 $this->notify();
108 });
109 while(!$this->isKilled){
110 $manager->tickProcessor();
111 }
112 $manager->waitShutdown();
113 }
114
115 public function getThreadName() : string{
116 return "RakLib";
117 }
118}
__construct(protected ThreadSafeLogger $logger, protected ThreadSafeArray $mainToThreadBuffer, protected ThreadSafeArray $threadToMainBuffer, InternetAddress $address, protected int $serverId, protected int $maxMtuSize, protected int $protocolVersion, protected SleeperHandlerEntry $sleeperEntry)