示例#1
0
        private string MakeHttpUrl(bool isHttps)
        {
            StringBuilder str = new StringBuilder();

            if (isHttps)
            {
                str.Append("https://");
            }
            else
            {
                str.Append("http://");
            }
            if (IsInInternalNetwork)
            {
                if (InternalAddress == null)
                {
                    return(null);
                }
                str.Append(InternalAddress.Trim());
                if (isHttps && string.IsNullOrEmpty(PortHTTPS) == false)
                {
                    str.Append(":");
                    str.Append(PortHTTPS);
                }
                else if (string.IsNullOrEmpty(PortHTTP) == false)
                {
                    str.Append(":");
                    str.Append(PortHTTP);
                }
            }
            else
            {
                if (ExternalAddress == null)
                {
                    return(null);
                }
                str.Append(ExternalAddress.Trim());
                if (isHttps && string.IsNullOrEmpty(ExtPortHTTPS) == false)
                {
                    str.Append(":");
                    str.Append(ExtPortHTTPS);
                }
                else if (string.IsNullOrEmpty(ExtPortHTTP) == false)
                {
                    str.Append(":");
                    str.Append(ExtPortHTTP);
                }
            }
            if (Path != null)
            {
                if (Path.StartsWith("/") == false)
                {
                    str.Append("/");
                }
                str.Append(Path);
            }
            return(str.ToString());
        }
示例#2
0
        public HostRouteData ToRouteData()
        {
            var routeData = new HostRouteData();

            routeData.HostId      = this.Id;
            routeData.HostIntAddr = InternalAddress.ToIPv4String();
            routeData.HostExtAddr = ExternalAddress.ToIPv4String();
            routeData.HostName    = this.UniqueName;
            routeData.IsClient    = this.IsClientMode;
            foreach (var aId in this.actorDic.Keys.ToArray())
            {
                routeData.ActorIds.Add(aId);
            }
            return(routeData);
        }
示例#3
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.InternalAddress = 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);

                this.RegisterGlobalManager(this);
                Global.NetManager.RegisterHost(this);
            }

            if (!this.IsClientMode)
            {
                Log.Info(string.Format("{0}(ID:{1}) is running at {2} as ServerMode", this.UniqueName, this.Id, InternalAddress.ToString()));
            }
            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");
            });

            internalThread = new Thread(new ThreadStart(StartHost));
            internalThread.Start();
        }