public override byte[] Serialize() { var encoder = new RakEncoder(); encoder.Encode(Flags); if (IsACKorNAK) { encoder.Encode(Count); encoder.Encode(MinEqualsMax); encoder.Encode(PacketMin); if (!MinEqualsMax) { encoder.Encode(PacketMax); } } else { encoder.Encode(SequenceNumber); foreach (var enclosing in Enclosing) { if (enclosing != null) { encoder.Encode(RakPacket.Create(enclosing)); } } } return(encoder.Get()); }
public RakNetPacket(byte[] data) { Type = PacketType.RakNetPacket; var decoder = new RakDecoder(data); Flags = decoder.Byte(); if (IsACKorNAK) { Enclosing = new List <RakPacket>(); Count = decoder.Short(); MinEqualsMax = decoder.Byte() != 0; if (MinEqualsMax) { PacketMin = decoder.Triad(); PacketMax = PacketMin; return; } PacketMax = decoder.Triad(); } else { SequenceNumber = decoder.Triad(); Enclosing = new List <RakPacket>(); while (!decoder.AtEnd) { Enclosing.Add(RakPacket.Parse(ref decoder)); } } }
public static byte[] Create(RakPacket packet) { var encoder = new RakEncoder(); var serialized = packet.Serialize(); encoder.Encode(packet.MessageFlags); encoder.Encode((short)((serialized.Length + 1) * 8)); if ((packet.MessageFlags & IS_RELIABLE) != 0) { encoder.Encode(packet.ReliableNum); } if ((packet.MessageFlags & IS_ORDERED) != 0) { encoder.Encode(packet.OrderingIndex); encoder.Encode(packet.OrderingChannel); } encoder.Encode((byte)packet.MessageID); encoder.Encode(serialized); return(encoder.Get()); }
// RakPacket should always be parsed from RakNetPacket // which provides the RakDecoder as there can be multiple. public static RakPacket Parse(ref RakDecoder decoder) { var flags = decoder.Byte(); var length = decoder.Short(); var relnum = RakTriad.FromInt(0, false); var orderidx = RakTriad.FromInt(0, false); byte orderch = 0; if ((flags & IS_RELIABLE) != 0) // reliable { relnum = decoder.Triad(); } if ((flags & IS_ORDERED) != 0) // ordered { orderidx = decoder.Triad(); orderch = decoder.Byte(); } var msgid = (RakPacketType)decoder.Byte(); var packet = new RakPacket(); switch (msgid) { case RakPacketType.ConnectedPing: packet = new ConnectedPingPacket(ref decoder); break; case RakPacketType.ConnectionRequest: packet = new ConnectionRequestPacket(ref decoder); break; case RakPacketType.NewIncomingConnection: packet = new NewIncomingConnectionPacket(ref decoder); break; case RakPacketType.PlayerDisconnect: break; case RakPacketType.LoginRequest: packet = new LoginRequestPacket(ref decoder); break; case RakPacketType.Ready: packet = new ReadyPacket(ref decoder); break; case RakPacketType.RequestChunk: packet = new RequestChunkPacket(ref decoder); break; case RakPacketType.MovePlayer: packet = new MovePlayerPacket(ref decoder); break; case RakPacketType.SetHealth: packet = new SetHealthPacket(ref decoder); break; case RakPacketType.Animate: packet = new AnimatePacket(ref decoder); break; case RakPacketType.RemoveBlock: packet = new RemoveBlockPacket(ref decoder); break; case RakPacketType.Message: packet = new MessagePacket(ref decoder); break; case RakPacketType.UseItem: packet = new UseItemPacket(ref decoder); break; case RakPacketType.Interact: packet = new InteractPacket(ref decoder); break; default: Console.WriteLine($"[!!] Unhandled RakPacket Type {msgid}!"); break; } packet.MessageFlags = flags; packet.Length = length; packet.ReliableNum = relnum; packet.OrderingIndex = orderidx; packet.OrderingChannel = orderch; packet.MessageID = msgid; return(packet); }