Ejemplo n.º 1
0
        public override NFIDENTID CreateActor(NFIActor.Handler handler)
        {
            NFIDENTID xID = new NFIDENTID(0, ++mnActorIndex);
            NFIActor xActor = new NFCActor(xID, this);

            //添加仍旧有问题,foreach中万一有其他线程添加
            bool bRet = mxActorDic.TryAdd(xID, xActor);
            if (bRet)
            {
                if (null != handler)
                {
                    RegisterHandler(xID, handler);

                    NFIActorMessage xMessage = new NFIActorMessage();
                    xMessage.bAsync = false;//同步消息
                    xMessage.bReturn = false;//无需返回

                    xMessage.eType = NFIActorMessage.EACTOR_MESSAGE_ID.EACTOR_INIT;
                    SendMsg(xActor.GetAddress(), null, xMessage);

                    xMessage.eType = NFIActorMessage.EACTOR_MESSAGE_ID.EACTOR_AFTER_INIT;
                    SendMsg(xActor.GetAddress(), null, xMessage);
                }

                return xID;
            }

            return null;
        }
Ejemplo n.º 2
0
        public override bool PushMessages(NFIDENTID from, NFIActorMessage xMessage)
        {
            xMessage.nMasterActor = mxID;
            xMessage.nFromActor   = from;

            if (null != mxMessageHandler)
            {
                xMessage.xMasterHandler = new ConcurrentQueue <NFIActor.Handler>(mxMessageHandler);
            }

            if (!xMessage.bAsync)
            {
                //同步消息,也不用排队,就等吧
                ProcessMessageSyns(xMessage);
            }
            else
            {
                //异步消息,需要new新的msg,否则担心masteractor还需使用它
                NFIActorMessage xMsg = new NFIActorMessage(xMessage);
                mxMessageQueue.Enqueue(xMsg);
                NFIScheduler xScheduler = mxActorMng.GetScheduler();
                if (null != xScheduler)
                {
                    xScheduler.AddToScheduler(mxID);
                }
            }

            return(true);
        }
Ejemplo n.º 3
0
        public override NFIDENTID CreateActor(NFIActor.Handler handler)
        {
            NFIDENTID xID    = new NFIDENTID(0, ++mnActorIndex);
            NFIActor  xActor = new NFCActor(xID, this);

            //添加仍旧有问题,foreach中万一有其他线程添加
            bool bRet = mxActorDic.TryAdd(xID, xActor);

            if (bRet)
            {
                if (null != handler)
                {
                    RegisterHandler(xID, handler);

                    NFIActorMessage xMessage = new NFIActorMessage();
                    xMessage.bAsync  = false; //同步消息
                    xMessage.bReturn = false; //无需返回

                    xMessage.eType = NFIActorMessage.EACTOR_MESSAGE_ID.EACTOR_INIT;
                    SendMsg(xActor.GetAddress(), null, xMessage);

                    xMessage.eType = NFIActorMessage.EACTOR_MESSAGE_ID.EACTOR_AFTER_INIT;
                    SendMsg(xActor.GetAddress(), null, xMessage);
                }

                return(xID);
            }

            return(null);
        }
