Exemplo n.º 1
0
        //客户端请求
        private static void runClient(object state)
        {
            try
            {
                //构造MyData
                MyData.Builder myDataBuilder = MyData.CreateBuilder();
                myDataBuilder.Resume = "我的个人简介";
                MyData myData = myDataBuilder.Build();

                //构造MyRequest
                MyRequest.Builder myRequestBuilder = MyRequest.CreateBuilder();
                myRequestBuilder.Version = 1;
                myRequestBuilder.Name    = "吴剑";
                myRequestBuilder.Website = "www.paotiao.com";
                //注:直接支持ByteString类型
                myRequestBuilder.Data = myData.ToByteString();
                MyRequest myRequest = myRequestBuilder.Build();

                Console.WriteLine("CLIENT : 对象构造完毕 ...");

                using (TcpClient client = new TcpClient())
                {
                    client.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9528));
                    Console.WriteLine("CLIENT : socket 连接成功 ...");

                    using (NetworkStream stream = client.GetStream())
                    {
                        //发送
                        Console.WriteLine("CLIENT : 发送数据 ...");
                        myRequest.WriteTo(stream);

                        //接收
                        Console.WriteLine("CLIENT : 等待响应 ...");
                        byte[] myResponseBuffer = new byte[4];
                        int    myResponseLength = 0;
                        do
                        {
                            myResponseLength = stream.Read(myResponseBuffer, 0, myResponseBuffer.Length);
                        }while (stream.DataAvailable);
                        MyResponse myResponse = MyResponse.ParseFrom(myResponseBuffer);
                        Console.WriteLine("CLIENT : 成功获取结果, myResponse.Version={0}, myResponse.Result={1}", myResponse.Version, myResponse.Result);

                        //关闭
                        stream.Close();
                    }
                    client.Close();
                    Console.WriteLine("CLIENT : 关闭 ...");
                }
            }
            catch (Exception error)
            {
                Console.WriteLine("CLIENT ERROR : {0}", error.ToString());
            }
        }
        static void Main(string[] args)
        {
            args    = new[] { "" };
            args[0] = "listen";

            switch (args[0].ToLower())
            {
            case "listen":
            {
                ////using (RpcServer.CreateRpc(IID, new Impersonation(new MyService.ServerStub(new Implementation())))
                //using (RpcServer.CreateRpc(IID, new Impersonation(new Greeter.ServerStub(new Implementation_Greeter())))
                //    //.AddAuthentication(CSharpTest.Net.RpcLibrary.RpcAuthentication.RPC_C_AUTHN_NONE)
                //    //.AddAuthNegotiate()
                //    .AddProtocol("ncacn_ip_tcp", "50051")
                //    //.AddProtocol("ncacn_np", @"\pipe\Greeter")
                //    ////.AddProtocol("ncalrpc", "MyService")
                //    //.AddProtocol("ncalrpc", "Greeter")
                //    .StartListening())
                //{
                //    Console.WriteLine("Waiting for connections...");
                //    Console.ReadLine();
                //}


                Guid iid = Marshal.GenerateGuidForType(typeof(IGreeter));
                using (RpcServer.CreateRpc(iid, new Greeter.ServerStub(new Anonymous_Greeter()))
                       //.AddAuthNegotiate()
                       .AddAuthentication(CSharpTest.Net.RpcLibrary.RpcAuthentication.RPC_C_AUTHN_NONE)
                       .AddAuthNegotiate()
                       .AddProtocol("ncacn_ip_tcp", "50051")
                       //.AddProtocol("ncalrpc", "Greeter")
                       .StartListening())
                {
                    Console.WriteLine("Waiting for connections...");
                    string name = "123";         // Console.ReadLine();


                    using (Greeter client = new Greeter(RpcClient
                                                        .ConnectRpc(iid, "ncacn_ip_tcp", @"localhost", "50051")
                                                        .Authenticate(RpcAuthenticationType.Self)
                                                        //.Authenticate(RpcAuthenticationType.None)
                                                        ))
                    {
                        HelloReply response = client.SayHello(HelloRequest.CreateBuilder().SetName(name).Build());
                        Console.WriteLine("OK: " + response.Message);
                    }
                    Console.ReadLine();
                }

                //Guid iid = Marshal.GenerateGuidForType(typeof(IBookService));
                //using (RpcServer.CreateRpc(iid, new BookService.ServerStub(new Anonymous_BookService()))
                //    .AddProtocol("ncacn_ip_tcp", "50051")
                //    .AddProtocol("ncalrpc", "BookService")
                //    .StartListening())
                //{
                //    Console.WriteLine("Waiting for connections...");
                //    Console.ReadLine();
                //}


                break;
            }

            case "send-lrpc":
            {
                using (MyService client = new MyService(
                           RpcClient.ConnectRpc(IID, "ncalrpc", null, "MyService")
                           .Authenticate(RpcAuthenticationType.Self)))
                {
                    MyResponse response = client.Send(
                        MyRequest.CreateBuilder().SetMessage("Hello via LRPC!").Build());
                }
                break;
            }

            case "send-tcp":
            {
                using (MyService client = new MyService(
                           RpcClient.ConnectRpc(IID, "ncacn_ip_tcp", @"localhost", "8080")
                           .Authenticate(RpcAuthenticationType.Self)))
                {
                    MyResponse response = client.Send(
                        MyRequest.CreateBuilder().SetMessage("Hello via Tcp/Ip!").Build());
                }
                break;
            }

            case "send-np":
            {
                using (MyService client = new MyService(
                           RpcClient.ConnectRpc(IID, "ncacn_np", @"\\localhost", @"\pipe\MyService")
                           .Authenticate(RpcAuthenticationType.Self)))
                {
                    MyResponse response = client.Send(
                        MyRequest.CreateBuilder().SetMessage("Hello via Named Pipe!").Build());
                }
                break;
            }

            case "send-anon":
            {
                using (MyService client = new MyService(
                           RpcClient.ConnectRpc(IID, "ncacn_np", @"\\localhost", @"\pipe\MyService")
                           .Authenticate(RpcAuthenticationType.Anonymous)))
                {
                    try
                    {
                        MyResponse response = client.Send(
                            MyRequest.CreateBuilder().SetMessage("Hello from Anonymous!").Build());
                    }
                    catch (Exception e)
                    {
                        Console.Error.WriteLine(e);
                    }
                }
                break;
            }
            }
        }