-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathRakLibToUserThreadMessageSender.php
More file actions
94 lines (82 loc) · 2.52 KB
/
RakLibToUserThreadMessageSender.php
File metadata and controls
94 lines (82 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
/*
* RakLib network library
*
*
* This project is not affiliated with Jenkins Software LLC nor RakNet.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*/
declare(strict_types=1);
namespace raklib\server\ipc;
use pmmp\encoding\LE;
use raklib\server\ipc\RakLibToUserThreadMessageProtocol as ITCProtocol;
use raklib\server\ServerEventListener;
use function chr;
use function inet_pton;
use function strlen;
final class RakLibToUserThreadMessageSender implements ServerEventListener{
public function __construct(
private InterThreadChannelWriter $channel
){}
public function onClientConnect(int $sessionId, string $address, int $port, int $clientId) : void{
$rawAddr = inet_pton($address);
if($rawAddr === false){
throw new \InvalidArgumentException("Invalid IP address");
}
$this->channel->write(
chr(ITCProtocol::PACKET_OPEN_SESSION) .
LE::packUnsignedInt($sessionId) .
chr(strlen($rawAddr)) . $rawAddr .
LE::packUnsignedShort($port) .
LE::packUnsignedLong($clientId)
);
}
public function onClientDisconnect(int $sessionId, int $reason) : void{
$this->channel->write(
chr(ITCProtocol::PACKET_CLOSE_SESSION) .
LE::packUnsignedInt($sessionId) .
chr($reason)
);
}
public function onPacketReceive(int $sessionId, string $packet) : void{
$this->channel->write(
chr(ITCProtocol::PACKET_ENCAPSULATED) .
LE::packUnsignedInt($sessionId) .
$packet
);
}
public function onRawPacketReceive(string $address, int $port, string $payload) : void{
$this->channel->write(
chr(ITCProtocol::PACKET_RAW) .
chr(strlen($address)) . $address .
LE::packUnsignedShort($port) .
$payload
);
}
public function onPacketAck(int $sessionId, int $identifierACK) : void{
$this->channel->write(
chr(ITCProtocol::PACKET_ACK_NOTIFICATION) .
LE::packUnsignedInt($sessionId) .
LE::packSignedInt($identifierACK)
);
}
public function onBandwidthStatsUpdate(int $bytesSentDiff, int $bytesReceivedDiff) : void{
$this->channel->write(
chr(ITCProtocol::PACKET_REPORT_BANDWIDTH_STATS) .
LE::packUnsignedLong($bytesSentDiff) .
LE::packSignedLong($bytesReceivedDiff)
);
}
public function onPingMeasure(int $sessionId, int $pingMS) : void{
$this->channel->write(
chr(ITCProtocol::PACKET_REPORT_PING) .
LE::packUnsignedInt($sessionId) .
LE::packUnsignedInt($pingMS)
);
}
}