Exemplo n.º 1
0
        public void RetrievePackets()
        {
            packetBufferMutex.WaitOne();
            try
            {
                while (tcp.Available > 0)
                {
                    timeout = 0;
                    packetBuffer.Add(ReadPacket());
                }
            }
            catch (ObjectDisposedException e)
            {
                Close();
                onDisconnect?.Invoke(this, e.Message);
            }

            if (onPacketReceived != null)
            {
                var packets = packetBuffer.ToArray();
                for (int i = 0; i < packets.Length; i++)
                {
                    Packet packet  = packets[i];
                    int    discard = 2;

                    foreach (OnPacketReceived invocation in onPacketReceived.GetInvocationList())
                    {
                        discard = Math.Min(discard, (int)invocation(this, packet));
                    }

                    if (discard == 1)
                    {
                        packetBuffer.Remove(packet);
                    }
                }
            }
            packetBufferMutex.ReleaseMutex();
        }