Ejemplo n.º 4
0
        static void Main()
        {
            TestHandler1 xTestHandler1 = new TestHandler1();
            TestHandler2 xTestHandler2 = new TestHandler2();

            Console.WriteLine("start run... ThreadID: " + Thread.CurrentThread.ManagedThreadId);

            NFIDENTID xID1 = NFCActorMng.Intance().CreateActor(xTestHandler1.Handler);
            NFIDENTID xID2 = NFCActorMng.Intance().CreateActor(xTestHandler2.Handler);

             NFIActorMessage xMsgData = new NFIActorMessage();
             xMsgData.data = "test";
             //xMsgData.bAsync = false;//控制为同步消息还是异步消息
             xMsgData.eType = NFIActorMessage.EACTOR_MESSAGE_ID.EACTOR_TEST_MSG;

             System.DateTime currentTime = new System.DateTime();
             currentTime = System.DateTime.Now;

             NFCActorMng.Intance().SendMsg(xID1, xID2, xMsgData);

             for (int i = 0; i < 100000; ++i )
             {
                 NFIActorMessage xMsg = new NFIActorMessage();
                 xMsg.data = "test";
                 //xMsg.bAsync = false;//控制为同步消息还是异步消息
                 xMsg.eType = NFIActorMessage.EACTOR_MESSAGE_ID.EACTOR_TEST_MSG;

                 TestHandler1 xTest1 = new TestHandler1();
                 TestHandler2 xTest2 = new TestHandler2();

                 NFIDENTID x1 = NFCActorMng.Intance().CreateActor(xTest1.Handler);
                 NFIDENTID x2 = NFCActorMng.Intance().CreateActor(xTest2.Handler);

                 NFCActorMng.Intance().SendMsg(x1, x2, xMsg);
             }

             Console.WriteLine("start loop... ThreadID: " + Thread.CurrentThread.ManagedThreadId);

            while(true)
            {
                Thread.Sleep(1);

                System.TimeSpan ts = System.DateTime.Now - currentTime;
                if (ts.TotalMilliseconds > 10000)
                {
                    int nCount = TestHandler1.i + TestHandler2.i;
                    Console.WriteLine("Count : " + nCount);

                    break;
                }
            }

            while (true)
            {
                Thread.Sleep(1);
            }

            NFCActorMng.Intance().ReleaseAllActor();
        }
Ejemplo n.º 5
0
        /////////////////////////////////////////////////////////////

        private static void TaskMethodSync(object param)
        {
            NFIActorMessage xMsg = (NFIActorMessage)param;

            if (null != xMsg.xMasterHandler)
            {
                foreach (Handler xHandler in xMsg.xMasterHandler)
                {
                    xHandler(xMsg);
                }
            }
        }
Ejemplo n.º 6
0
        public override bool SendMsg(NFIDENTID address, NFIDENTID from, NFIActorMessage xMessage)
        {
            if (null == address || null == xMessage)
            {
                return(false);;
            }

            NFIActor xActor = GetActor(address);

            if (null != xActor)
            {
                return(xActor.PushMessages(from, xMessage));
            }

            return(false);
        }
Ejemplo n.º 7
0
            public void Handler(NFIActorMessage xMessage)
            {
                ++i;

                //Console.WriteLine("handler11 ThreadID: " + Thread.CurrentThread.ManagedThreadId + " " + i);

                switch (xMessage.eType)
                {
                case NFIActorMessage.EACTOR_MESSAGE_ID.EACTOR_INIT:
                    Init();
                    break;

                case NFIActorMessage.EACTOR_MESSAGE_ID.EACTOR_AFTER_INIT:
                    AfterInit();
                    break;

                case NFIActorMessage.EACTOR_MESSAGE_ID.EACTOR_EXCUTE:
                    Execute();
                    break;

                case NFIActorMessage.EACTOR_MESSAGE_ID.EACTOR_BEFORE_SHUT:
                    BeforeShut();
                    break;

                case NFIActorMessage.EACTOR_MESSAGE_ID.EACTOR_SHUT:
                    Shut();
                    break;

                case NFIActorMessage.EACTOR_MESSAGE_ID.EACTOR_TEST_MSG:
                {
                    NFIActorMessage xMsgData = new NFIActorMessage();
                    xMsgData.data = "test1";
                    //xMsgData.bAsync = false;
                    xMsgData.eType = NFIActorMessage.EACTOR_MESSAGE_ID.EACTOR_DATA_MSG;

                    // Console.WriteLine("handler11 ThreadID: " + Thread.CurrentThread.ManagedThreadId + " " + xMsgData.data);

                    NFCActorMng.Intance().SendMsg(xMessage.nFromActor, xMessage.nMasterActor, xMsgData);

                    // Console.WriteLine("handler11 ThreadID: " + Thread.CurrentThread.ManagedThreadId + " " + xMsgData.data);
                }
                break;

                default:
                    break;
                }
            }
Ejemplo n.º 8
0
        public NFIActorMessage(NFIActorMessage x)
        {
            eType = x.eType;
            bAsync = x.bAsync;
            bReturn = x.bReturn;
            nSubMsgID = x.nSubMsgID;
            nFromActor = x.nFromActor;
            nMasterActor = x.nMasterActor;
            if (null != x.xMasterHandler)
            {
                xMasterHandler = new ConcurrentQueue<NFIActor.Handler>(x.xMasterHandler);
            }

            if (null != x.data)
            {
                data = (string)x.data.Clone();
            }
        }
