Channel.swift 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright 2016, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #if SWIFT_PACKAGE
  17. import CgRPC
  18. #endif
  19. import Foundation
  20. /// A gRPC Channel
  21. public class Channel {
  22. /// Pointer to underlying C representation
  23. private var underlyingChannel: UnsafeMutableRawPointer
  24. /// Completion queue for channel call operations
  25. private var completionQueue: CompletionQueue
  26. /// Timeout for new calls
  27. public var timeout: TimeInterval = 600.0
  28. /// Default host to use for new calls
  29. public var host: String
  30. /// Initializes a gRPC channel
  31. ///
  32. /// - Parameter address: the address of the server to be called
  33. public init(address: String) {
  34. self.host = address
  35. underlyingChannel = cgrpc_channel_create(address)
  36. completionQueue = CompletionQueue(underlyingCompletionQueue:cgrpc_channel_completion_queue(underlyingChannel))
  37. completionQueue.name = "Client" // only for debugging
  38. self.completionQueue.run() // start a loop that watches the channel's completion queue
  39. }
  40. /// Initializes a gRPC channel
  41. ///
  42. /// - Parameter address: the address of the server to be called
  43. public init(address: String, certificates: String?, host: String?) {
  44. self.host = address
  45. if let certificates = certificates {
  46. underlyingChannel = cgrpc_channel_create_secure(address, certificates, host)
  47. } else {
  48. let bundle = Bundle(for: Channel.self)
  49. let url = bundle.url(forResource: "roots", withExtension: "pem")!
  50. let data = try! Data(contentsOf: url)
  51. let s = String(data: data, encoding: .ascii)
  52. underlyingChannel = cgrpc_channel_create_secure(address, s, host)
  53. }
  54. completionQueue = CompletionQueue(underlyingCompletionQueue:cgrpc_channel_completion_queue(underlyingChannel))
  55. completionQueue.name = "Client" // only for debugging
  56. self.completionQueue.run() // start a loop that watches the channel's completion queue
  57. }
  58. deinit {
  59. cgrpc_channel_destroy(underlyingChannel)
  60. }
  61. /// Constructs a Call object to make a gRPC API call
  62. ///
  63. /// - Parameter method: the gRPC method name for the call
  64. /// - Parameter host: the gRPC host name for the call. If unspecified, defaults to the Client host
  65. /// - Parameter timeout: a timeout value in seconds
  66. /// - Returns: a Call object that can be used to perform the request
  67. public func makeCall(_ method:String, host:String="") -> Call {
  68. let host = (host == "") ? self.host : host
  69. let underlyingCall = cgrpc_channel_create_call(underlyingChannel, method, host, timeout)!
  70. return Call(underlyingCall:underlyingCall, owned:true, completionQueue:self.completionQueue)
  71. }
  72. }