| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- /*
- * Copyright 2016, gRPC Authors All rights reserved.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- #if SWIFT_PACKAGE
- import CgRPC
- #endif
- import Foundation
- /// A gRPC Channel
- public class Channel {
- /// Pointer to underlying C representation
- private let underlyingChannel: UnsafeMutableRawPointer
- /// Completion queue for channel call operations
- private let completionQueue: CompletionQueue
- /// Timeout for new calls
- public var timeout: TimeInterval = 600.0
- /// Default host to use for new calls
- public var host: String
-
- public var connectivityState: ConnectivityState? {
- return ConnectivityState.fromCEnum(cgrpc_channel_check_connectivity_state(underlyingChannel, 0))
- }
- /// Initializes a gRPC channel
- ///
- /// - Parameter address: the address of the server to be called
- /// - Parameter secure: if true, use TLS
- /// - Parameter arguments: list of channel configuration options
- public init(address: String, secure: Bool = true, arguments: [Argument] = []) {
- gRPC.initialize()
- host = address
- let argumentWrappers = arguments.map { $0.toCArg() }
- var argumentValues = argumentWrappers.map { $0.wrapped }
- if secure {
- underlyingChannel = cgrpc_channel_create_secure(address, roots_pem(), &argumentValues, Int32(arguments.count))
- } else {
- underlyingChannel = cgrpc_channel_create(address, &argumentValues, Int32(arguments.count))
- }
- completionQueue = CompletionQueue(
- underlyingCompletionQueue: cgrpc_channel_completion_queue(underlyingChannel), name: "Client")
- completionQueue.run() // start a loop that watches the channel's completion queue
- }
- /// Initializes a gRPC channel
- ///
- /// - Parameter address: the address of the server to be called
- /// - Parameter certificates: a PEM representation of certificates to use
- /// - Parameter arguments: list of channel configuration options
- public init(address: String, certificates: String, arguments: [Argument] = []) {
- gRPC.initialize()
- self.host = address
- let argumentWrappers = arguments.map { $0.toCArg() }
- var argumentValues = argumentWrappers.map { $0.wrapped }
- underlyingChannel = cgrpc_channel_create_secure(address, certificates, &argumentValues, Int32(arguments.count))
- completionQueue = CompletionQueue(
- underlyingCompletionQueue: cgrpc_channel_completion_queue(underlyingChannel), name: "Client")
- completionQueue.run() // start a loop that watches the channel's completion queue
- }
- deinit {
- cgrpc_channel_destroy(underlyingChannel)
- completionQueue.shutdown()
- }
- /// Constructs a Call object to make a gRPC API call
- ///
- /// - Parameter method: the gRPC method name for the call
- /// - Parameter host: the gRPC host name for the call. If unspecified, defaults to the Client host
- /// - Parameter timeout: a timeout value in seconds
- /// - Returns: a Call object that can be used to perform the request
- public func makeCall(_ method: String, host: String = "", timeout: TimeInterval? = nil) -> Call {
- let host = (host == "") ? self.host : host
- let timeout = timeout ?? self.timeout
- let underlyingCall = cgrpc_channel_create_call(underlyingChannel, method, host, timeout)!
- return Call(underlyingCall: underlyingCall, owned: true, completionQueue: completionQueue)
- }
- }
|