Beispiel #1
0
        public void TestLeakyBucketOrder()
        {
            QueueBuffer controller = new QueueBuffer();

            controller.Enqueue(NewPacket(1, 0));
            controller.Enqueue(NewPacket(2, 0));
            controller.Enqueue(NewPacket(3, 0));
            AssertPacket(controller.Dequeue(), 1, 0);
            AssertPacket(controller.Dequeue(), 2, 0);
            AssertPacket(controller.Dequeue(), 3, 0);
            Assert.AreEqual(controller.Dequeue(), null);
        }
Beispiel #2
0
        public void TestLeakyBucketEmpty()
        {
            QueueBuffer controller = new QueueBuffer();

            Assert.AreEqual(controller.Peek(), null);
            Assert.AreEqual(controller.Dequeue(), null);

            controller.Enqueue(NewPacket(10, 0));
            AssertPacket(controller.Peek(), 10, 0);
            AssertPacket(controller.Dequeue(), 10, 0);
            Assert.AreEqual(controller.Peek(), null);
            Assert.AreEqual(controller.Dequeue(), null);
        }
        protected TreeViewItem CreateItem()
        {
            TreeViewItem it = queue.Dequeue();

            if (it != null)
            {
                it.target.activeSelf = true;
                return(it);
            }
            ModelElement mod = new ModelElement();

            mod.Load(ItemMod.ModData);
            mod.SetParent(View);
            mod.data.localPosition = new Vector3(10000, 10000);
            mod.data.localScale    = Vector3.one;
            mod.IsChanged          = true;
            TreeViewItem a = new TreeViewItem();

            a.target         = mod;
            a.text           = mod.GetComponent <TextElement>();
            a.callBack       = EventCallBack.RegEvent <EventCallBack>(mod);
            a.callBack.Click = (o, e) => {
                var item = o.DataContext as TreeViewItem;
                if (item.node != null)
                {
                    item.node.extand = !item.node.extand;
                    Refresh();
                }
            };
            a.callBack.DataContext = a;
            return(a);
        }
Beispiel #4
0
        protected TreeViewItem CreateItem()
        {
            TreeViewItem it = queue.Dequeue();

            if (it != null)
            {
                it.target.SetActive(true);
                return(it);
            }
            GameObject g = ModelManagerUI.LoadToGame(ItemMod, null);
            var        t = g.transform;

            t.SetParent(View);
            t.localPosition    = new Vector3(10000, 10000);
            t.localScale       = Vector3.one;
            t.localEulerAngles = Vector3.zero;
            TreeViewItem a = new TreeViewItem();

            a.target         = g;
            a.text           = g.GetComponent <Text>();
            a.callBack       = EventCallBack.RegEvent <EventCallBack>(g.transform as RectTransform);
            a.callBack.Click = (o, e) => {
                var item = o.DataContext as TreeViewItem;
                if (item.node != null)
                {
                    item.node.extand = !item.node.extand;
                    Refresh();
                }
            };
            a.callBack.DataContext = a;
            return(a);
        }
Beispiel #5
0
 void Run()
 {
     while (running)
     {
         var now = DateTime.Now.Ticks;
         try
         {
             Recive();
             int c = missions.Count;
             for (int i = 0; i < c; i++)
             {
                 var mis = missions.Dequeue();
                 if (mis != null)
                 {
                     if (mis.action != null)
                     {
                         mis.action(mis.data);
                     }
                 }
             }
         }
         catch (Exception ex)
         {
             System.Diagnostics.Debug.WriteLine(ex.StackTrace);
         }
         long t = DateTime.Now.Ticks;
         t -= now;
         t /= 10000;
         if (t < 10)
         {
             Thread.Sleep(1);
         }
     }
 }
