Exemplo n.º 1
0
 private bool ShouldStopAsync(UpdateArgs ua)
 {
     Logger.WriteLine(ua.ToString());
     return(false);
 }
Exemplo n.º 2
0
        private unsafe void Listen(
            UdpClient cl,
            PacketRecieved packet_recieved_sync,
            ShouldCancel should_cancel_async)
        {
            DateTime start           = DateTime.Now;
            int      elapsed_seconds = 0;

            int corrects     = 0;
            int incorrects   = 0;
            int timeouts     = 0;
            int socketerrors = 0;

            while (true)
            {
                try
                {
                    IPEndPoint iep  = null;
                    var        data = cl.Receive(ref iep);

                    if (data.Length == Packet.Raw._Size)
                    {
                        Packet p;
                        bool   valid;

                        fixed(byte *b = data)
                        p = new Packet(b, out valid);

                        if (valid)
                        {
                            corrects++;

                            if (packet_recieved_sync != null)
                            {
                                packet_recieved_sync(p, iep);
                            }
                        }
                        else
                        {
                            incorrects--;
                        }
                    }
                    else
                    {
                        incorrects++;
                    }
                }
                catch (TimeoutException)
                {
                    timeouts++;
                }
                catch (SocketException)
                {
                    socketerrors++;
                }
                catch (Exception e)
                {
                    System.Diagnostics.Debug.WriteLine(this.GetType().FullName + ": " + e.ToString());
                    incorrects++;
                }

                var now = DateTime.Now;
                if ((now - start).TotalSeconds > elapsed_seconds)
                {
                    elapsed_seconds++;

                    var st = new UpdateArgs();

                    st.SocketErrors               = timeouts;
                    st.PacketsReceivedCorrectly   = corrects;
                    st.PacketsReceivedIncorrectly = incorrects;
                    st.SocketErrors               = socketerrors;

                    timeouts     = 0;
                    corrects     = 0;
                    incorrects   = 0;
                    socketerrors = 0;

                    if (should_cancel_async != null)
                    {
                        if (should_cancel_async(st))
                        {
                            return;
                        }
                    }
                }
            }
        }