PocketMine-MP 5.35.1 git-e32e836dad793a3a3c8ddd8927c00e112b1e576a
Loading...
Searching...
No Matches
LoginPacket.php
1<?php
2
3/*
4 * This file is part of BedrockProtocol.
5 * Copyright (C) 2014-2022 PocketMine Team <https://github.com/pmmp/BedrockProtocol>
6 *
7 * BedrockProtocol is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 */
12
13declare(strict_types=1);
14
15namespace pocketmine\network\mcpe\protocol;
16
17use pmmp\encoding\BE;
18use pmmp\encoding\ByteBufferReader;
19use pmmp\encoding\ByteBufferWriter;
20use pmmp\encoding\LE;
22use function strlen;
23
24class LoginPacket extends DataPacket implements ServerboundPacket{
25 public const NETWORK_ID = ProtocolInfo::LOGIN_PACKET;
26
27 public int $protocol;
28 public string $authInfoJson;
29 public string $clientDataJwt;
30
34 public static function create(int $protocol, string $authInfoJson, string $clientDataJwt) : self{
35 $result = new self;
36 $result->protocol = $protocol;
37 $result->authInfoJson = $authInfoJson;
38 $result->clientDataJwt = $clientDataJwt;
39 return $result;
40 }
41
42 public function canBeSentBeforeLogin() : bool{
43 return true;
44 }
45
46 protected function decodePayload(ByteBufferReader $in) : void{
47 $this->protocol = BE::readUnsignedInt($in);
48 $this->decodeConnectionRequest(CommonTypes::getString($in));
49 }
50
51 protected function decodeConnectionRequest(string $binary) : void{
52 $connRequestReader = new ByteBufferReader($binary);
53
54 $authInfoJsonLength = LE::readUnsignedInt($connRequestReader);
55 $this->authInfoJson = $connRequestReader->readByteArray($authInfoJsonLength);
56
57 $clientDataJwtLength = LE::readUnsignedInt($connRequestReader);
58 $this->clientDataJwt = $connRequestReader->readByteArray($clientDataJwtLength);
59 }
60
61 protected function encodePayload(ByteBufferWriter $out) : void{
62 BE::writeUnsignedInt($out, $this->protocol);
63 CommonTypes::putString($out, $this->encodeConnectionRequest());
64 }
65
66 protected function encodeConnectionRequest() : string{
67 $connRequestWriter = new ByteBufferWriter();
68
69 LE::writeUnsignedInt($connRequestWriter, strlen($this->authInfoJson));
70 $connRequestWriter->writeByteArray($this->authInfoJson);
71
72 LE::writeUnsignedInt($connRequestWriter, strlen($this->clientDataJwt));
73 $connRequestWriter->writeByteArray($this->clientDataJwt);
74
75 return $connRequestWriter->getData();
76 }
77
78 public function handle(PacketHandlerInterface $handler) : bool{
79 return $handler->handleLogin($this);
80 }
81}
handle(PacketHandlerInterface $handler)
static create(int $protocol, string $authInfoJson, string $clientDataJwt)