Browse Source

PR changes

SebastianThiebaud 7 years ago
parent
commit
0ff2fff385

+ 10 - 10
Sources/CgRPC/shim/cgrpc.h

@@ -125,20 +125,20 @@ typedef enum grpc_arg_type {
 } grpc_arg_type;
 
 typedef struct grpc_arg_pointer_vtable {
-  void* (*copy)(void* p);
-  void (*destroy)(void* p);
-  int (*cmp)(void* p, void* q);
+  void *(*copy)(void *p);
+  void (*destroy)(void *p);
+  int (*cmp)(void *p, void *q);
 } grpc_arg_pointer_vtable;
 
 typedef struct grpc_arg {
   grpc_arg_type type;
-  char* key;
+  char *key;
   union grpc_arg_value {
-    char* string;
+    char *string;
     int integer;
     struct grpc_arg_pointer {
-      void* p;
-      const grpc_arg_pointer_vtable* vtable;
+      void *p;
+      const grpc_arg_pointer_vtable *vtable;
     } pointer;
   } value;
 } grpc_arg;
@@ -151,7 +151,7 @@ void grpc_shutdown(void);
 const char *grpc_version_string(void);
 const char *grpc_g_stands_for(void);
 
-char* gpr_strdup(const char* src);
+char *gpr_strdup(const char *src);
 
 void cgrpc_completion_queue_drain(cgrpc_completion_queue *cq);
 void grpc_completion_queue_destroy(cgrpc_completion_queue *cq);
