Example #1
0
        static void Main(String[] args)
        {
            XTrace.UseConsole();

            var sc = new RpcServer()
            {
                Port       = 1234,
                Log        = XTrace.Log,
                EncoderLog = XTrace.Log,

                NameSpace = "NewLife.Test",
            };

            var star = new StarClient("tcp://127.0.0.1:6666")
            {
                Code   = "test",
                Secret = "pass"
            };

            sc.Star = star;

            sc.Start();

            _Server = sc;

            Thread.Sleep(-1);
        }
Example #2
0
        public void GetHeartInfoTest()
        {
            var client = new StarClient();
            var inf    = client.GetHeartInfo();

            Assert.NotNull(inf);
            Assert.NotEmpty(inf.Macs);
        }
Example #3
0
        public async void LogoutTest()
        {
            var client = new StarClient(Server);

            await client.Login();

            await client.Logout("test");
        }
Example #4
0
        /// <summary>服务停止</summary>
        /// <remarks>
        /// 安装Windows服务后,服务停止会执行该方法。
        /// 控制台菜单按5进入循环调试,任意键结束时也会执行该方法。
        /// </remarks>
        protected override void StopWork(String reason)
        {
            base.StopWork(reason);

            _Manager.Stop(reason);
            //_Manager.TryDispose();

            _Client.TryDispose();
            _Client = null;
        }
Example #5
0
        private void StartClient()
        {
            var set    = Setting.Current;
            var server = Stardust.Setting.Current.Server;

            if (server.IsNullOrEmpty())
            {
                return;
            }

            WriteLog("初始化服务端地址:{0}", server);

            var client = new StarClient(server)
            {
                Code   = set.Code,
                Secret = set.Secret,
                Log    = XTrace.Log,
            };

            // 登录后保存证书
            client.OnLogined += (s, e) =>
            {
                var inf = client.Info;
                if (inf != null && !inf.Code.IsNullOrEmpty())
                {
                    set.Code   = inf.Code;
                    set.Secret = inf.Secret;
                    set.Save();
                }
            };

            // APM埋点。独立应用名
            var tracer = new StarTracer
            {
                AppId = "StarAgent",
                //AppSecret = null,
                Client = new ApiHttpClient(server),
                Log    = XTrace.Log
            };

            DefaultTracer.Instance = tracer;
            ApiHelper.Tracer       = tracer;
            client.Tracer          = tracer;

            // 使用跟踪
            client.UseTrace();

            _Client = client;

            // 可能需要多次尝试
            _timer = new TimerX(TryConnectServer, client, 0, 5_000)
            {
                Async = true
            };
        }
Example #6
0
        public async void LoginTest(String code, String secret)
        {
            var client = new StarClient(Server)
            {
                Code   = code,
                Secret = secret
            };

            var rs = await client.Login();

            Assert.NotNull(rs);
            Assert.NotNull(client.Info);
            Assert.True(client.Logined);
        }
Example #7
0
        public void StartClient()
        {
            var server = StarSetting.Server;

            if (server.IsNullOrEmpty())
            {
                return;
            }

            WriteLog("初始化服务端地址:{0}", server);

            var set    = AgentSetting;
            var client = new StarClient(server)
            {
                Code   = set.Code,
                Secret = set.Secret,
                Log    = XTrace.Log,

                Manager = _Manager,
            };

            // 登录后保存证书
            client.OnLogined += (s, e) =>
            {
                var inf = client.Info;
                if (inf != null && !inf.Code.IsNullOrEmpty())
                {
                    set.Code   = inf.Code;
                    set.Secret = inf.Secret;
                    set.Save();
                }
            };

            // APM埋点。独立应用名
            client.Tracer = _factory.Tracer;

            _Manager.Attach(client.CommandQueue);

            // 使用跟踪
            client.UseTrace();

            _Client = client;

            // 可能需要多次尝试
            _timer = new TimerX(TryConnectServer, client, 0, 5_000)
            {
                Async = true
            };
        }
Example #8
0
        private static async Task CheckUpgrade(StarClient client, String channel)
        {
            var ug = new Stardust.Web.Upgrade {
                Log = XTrace.Log
            };

            // 检查更新
            var ur = await client.Upgrade(channel);

            if (ur != null && ur.Version != _lastVersion)
            {
                ug.Url = ur.Source;
                await ug.Download();

                // 检查文件完整性
                if (ur.FileHash.IsNullOrEmpty() || ug.CheckFileHash(ur.FileHash))
                {
                    // 执行更新,解压缩覆盖文件
                    var rs = ug.Update();
                    if (rs && !ur.Executor.IsNullOrEmpty())
                    {
                        ug.Run(ur.Executor);
                    }
                    _lastVersion = ur.Version;

                    // 去除多余入口文件
                    ug.Trim("StarAgent");

                    // 强制更新时,马上重启
                    if (rs && ur.Force)
                    {
                        //StopWork("Upgrade");

                        // 重新拉起进程
                        var star = "XCoder.exe";
                        XTrace.WriteLine("强制升级,拉起进程 {0} -upgrade", star.GetFullPath());
                        Process.Start(star.GetFullPath(), "-upgrade");

                        //var p = Process.GetCurrentProcess();
                        //p.Close();
                        //p.Kill();
                        Application.Exit();
                    }
                }
            }
        }
