コード例 #1
0
        private async Task <bool> OnPacketReceived()
        {
            BinaryDataReadEventHandler h = this.OnReceived;

            if (!(h is null))
            {
                try
                {
                    return(await h(this, this.packetBuffer, 0, this.packetSize));
                }
                catch (Exception ex)
                {
                    Log.Critical(ex);
                }
            }

            return(true);
        }
コード例 #2
0
        internal void UdpDatagramReceived(object _, UdpDatagramEventArgs e)
        {
            if (this.encapsulatePackets)
            {
                LinkedList <KeyValuePair <ushort, byte[]> > LostPackets = null;
                byte[] FirstPacket   = null;
                ushort FirstPacketNr = 0;
                ushort PacketNr;
                byte[] Packet;
                byte[] Data = e.Data;
                int    Len  = Data.Length;
                int    Pos  = 0;
                int    PacketLen;
                int    Offset;
                byte   b;

                lock (this.udpReceiveLock)
                {
                    while (Pos < Len)
                    {
                        b         = Data[Pos++];
                        PacketLen = (b & 127);
                        Offset    = 7;
                        while (Pos < Len && (b & 128) != 0)
                        {
                            b          = Data[Pos++];
                            PacketLen |= (b & 127) << Offset;
                            Offset    += 7;
                        }

                        if (Pos + 2 > Len)
                        {
                            break;
                        }

                        PacketNr  = Data[Pos++];
                        PacketNr |= (ushort)(Data[Pos++] << 8);

                        if (Pos + PacketLen > Len)
                        {
                            break;
                        }

                        Packet = new byte[PacketLen];
                        Array.Copy(Data, Pos, Packet, 0, PacketLen);
                        Pos += PacketLen;

                        if ((short)(PacketNr - this.lastReceivedPacket) > 0)
                        {
                            if (FirstPacket is null)
                            {
                                FirstPacket   = Packet;
                                FirstPacketNr = PacketNr;
                            }
                            else
                            {
                                if (LostPackets is null)
                                {
                                    LostPackets = new LinkedList <KeyValuePair <ushort, byte[]> >();
                                }

                                LostPackets.AddFirst(new KeyValuePair <ushort, byte[]>(PacketNr, Packet));                                  // Reverse order
                            }
                        }
                    }

                    if (FirstPacket != null)
                    {
                        this.lastReceivedPacket = FirstPacketNr;
                    }
                }

                BinaryDataReadEventHandler h = this.OnReceived;
                if (h != null)
                {
                    if (LostPackets != null)
                    {
                        foreach (KeyValuePair <ushort, byte[]> P in LostPackets)
                        {
                            try
                            {
                                h(this, P.Value, 0, P.Value.Length);
                            }
                            catch (Exception ex)
                            {
                                Events.Log.Critical(ex);
                            }
                        }
                    }

                    if (FirstPacket != null)
                    {
                        try
                        {
                            h(this, FirstPacket, 0, FirstPacket.Length);
                        }
                        catch (Exception ex)
                        {
                            Events.Log.Critical(ex);
                        }
                    }
                }
            }
            else
            {
                byte[] Data   = e.Data;
                int    Len    = Data.Length;
                byte[] Packet = new byte[Len];

                Array.Copy(Data, 0, Packet, 0, Len);

                BinaryDataReadEventHandler h = this.OnReceived;
                if (h != null)
                {
                    try
                    {
                        h(this, Packet, 0, Packet.Length);
                    }
                    catch (Exception ex)
                    {
                        Events.Log.Critical(ex);
                    }
                }
            }
        }