Beispiel #6
0
        /// <summary>
        /// 创建项目实例,如果缓存中有则从缓存中提前
        /// </summary>
        /// <returns></returns>
        protected TreeViewItem CreateItem()
        {
            TreeViewItem it = queue.Dequeue();

            if (it != null)
            {
                it.target.activeSelf = true;
                return(it);
            }
            if (creator != null)
            {
                var t = creator.Create();
                t.target = HGUIManager.Clone(ItemMod, creator.initializer);
                var trans = t.target;
                trans.SetParent(Enity);
                trans.localScale    = Vector3.one;
                trans.localRotation = Quaternion.identity;
                return(t);
            }
            else
            {
                var go = HGUIManager.Clone(ItemMod);
                go.SetParent(Enity);
                go.localScale    = Vector3.one;
                go.localRotation = Quaternion.identity;
                TreeViewItem a = new TreeViewItem();
                a.target           = go;
                a.Text             = go as HText;
                a.Item             = go.RegEvent <UserEvent>();
                a.Item.Click       = DefultItemClick;
                a.Item.DataContext = a;
                return(a);
            }
        }
        public static void RecycleGameObject()
        {
            int c = RecycleQueue.Count;

            for (int j = 0; j < c; j++)
            {
                var mod = RecycleQueue.Dequeue();
                if (mod != null)
                {
                    if (mod.Context != null)
                    {
                        long type = mod.data.type;
                        for (int i = 0; i < models.Count; i++)
                        {
                            if (models[i].type == type)
                            {
                                var g = mod.Main;
                                if (models[i].ReCycle(g))
                                {
                                    g.SetActive(false);
                                    mod.Context.SetParent(CycleBuffer);
                                    mod.Context = null;
                                    mod.Main    = null;
                                }
                                break;
                            }
                        }
                    }
                }
            }
        }
Beispiel #8
0
        /// <summary>
        /// 创建项目实例,如果缓存中有则从缓存中提前
        /// </summary>
        /// <returns></returns>
        protected TreeViewItem CreateItem()
        {
            TreeViewItem it = queue.Dequeue();

            if (it != null)
            {
                it.target.SetActive(true);
                return(it);
            }
            if (creator != null)
            {
                var t = creator.Create();
                t.target = HGUIManager.GameBuffer.Clone(ItemMod, creator.initializer);
                var trans = t.target.transform;
                trans.SetParent(Enity.transform);
                trans.localScale    = Vector3.one;
                trans.localRotation = Quaternion.identity;
                return(t);
            }
            else
            {
                var go    = HGUIManager.GameBuffer.Clone(ItemMod);
                var trans = go.transform;
                trans.SetParent(Enity.transform);
                trans.localScale    = Vector3.one;
                trans.localRotation = Quaternion.identity;
                TreeViewItem a = new TreeViewItem();
                a.target           = go;
                a.Text             = go.GetComponent <HText>();
                a.Item             = a.Text.RegEvent <UserEvent>();
                a.Item.Click       = DefultItemClick;
                a.Item.DataContext = a;
                return(a);
            }
        }
Beispiel #9
0
        public static bool AwaitCompletion(IEnumerable <Task> initialTasks)
        {
            Dispatch(initialTasks);
            while (Interlocked.CompareExchange(ref activeTasks, 0, 0) > 0)
            {
                if (!Application.ProcessLogMessages())
                {
                    Thread.Sleep(TimerTickInterval);
                }

                Task task; while (pendingTasks.Dequeue(out task))
                {
                    if (!task.IsPending)
                    {
                        if (!dispatcher.Dispatch(instance, task))
                        {
                            throw new EntryPointNotFoundException();
                        }
                    }
                    else if (!pendingTasks.Enqueue(task))
                    {
                        throw new IndexOutOfRangeException();
                    }
                }
            }
            return(Interlocked.CompareExchange(ref errors, 0, 0) == 0);
        }
Beispiel #10
0
 public static void ProcessModePackets()
 {
     for (int i = 0; !ModePackets.IsEmpty() && i < NUM_PACKETS_TO_PROCESS; i++)
     {
         Packet p = ModePackets.Dequeue();
         CurDriveMode = (DriveMode)p.Data.Payload[1];
     }
 }
