コード例 #1
0
        /// <summary>
        /// The background worker. Will exit after 10 consectutive errors
        /// </summary>
        private void DoWork()
        {
            //number of errors
            int nume = 0;
            while (isrunning)
            {
                try
                {
                    IPEndPoint RIPend = null;
                    //Recieve the data
                    while (cRIOConnection.Available < 1018 && isrunning) { Thread.Sleep(2); }
                    if (!isrunning)
                    {
                        cRIOConnection.Close();
                        return;
                    }
                    byte[] buffer = cRIOConnection.Receive(ref RIPend);

                    //Check first
                    if (!VerifyPacket(buffer))
                    {
                        if (InvalidPacketRecieved != null && !peeking)
                        {
                            //Create our e
                            InvalidPacketRecievedEventArgs e = new InvalidPacketRecievedEventArgs(buffer, ddh.InvalidPacketAction == InvalidPacketActions.AlwaysContinue || ddh.InvalidPacketAction == InvalidPacketActions.Continue);
                            InvalidPacketRecieved(this, e);
                            if ((int)ddh.InvalidPacketAction < 3)//1-4
                            {
                                if (!e.ContinueAnyway)
                                    break;
                            }
                            else if (ddh.InvalidPacketAction == InvalidPacketActions.AlwaysIgnore)
                                break;
                        }
                    }

                    //DS Status
                    cStat = ParseDSBytes(buffer);

                    //Get a stream
                    MemoryStream Output = new MemoryStream(buffer);
                    Output.Seek(27, SeekOrigin.Begin);
                    EBinaryReader binladen = new EBinaryReader(Output);

                    //get length
                    int totallength = (binladen.ReadUInt16() << 16);
                    totallength += binladen.ReadUInt16();

                    //clear last loop's controls
                    kys = new ZomBDataLookup();

                    //loop all controls
                    while (totallength > (Output.Position - 27))
                    {
                        byte b = binladen.ReadByte();
                        switch (b)
                        {
                            case 0://Announce
                                {
                                    int id = binladen.ReadByte();
                                    SmartDataTypes type = (SmartDataTypes)binladen.ReadByte();
                                    int len = binladen.ReadUInt16();
                                    byte[] name = new byte[len];
                                    binladen.Read(name, 0, len);
                                    if (peeking)
                                    {
                                        dp.Invoke(cb, Encoding.UTF8.GetString(name));
                                        nametable[id] = new SmartInfo { Name = Encoding.UTF8.GetString(name), Type = type };
                                    }
                                    else
                                    {
                                        nametable[id] = new SmartInfo { Name = Encoding.UTF8.GetString(name), Type = type };
                                    }
                                    break;
                                }
                            case 1://Update
                                {
                                    int id = binladen.ReadByte();
                                    if (nametable.ContainsKey(id))
                                    {
                                        var info = nametable[id];
                                        string value = info.Parse(binladen);
                                        kys[info.Name] = new ZomBDataObject(value, info.ZomBTypeHint);
                                    }
                                    else
                                    {
                                        b = binladen.ReadByte();//get length
                                        binladen.Read(new byte[b], 0, b);//skip it
                                    }
                                    break;
                                }
                            case 2://GUI Announce, not impl
                                {
                                    System.Windows.Forms.MessageBox.Show("Dashboard selection via Protocol has not been implemented in ZomB, try WPF tabs instead");
                                    break;
                                }
                            default:
                                throw new Exception("Bin ("+b+") not a 1 or zero, exiting");
                        }
                    }

                    if (kys.Count > 0)//Empty keys in empty packet, "Smart" Dashboard can be dumb
                        //All in favor of chaging the name to DumbDashboard say "aye!" "sqrt(-1)!"
                    {
                        //Fire events
                        if (DataRecieved != null)
                            DataRecieved(this, new EventArgs());
                        if (NewStatusRecieved != null)
                            NewStatusRecieved(this, new NewStatusRecievedEventArgs(cStat));
                        if (NewDataRecieved != null)
                            NewDataRecieved(this, new NewDataRecievedEventArgs(kys));
                    }

                    if (nume > 0)
                        nume--;
                }
                catch (ThreadAbortException)
                {
                    isrunning = false;
                    cRIOConnection.Close();
                    return;
                }
                catch (Exception ex)
                {
                    nume++;
                    DoError(ex);
                    if (nume > 10)
                    {
                        isrunning = false;
                        DoError(new Exception("10 consecutive errors were encountered, stopping Smart"));
                        isrunning = false;
                        try
                        {
                            cRIOConnection.Close();
                        }
                        catch { }
                        return;
                    }
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// The background worker. Will exit after 10 consectutive errors
        /// </summary>
        private void DoWork()
        {
            //number of errors
            int nume = 0;
            while (isrunning)
            {
                try
                {
                    IPEndPoint RIPend = null;
                    //Recieve the data
                    while (cRIOConnection.Available < 1018 && isrunning) { Thread.Sleep(2); }
                    if (!isrunning)
                    {
                        cRIOConnection.Close();
                        return;
                    }
                    byte[] buffer = cRIOConnection.Receive(ref RIPend);
                    string Output;

                    //Convert
                    Output = UTF8Encoding.UTF8.GetString(buffer);

                    //Find segment of data
                    if (Output.Contains("@@ZomB:|") && Output.Contains("|:ZomB@@") && !Output.Contains("@@ZomB:|:ZomB@@"))
                    {
                        Output = Output.Substring(Output.IndexOf("@@ZomB:|") + 8, (Output.IndexOf("|:ZomB@@") - (Output.IndexOf("@@ZomB:|") + 8)));
                        if (Output != "")
                        {
                            //Check first
                            if (!VerifyPacket(buffer))
                            {
                                if (InvalidPacketRecieved != null && !peeking)
                                {
                                    //Create our e
                                    InvalidPacketRecievedEventArgs e = new InvalidPacketRecievedEventArgs(buffer, ddh.InvalidPacketAction == InvalidPacketActions.AlwaysContinue || ddh.InvalidPacketAction == InvalidPacketActions.Continue);
                                    InvalidPacketRecieved(this, e);
                                    if ((int)ddh.InvalidPacketAction < 3)//1-4
                                    {
                                        if (!e.ContinueAnyway)
                                            break;
                                    }
                                    else if (ddh.InvalidPacketAction == InvalidPacketActions.AlwaysIgnore)
                                        break;
                                }
                            }

                            //Get the items in a dictionary
                            ZomBDataLookup vals = SplitParams(Output);
                            if (peeking)
                            {
                                foreach (var keys in vals)
                                {
                                    dp.Invoke(cb, keys.Key);
                                }
                            }
                            else
                            {
                                FRCDSStatus status = ParseDSBytes(buffer);
                                cStat = status;
                                kys = vals;

                                //Fire events
                                if (DataRecieved != null)
                                    DataRecieved(this, new EventArgs());
                                if (NewStatusRecieved != null)
                                    NewStatusRecieved(this, new NewStatusRecievedEventArgs(status));
                                if (NewDataRecieved != null)
                                    NewDataRecieved(this, new NewDataRecievedEventArgs(vals));
                            }
                        }
                    }
                    if (nume > 0)
                        nume--;
                }
                catch (ThreadAbortException)
                {
                    isrunning = false;
                    cRIOConnection.Close();
                    return;
                }
                catch (Exception ex)
                {
                    nume++;
                    DoError(ex);
                    if (nume > 10)
                    {
                        isrunning = false;
                        DoError(new Exception("10 consecutive errors were encountered, stopping DashboardDataHub"));
                        isrunning = false;
                        try
                        {
                            cRIOConnection.Close();
                        }
                        catch { }
                        return;
                    }
                }
            }
        }