client-call-serverstreaming.swift 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. //
  2. // {{ method.name }} (Server streaming)
  3. //
  4. public class {{ .|call:protoFile,service,method }} {
  5. var call : Call
  6. fileprivate init(_ channel: Channel) {
  7. self.call = channel.makeCall("{{ .|path:protoFile,service,method }}")
  8. }
  9. // Call this once with the message to send.
  10. fileprivate func run(request: {{ method|input }}, metadata: Metadata) throws -> {{ .|call:protoFile,service,method }} {
  11. let requestMessageData = try! request.serializeProtobuf()
  12. try! call.startServerStreaming(message: requestMessageData,
  13. metadata: metadata,
  14. completion:{(CallResult) in })
  15. return self
  16. }
  17. // Call this to wait for a result. Blocks.
  18. public func Receive() throws -> {{ method|output }} {
  19. var returnError : {{ .|clienterror:protoFile,service }}?
  20. var returnMessage : {{ method|output }}!
  21. let done = NSCondition()
  22. do {
  23. try call.receiveMessage() {(data) in
  24. if let data = data {
  25. returnMessage = try? {{ method|output }}(protobuf:data)
  26. if returnMessage == nil {
  27. returnError = {{ .|clienterror:protoFile,service }}.invalidMessageReceived
  28. }
  29. } else {
  30. returnError = {{ .|clienterror:protoFile,service }}.endOfStream
  31. }
  32. done.lock()
  33. done.signal()
  34. done.unlock()
  35. }
  36. done.lock()
  37. done.wait()
  38. done.unlock()
  39. }
  40. if let returnError = returnError {
  41. throw returnError
  42. }
  43. return returnMessage
  44. }
  45. }