Beispiel #11
0
        void Run()
        {
            while (run)
            {
                try
                {
                    var m = SubMission.Dequeue();
                    if (m == null)
                    {
                        subFree = true;
                        are.WaitOne(1);
                    }
                    else
                    {
                        subFree = false;
                        try
                        {
                            m.action(m.data);
                        }
                        catch (Exception ex)
                        {
#if DEBUG
                            Debug.LogError(ex.StackTrace);
#endif
                        }
                        if (m.waitAction != null) //如果有等待的任务
                        {
                            if (m.Id == MainID)   //交给主线程
                            {
                                m.action     = m.waitAction;
                                m.waitAction = null;
                                MainMission.Enqueue(m);
                                goto label;
                            }
                            for (int i = 0; i < threads.Count; i++)
                            {
                                if (m.Id == threads[i].Id)
                                {
                                    threads[i].AddMainMission(m.waitAction, m.data);//任务交给源线程
                                    goto label;
                                }
                            }
                            m.action     = m.waitAction;
                            m.waitAction = null;
                            MainMission.Enqueue(m);//否则交给主线程
                        }
                        label :;
                    }
                }
                catch (Exception ex)
                {
#if DEBUG
                    Debug.LogError(ex.StackTrace);
#endif
                }
            }
            are.Dispose();
        }
Beispiel #12
0
        public static void ProcessBasePackets()
        {
            for (int i = 0; !DrivePackets.IsEmpty() && i < NUM_PACKETS_TO_PROCESS; i++)
            {
                Packet p = DrivePackets.Dequeue();
                switch ((PacketID)p.Data.ID)
                {
                case PacketID.RPMAllDriveMotors:
                    MotorControl.SetAllRPM((sbyte)p.Data.Payload[0]);
                    break;

                case PacketID.RPMFrontRight:
                case PacketID.RPMFrontLeft:
                case PacketID.RPMBackRight:
                case PacketID.RPMBackLeft:
                    int MotorID = p.Data.ID - (byte)PacketID.RPMFrontRight;
                    MotorControl.SetRPM(MotorID, (sbyte)p.Data.Payload[1]);
                    break;

                case PacketID.RPMSteeringMotor:
                    float SteerSpeed = UtilData.ToFloat(p.Data.Payload);
                    //MotorControl.SetSteerSpeed(SteerSpeed);
                    break;

                case PacketID.SteerPosition:
                    float Position = UtilData.ToFloat(p.Data.Payload);
                    //MotorControl.SetRackAndPinionPosition(Position);
                    break;

                case PacketID.SpeedAllDriveMotors:
                    float Speed = UtilData.ToFloat(p.Data.Payload);
                    MotorControl.SetAllSpeed(Speed);
                    break;

                case PacketID.BaseSpeed:
                case PacketID.ShoulderSpeed:
                case PacketID.ElbowSpeed:
                case PacketID.WristSpeed:
                case PacketID.DifferentialVert:
                case PacketID.DifferentialRotate:
                case PacketID.HandGrip:
                    byte address   = (byte)(p.Data.ID - 0x8A);
                    byte direction = 0x00;
                    if (p.Data.Payload[0] > 0)
                    {
                        direction = 0x01;
                    }
                    UtilCan.SpeedDir(CANBBB.CANBus0, false, 0x02, address, p.Data.Payload[1], direction);
                    break;
                }
            }
        }
Beispiel #13
0
        public override void Recive(long now)
        {
            int c = metaData.Count;

            if (c == 0)
            {
                if (now - lastTime > TimeOut)
                {
                    lastTime = now;
                    envelope.Clear();
                    if (_connect)
                    {
                        Disconnect();
                        _connect = false;
                    }
                }
            }
            else
            {
                lastTime = now;
                if (!_connect)
                {
                    ConnectionOK();
                }
                _connect = true;
            }
            for (int i = 0; i < c; i++)
            {
                var dat = metaData.Dequeue();
                if (dat != null)
                {
                    envelope.Unpack(dat, dat.Length, now);
                }
            }
            var queue = envelope.QueueBuf;

            c = queue.Count;
            try
            {
                for (int i = 0; i < c; i++)
                {
                    var dat = queue.Dequeue();
                    if (dat != null)
                    {
                        Dispatch(dat.data, dat.type);
                    }
                }
            }
            catch
            {
            }
        }
