(Advanced) Packets and how they work - bdsx/bdsx GitHub Wiki
Written by: Hanprogramer
Disclaimer: This wiki page is a tutorial for beginners on networking like me. Before reading: Packet Event wiki
What are packets? Packets are data that is received and sent by and from the server and/or clients.
How are packets stored? Packets are composed of pointers, which point to a place(address) in your computer's memory. They are stored in a hex value. There are many tools online to convert hexadecimal to regular numbers(decimal). Other than that, a hexadecimal value can store more than one value.
Below is an example of parsing a raw event CraftingEvent. Below we can see the usage of pointer(ptr).
// referenced from https://github.com/pmmp/PocketMine-MP/blob/stable/src/pocketmine/network/mcpe/protocol/CraftingEventPacket.php
events.packetRaw(PacketId.CraftingEvent).on((ptr, size, ni)=>{
console.log(`Packet Id: ${ptr.readUint8()}`);
const windowId = ptr.readUint8();
const type = ptr.readVarInt();
const uuid1 = ptr.readUint32();
const uuid2 = ptr.readUint32();
const uuid3 = ptr.readUint32();
const uuid4 = ptr.readUint32();
console.log(`crafting: ${windowId} ${type} ${uuid1} ${uuid2} ${uuid3} ${uuid4}`);
const size1 = ptr.readVarUint();
// need to parse more
});
To send packets:
To send packets, you'll need a NetworkIdentifier and make a packet to send. Then send it from Packet.sendTo(NetworkIdentifier). There are a lot of packets type available.
function sendMessage(message: string, netid: NetworkIdentifier):void
{
var tp: TextPacket = TextPacket.allocate();
tp.name = "Server";
tp.message = message;
netid.sendPacket(network, tp);
tp.dispose();
}
Below is an example of the inner structure of a DisconnectPacket:
@nativeClass(null)
export class DisconnectPacket extends Packet {
@nativeField(bool_t)
skipMessage:bool_t;
@nativeField(CxxString, 0x38)
message:CxxString;
}