Ejemplo n.º 1
0
        public void Connect(string net, string ip, int port)
        {
            //Disconnect();
            if (network != null)
            {
                StopHeart();

                network.OnPacket    -= OnPacket;
                network.OnConnected -= Connected;
                network.OnClosed    -= Closed;
                network.OnError     -= Error;

                network.Disconnect();

                network = null;

                //recvMsgList = null;
                //tmpMsgList = null;
                bConnected = false;
                entityDict.Clear();
            }

            try
            {
                if (net == "tcp")
                {
                    network = GBox.Make <INetworkManager>().Create("cratos", "cratos.tcp://" + ip + ":" + port);
                }
                else if (net == "kcp")
                {
                    network = GBox.Make <INetworkManager>().Create("cratos", "cratos.kcp://" + ip + ":" + port + "?datashards=3&parityshards=2");
                }
                network.OnConnected += Connected;
                network.OnClosed    += Closed;
                network.OnError     += Error;

                tmpMsgList  = new List <object>();
                recvMsgList = new List <object>();
                eventList   = new List <EventData>();

                network.OnPacket += OnPacket;
                network.SetPlugins(packer);
                network.Connect();
            }
            catch (Exception e)
            {
                if (WhenError != null)
                {
                    WhenError.Invoke(e);
                }
            }
        }
Ejemplo n.º 2
0
        public virtual void Update(float delta)
        {
            if (recvMsgList == null)
            {
                return;
            }

            lock (recvMsgList)
            {
                tmpMsgList.AddRange(recvMsgList);
                recvMsgList.Clear();
            }

            if (tmpMsgList.Count > 0)
            {
                for (int i = 0; i < tmpMsgList.Count; i++)
                {
                    var msg      = tmpMsgList[i];
                    var fullname = msg.GetType().FullName;

                    if (msg.GetType().Name == "RpcMsg")
                    {
                        OnRpc((RpcMsg)msg);
                    }
                    else
                    {
                        var msgInfo = msgDefined.GetMsgByName(fullname);
                        if (msgInfo != null)
                        {
                            events.Fire(msgInfo.Name, msg);
                            if (msg is IMsg)
                            {
                                ((IMsg)msg).Recycle();
                            }
                        }
                    }
                }

                tmpMsgList.Clear();
            }

            lock (eventList)
            {
                for (int i = 0; i < eventList.Count; i++)
                {
                    var e = eventList[i];
                    switch (e.eventType)
                    {
                    case EventType.Connected:
                        if (WhenConnected != null)
                        {
                            WhenConnected.Invoke();
                        }

                        heartThread = new Thread(HeartThread);
                        heartThread.IsBackground = true;
                        bHeartThreadQuit         = false;
                        heartThread.Start();
                        break;

                    case EventType.Closed:
                        StopHeart();
                        if (WhenClosed != null)
                        {
                            WhenClosed.Invoke(null);
                        }
                        break;

                    case EventType.Error:
                        if (WhenError != null)
                        {
                            WhenError.Invoke(e.ex);
                        }
                        break;
                    }
                }

                eventList.Clear();
            }
        }