client-call-clientstreaming.swift 1.6 KB

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