|
18 | 18 | namespace raklib\server\ipc;
|
19 | 19 |
|
20 | 20 | use pocketmine\utils\Binary;
|
21 |
| -use raklib\protocol\EncapsulatedPacket; |
22 |
| -use raklib\protocol\PacketReliability; |
23 | 21 | use raklib\server\ipc\UserToRakLibThreadMessageProtocol as ITCProtocol;
|
24 | 22 | use raklib\server\ServerEventSource;
|
25 | 23 | use raklib\server\ServerInterface;
|
| 24 | +use raklib\server\SessionInterface; |
26 | 25 | use function ord;
|
27 | 26 | use function substr;
|
28 | 27 |
|
29 | 28 | final class UserToRakLibThreadMessageReceiver implements ServerEventSource{
|
30 | 29 | /** @var InterThreadChannelReader */
|
31 | 30 | private $channel;
|
32 | 31 |
|
33 |
| - public function __construct(InterThreadChannelReader $channel){ |
34 |
| - $this->channel = $channel; |
35 |
| - } |
| 32 | + private InterThreadChannelReaderDeserializer $channelReaderDeserializer; |
36 | 33 |
|
37 |
| - public function process(ServerInterface $server) : bool{ |
38 |
| - if(($packet = $this->channel->read()) !== null){ |
39 |
| - $id = ord($packet[0]); |
40 |
| - $offset = 1; |
41 |
| - if($id === ITCProtocol::PACKET_ENCAPSULATED){ |
42 |
| - $sessionId = Binary::readInt(substr($packet, $offset, 4)); |
43 |
| - $offset += 4; |
44 |
| - $flags = ord($packet[$offset++]); |
45 |
| - $immediate = ($flags & ITCProtocol::ENCAPSULATED_FLAG_IMMEDIATE) !== 0; |
46 |
| - $needACK = ($flags & ITCProtocol::ENCAPSULATED_FLAG_NEED_ACK) !== 0; |
| 34 | + /** |
| 35 | + * @var SessionInterface[][]|UserToRakLibThreadSessionMessageReceiver[][] |
| 36 | + * @phpstan-var array<int, array{UserToRakLibThreadSessionMessageReceiver, SessionInterface}> |
| 37 | + */ |
| 38 | + private array $sessionMap = []; |
47 | 39 |
|
48 |
| - $encapsulated = new EncapsulatedPacket(); |
49 |
| - $encapsulated->reliability = ord($packet[$offset++]); |
| 40 | + public function __construct(InterThreadChannelReader $channel, InterThreadChannelReaderDeserializer $channelReaderDeserializer){ |
| 41 | + $this->channel = $channel; |
| 42 | + $this->channelReaderDeserializer = $channelReaderDeserializer; |
| 43 | + } |
50 | 44 |
|
51 |
| - if($needACK){ |
52 |
| - $encapsulated->identifierACK = Binary::readInt(substr($packet, $offset, 4)); |
| 45 | + /** |
| 46 | + * @phpstan-return \Generator<int, null, void, void> |
| 47 | + */ |
| 48 | + public function process(ServerInterface $server) : \Generator{ |
| 49 | + do{ |
| 50 | + $processed = false; |
| 51 | + if(($packet = $this->channel->read()) !== null){ |
| 52 | + $id = ord($packet[0]); |
| 53 | + $offset = 1; |
| 54 | + if($id === ITCProtocol::PACKET_RAW){ |
| 55 | + $len = ord($packet[$offset++]); |
| 56 | + $address = substr($packet, $offset, $len); |
| 57 | + $offset += $len; |
| 58 | + $port = Binary::readShort(substr($packet, $offset, 2)); |
| 59 | + $offset += 2; |
| 60 | + $payload = substr($packet, $offset); |
| 61 | + $server->sendRaw($address, $port, $payload); |
| 62 | + }elseif($id === ITCProtocol::PACKET_SET_NAME){ |
| 63 | + $server->setName(substr($packet, $offset)); |
| 64 | + }elseif($id === ITCProtocol::PACKET_ENABLE_PORT_CHECK){ |
| 65 | + $server->setPortCheck(true); |
| 66 | + }elseif($id === ITCProtocol::PACKET_DISABLE_PORT_CHECK){ |
| 67 | + $server->setPortCheck(false); |
| 68 | + }elseif($id === ITCProtocol::PACKET_SET_PACKETS_PER_TICK_LIMIT){ |
| 69 | + $limit = Binary::readLong(substr($packet, $offset, 8)); |
| 70 | + $server->setPacketsPerTickLimit($limit); |
| 71 | + }elseif($id === ITCProtocol::PACKET_BLOCK_ADDRESS){ |
| 72 | + $len = ord($packet[$offset++]); |
| 73 | + $address = substr($packet, $offset, $len); |
| 74 | + $offset += $len; |
| 75 | + $timeout = Binary::readInt(substr($packet, $offset, 4)); |
| 76 | + $server->blockAddress($address, $timeout); |
| 77 | + }elseif($id === ITCProtocol::PACKET_UNBLOCK_ADDRESS){ |
| 78 | + $len = ord($packet[$offset++]); |
| 79 | + $address = substr($packet, $offset, $len); |
| 80 | + $server->unblockAddress($address); |
| 81 | + }elseif($id === ITCProtocol::PACKET_RAW_FILTER){ |
| 82 | + $pattern = substr($packet, $offset); |
| 83 | + $server->addRawPacketFilter($pattern); |
| 84 | + }elseif($id === ITCProtocol::PACKET_OPEN_SESSION_RESPONSE){ |
| 85 | + $sessionId = Binary::readInt(substr($packet, $offset, 4)); |
53 | 86 | $offset += 4;
|
| 87 | + $session = $server->getSession($sessionId); |
| 88 | + if($session !== null){ |
| 89 | + $channelInfo = substr($packet, $offset); |
| 90 | + $channel = $this->channelReaderDeserializer->deserialize($channelInfo); |
| 91 | + if($channel !== null){ |
| 92 | + $this->sessionMap[$sessionId] = [new UserToRakLibThreadSessionMessageReceiver($channel), $session]; |
| 93 | + } |
| 94 | + } |
54 | 95 | }
|
55 | 96 |
|
56 |
| - if(PacketReliability::isSequencedOrOrdered($encapsulated->reliability)){ |
57 |
| - $encapsulated->orderChannel = ord($packet[$offset++]); |
58 |
| - } |
59 |
| - |
60 |
| - $encapsulated->buffer = substr($packet, $offset); |
61 |
| - $server->sendEncapsulated($sessionId, $encapsulated, $immediate); |
62 |
| - }elseif($id === ITCProtocol::PACKET_RAW){ |
63 |
| - $len = ord($packet[$offset++]); |
64 |
| - $address = substr($packet, $offset, $len); |
65 |
| - $offset += $len; |
66 |
| - $port = Binary::readShort(substr($packet, $offset, 2)); |
67 |
| - $offset += 2; |
68 |
| - $payload = substr($packet, $offset); |
69 |
| - $server->sendRaw($address, $port, $payload); |
70 |
| - }elseif($id === ITCProtocol::PACKET_CLOSE_SESSION){ |
71 |
| - $sessionId = Binary::readInt(substr($packet, $offset, 4)); |
72 |
| - $server->closeSession($sessionId); |
73 |
| - }elseif($id === ITCProtocol::PACKET_SET_NAME){ |
74 |
| - $server->setName(substr($packet, $offset)); |
75 |
| - }elseif($id === ITCProtocol::PACKET_ENABLE_PORT_CHECK){ |
76 |
| - $server->setPortCheck(true); |
77 |
| - }elseif($id === ITCProtocol::PACKET_DISABLE_PORT_CHECK){ |
78 |
| - $server->setPortCheck(false); |
79 |
| - }elseif($id === ITCProtocol::PACKET_SET_PACKETS_PER_TICK_LIMIT){ |
80 |
| - $limit = Binary::readLong(substr($packet, $offset, 8)); |
81 |
| - $server->setPacketsPerTickLimit($limit); |
82 |
| - }elseif($id === ITCProtocol::PACKET_BLOCK_ADDRESS){ |
83 |
| - $len = ord($packet[$offset++]); |
84 |
| - $address = substr($packet, $offset, $len); |
85 |
| - $offset += $len; |
86 |
| - $timeout = Binary::readInt(substr($packet, $offset, 4)); |
87 |
| - $server->blockAddress($address, $timeout); |
88 |
| - }elseif($id === ITCProtocol::PACKET_UNBLOCK_ADDRESS){ |
89 |
| - $len = ord($packet[$offset++]); |
90 |
| - $address = substr($packet, $offset, $len); |
91 |
| - $server->unblockAddress($address); |
92 |
| - }elseif($id === ITCProtocol::PACKET_RAW_FILTER){ |
93 |
| - $pattern = substr($packet, $offset); |
94 |
| - $server->addRawPacketFilter($pattern); |
| 97 | + $processed = true; |
| 98 | + yield; |
95 | 99 | }
|
96 | 100 |
|
97 |
| - return true; |
98 |
| - } |
99 |
| - |
100 |
| - return false; |
| 101 | + foreach($this->sessionMap as $sessionId => [$receiver, $session]){ |
| 102 | + try{ |
| 103 | + if($receiver->process($session)){ |
| 104 | + $processed = true; |
| 105 | + yield; |
| 106 | + } |
| 107 | + }finally{ |
| 108 | + if($receiver->isClosed()){ |
| 109 | + unset($this->sessionMap[$sessionId]); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + }while($processed); |
101 | 114 | }
|
102 | 115 | }
|
0 commit comments