Ejemplo n.º 9
0
        public NFIActorMessage(NFIActorMessage x)
        {
            eType        = x.eType;
            bAsync       = x.bAsync;
            bReturn      = x.bReturn;
            nSubMsgID    = x.nSubMsgID;
            nFromActor   = x.nFromActor;
            nMasterActor = x.nMasterActor;
            if (null != x.xMasterHandler)
            {
                xMasterHandler = new ConcurrentQueue <NFIActor.Handler>(x.xMasterHandler);
            }

            if (null != x.data)
            {
                data = (string)x.data.Clone();
            }
        }
Ejemplo n.º 10
0
        ///////////////////////////////////////////////////////

        private bool ReleaseActor(NFIActor xActor)
        {
            if (null == xActor)
            {
                return(false);
            }

            NFIActorMessage xMessage = new NFIActorMessage();

            xMessage.bAsync = false;//同步消息

            xMessage.eType = NFIActorMessage.EACTOR_MESSAGE_ID.EACTOR_BEFORE_SHUT;
            xActor.PushMessages(null, xMessage);

            xMessage.eType = NFIActorMessage.EACTOR_MESSAGE_ID.EACTOR_SHUT;
            xActor.PushMessages(null, xMessage);

            return(true);
        }
Ejemplo n.º 11
0
        private void ProcessMessageSyns(NFIActorMessage xMessage)
        {
            if (xMessage.nMasterActor != GetAddress())
            {
                return;
            }

            if (null == xMessage.xMasterHandler)
            {
                return;
            }
            if (xMessage.bAsync)
            {
                return;
            }

            Task xTask = Task.Factory.StartNew(TaskMethodSync, xMessage);

            if (null != xTask)
            {
                //同步消息需要wait
                xTask.Wait();
            }
        }
Ejemplo n.º 12
0
        private void ProcessMessageSyns(NFIActorMessage xMessage)
        {
            if (xMessage.nMasterActor != GetAddress())
            {
                return;
            }

            if (null == xMessage.xMasterHandler)
            {
                return;
            }
            if (xMessage.bAsync)
            {
                return;
            }

            Task xTask = Task.Factory.StartNew(TaskMethodSync, xMessage);
            if (null != xTask)
            {
                //同步消息需要wait
                xTask.Wait();
            }
        }
Ejemplo n.º 13
0
        public override bool PushMessages(NFGUID from, NFIActorMessage xMessage)
        {
            xMessage.nMasterActor = mxID;
            xMessage.nFromActor = from;

            if (null != mxMessageHandler)
            {
                xMessage.xMasterHandler = new ConcurrentQueue<NFIActor.Handler>(mxMessageHandler);
            }

            if (!xMessage.bAsync)
            {
                //同步消息,也不用排队,就等吧
                ProcessMessageSyns(xMessage);
            }
            else
            {
                //异步消息,需要new新的msg,否则担心masteractor还需使用它
                NFIActorMessage xMsg = new NFIActorMessage(xMessage);
                mxMessageQueue.Enqueue(xMsg);
                NFIScheduler xScheduler = mxActorMng.GetScheduler();
                if(null != xScheduler)
                {
                    xScheduler.AddToScheduler(mxID);
                }
            }

            return true;
        }
Ejemplo n.º 14
0
        ///////////////////////////////////////////////////////
        private bool ReleaseActor(NFIActor xActor)
        {
            if (null == xActor)
            {
                return false;
            }

            NFIActorMessage xMessage = new NFIActorMessage();
            xMessage.bAsync = false;//同步消息

            xMessage.eType = NFIActorMessage.EACTOR_MESSAGE_ID.EACTOR_BEFORE_SHUT;
            xActor.PushMessages(null, xMessage);

            xMessage.eType = NFIActorMessage.EACTOR_MESSAGE_ID.EACTOR_SHUT;
            xActor.PushMessages(null, xMessage);

            return true;
        }