Beispiel #14
0
 public static void ProcessModePackets()
 {
     for (int i = 0; !ModePackets.IsEmpty() && i < NUM_PACKETS_TO_PROCESS; i++)
     {
         Packet p = ModePackets.Dequeue();
         CurDriveMode = (DriveMode)p.Data.Payload[0];
         //temporary fix to test, actually fix it later to get corect values form payload
         if (p.Data.Payload[0] > 0)
         {
             CurDriveMode = DriveMode.toGPS;
         }
     }
 }
Beispiel #15
0
        /// <summary>
        /// 创建一个实例,如果回收缓存中有就从缓存中提取
        /// </summary>
        /// <returns></returns>
        public DataGridItem CreateEnity()
        {
            DataGridItem it = buf.Dequeue();

            if (it != null)
            {
                it.target.SetActive(true);
                return(it);
            }
            var t = itemCreator.Create() as DataGridItem;

            t.target = HGUIManager.GameBuffer.Clone(mod, itemCreator.initializer);
            return(t);
        }
Beispiel #16
0
        public void TestLeakyBucketCapacity()
        {
            QueueBuffer controller = new QueueBuffer();
            int         capacity   = 200000;

            for (int i = 0; i < capacity; i++)
            {
                controller.Enqueue(NewPacket(10, 0));
            }
            controller.Peek();
            for (int i = 0; i < capacity; i++)
            {
                controller.Dequeue();
            }
        }
Beispiel #17
0
        public static bool ProcessLogMessages()
        {
            KeyValuePair <byte, string> message; while (logBuffer.Dequeue(out message))

            {
                switch (message.Key)
                {
                case 0: logSystem.Error(message.Value); break;

                case 1: logSystem.Warning(message.Value); break;

                default: logSystem.Log(message.Value); break;
                }
            }
            return(!string.IsNullOrWhiteSpace(message.Value));
        }
Beispiel #18
0
 void Run()
 {
     while (running)
     {
         var now = DateTime.Now.Ticks;
         try
         {
             Recive();
             int c = queue.Count;
             for (int i = 0; i < c; i++)
             {
                 var mis = queue.Dequeue();
                 if (mis != null)
                 {
                     if (mis.action != null)
                     {
                         mis.action(mis.data);
                     }
                 }
             }
         }
         catch (Exception ex)
         {
             ServerLog.Error(ex.StackTrace);
         }
         try
         {
             if (soc != null)
             {
                 SendAll(soc, now);
             }
         }
         catch
         {
         }
         long t = DateTime.Now.Ticks;
         t -= now;
         t /= 10000;
         if (t < 10)
         {
             Thread.Sleep(1);
         }
     }
 }
Beispiel #19
0
 /// <summary>
 /// 派发接收到的消息
 /// </summary>
 public void Dispatch()
 {
     if (queue != null)
     {
         int     c = queue.Count;
         SocData soc;
         for (int i = 0; i < c; i++)
         {
             soc = queue.Dequeue();
             if (soc != null)
             {
                 if (MainDispatch != null)
                 {
                     MainDispatch(soc.data, soc.tag, soc.obj as IPEndPoint);
                 }
             }
         }
     }
 }
Beispiel #20
0
        public static void ProcessPathPackets()
        {
            for (int i = 0; !PathPackets.IsEmpty() && i < NUM_PACKETS_TO_PROCESS; i++)
            {
                Packet p = PathPackets.Dequeue();
                switch ((PacketID)p.Data.ID)
                {
                // TODO Maybe: Combine pathing speed and turn in same packet???
                case PacketID.PathingSpeed:
                    PathSpeed = UtilData.ToFloat(p.Data.Payload);
                    MotorControl.SkidSteerDriveSpeed(PathSpeed, PathAngle);
                    break;

                case PacketID.PathingTurnAngle:
                    PathAngle = UtilData.ToFloat(p.Data.Payload);
                    MotorControl.SkidSteerDriveSpeed(PathSpeed, PathAngle);
                    break;
                }
            }
        }
