client-call-unary.swift 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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 response : {{ method|output }}?
  14. let requestData = try request.serializeProtobuf()
  15. try call.start(.unary,
  16. metadata:metadata,
  17. message:requestData)
  18. {(_callResult) in
  19. callResult = _callResult
  20. if let responseData = callResult.resultData {
  21. response = try? {{ method|output }}(protobuf:responseData)
  22. }
  23. done.lock()
  24. done.signal()
  25. done.unlock()
  26. }
  27. done.lock()
  28. done.wait()
  29. done.unlock()
  30. if let response = response {
  31. return response
  32. } else {
  33. throw {{ .|clienterror:protoFile,service }}.error(c: callResult)
  34. }
  35. }
  36. }