Ejemplo n.º 1
0
        static void RunEchoServer()
        {
            var echoDescriptor = new ServiceDescriptor
            {
                Name        = "echo",
                Description = "demo service",
                ServiceHost = "localhost",
                ServicePort = 0, //the port can be set to any
            };
            var descManager = new ServiceDescriptor
            {
                Name        = "service_manager",
                Description = "global service manager",
                ServiceHost = "localhost",
                ServicePort = 3324,
                AccessToken = "AnyClient"
            };

            using var resolver = new TcpServiceResolver(descManager, clientID: "logger_client", secretKey: "02384Je5");
            var remoteLogger = resolver.GetService <ILogger>("logger");

            var echoService = new EchoImpl();

            using var echoRunner = new TcpServiceRunner <IEcho2>(
                      service: echoService,
                      descriptor: echoDescriptor,
                      logger: remoteLogger,
                      sslCertificate: null);
            echoRunner.Start(descManager, clientID: "echo_provider", secretKey: "F*ooE3");

            echoService.WaitForStop();
            Thread.Sleep(2000); //sleep a while so that the client can release the proxy first
        }
Ejemplo n.º 2
0
        static void TestLocal()
        {
            //Server site code
            var descManager = new ServiceDescriptor
            {
                Name        = "service_manager",
                Description = "service manager",
            };

            using var managerRunner = LocalManagerRunner.Instance;
            var config = AccessConfig.FromJson(ManagerConfigJson);

            managerRunner.Config(config);
            managerRunner.Start(descManager, sslCertificate: null);

            var descLogger = new ServiceDescriptor
            {
                Name        = "logger",
                Description = "log service",
            };

            using var loggerService = BuilderLogger();
            using var loggerRunner  = new LocalServiceRunner <ILogger>(loggerService, descLogger);
            loggerRunner.Start(descManager, clientID: "logger_provider", secretKey: "Dx90et54");

            var echoDescriptor = new ServiceDescriptor
            {
                Name        = "echo",
                Description = "demo service",
            };

            var echoService = new EchoImpl();

            using var echoRunner = new LocalServiceRunner <IEcho2>(echoService, echoDescriptor);
            echoRunner.Start(descManager, clientID: "echo_provider", secretKey: "F*ooE3");

            //client site code
            using var resolver = new LocalServiceResolver(descLogger, clientID: "logger_client", secretKey: "02384Je5");
            var services = resolver.ServiceManager.ListService();

            foreach (var desc in services)
            {
                Console.WriteLine(desc.ToString());
            }

            var loggerClient = resolver.GetService <ILogger>("logger");

            loggerClient.Debug(tag: "local_test", message: "Hello XPRPC");

            var echoClient = resolver.GetService <IEcho2>("echo");

            //call sync methods
            Console.WriteLine(echoClient.SayHello("World!"));
            Console.WriteLine(echoClient.SayHi("XP!"));
            //add event handler
            echoClient.GreetingEvent += (sender, args) => { Console.WriteLine(args.Greeting); };
            echoService.Greeting("Hello clients!");
            echoClient.Greeting2Event += OnEchoGreeting;
            echoService.Greeting("Hello echo!");
            //callback & async method call
            echoClient.SetCallback(new Callback());
            echoService.GreetingAsync("Hello echo async!").Wait();
            echoClient.SetCallback(null);
            echoService.GreetingAsync("Hello echo async agin!").Wait();
            //remove event handler
            echoClient.Greeting2Event -= OnEchoGreeting;
            echoService.Greeting("Hello echo two!");
            Console.WriteLine(echoClient.SayHelloAsync("World!").Result);
        }