@@ -162,11 +162,11 @@ void cgrpc_free_copied_string(char *string);
 // channel support
 cgrpc_channel *cgrpc_channel_create(const char *address, 
                                     grpc_arg *args,
-                                    int number_args);
+                                    int num_args);
 cgrpc_channel *cgrpc_channel_create_secure(const char *address,
                                            const char *pem_root_certs,
                                            grpc_arg *args,
-                                           int number_args);
+                                           int num_args);
 
 void cgrpc_channel_destroy(cgrpc_channel *channel);
 cgrpc_call *cgrpc_channel_create_call(cgrpc_channel *channel,

+ 2 - 0
Sources/CgRPC/shim/channel.c

@@ -35,6 +35,7 @@ cgrpc_channel *cgrpc_channel_create(const char *address,
   // create the channel
   c->channel = grpc_insecure_channel_create(address, channel_args, NULL);
   c->completion_queue = grpc_completion_queue_create_for_next(NULL);
+  gpr_free(channel_args);
   return c;
 }
 
@@ -52,6 +53,7 @@ cgrpc_channel *cgrpc_channel_create_secure(const char *address,
   grpc_channel_credentials *creds = grpc_ssl_credentials_create(pem_root_certs, NULL, NULL);
   c->channel = grpc_secure_channel_create(creds, address, channel_args, NULL);
   c->completion_queue = grpc_completion_queue_create_for_next(NULL);
+  gpr_free(channel_args);
   return c;
 }
 

+ 2 - 2
Sources/Examples/Echo/main.swift

@@ -38,11 +38,11 @@ func buildEchoService(_ ssl: Bool, _ address: String, _ port: String, _: String)
   if ssl {
     let certificateURL = URL(fileURLWithPath: "ssl.crt")
     let certificates = try! String(contentsOf: certificateURL)
-    let args: [Arg] = [.sslTargetNameOverride("example.com")]
+    let arguments: [Channel.Argument] = [.sslTargetNameOverride("example.com")]
 
     service = Echo_EchoServiceClient(address: address + ":" + port,
                                certificates: certificates,
-                               args: args)
+                               arguments: arguments)
     service.host = "example.com"
   } else {
     service = Echo_EchoServiceClient(address: address + ":" + port, secure: false)

+ 0 - 138
Sources/SwiftGRPC/Core/Arg.swift

@@ -1,138 +0,0 @@
-/*
- * 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 // for String.Encoding
-
-public enum Arg {
-  /// Default authority to pass if none specified on call construction.
-  case defaultAuthority(String)
-
-  /// Primary user agent. Goes at the start of the user-agent metadata sent
-  /// on each request.
-  case primaryUserAgent(String)
-
-  /// Secondary user agent. Goes at the end of the user-agent metadata sent
-  /// on each request.
-  case secondaryUserAgent(String)
-
-  /// After a duration of this time, the client/server pings its peer to see
-  /// if the transport is still alive.
-  case keepAliveTime(TimeInterval)
-
-  /// After waiting for a duration of this time, if the keepalive ping sender does
-  /// not receive the ping ack, it will close the transport.
-  case keepAliveTimeout(TimeInterval)
-
-  /// Is it permissible to send keepalive pings without any outstanding streams?
-  case keepAlivePermitWithoutCalls(Bool)
-
-  /// The time between the first and second connection attempts.
-  case reconnectBackoffInitial(TimeInterval)
-
-  /// The minimum time between subsequent connection attempts.
-  case reconnectBackoffMin(TimeInterval)
-
-  /// The maximum time between subsequent connection attempts.
-  case reconnectBackoffMax(TimeInterval)
-
-  /// Should we allow receipt of true-binary data on http2 connections?
-  /// Defaults to on (true)
-  case http2EnableTrueBinary(Bool)
-
-  /// Minimum time between sending successive ping frames without receiving
-  /// any data frame.
-  case http2MinSentPingInterval(TimeInterval)
-
-  /// Number of pings before needing to send a data frame or header frame.
-  /// `0` indicates that an infinite number of pings can be sent without
-  /// sending a data frame or header frame.
-  case http2MaxPingsWithoutData(UInt)
-
-  /// This *should* be used for testing only.
-  /// Override the target name used for SSL host name checking using this
-  /// channel argument. If this argument is not specified, the name used
-  /// for SSL host name checking will be the target parameter (assuming that the
-  /// secure channel is an SSL channel). If this parameter is specified and the
-  /// underlying is not an SSL channel, it will just be ignored.
-  case sslTargetNameOverride(String)
-}
-
-extension Arg {
-  func toCArg() -> grpc_arg {
-    switch self {
-    case let .defaultAuthority(value):
-      return arg("grpc.default_authority", value: value)
-    case let .primaryUserAgent(value):
-      return arg("grpc.primary_user_agent", value: value)
-    case let .secondaryUserAgent(value):
-      return arg("grpc.secondary_user_agent", value: value)
-    case let .keepAliveTime(value):
-      return arg("grpc.keepalive_time_ms", value: value * 1_000)
-    case let .keepAliveTimeout(value):
-      return arg("grpc.keepalive_timeout_ms", value: value * 1_000)
-    case let .keepAlivePermitWithoutCalls(value):
-      return arg("grpc.keepalive_permit_without_calls", value: value)
-    case let .reconnectBackoffMin(value):
-      return arg("grpc.min_reconnect_backoff_ms", value: value * 1_000)
-    case let .reconnectBackoffMax(value):
-      return arg("grpc.max_reconnect_backoff_ms", value: value * 1_000)
-    case let .reconnectBackoffInitial(value):
-      return arg("grpc.initial_reconnect_backoff_ms", value: value * 1_000)
-    case let .http2EnableTrueBinary(value):
-      return arg("grpc.http2.true_binary", value: value)
-    case let .http2MinSentPingInterval(value):
-      return arg("grpc.http2.min_time_between_pings_ms", value: value * 1_000)
-    case let .http2MaxPingsWithoutData(value):
-      return arg("grpc.http2.max_pings_without_data", value: value)
-    case let .sslTargetNameOverride(value):
-      return arg("grpc.ssl_target_name_override", value: value)
-    }
-  }
-
-  private func arg(_ key: String, value: String) -> grpc_arg {
-    var arg = grpc_arg()
-    arg.key = gpr_strdup(key)
-    arg.type = GRPC_ARG_STRING
-    arg.value.string = gpr_strdup(value)
-    return arg
-  }
-
-  private func arg(_ key: String, value: Bool) -> grpc_arg {
-    return arg(key, value: Int32(value ? 1 : 0))
-  }
-
-  private func arg(_ key: String, value: Double) -> grpc_arg {
-    return arg(key, value: Int32(value))
-  }
-
-  private func arg(_ key: String, value: UInt) -> grpc_arg {
-    return arg(key, value: Int32(value))
-  }
-
-  private func arg(_ key: String, value: Int) -> grpc_arg {
-    return arg(key, value: Int32(value))
-  }
-
-  private func arg(_ key: String, value: Int32) -> grpc_arg {
-    var arg = grpc_arg()
-    arg.key = gpr_strdup(key)
-    arg.type = GRPC_ARG_INTEGER
-    arg.value.integer = value
-    return arg
-  }
-}

+ 9 - 9
Sources/SwiftGRPC/Core/Channel.swift

@@ -40,15 +40,15 @@ public class Channel {
   ///
   /// - Parameter address: the address of the server to be called
   /// - Parameter secure: if true, use TLS
-  /// - Parameter args: list of arguments
-  public init(address: String, secure: Bool = true, args: [Arg] = []) {
+  /// - Parameter arguments: list of channel configuration options
+  public init(address: String, secure: Bool = true, arguments: [Argument] = []) {
     host = address
-    var cargs = args.map({ $0.toCArg() })
+    var cargs = arguments.map { $0.toCArg() }
 
     if secure {
-      underlyingChannel = cgrpc_channel_create_secure(address, roots_pem(), &cargs, Int32(args.count))
+      underlyingChannel = cgrpc_channel_create_secure(address, roots_pem(), &cargs, Int32(arguments.count))
     } else {
-      underlyingChannel = cgrpc_channel_create(address, &cargs, Int32(args.count))
+      underlyingChannel = cgrpc_channel_create(address, &cargs, Int32(arguments.count))
     }
     completionQueue = CompletionQueue(
       underlyingCompletionQueue: cgrpc_channel_completion_queue(underlyingChannel), name: "Client")
@@ -59,12 +59,12 @@ public class Channel {
   ///
   /// - Parameter address: the address of the server to be called
   /// - Parameter certificates: a PEM representation of certificates to use
-  /// - Parameter args: list of arguments
-  public init(address: String, certificates: String, args: [Arg] = []) {
+  /// - Parameter arguments: list of channel configuration options
+  public init(address: String, certificates: String, arguments: [Argument] = []) {
     self.host = address
-    var cargs = args.map({ $0.toCArg() })
+    var cargs = arguments.map { $0.toCArg() }
 
-    underlyingChannel = cgrpc_channel_create_secure(address, certificates, &cargs, Int32(args.count))
+    underlyingChannel = cgrpc_channel_create_secure(address, certificates, &cargs, 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

+ 140 - 0
Sources/SwiftGRPC/Core/ChannelArgument.swift

@@ -0,0 +1,140 @@
+/*
+ * Copyright 2018, 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 // for String.Encoding
+
+public extension Channel {
+  enum Argument {
+    /// Default authority to pass if none specified on call construction.
+    case defaultAuthority(String)
+
+    /// Primary user agent. Goes at the start of the user-agent metadata sent
+    /// on each request.
+    case primaryUserAgent(String)
+
+    /// Secondary user agent. Goes at the end of the user-agent metadata sent
+    /// on each request.
+    case secondaryUserAgent(String)
+
+    /// After a duration of this time, the client/server pings its peer to see
+    /// if the transport is still alive.
+    case keepAliveTime(TimeInterval)
+
+    /// After waiting for a duration of this time, if the keepalive ping sender does
+    /// not receive the ping ack, it will close the transport.
+    case keepAliveTimeout(TimeInterval)
+
+    /// Is it permissible to send keepalive pings without any outstanding streams?
+    case keepAlivePermitWithoutCalls(Bool)
+
+    /// The time between the first and second connection attempts.
+    case reconnectBackoffInitial(TimeInterval)
+
+    /// The minimum time between subsequent connection attempts.
+    case reconnectBackoffMin(TimeInterval)
+
+    /// The maximum time between subsequent connection attempts.
+    case reconnectBackoffMax(TimeInterval)
+
+    /// Should we allow receipt of true-binary data on http2 connections?
+    /// Defaults to on (true)
+    case http2EnableTrueBinary(Bool)
+
+    /// Minimum time between sending successive ping frames without receiving
+    /// any data frame.
+    case http2MinSentPingInterval(TimeInterval)
+
+    /// Number of pings before needing to send a data frame or header frame.
+    /// `0` indicates that an infinite number of pings can be sent without
+    /// sending a data frame or header frame.
+    case http2MaxPingsWithoutData(UInt)
+
+    /// This *should* be used for testing only.
+    /// Override the target name used for SSL host name checking using this
+    /// channel argument. If this argument is not specified, the name used
+    /// for SSL host name checking will be the target parameter (assuming that the
+    /// secure channel is an SSL channel). If this parameter is specified and the
+    /// underlying is not an SSL channel, it will just be ignored.
+    case sslTargetNameOverride(String)
+  }
+}
+
+extension Channel.Argument {
+  func toCArg() -> grpc_arg {
+    switch self {
+    case let .defaultAuthority(value):
+      return makeArgument("grpc.default_authority", value: value)
+    case let .primaryUserAgent(value):
+      return makeArgument("grpc.primary_user_agent", value: value)
+    case let .secondaryUserAgent(value):
+      return makeArgument("grpc.secondary_user_agent", value: value)
+    case let .keepAliveTime(value):
+      return makeArgument("grpc.keepalive_time_ms", value: value * 1_000)
+    case let .keepAliveTimeout(value):
+      return makeArgument("grpc.keepalive_timeout_ms", value: value * 1_000)
+    case let .keepAlivePermitWithoutCalls(value):
+      return makeArgument("grpc.keepalive_permit_without_calls", value: value)
+    case let .reconnectBackoffMin(value):
+      return makeArgument("grpc.min_reconnect_backoff_ms", value: value * 1_000)
+    case let .reconnectBackoffMax(value):
+      return makeArgument("grpc.max_reconnect_backoff_ms", value: value * 1_000)
+    case let .reconnectBackoffInitial(value):
+      return makeArgument("grpc.initial_reconnect_backoff_ms", value: value * 1_000)
+    case let .http2EnableTrueBinary(value):
+      return makeArgument("grpc.http2.true_binary", value: value)
+    case let .http2MinSentPingInterval(value):
+      return makeArgument("grpc.http2.min_time_between_pings_ms", value: value * 1_000)
+    case let .http2MaxPingsWithoutData(value):
+      return makeArgument("grpc.http2.max_pings_without_data", value: value)
+    case let .sslTargetNameOverride(value):
+      return makeArgument("grpc.ssl_target_name_override", value: value)
+    }
+  }
+}
+
+private func makeArgument(_ key: String, value: String) -> grpc_arg {
+  var arg = grpc_arg()
+  arg.key = gpr_strdup(key)
+  arg.type = GRPC_ARG_STRING
+  arg.value.string = gpr_strdup(value)
+  return arg
+}
+
+private func makeArgument(_ key: String, value: Bool) -> grpc_arg {
+  return makeArgument(key, value: Int32(value ? 1 : 0))
+}
+
+private func makeArgument(_ key: String, value: Double) -> grpc_arg {
+  return makeArgument(key, value: Int32(value))
+}
+
+private func makeArgument(_ key: String, value: UInt) -> grpc_arg {
+  return makeArgument(key, value: Int32(value))
+}
+
+private func makeArgument(_ key: String, value: Int) -> grpc_arg {
+  return makeArgument(key, value: Int32(value))
+}
+
+private func makeArgument(_ key: String, value: Int32) -> grpc_arg {
+  var arg = grpc_arg()
+  arg.key = gpr_strdup(key)
+  arg.type = GRPC_ARG_INTEGER
+  arg.value.integer = value
+  return arg
+}

+ 4 - 4
Sources/SwiftGRPC/Runtime/ServiceClient.swift

@@ -49,16 +49,16 @@ open class ServiceClientBase: ServiceClient {
   }
 
   /// Create a client.
-  public init(address: String, secure: Bool = true, args: [Arg] = []) {
+  public init(address: String, secure: Bool = true, arguments: [Channel.Argument] = []) {
     gRPC.initialize()
-    channel = Channel(address: address, secure: secure, args: args)
+    channel = Channel(address: address, secure: secure, arguments: arguments)
     metadata = Metadata()
   }
 
   /// Create a client that makes secure connections with a custom certificate.
-  public init(address: String, certificates: String, args: [Arg] = []) {
+  public init(address: String, certificates: String, arguments: [Channel.Argument] = []) {
     gRPC.initialize()
-    channel = Channel(address: address, certificates: certificates, args: args)
+    channel = Channel(address: address, certificates: certificates, arguments: arguments)
     metadata = Metadata()
   }
 }

+ 1 - 1
Tests/SwiftGRPCTests/BasicEchoTestCase.swift

@@ -53,7 +53,7 @@ class BasicEchoTestCase: XCTestCase {
                                keyString: String(data: keyForTests, encoding: .utf8)!,
                                provider: provider)
       server.start(queue: DispatchQueue.global())
-      client = Echo_EchoServiceClient(address: address, certificates: certificateString, args: [Arg.sslTargetNameOverride("example.com")])
+      client = Echo_EchoServiceClient(address: address, certificates: certificateString, arguments: [.sslTargetNameOverride("example.com")])
       client.host = "example.com"
     } else {
       server = Echo_EchoServer(address: address, provider: provider)

+ 1 - 1
Tests/SwiftGRPCTests/GRPCTests.swift

@@ -146,7 +146,7 @@ func runClient(useSSL: Bool) throws {
   if useSSL {
     channel = Channel(address: address,
                       certificates: String(data: certificateForTests, encoding: .utf8)!,
-                      args: [Arg.sslTargetNameOverride(host)])
+                      arguments: [.sslTargetNameOverride(host)])
   } else {
     channel = Channel(address: address, secure: false)
   }