Example #9
0
        private static void CheckUpgrade(StarClient client, String channel)
        {
            // 检查更新
            var ur = client.Upgrade(channel).Result;

            if (ur != null)
            {
                var rs = client.ProcessUpgrade(ur);

                // 强制更新时,马上重启
                if (rs && ur.Force)
                {
                    var p = Process.GetCurrentProcess();
                    p.Close();
                    p.Kill(true);
                }
            }
        }
Example #10
0
        //static ServiceManager _Manager;
        private static void StartClient()
        {
            var set    = XConfig.Current;
            var server = set.Server;

            if (server.IsNullOrEmpty())
            {
                return;
            }

            XTrace.WriteLine("初始化服务端地址:{0}", server);

            var client = new StarClient(server)
            {
                Code        = set.Code,
                Secret      = set.Secret,
                ProductCode = "CrazyCoder",
                Log         = XTrace.Log,
            };

            // 登录后保存证书
            client.OnLogined += (s, e) =>
            {
                var inf = client.Info;
                if (inf != null && !inf.Code.IsNullOrEmpty())
                {
                    set.Code   = inf.Code;
                    set.Secret = inf.Secret;
                    set.Save();
                }
            };

            client.UseTrace();

            Application.ApplicationExit += (s, e) => client.Logout("ApplicationExit");

            // 可能需要多次尝试
            _timer = new TimerX(TryConnectServer, client, 0, 5_000)
            {
                Async = true
            };

            _Client = client;
        }
Example #11
0
        /// <summary>服务停止</summary>
        /// <remarks>
        /// 安装Windows服务后,服务停止会执行该方法。
        /// 控制台菜单按5进入循环调试,任意键结束时也会执行该方法。
        /// </remarks>
        protected override void StopWork(String reason)
        {
            base.StopWork(reason);

            _timer.TryDispose();
            _timer = null;

            _Manager.Stop(reason);
            //_Manager.TryDispose();

            _Client?.Logout(reason);
            //_Client.TryDispose();
            _Client = null;

            _factory = null;

            _server.TryDispose();
            _server = null;
        }
Example #12
0
        private static void StartClient()
        {
            var set    = StarAgent.Setting.Current;
            var server = set.Server;

            if (server.IsNullOrEmpty())
            {
                return;
            }

            XTrace.WriteLine("初始化服务端地址:{0}", server);

            var client = new StarClient(server)
            {
                Code   = set.Code,
                Secret = set.Secret,
                Log    = XTrace.Log,
            };

            // 登录后保存证书
            client.OnLogined += (s, e) =>
            {
                var inf = client.Info;
                if (inf != null && !inf.Code.IsNullOrEmpty())
                {
                    set.Code   = inf.Code;
                    set.Secret = inf.Secret;
                    set.Save();
                }
            };

            // 可能需要多次尝试
            _timer = new TimerX(TryConnectServer, client, 0, 5_000)
            {
                Async = true
            };

            _Client = client;
        }
Example #13
0
        private static void StartClient()
        {
            var set    = StarSetting.Current;
            var server = "http://star.newlifex.com:6600";

            XTrace.WriteLine("初始化服务端地址:{0}", server);

            var client = new StarClient(server)
            {
                Code        = set.AppKey,
                Secret      = set.Secret,
                ProductCode = "MobileApp",
                Log         = XTrace.Log,
            };

            // 登录后保存证书
            client.OnLogined += (s, e) =>
            {
                var inf = client.Info;
                if (inf != null && !inf.Code.IsNullOrEmpty())
                {
                    set.AppKey = inf.Code;
                    set.Secret = inf.Secret;
                    set.Save();
                }
            };

            client.UseTrace();

            //Application.ApplicationExit += (s, e) => client.Logout("ApplicationExit");

            // 可能需要多次尝试
            _timer = new TimerX(TryConnectServer, client, 0, 5_000)
            {
                Async = true
            };

            _Client = client;
        }
Example #14
0
        public void GetLoginInfoTest()
        {
            var client = new StarClient
            {
                Code   = Rand.NextString(8),
                Secret = Rand.NextString(16)
            };

            var inf = client.GetLoginInfo();

            Assert.NotNull(inf);
            Assert.NotNull(inf.Node);

            Assert.Equal(client.Code, inf.Code);
            Assert.Equal(client.Secret.MD5(), inf.Secret);

            var node = client.GetNodeInfo();
            var mi   = MachineInfo.Current;

            Assert.Equal(mi.UUID, node.UUID);
            Assert.Equal(mi.Guid, node.MachineGuid);
        }
Example #15
0
        static void Test1()
        {
            var sc = new RpcServer()
            {
                Port       = 1234,
                Log        = XTrace.Log,
                EncoderLog = XTrace.Log,

                NameSpace = "NewLife.Test",
            };

            var star = new StarClient("tcp://127.0.0.1:6666")
            {
                Code   = "test",
                Secret = "pass"
            };

            sc.Star = star;

            sc.Start();

            _Server = sc;
        }