PocketMine-MP 5.15.1 git-5ef247620a7c6301a849b54e5ef1009217729fc8
RakLibToUserThreadMessageReceiver.php
1<?php
2
3/*
4 * RakLib network library
5 *
6 *
7 * This project is not affiliated with Jenkins Software LLC nor RakNet.
8 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 */
15
16declare(strict_types=1);
17
18namespace raklib\server\ipc;
19
23use function inet_ntop;
24use function ord;
25use function substr;
26
28 public function __construct(
29 private InterThreadChannelReader $channel
30 ){}
31
32 public function handle(ServerEventListener $listener) : bool{
33 if(($packet = $this->channel->read()) !== null){
34 $id = ord($packet[0]);
35 $offset = 1;
36 if($id === ITCProtocol::PACKET_ENCAPSULATED){
37 $sessionId = Binary::readInt(substr($packet, $offset, 4));
38 $offset += 4;
39 $buffer = substr($packet, $offset);
40 $listener->onPacketReceive($sessionId, $buffer);
41 }elseif($id === ITCProtocol::PACKET_RAW){
42 $len = ord($packet[$offset++]);
43 $address = substr($packet, $offset, $len);
44 $offset += $len;
45 $port = Binary::readShort(substr($packet, $offset, 2));
46 $offset += 2;
47 $payload = substr($packet, $offset);
48 $listener->onRawPacketReceive($address, $port, $payload);
49 }elseif($id === ITCProtocol::PACKET_REPORT_BANDWIDTH_STATS){
50 $sentBytes = Binary::readLong(substr($packet, $offset, 8));
51 $offset += 8;
52 $receivedBytes = Binary::readLong(substr($packet, $offset, 8));
53 $listener->onBandwidthStatsUpdate($sentBytes, $receivedBytes);
54 }elseif($id === ITCProtocol::PACKET_OPEN_SESSION){
55 $sessionId = Binary::readInt(substr($packet, $offset, 4));
56 $offset += 4;
57 $len = ord($packet[$offset++]);
58 $rawAddr = substr($packet, $offset, $len);
59 $offset += $len;
60 $address = inet_ntop($rawAddr);
61 if($address === false){
62 throw new \RuntimeException("Unexpected invalid IP address in inter-thread message");
63 }
64 $port = Binary::readShort(substr($packet, $offset, 2));
65 $offset += 2;
66 $clientID = Binary::readLong(substr($packet, $offset, 8));
67 $listener->onClientConnect($sessionId, $address, $port, $clientID);
68 }elseif($id === ITCProtocol::PACKET_CLOSE_SESSION){
69 $sessionId = Binary::readInt(substr($packet, $offset, 4));
70 $offset += 4;
71 $reason = ord($packet[$offset]);
72 $listener->onClientDisconnect($sessionId, $reason);
73 }elseif($id === ITCProtocol::PACKET_ACK_NOTIFICATION){
74 $sessionId = Binary::readInt(substr($packet, $offset, 4));
75 $offset += 4;
76 $identifierACK = Binary::readInt(substr($packet, $offset, 4));
77 $listener->onPacketAck($sessionId, $identifierACK);
78 }elseif($id === ITCProtocol::PACKET_REPORT_PING){
79 $sessionId = Binary::readInt(substr($packet, $offset, 4));
80 $offset += 4;
81 $pingMS = Binary::readInt(substr($packet, $offset, 4));
82 $listener->onPingMeasure($sessionId, $pingMS);
83 }
84
85 return true;
86 }
87
88 return false;
89 }
90}
static readShort(string $str)
Definition: Binary.php:153
static readLong(string $str)
Definition: Binary.php:356
static readInt(string $str)
Definition: Binary.php:235
onClientDisconnect(int $sessionId, int $reason)