PocketMine-MP 5.23.3 git-f7687af337d001ddbcc47b8e773f014a33faa662
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_enable;
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 gc_enable();
86 ini_set("display_errors", '1');
87 ini_set("display_startup_errors", '1');
88 \GlobalLogger::set($this->logger);
89
90 $socket = new ServerSocket($this->address->deserialize());
91 $manager = new Server(
92 $this->serverId,
93 $this->logger,
94 $socket,
95 $this->maxMtuSize,
96 new SimpleProtocolAcceptor($this->protocolVersion),
97 new UserToRakLibThreadMessageReceiver(new PthreadsChannelReader($this->mainToThreadBuffer)),
98 new RakLibToUserThreadMessageSender(new SnoozeAwarePthreadsChannelWriter($this->threadToMainBuffer, $this->sleeperEntry->createNotifier())),
99 new ExceptionTraceCleaner($this->mainPath),
100 recvMaxSplitParts: 512
101 );
102 $this->synchronized(function() : void{
103 $this->ready = true;
104 $this->notify();
105 });
106 while(!$this->isKilled){
107 $manager->tickProcessor();
108 }
109 $manager->waitShutdown();
110 }
111
112 public function getThreadName() : string{
113 return "RakLib";
114 }
115}
__construct(protected ThreadSafeLogger $logger, protected ThreadSafeArray $mainToThreadBuffer, protected ThreadSafeArray $threadToMainBuffer, InternetAddress $address, protected int $serverId, protected int $maxMtuSize, protected int $protocolVersion, protected SleeperHandlerEntry $sleeperEntry)