public async Task <RakNetPacket> ReceivePacketAsync() { UdpReceiveResult result = await ReceiveAsync(); byte[] buffer = result.Buffer; RakNetPacket packet = PacketIdentifier.GetPacketFormId(buffer[0]); packet.SetBuffer(buffer); packet.EndPoint = result.RemoteEndPoint; DownloadBytes += (uint)result.Buffer.Length; try { packet.DecodeHeader(); packet.DecodePayload(); } catch (Exception e) { throw new PacketDecodeException(e.Message, e); } new SocketPacketReceiveEventArgs(this, packet, DownloadBytes) .Invoke(this, ReceivePacketEvent); return(packet); }
public RakNetPacket ReceivePacket() { IPEndPoint endPoint = null; byte[] buffer = Receive(ref endPoint); RakNetPacket packet = PacketIdentifier.GetPacketFormId(buffer[0]); packet.SetBuffer(buffer); packet.EndPoint = endPoint; DownloadBytes += (uint)buffer.Length; try { packet.DecodeHeader(); packet.DecodePayload(); } catch (Exception e) { throw new PacketDecodeException(e.Message, e); } new SocketPacketReceiveEventArgs(this, packet, DownloadBytes) .Invoke(this, ReceivePacketEvent); return(packet); }
public RakNetPacket GetRakNetPacket(int msgId, byte[] buffer = null) { RakNetPacket pk = null; MineNET_Registries.RakNetPacket.TryGetValue(msgId, out pk); pk = pk?.Clone(); if (pk != null && buffer != null) { pk.SetBuffer(buffer); pk.Decode(); } return(pk); }
public void HandleConnectedPacket(EncapsulatedPacket packet) { byte[] buffer = packet.Payload; RakNetPacket pk = Server.Socket.PacketIdentifier.GetPacketFormId(buffer[0]); pk.EndPoint = PeerEndPoint; pk.SetBuffer(buffer); pk.DecodeHeader(); pk.DecodePayload(); if (pk is ConnectionRequest connectionRequest && State == RakNetPeerState.Connected) { ConnectionRequestAccepted accepted = new ConnectionRequestAccepted(); accepted.PeerAddress = PeerEndPoint; accepted.ClientTimestamp = connectionRequest.Timestamp; accepted.ServerTimestamp = TimeSpan.FromMilliseconds(Environment.TickCount); accepted.EndPoint = PeerEndPoint; SendEncapsulatedPacket(accepted, Reliability.Unreliable, 0); State = RakNetPeerState.Handshaking; }