Exemplo n.º 1
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");
            });
        }
Exemplo n.º 2
0
 public static ulong GetUniqueId(this Ukcp ukcp)
 {
     return(Basic.GenID64FromName(ukcp.user().Channel.Id.AsLongText() +
                                  ukcp.user().Channel.LocalAddress.ToIPv4String() +
                                  ukcp.user().RemoteAddress.ToIPv4String() + ukcp.getConv().ToString()));
 }
Exemplo n.º 3
0
 public static ulong GetUniqueId(this IChannel ch)
 {
     return(Basic.GenID64FromName(ch.Id.AsLongText() +
                                  ch.LocalAddress.ToIPv4String() +
                                  ch.RemoteAddress.ToIPv4String()));
 }