PocketMine-MP 5.21.1 git-2ff647079265e7c600203af4fd902b15e99d49a4
ServerSession.php
1<?php
2
3/*
4 * This file is part of RakLib.
5 * Copyright (C) 2014-2022 PocketMine Team <https://github.com/pmmp/RakLib>
6 *
7 * RakLib is not affiliated with Jenkins Software LLC nor RakNet.
8 *
9 * RakLib 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
15declare(strict_types=1);
16
17namespace raklib\server;
18
28use function ord;
29
30class ServerSession extends Session{
31 public const DEFAULT_MAX_SPLIT_PART_COUNT = 128;
32 public const DEFAULT_MAX_CONCURRENT_SPLIT_COUNT = 4;
33
34 private Server $server;
35 private int $internalId;
36
37 public function __construct(
38 Server $server,
39 \Logger $logger,
40 InternetAddress $address,
41 int $clientId,
42 int $mtuSize,
43 int $internalId,
44 int $recvMaxSplitParts = self::DEFAULT_MAX_SPLIT_PART_COUNT,
45 int $recvMaxConcurrentSplits = self::DEFAULT_MAX_CONCURRENT_SPLIT_COUNT
46 ){
47 $this->server = $server;
48 $this->internalId = $internalId;
49 parent::__construct($logger, $address, $clientId, $mtuSize, $recvMaxSplitParts, $recvMaxConcurrentSplits);
50 }
51
55 public function getInternalId() : int{
56 return $this->internalId;
57 }
58
59 final protected function sendPacket(Packet $packet) : void{
60 $this->server->sendPacket($packet, $this->address);
61 }
62
63 protected function onPacketAck(int $identifierACK) : void{
64 $this->server->getEventListener()->onPacketAck($this->internalId, $identifierACK);
65 }
66
67 protected function onDisconnect(int $reason) : void{
68 $this->server->getEventListener()->onClientDisconnect($this->internalId, $reason);
69 }
70
71 final protected function handleRakNetConnectionPacket(string $packet) : void{
72 $id = ord($packet[0]);
73 if($id === MessageIdentifiers::ID_CONNECTION_REQUEST){
74 $dataPacket = new ConnectionRequest();
75 $dataPacket->decode(new PacketSerializer($packet));
76 $this->queueConnectedPacket(ConnectionRequestAccepted::create(
77 $this->address,
78 [],
79 $dataPacket->sendPingTime,
80 $this->getRakNetTimeMS()
81 ), PacketReliability::UNRELIABLE, 0, true);
82 }elseif($id === MessageIdentifiers::ID_NEW_INCOMING_CONNECTION){
83 $dataPacket = new NewIncomingConnection();
84 $dataPacket->decode(new PacketSerializer($packet));
85
86 if($dataPacket->address->getPort() === $this->server->getPort() or !$this->server->portChecking){
87 $this->state = self::STATE_CONNECTED; //FINALLY!
88 $this->server->openSession($this);
89
90 //$this->handlePong($dataPacket->sendPingTime, $dataPacket->sendPongTime); //can't use this due to system-address count issues in MCPE >.<
91 $this->sendPing();
92 }
93 }
94 }
95
96 protected function onPacketReceive(string $packet) : void{
97 $this->server->getEventListener()->onPacketReceive($this->internalId, $packet);
98 }
99
100 protected function onPingMeasure(int $pingMS) : void{
101 $this->server->getEventListener()->onPingMeasure($this->internalId, $pingMS);
102 }
103}
onPacketAck(int $identifierACK)
handleRakNetConnectionPacket(string $packet)
onPacketReceive(string $packet)