client-call-bidistreaming.swift 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // {{ method.name }} (Bidirectional Streaming)
  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. fileprivate func run(metadata:Metadata) throws -> {{ .|call:protoFile,service,method }} {
  9. let latch = CountDownLatch(1)
  10. try self.call.start(.bidiStreaming,
  11. metadata:metadata)
  12. {callResult in
  13. latch.signal()
  14. }
  15. latch.wait()
  16. return self
  17. }
  18. public func receive() throws -> {{ method|output }} {
  19. var returnError : {{ .|clienterror:protoFile,service }}?
  20. var returnMessage : {{ method|output }}!
  21. let latch = CountDownLatch(1)
  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. latch.signal()
  33. }
  34. latch.wait()
  35. }
  36. if let returnError = returnError {
  37. throw returnError
  38. }
  39. return returnMessage
  40. }
  41. public func send(_ message:{{ method|input }}) throws {
  42. let messageData = try message.serializeProtobuf()
  43. try call.sendMessage(data:messageData)
  44. }
  45. public func closeSend() throws {
  46. let latch = CountDownLatch(1)
  47. try call.close() {
  48. latch.signal()
  49. }
  50. latch.wait()
  51. }
  52. }