1 #region Copyright notice and license 2 3 // Copyright 2019 The gRPC Authors 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 17 #endregion 18 19 using UnityEngine; 20 using System.Threading.Tasks; 21 using System; 22 using Grpc.Core; 23 using Helloworld; 24 25 class HelloWorldTest 26 { 27 // Can be run from commandline. 28 // Example command: 29 // "/Applications/Unity/Unity.app/Contents/MacOS/Unity -quit -batchmode -nographics -executeMethod HelloWorldTest.RunHelloWorld -logfile" RunHelloWorld()30 public static void RunHelloWorld() 31 { 32 Application.SetStackTraceLogType(LogType.Log, StackTraceLogType.None); 33 34 Debug.Log("=============================================================="); 35 Debug.Log("Starting tests"); 36 Debug.Log("=============================================================="); 37 38 Debug.Log("Application.platform: " + Application.platform); 39 Debug.Log("Environment.OSVersion: " + Environment.OSVersion); 40 41 var reply = Greet("Unity"); 42 Debug.Log("Greeting: " + reply.Message); 43 44 Debug.Log("=============================================================="); 45 Debug.Log("Tests finished successfully."); 46 Debug.Log("=============================================================="); 47 } 48 Greet(string greeting)49 public static HelloReply Greet(string greeting) 50 { 51 const int Port = 50051; 52 53 Server server = new Server 54 { 55 Services = { Greeter.BindService(new GreeterImpl()) }, 56 Ports = { new ServerPort("localhost", Port, ServerCredentials.Insecure) } 57 }; 58 server.Start(); 59 60 Channel channel = new Channel("127.0.0.1:50051", ChannelCredentials.Insecure); 61 62 var client = new Greeter.GreeterClient(channel); 63 64 var reply = client.SayHello(new HelloRequest { Name = greeting }); 65 66 channel.ShutdownAsync().Wait(); 67 68 server.ShutdownAsync().Wait(); 69 70 return reply; 71 } 72 73 class GreeterImpl : Greeter.GreeterBase 74 { 75 // Server side handler of the SayHello RPC SayHello(HelloRequest request, ServerCallContext context)76 public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context) 77 { 78 return Task.FromResult(new HelloReply { Message = "Hello " + request.Name }); 79 } 80 } 81 } 82