Ejemplo n.º 15
0
        static void Main()
        {
            TestHandler1 xTestHandler1 = new TestHandler1();
            TestHandler2 xTestHandler2 = new TestHandler2();

            Console.WriteLine("start run... ThreadID: " + Thread.CurrentThread.ManagedThreadId);

            NFGUID xID1 = NFCActorMng.Intance().CreateActor(xTestHandler1.Handler);
            NFGUID xID2 = NFCActorMng.Intance().CreateActor(xTestHandler2.Handler);

            NFIActorMessage xMsgData = new NFIActorMessage();

            xMsgData.data = "test";
            //xMsgData.bAsync = false;//控制为同步消息还是异步消息
            xMsgData.eType = NFIActorMessage.EACTOR_MESSAGE_ID.EACTOR_TEST_MSG;

            System.DateTime currentTime = new System.DateTime();
            currentTime = System.DateTime.Now;

            NFCActorMng.Intance().SendMsg(xID1, xID2, xMsgData);


            for (int i = 0; i < 100000; ++i)
            {
                NFIActorMessage xMsg = new NFIActorMessage();
                xMsg.data = "test";
                //xMsg.bAsync = false;//控制为同步消息还是异步消息
                xMsg.eType = NFIActorMessage.EACTOR_MESSAGE_ID.EACTOR_TEST_MSG;

                TestHandler1 xTest1 = new TestHandler1();
                TestHandler2 xTest2 = new TestHandler2();

                NFGUID x1 = NFCActorMng.Intance().CreateActor(xTest1.Handler);
                NFGUID x2 = NFCActorMng.Intance().CreateActor(xTest2.Handler);

                NFCActorMng.Intance().SendMsg(x1, x2, xMsg);
            }

            Console.WriteLine("start loop... ThreadID: " + Thread.CurrentThread.ManagedThreadId);


            while (true)
            {
                Thread.Sleep(1);

                System.TimeSpan ts = System.DateTime.Now - currentTime;
                if (ts.TotalMilliseconds > 10000)
                {
                    int nCount = TestHandler1.i + TestHandler2.i;
                    Console.WriteLine("Count : " + nCount);

                    break;
                }
            }

            while (true)
            {
                Thread.Sleep(1);
            }

            NFCActorMng.Intance().ReleaseAllActor();
        }
Ejemplo n.º 16
0
        public override bool SendMsg(NFIDENTID address, NFIDENTID from, NFIActorMessage xMessage)
        {
            if (null == address || null == xMessage)
            {
                return false; ;
            }

            NFIActor xActor = GetActor(address);
            if (null != xActor)
            {
                return xActor.PushMessages(from, xMessage);
            }

            return false;
        }
Ejemplo n.º 17
0
            public void Handler(NFIActorMessage xMessage)
            {
                ++i;

                //Console.WriteLine("handler222222 ThreadID: " + Thread.CurrentThread.ManagedThreadId + " " + i);
                switch (xMessage.eType)
                {
                    case NFIActorMessage.EACTOR_MESSAGE_ID.EACTOR_INIT:
                        Init();
                        break;
                    case NFIActorMessage.EACTOR_MESSAGE_ID.EACTOR_AFTER_INIT:
                        AfterInit();
                        break;
                    case NFIActorMessage.EACTOR_MESSAGE_ID.EACTOR_EXCUTE:
                        Execute();
                        break;
                    case NFIActorMessage.EACTOR_MESSAGE_ID.EACTOR_BEFORE_SHUT:
                        BeforeShut();
                        break;
                    case NFIActorMessage.EACTOR_MESSAGE_ID.EACTOR_SHUT:
                        Shut();
                        break;
                    case NFIActorMessage.EACTOR_MESSAGE_ID.EACTOR_DATA_MSG:
                        {
                            xMessage.data = "1111111111111111111";
                            xMessage.eType = NFIActorMessage.EACTOR_MESSAGE_ID.EACTOR_TEST_MSG;

                            NFCActorMng.Intance().SendMsg(xMessage.nFromActor, xMessage.nMasterActor, xMessage);

                        }
                        break;
                    default:
                        break;
                }
            }
Ejemplo n.º 18
0
 public abstract bool SendMsg(NFIDENTID address, NFIDENTID from, NFIActorMessage xMessage);
Ejemplo n.º 19
0
 public abstract bool PushMessages(NFIDENTID from, NFIActorMessage xMessage);
Ejemplo n.º 20
0
 public abstract bool SendMsg(NFGUID address, NFGUID from, NFIActorMessage xMessage);