Ejemplo n.º 1
0
        protected Host(string name, string ip, string extIp, int port = 0, bool clientMode = false) : base()
        {
            this.IsClientMode = clientMode;

            //NetManager.Instance.OnConnect += (peer) =>
            NetManager.Instance.OnReceive   += (peer, buffer) => OnReceiveBuffer(peer, buffer);
            NetManager.Instance.OnClose     += (peer) => NetManager.Instance.Deregister(peer);
            NetManager.Instance.OnException += (peer, ex) =>
            {
                Log.Error(ex.ToString());
                NetManager.Instance.Deregister(peer);
            };

            NetManager.Instance.OnPeerLost += (peer) =>
            {
                if (this.actorDic.Any(m => m.Key == peer.ConnId && m.Value.GetType().Name == "Avatar"))
                {
                    //客户端没了,就先删除actor吧(玩家avatar)

                    this.actorDic[peer.ConnId].Destroy();
                    this.actorDic.TryRemove(peer.ConnId, out var _);
                }
            };

            //如果是客户端,则用本地连接做为id
            //如果是服务端,则从名称计算一个id, 方便路由查找
            if (!clientMode)
            {
                string _ip    = ip;
                string _extIp = extIp;
                int    _port  = port;

                if (ip == "auto")
                {
                    _ip = Basic.GetLocalIPv4(NetworkInterfaceType.Ethernet);
                }

                if (extIp == "auto")
                {
                    _extIp = Basic.GetLocalIPv4(NetworkInterfaceType.Ethernet);
                }

                if (port == 0)
                {
                    _port = Basic.GetAvailablePort(IPAddress.Parse(_ip));
                }

                this.LocalAddress    = new IPEndPoint(IPAddress.Parse(_ip), _port);
                this.ExternalAddress = new IPEndPoint(IPAddress.Parse(_extIp), port);

                string addr = LocalAddress.ToIPv4String();

                if (name == null)
                {
                    this.UniqueName = Basic.GenID64().ToString();
                }
                else
                {
                    this.UniqueName = name;
                }

                this.Id = Basic.GenID32FromName(this.UniqueName);

                this.RegisterGlobalManager(this);

                this.SetupKcpServer();
                this.SetupTcpServer();
            }
            else
            {
                if (name == null)
                {
                    this.UniqueName = Basic.GenID64().ToString();
                }
                else
                {
                    this.UniqueName = name;
                }
                this.Id = Basic.GenID32FromName(this.UniqueName);
            }

            if (!this.IsClientMode)
            {
                Log.Info(string.Format("{0}(ID:{1}) is running at {2} as ServerMode", this.UniqueName, this.Id, LocalAddress.ToIPv4String()));
            }
            else
            {
                Log.Info(string.Format("{0}(ID:{1}) is running as ClientMode", this.UniqueName, this.Id));
            }

            heartbeatTh = new Thread(new ThreadStart(Heartbeat));
            heartbeatTh.Start();

            this.AddRepeatedTimer(3000, 3000, () =>
            {
                NetManager.Instance.PrintPeerInfo("All peers:");

                foreach (var a in this.actorDic.Values)
                {
                    Log.Info("===========Actor info", a.Id, a.UniqueName);
                }

                Log.Info("End of Print");
            });
        }
Ejemplo n.º 2
0
        protected Host(string name, string ip, string extIp, int port = 0, bool clientMode = false) : base()
        {
            this.IsClientMode = clientMode;

            Global.NetManager.OnConnect   += OnConnect;
            Global.NetManager.OnReceive   += OnReceive;
            Global.NetManager.OnClose     += OnClose;
            Global.NetManager.OnException += OnExcept;
            Global.NetManager.OnHeartBeat += OnHeartBeat;

            //如果是客户端,则用本地连接做为id
            //如果是服务端,则从名称计算一个id, 方便路由查找
            if (!clientMode)
            {
                string _ip    = ip;
                string _extIp = extIp;
                int    _port  = port;

                if (ip == "auto")
                {
                    _ip = Basic.GetLocalIPv4(NetworkInterfaceType.Ethernet);
                }

                if (extIp == "auto")
                {
                    _extIp = Basic.GetLocalIPv4(NetworkInterfaceType.Ethernet);
                }

                if (port == 0)
                {
                    _port = Basic.GetAvailablePort(IPAddress.Parse(_ip));
                }

                this.LocalAddress    = new IPEndPoint(IPAddress.Parse(_ip), _port);
                this.ExternalAddress = new IPEndPoint(IPAddress.Parse(_extIp), port);

                string addr = LocalAddress.ToIPv4String();

                if (name == null)
                {
                    this.UniqueName = Basic.GenID64().ToString();
                }
                else
                {
                    this.UniqueName = name;
                }

                this.Id = Basic.GenID64FromName(this.UniqueName);
                this.RegisterGlobalManager(this);
                Global.NetManager.RegisterHost(this);
            }
            else
            {
                if (name == null)
                {
                    this.UniqueName = Basic.GenID64().ToString();
                }
                else
                {
                    this.UniqueName = name;
                }
                this.Id = Basic.GenID64FromName(this.UniqueName);

                Global.NetManager.RegisterHost(this);
            }

            if (!this.IsClientMode)
            {
                Log.Info(string.Format("{0}(ID:{1}) is running at {2} as ServerMode", this.UniqueName, this.Id, LocalAddress.ToIPv4String()));
            }
            else
            {
                Log.Info(string.Format("{0}(ID:{1}) is running as ClientMode", this.UniqueName, this.Id));
            }

            this.AddRepeatedTimer(3000, 10000, () =>
            {
                Global.NetManager.PrintPeerInfo("All peers:");

                foreach (var a in this.actorDic.Values)
                {
                    Log.Info("===========Actor info", a.Id, a.UniqueName);
                }

                Log.Info("End of Print");
            });
        }