Channel.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. *
  3. * Copyright 2016, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #if SWIFT_PACKAGE
  34. import CgRPC
  35. #endif
  36. import Foundation
  37. /// A gRPC Channel
  38. public class Channel {
  39. /// Pointer to underlying C representation
  40. private var underlyingChannel: UnsafeMutableRawPointer
  41. /// Completion queue for channel call operations
  42. private var completionQueue: CompletionQueue
  43. /// Timeout for new calls
  44. public var timeout: TimeInterval = 600.0
  45. /// Default host to use for new calls
  46. public var host: String
  47. /// Initializes a gRPC channel
  48. ///
  49. /// - Parameter address: the address of the server to be called
  50. public init(address: String) {
  51. self.host = address
  52. underlyingChannel = cgrpc_channel_create(address)
  53. completionQueue = CompletionQueue(underlyingCompletionQueue:cgrpc_channel_completion_queue(underlyingChannel))
  54. completionQueue.name = "Client" // only for debugging
  55. self.completionQueue.run() // start a loop that watches the channel's completion queue
  56. }
  57. /// Initializes a gRPC channel
  58. ///
  59. /// - Parameter address: the address of the server to be called
  60. public init(address: String, certificates: String?, host: String?) {
  61. self.host = address
  62. if certificates == nil {
  63. let bundle = Bundle(for: Channel.self)
  64. let url = bundle.url(forResource: "roots", withExtension: "pem")!
  65. let data = try! Data(contentsOf: url)
  66. let s = String(data: data, encoding: .ascii)
  67. underlyingChannel = cgrpc_channel_create_secure(address, s, host)
  68. } else {
  69. underlyingChannel = cgrpc_channel_create_secure(address, certificates, host)
  70. }
  71. completionQueue = CompletionQueue(underlyingCompletionQueue:cgrpc_channel_completion_queue(underlyingChannel))
  72. completionQueue.name = "Client" // only for debugging
  73. self.completionQueue.run() // start a loop that watches the channel's completion queue
  74. }
  75. deinit {
  76. cgrpc_channel_destroy(underlyingChannel)
  77. }
  78. /// Constructs a Call object to make a gRPC API call
  79. ///
  80. /// - Parameter method: the gRPC method name for the call
  81. /// - Parameter host: the gRPC host name for the call. If unspecified, defaults to the Client host
  82. /// - Parameter timeout: a timeout value in seconds
  83. /// - Returns: a Call object that can be used to perform the request
  84. public func makeCall(_ method:String, host:String="") -> Call {
  85. let host = (host == "") ? self.host : host
  86. let underlyingCall = cgrpc_channel_create_call(underlyingChannel, method, host, timeout)!
  87. return Call(underlyingCall:underlyingCall, owned:true, completionQueue:self.completionQueue)
  88. }
  89. }