• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #region Copyright notice and license
2 
3 // Copyright 2018 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 System;
20 using System.Threading.Tasks;
21 
22 using Grpc.Core;
23 using Helloworld;
24 
25 using UIKit;
26 
27 namespace HelloworldXamarin.iOS
28 {
29     public partial class ViewController : UIViewController
30     {
31         const int Port = 50051;
32         int count = 1;
33 
ViewController(IntPtr handle)34         public ViewController(IntPtr handle) : base(handle)
35         {
36         }
37 
ViewDidLoad()38         public override void ViewDidLoad()
39         {
40             base.ViewDidLoad();
41 
42             // Perform any additional setup after loading the view, typically from a nib.
43             Button.AccessibilityIdentifier = "myButton";
44             Button.TouchUpInside += delegate
45             {
46                 var title = SayHello();
47                 Button.SetTitle(title, UIControlState.Normal);
48             };
49         }
50 
DidReceiveMemoryWarning()51         public override void DidReceiveMemoryWarning()
52         {
53             base.DidReceiveMemoryWarning();
54             // Release any cached data, images, etc that aren't in use.
55         }
56 
SayHello()57         private string SayHello()
58         {
59             Server server = new Server
60             {
61                 Services = { Greeter.BindService(new GreeterImpl()) },
62                 Ports = { new ServerPort("localhost", Port, ServerCredentials.Insecure) }
63             };
64             server.Start();
65 
66             Channel channel = new Channel("localhost:50051", ChannelCredentials.Insecure);
67 
68             var client = new Greeter.GreeterClient(channel);
69             string user = "Xamarin " + count;
70 
71             var reply = client.SayHello(new HelloRequest { Name = user });
72 
73             channel.ShutdownAsync().Wait();
74             server.ShutdownAsync().Wait();
75 
76             count++;
77 
78             return "Greeting: " + reply.Message;
79         }
80 
81 
82         class GreeterImpl : Greeter.GreeterBase
83         {
84             // Server side handler of the SayHello RPC
SayHello(HelloRequest request, ServerCallContext context)85             public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
86             {
87               return Task.FromResult(new HelloReply { Message = "Hello " + request.Name });
88             }
89         }
90     }
91 }
92