Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.71 KB | None | 0 0
  1. using HoneycombServer.Utility;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Text;
  6.  
  7. namespace HoneycombServer.Networking.Packets
  8. {
  9.     /// <summary>
  10.     /// 0x00: Handshake Packet (Serverbound)
  11.     ///
  12.     /// This causes the server to switch into the target state.
  13.     /// </summary>
  14.     class HandshakePacket_C2S : Packet
  15.     {
  16.         internal struct PacketFields
  17.         {
  18.             public int protocolVersion;       // VarInt
  19.             public string serverAddress;      // Length prefixed string
  20.             public ushort serverPort;         // ushort
  21.             public ConnectionState nextState; // VarInt enum
  22.         }
  23.  
  24.         private PacketFields _fields;
  25.         public PacketFields Fields => _fields;
  26.  
  27.         public override void ReadPacketData(byte[] data)
  28.         {
  29.             var stream = new MemoryStream(data);
  30.             var reader = new BinaryReader(stream);
  31.  
  32.             // Read packet fields (protocol version, address)
  33.             int protocolVersion = reader.ReadVarInt();
  34.  
  35.             int addressLength = reader.ReadVarInt();
  36.             var address = new byte[addressLength];
  37.             int read = stream.Read(address, 0, addressLength);
  38.  
  39.             // Read the port as a unsigned short (UInt16)
  40.             ushort port = reader.ReadUInt16();
  41.             int nextState = reader.ReadVarInt();
  42.  
  43.             // Store the packet data in the Data property
  44.             _fields = new PacketFields
  45.             {
  46.                 protocolVersion = protocolVersion,
  47.                 serverAddress = address.ToString(),
  48.                 serverPort = port,
  49.                 nextState = (ConnectionState)nextState
  50.             };
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement