client-call-unary.swift 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. // {{ method.name }} (Unary)
  2. public class {{ .|call:protoFile,service,method }} {
  3. private var call : Call
  4. /// Create a call.
  5. fileprivate init(_ channel: Channel) {
  6. self.call = channel.makeCall("{{ .|path:protoFile,service,method }}")
  7. }
  8. /// Run the call. Blocks until the reply is received.
  9. fileprivate func run(request: {{ method|input }},
  10. metadata: Metadata) throws -> {{ method|output }} {
  11. let done = NSCondition()
  12. var callResult : CallResult!
  13. var responseMessage : {{ method|output }}?
  14. let requestMessageData = try request.serializeProtobuf()
  15. try call.perform(message: requestMessageData,
  16. metadata: metadata)
  17. {(_callResult) in
  18. callResult = _callResult
  19. if let messageData = callResult.resultData {
  20. responseMessage = try? {{ method|output }}(protobuf:messageData)
  21. }
  22. done.lock()
  23. done.signal()
  24. done.unlock()
  25. }
  26. done.lock()
  27. done.wait()
  28. done.unlock()
  29. if let responseMessage = responseMessage {
  30. return responseMessage
  31. } else {
  32. throw {{ .|clienterror:protoFile,service }}.error(c: callResult)
  33. }
  34. }
  35. }