Channel.swift 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 let underlyingChannel: UnsafeMutableRawPointer
  24. /// Completion queue for channel call operations
  25. private let 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. public var connectivityState: ConnectivityState? {
  31. return ConnectivityState.fromCEnum(cgrpc_channel_check_connectivity_state(underlyingChannel, 0))
  32. }
  33. /// Initializes a gRPC channel
  34. ///
  35. /// - Parameter address: the address of the server to be called
  36. /// - Parameter secure: if true, use TLS
  37. /// - Parameter arguments: list of channel configuration options
  38. public init(address: String, secure: Bool = true, arguments: [Argument] = []) {
  39. host = address
  40. var cargs = arguments.map { $0.toCArg() }
  41. if secure {
  42. underlyingChannel = cgrpc_channel_create_secure(address, roots_pem(), &cargs, Int32(arguments.count))
  43. } else {
  44. underlyingChannel = cgrpc_channel_create(address, &cargs, Int32(arguments.count))
  45. }
  46. completionQueue = CompletionQueue(
  47. underlyingCompletionQueue: cgrpc_channel_completion_queue(underlyingChannel), name: "Client")
  48. completionQueue.run() // start a loop that watches the channel's completion queue
  49. }
  50. /// Initializes a gRPC channel
  51. ///
  52. /// - Parameter address: the address of the server to be called
  53. /// - Parameter certificates: a PEM representation of certificates to use
  54. /// - Parameter arguments: list of channel configuration options
  55. public init(address: String, certificates: String, arguments: [Argument] = []) {
  56. self.host = address
  57. var cargs = arguments.map { $0.toCArg() }
  58. underlyingChannel = cgrpc_channel_create_secure(address, certificates, &cargs, Int32(arguments.count))
  59. completionQueue = CompletionQueue(
  60. underlyingCompletionQueue: cgrpc_channel_completion_queue(underlyingChannel), name: "Client")
  61. completionQueue.run() // start a loop that watches the channel's completion queue
  62. }
  63. deinit {
  64. cgrpc_channel_destroy(underlyingChannel)
  65. completionQueue.shutdown()
  66. }
  67. /// Constructs a Call object to make a gRPC API call
  68. ///
  69. /// - Parameter method: the gRPC method name for the call
  70. /// - Parameter host: the gRPC host name for the call. If unspecified, defaults to the Client host
  71. /// - Parameter timeout: a timeout value in seconds
  72. /// - Returns: a Call object that can be used to perform the request
  73. public func makeCall(_ method: String, host: String = "") -> Call {
  74. let host = (host == "") ? self.host : host
  75. let underlyingCall = cgrpc_channel_create_call(underlyingChannel, method, host, timeout)!
  76. return Call(underlyingCall: underlyingCall, owned: true, completionQueue: completionQueue)
  77. }
  78. }