grpc-swiftの紹介

 grpc-swiftの紹介

E1cf27d9fa565b2867d171f21a39759c?s=128

tikidunpon

May 23, 2018
Tweet

Transcript

  1. 8.

    Protocol Buffersͱ͸ʁ syntax = "proto3"; // όʔδϣϯఆٛ service Greeter {

    // αʔϏεఆٛ rpc SayHello (HelloRequest) returns (HelloReply) {}ɹ // RPCϝιουఆٛ } message HelloRequest {ɹ// ϦΫΤετϝοηʔδఆٛ string name = 1; // ϑΟʔϧυܕ ϑΟʔϧυ໊ ϑΟʔϧυ൪߸ } message HelloReply { // Ϩεϙϯεϝοηʔδ string message = 1; }
  2. 17.

    ετϦʔϛϯάͷछྨ RPC ϦΫΤετ਺ Ϩεϙϯε਺ ར༻ྫ Unary 1 1 ୯७ͳrpc Server

    streaming 1 n ϑΟʔυ഑৴ɺα ʔόʔPush Client streaming n 1 ෳ਺σʔλૹ৴ Bidirectional streaming n n speech-to-textɺν ϟοτ
  3. 36.

    ੜ੒ίʔυʢൈਮʣhelloworld.pb.swift /// The request message containing the user's name. struct

    Greeter_HelloRequest { var name: String = String() var unknownFields = SwiftProtobuf.UnknownStorage() init() {} }
  4. 37.

    ੜ੒ίʔυʢൈਮʣhelloworld.grpc.swift internal protocol Greeter_GreeterSayHelloCall: ClientCallUnary {} fileprivate final class Greeter_GreeterSayHelloCallBase:

    ClientCallUnaryBase<Greeter_HelloRequest, Greeter_HelloReply>, Greeter_GreeterSayHelloCall { override class var method: String { return "/greeter.Greeter/SayHello" } } internal protocol Greeter_GreeterService: ServiceClient { /// Synchronous. Unary. func sayHello(_ request: Greeter_HelloRequest) throws -> Greeter_HelloReply /// Asynchronous. Unary. func sayHello(_ request: Greeter_HelloRequest, completion: @escaping (Greeter_HelloReply?, CallResult) -> Void) throws -> Greeter_GreeterSayHelloCall } internal final class Greeter_GreeterServiceClient: ServiceClientBase, Greeter_GreeterService { //লུ }
  5. 38.

    gRPCαʔόʔ //ϓϩτίϧΛ࣮૷͠·͢ final class GreeterProvider: Greeter_GreeterProvider { func sayHello( request:

    Greeter_HelloRequest, session: Greeter_GreeterSayHelloSession) throws -> Greeter_HelloReply{ var g = Greeter_HelloReply() g.message = request.name + " world" return g } }
  6. 39.

    gRPCΫϥΠΞϯτ class ViewController: UIViewController { let client = Greeter_GreeterServiceClient.init(address: "127.0.0.1:50051",

    secure: false) override func viewDidLoad() { super.viewDidLoad() var request = Greeter_HelloRequest() request.name = "hello" let ret = try? client.sayHello(request) //ಉظॲཧ print("gRPC Server returns " + ret!.message) } }
  7. 40.

    gRPCΫϥΠΞϯτ // ඇಉظॲཧ var request = Greeter_HelloRequest() request.name = "hello"

    let _ = try? client.sayHello(request) { (reply, result) in if result.success, result.statusCode == .ok { print("gRPC Server returns " + reply!.message) } else { print("result \(result)") } }
  8. 41.

    ͪͳΈʹଞͷετϦʔϛϯάํࣜΛ࢖͏ʹ͸ service Echo { //Unary rpc Get(EchoRequest) returns (EchoResponse) {}

    //Server streaming rpc Expand(EchoRequest) returns (stream EchoResponse) {} //Client streaming rpc Collect(stream EchoRequest) returns (EchoResponse) {} //Bidirectional streaming rpc Update(stream EchoRequest) returns (stream EchoResponse) {} }