Upgrade to Pro — share decks privately, control downloads, hide ads and more …

grpc-swiftの紹介

tanako
May 23, 2018

 grpc-swiftの紹介

tanako

May 23, 2018
Tweet

More Decks by tanako

Other Decks in Technology

Transcript

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

    // αʔϏεఆٛ rpc SayHello (HelloRequest) returns (HelloReply) {}ɹ // RPCϝιουఆٛ } message HelloRequest {ɹ// ϦΫΤετϝοηʔδఆٛ string name = 1; // ϑΟʔϧυܕ ϑΟʔϧυ໊ ϑΟʔϧυ൪߸ } message HelloReply { // Ϩεϙϯεϝοηʔδ string message = 1; }
  2. ετϦʔϛϯάͷछྨ RPC ϦΫΤετ਺ Ϩεϙϯε਺ ར༻ྫ Unary 1 1 ୯७ͳrpc Server

    streaming 1 n ϑΟʔυ഑৴ɺα ʔόʔPush Client streaming n 1 ෳ਺σʔλૹ৴ Bidirectional streaming n n speech-to-textɺν ϟοτ
  3. ੜ੒ίʔυʢൈਮʣhelloworld.pb.swift /// The request message containing the user's name. struct

    Greeter_HelloRequest { var name: String = String() var unknownFields = SwiftProtobuf.UnknownStorage() init() {} }
  4. ੜ੒ίʔυʢൈਮʣ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. 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. 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. 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. ͪͳΈʹଞͷετϦʔϛϯάํࣜΛ࢖͏ʹ͸ 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) {} }