client-call-clientstreaming.swift 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // {{ method.name }} (Client 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. // Call this to start a call.
  9. fileprivate func run(metadata:Metadata) throws -> {{ .|call:protoFile,service,method }} {
  10. let done = NSCondition()
  11. try self.call.start(.clientStreaming,
  12. metadata:metadata)
  13. {callResult in
  14. done.lock()
  15. done.signal()
  16. done.unlock()
  17. }
  18. done.lock()
  19. done.wait()
  20. done.unlock()
  21. return self
  22. }
  23. // Call this to send each message in the request stream.
  24. public func Send(_ message: {{ method|input }}) throws {
  25. let messageData = try message.serializeProtobuf()
  26. try call.sendMessage(data:messageData)
  27. }
  28. // Call this to close the connection and wait for a response. Blocks.
  29. public func CloseAndReceive() throws -> {{ method|output }} {
  30. var returnError : {{ .|clienterror:protoFile,service }}?
  31. var returnResponse : {{ method|output }}!
  32. let done = NSCondition()
  33. do {
  34. try call.receiveMessage() {(responseData) in
  35. if let responseData = responseData,
  36. let response = try? {{ method|output }}(protobuf:responseData) {
  37. returnResponse = response
  38. } else {
  39. returnError = {{ .|clienterror:protoFile,service }}.invalidMessageReceived
  40. }
  41. done.lock()
  42. done.signal()
  43. done.unlock()
  44. }
  45. try call.close(completion:{})
  46. done.lock()
  47. done.wait()
  48. done.unlock()
  49. } catch (let error) {
  50. throw error
  51. }
  52. if let returnError = returnError {
  53. throw returnError
  54. }
  55. return returnResponse
  56. }
  57. }