Beispiel #21
0
        public override void Recive(long now)
        {
            int c = metaData.Count;

            byte[][] tmp = new byte[c][];
            lock (metaData)
                for (int i = 0; i < c; i++)
                {
                    tmp[i] = metaData.Dequeue();
                }
            if (c == 0)
            {
                if (now - lastTime > TimeOut)
                {
                    envelope.Clear();
                    Disconnect();
                }
            }
            else
            {
                lastTime = now;
                if (!_connect)
                {
                    ConnectionOK();
                }
                _connect = true;
            }
            for (int i = 0; i < c; i++)
            {
                var list = envelope.Unpack(tmp[i], tmp[i].Length, now);
                try
                {
                    if (list != null)
                    {
                        for (int j = 0; j < list.Count; j++)
                        {
                            var dat = list[j];
                            Dispatch(dat.data, dat.type);
                        }
                    }
                }
                catch
                {
                }
            }
            var ss = envelope.GetFailedData(now);//获取超时未反馈的数据

            if (ss != null)
            {
                for (int i = 0; i < ss.Length; i++)
                {
                    kcp.soc.SendTo(ss[i], endpPoint);//重新发送超时的数据
                }
            }
            ss = envelope.ValidateData.ToArray();
            envelope.ValidateData.Clear();//获取接收成功的数据
            for (int i = 0; i < ss.Length; i++)
            {
                kcp.soc.SendTo(ss[i], endpPoint); //通知对方接收数据成功
            }
        }
Beispiel #22
0
        public static void ProcessBasePackets()
        {
            for (int i = 0; !DrivePackets.IsEmpty() && i < NUM_PACKETS_TO_PROCESS; i++)
            {
                Console.WriteLine("Processing Base Packets");
                Packet p = DrivePackets.Dequeue();
                switch ((PacketID)p.Data.ID)
                {
                //case PacketID.RPMAllDriveMotors:
                //    MotorControl.SetAllRPM((sbyte)p.Data.Payload[0]);
                //    break;
                case PacketID.RPMFrontRight:
                case PacketID.RPMFrontLeft:
                case PacketID.RPMBackRight:
                case PacketID.RPMBackLeft:
                    int MotorID = p.Data.ID - (byte)PacketID.RPMFrontRight;
                    MotorControl.SetRPM(MotorID, (sbyte)p.Data.Payload[1]);
                    break;

                case PacketID.SpeedAllDriveMotors:
                    float Speed = UtilData.ToFloat(p.Data.Payload);
                    MotorControl.SetAllSpeed(Speed);
                    break;

                case PacketID.BaseSpeed:
                case PacketID.ShoulderSpeed:
                case PacketID.ElbowSpeed:
                case PacketID.WristSpeed:
                case PacketID.DifferentialVert:
                case PacketID.DifferentialRotate:
                case PacketID.HandGrip:
                    byte address   = (byte)(p.Data.ID - 0x8A);
                    byte direction = 0x00;
                    if (p.Data.Payload[0] > 0)
                    {
                        direction = 0x01;
                        //UtilCan.SpeedDir(CANBBB.CANBus0, false, 2, address, (byte)(-p.Data.Payload[1]), direction);
                        Console.WriteLine("ADDRESS :" + address + "DIR :" + direction + "PAY :" + (byte)(-p.Data.Payload[1]));
                    }
                    else
                    {
                        direction = 0x00;
                        //UtilCan.SpeedDir(CANBBB.CANBus0, false, 2, address, p.Data.Payload[1], direction);
                        Console.WriteLine("ADDRESS :" + address + "DIR :" + direction + "PAY :" + p.Data.Payload[1]);
                    }
                    break;

                case PacketID.CameraRotation:
                    if ((sbyte)p.Data.Payload[1] > 0)
                    {
                        ServoSpinner -= 0.005f;
                        OutB.SetOutput(ServoSpinner);
                    }
                    else if ((sbyte)p.Data.Payload[1] < 0)
                    {
                        ServoSpinner += 0.005f;
                        OutB.SetOutput(ServoSpinner);
                    }
                    OutB.Dispose();
                    break;
                }
            }
        }