PocketMine-MP 5.21.2 git-b2aa6396c3cc2cafdd815eacc360e1ad89599899
Loading...
Searching...
No Matches
RakLibToUserThreadMessageSender.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 chr;
24use function inet_pton;
25use function strlen;
26
28 public function __construct(
29 private InterThreadChannelWriter $channel
30 ){}
31
32 public function onClientConnect(int $sessionId, string $address, int $port, int $clientId) : void{
33 $rawAddr = inet_pton($address);
34 if($rawAddr === false){
35 throw new \InvalidArgumentException("Invalid IP address");
36 }
37 $this->channel->write(
38 chr(ITCProtocol::PACKET_OPEN_SESSION) .
39 Binary::writeInt($sessionId) .
40 chr(strlen($rawAddr)) . $rawAddr .
41 Binary::writeShort($port) .
42 Binary::writeLong($clientId)
43 );
44 }
45
46 public function onClientDisconnect(int $sessionId, int $reason) : void{
47 $this->channel->write(
48 chr(ITCProtocol::PACKET_CLOSE_SESSION) .
49 Binary::writeInt($sessionId) .
50 chr($reason)
51 );
52 }
53
54 public function onPacketReceive(int $sessionId, string $packet) : void{
55 $this->channel->write(
56 chr(ITCProtocol::PACKET_ENCAPSULATED) .
57 Binary::writeInt($sessionId) .
58 $packet
59 );
60 }
61
62 public function onRawPacketReceive(string $address, int $port, string $payload) : void{
63 $this->channel->write(
64 chr(ITCProtocol::PACKET_RAW) .
65 chr(strlen($address)) . $address .
66 Binary::writeShort($port) .
67 $payload
68 );
69 }
70
71 public function onPacketAck(int $sessionId, int $identifierACK) : void{
72 $this->channel->write(
73 chr(ITCProtocol::PACKET_ACK_NOTIFICATION) .
74 Binary::writeInt($sessionId) .
75 Binary::writeInt($identifierACK)
76 );
77 }
78
79 public function onBandwidthStatsUpdate(int $bytesSentDiff, int $bytesReceivedDiff) : void{
80 $this->channel->write(
81 chr(ITCProtocol::PACKET_REPORT_BANDWIDTH_STATS) .
82 Binary::writeLong($bytesSentDiff) .
83 Binary::writeLong($bytesReceivedDiff)
84 );
85 }
86
87 public function onPingMeasure(int $sessionId, int $pingMS) : void{
88 $this->channel->write(
89 chr(ITCProtocol::PACKET_REPORT_PING) .
90 Binary::writeInt($sessionId) .
91 Binary::writeInt($pingMS)
92 );
93 }
94}