-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathUserToRakLibThreadMessageSender.php
More file actions
83 lines (68 loc) · 2.84 KB
/
UserToRakLibThreadMessageSender.php
File metadata and controls
83 lines (68 loc) · 2.84 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
<?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\protocol\EncapsulatedPacket;
use raklib\protocol\PacketReliability;
use raklib\server\ipc\UserToRakLibThreadMessageProtocol as ITCProtocol;
use raklib\server\ServerInterface;
use function chr;
use function strlen;
class UserToRakLibThreadMessageSender implements ServerInterface{
public function __construct(
private InterThreadChannelWriter $channel
){}
public function sendEncapsulated(int $sessionId, EncapsulatedPacket $packet, bool $immediate = false) : void{
$flags =
($immediate ? ITCProtocol::ENCAPSULATED_FLAG_IMMEDIATE : 0) |
($packet->identifierACK !== null ? ITCProtocol::ENCAPSULATED_FLAG_NEED_ACK : 0);
$buffer = chr(ITCProtocol::PACKET_ENCAPSULATED) .
LE::packUnsignedInt($sessionId) .
chr($flags) .
chr($packet->reliability) .
($packet->identifierACK !== null ? LE::packSignedInt($packet->identifierACK) : "") .
(PacketReliability::isSequencedOrOrdered($packet->reliability) ? chr($packet->orderChannel) : "") .
$packet->buffer;
$this->channel->write($buffer);
}
public function sendRaw(string $address, int $port, string $payload) : void{
$buffer = chr(ITCProtocol::PACKET_RAW) . chr(strlen($address)) . $address . LE::packUnsignedShort($port) . $payload;
$this->channel->write($buffer);
}
public function closeSession(int $sessionId) : void{
$buffer = chr(ITCProtocol::PACKET_CLOSE_SESSION) . LE::packUnsignedInt($sessionId);
$this->channel->write($buffer);
}
public function setName(string $name) : void{
$this->channel->write(chr(ITCProtocol::PACKET_SET_NAME) . $name);
}
public function setPortCheck(bool $value) : void{
$this->channel->write(chr($value ? ITCProtocol::PACKET_ENABLE_PORT_CHECK : ITCProtocol::PACKET_DISABLE_PORT_CHECK));
}
public function setPacketsPerTickLimit(int $limit) : void{
$this->channel->write(chr(ITCProtocol::PACKET_SET_PACKETS_PER_TICK_LIMIT) . LE::packUnsignedLong($limit));
}
public function blockAddress(string $address, int $timeout) : void{
$buffer = chr(ITCProtocol::PACKET_BLOCK_ADDRESS) . chr(strlen($address)) . $address . LE::packUnsignedInt($timeout);
$this->channel->write($buffer);
}
public function unblockAddress(string $address) : void{
$buffer = chr(ITCProtocol::PACKET_UNBLOCK_ADDRESS) . chr(strlen($address)) . $address;
$this->channel->write($buffer);
}
public function addRawPacketFilter(string $regex) : void{
$this->channel->write(chr(ITCProtocol::PACKET_RAW_FILTER) . $regex);
}
}