Browse Source

Also extract streaming sending methods into a dedicated `StreamSending` protocol.

Daniel Alm 7 years ago
parent
commit
be3fae437b

+ 5 - 5
Sources/Examples/Echo/Generated/echo.grpc.swift

@@ -72,7 +72,7 @@ internal protocol Echo_EchoUpdateCall: ClientCallBidirectionalStreaming {
   /// Call this to wait for a result. Nonblocking.
   func receive(completion: @escaping (ResultOrRPCError<Echo_EchoResponse?>) -> Void) throws
 
-  /// Call this to send each message in the request stream.
+  /// Call this to send each message in the request stream. Nonblocking.
   func send(_ message: Echo_EchoRequest, completion: @escaping (Error?) -> Void) throws
 
   /// Call this to close the sending connection. Blocking.
@@ -201,8 +201,8 @@ fileprivate final class Echo_EchoGetSessionBase: ServerSessionUnaryBase<Echo_Ech
 class Echo_EchoGetSessionTestStub: ServerSessionUnaryTestStub, Echo_EchoGetSession {}
 
 internal protocol Echo_EchoExpandSession: ServerSessionServerStreaming {
-  /// Send a message. Nonblocking.
-  func send(_ response: Echo_EchoResponse, completion: ((Error?) -> Void)?) throws
+  /// Call this to send each message in the request stream. Nonblocking.
+  func send(_ message: Echo_EchoResponse, completion: @escaping (Error?) -> Void) throws
 }
 
 fileprivate final class Echo_EchoExpandSessionBase: ServerSessionServerStreamingBase<Echo_EchoRequest, Echo_EchoResponse>, Echo_EchoExpandSession {}
@@ -229,8 +229,8 @@ internal protocol Echo_EchoUpdateSession: ServerSessionBidirectionalStreaming {
   /// Call this to wait for a result. Nonblocking.
   func receive(completion: @escaping (ResultOrRPCError<Echo_EchoRequest?>) -> Void) throws
 
-  /// Send a message. Nonblocking.
-  func send(_ response: Echo_EchoResponse, completion: ((Error?) -> Void)?) throws
+  /// Call this to send each message in the request stream. Nonblocking.
+  func send(_ message: Echo_EchoResponse, completion: @escaping (Error?) -> Void) throws
 
   /// Close a connection. Blocks until the connection is closed.
   func close() throws

+ 2 - 1
Sources/SwiftGRPC/Runtime/ClientCallBidirectionalStreaming.swift

@@ -25,8 +25,9 @@ public protocol ClientCallBidirectionalStreaming: ClientCall {
   // as the protocol would then have an associated type requirement (and become pretty much unusable in the process).
 }
 
-open class ClientCallBidirectionalStreamingBase<InputType: Message, OutputType: Message>: ClientCallBase, ClientCallBidirectionalStreaming, StreamReceiving {
+open class ClientCallBidirectionalStreamingBase<InputType: Message, OutputType: Message>: ClientCallBase, ClientCallBidirectionalStreaming, StreamReceiving, StreamSending {
   public typealias ReceivedType = OutputType
+  public typealias SentType = InputType
   
   /// Call this to start a call. Nonblocking.
   public func start(metadata: Metadata, completion: ((CallResult) -> Void)?)

+ 3 - 6
Sources/SwiftGRPC/Runtime/ClientCallClientStreaming.swift

@@ -25,18 +25,15 @@ public protocol ClientCallClientStreaming: ClientCall {
   // as the protocol would then have an associated type requirement (and become pretty much unusable in the process).
 }
 
-open class ClientCallClientStreamingBase<InputType: Message, OutputType: Message>: ClientCallBase, ClientCallClientStreaming {
+open class ClientCallClientStreamingBase<InputType: Message, OutputType: Message>: ClientCallBase, ClientCallClientStreaming, StreamSending {
+  public typealias SentType = InputType
+  
   /// Call this to start a call. Nonblocking.
   public func start(metadata: Metadata, completion: ((CallResult) -> Void)?) throws -> Self {
     try call.start(.clientStreaming, metadata: metadata, completion: completion)
     return self
   }
 
-  public func send(_ message: InputType, completion: @escaping (Error?) -> Void) throws {
-    let messageData = try message.serializedData()
-    try call.sendMessage(data: messageData, completion: completion)
-  }
-
   public func closeAndReceive(completion: @escaping (ResultOrRPCError<OutputType>) -> Void) throws {
     try call.closeAndReceiveMessage { callResult in
       guard let responseData = callResult.resultData else {

+ 4 - 7
Sources/SwiftGRPC/Runtime/ServerSessionBidirectionalStreaming.swift

@@ -22,8 +22,9 @@ public protocol ServerSessionBidirectionalStreaming: ServerSession {
   func waitForSendOperationsToFinish()
 }
 
-open class ServerSessionBidirectionalStreamingBase<InputType: Message, OutputType: Message>: ServerSessionBase, ServerSessionBidirectionalStreaming, StreamReceiving {
+open class ServerSessionBidirectionalStreamingBase<InputType: Message, OutputType: Message>: ServerSessionBase, ServerSessionBidirectionalStreaming, StreamReceiving, StreamSending {
   public typealias ReceivedType = InputType
+  public typealias SentType = OutputType
   
   public typealias ProviderBlock = (ServerSessionBidirectionalStreamingBase) throws -> Void
   private var providerBlock: ProviderBlock
@@ -33,10 +34,6 @@ open class ServerSessionBidirectionalStreamingBase<InputType: Message, OutputTyp
     super.init(handler: handler)
   }
 
-  public func send(_ response: OutputType, completion: ((Error?) -> Void)?) throws {
-    try handler.sendResponse(message: response.serializedData(), completion: completion)
-  }
-
   public func close() throws {
     let sem = DispatchSemaphore(value: 0)
     try handler.sendStatus(statusCode: statusCode,
@@ -78,8 +75,8 @@ open class ServerSessionBidirectionalStreamingTestStub<InputType: Message, Outpu
     inputs.removeFirst()
   }
 
-  open func send(_ response: OutputType, completion _: ((Error?) -> Void)?) throws {
-    outputs.append(response)
+  open func send(_ message: OutputType, completion _: @escaping (Error?) -> Void) throws {
+    outputs.append(message)
   }
 
   open func close() throws {}

+ 5 - 7
Sources/SwiftGRPC/Runtime/ServerSessionServerStreaming.swift

@@ -22,7 +22,9 @@ public protocol ServerSessionServerStreaming: ServerSession {
   func waitForSendOperationsToFinish()
 }
 
-open class ServerSessionServerStreamingBase<InputType: Message, OutputType: Message>: ServerSessionBase, ServerSessionServerStreaming {
+open class ServerSessionServerStreamingBase<InputType: Message, OutputType: Message>: ServerSessionBase, ServerSessionServerStreaming, StreamSending {
+  public typealias SentType = OutputType
+  
   public typealias ProviderBlock = (InputType, ServerSessionServerStreamingBase) throws -> Void
   private var providerBlock: ProviderBlock
 
@@ -31,10 +33,6 @@ open class ServerSessionServerStreamingBase<InputType: Message, OutputType: Mess
     super.init(handler: handler)
   }
 
-  public func send(_ response: OutputType, completion: ((Error?) -> Void)?) throws {
-    try handler.sendResponse(message: response.serializedData(), completion: completion)
-  }
-
   public func run(queue: DispatchQueue) throws {
     try handler.receiveMessage(initialMetadata: initialMetadata) { requestData in
       // TODO(danielalm): Unify this behavior with `ServerSessionBidirectionalStreamingBase.run()`.
@@ -71,8 +69,8 @@ open class ServerSessionServerStreamingBase<InputType: Message, OutputType: Mess
 open class ServerSessionServerStreamingTestStub<OutputType: Message>: ServerSessionTestStub, ServerSessionServerStreaming {
   open var outputs: [OutputType] = []
 
-  open func send(_ response: OutputType, completion _: ((Error?) -> Void)?) throws {
-    outputs.append(response)
+  open func send(_ message: OutputType, completion _: @escaping (Error?) -> Void) throws {
+    outputs.append(message)
   }
 
   open func close() throws {}

+ 31 - 0
Sources/SwiftGRPC/Runtime/StreamSending.swift

@@ -0,0 +1,31 @@
+/*
+ * 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.
+ */
+
+import Dispatch
+import Foundation
+import SwiftProtobuf
+
+public protocol StreamSending {
+  associatedtype SentType: Message
+  
+  var call: Call { get }
+}
+
+extension StreamSending {
+  public func send(_ message: SentType, completion: @escaping (Error?) -> Void) throws {
+    try call.sendMessage(data: message.serializedData(), completion: completion)
+  }
+}

+ 2 - 4
Sources/protoc-gen-swiftgrpc/Generator-Client.swift

@@ -79,8 +79,7 @@ extension Generator {
   private func printServiceClientMethodCallClientStreaming() {
     println("\(options.visibility.sourceSnippet) protocol \(callName): ClientCallClientStreaming {")
     indent()
-    println("/// Call this to send each message in the request stream. Nonblocking.")
-    println("func send(_ message: \(methodInputName), completion: @escaping (Error?) -> Void) throws")
+    printStreamSendMethods(sentType: methodInputName)
     println()
     println("/// Call this to close the connection and wait for a response. Blocking.")
     println("func closeAndReceive() throws -> \(methodOutputName)")
@@ -112,8 +111,7 @@ extension Generator {
     indent()
     printStreamReceiveMethods(receivedType: methodOutputName)
     println()
-    println("/// Call this to send each message in the request stream.")
-    println("func send(_ message: \(methodInputName), completion: @escaping (Error?) -> Void) throws")
+    printStreamSendMethods(sentType: methodInputName)
     println()
     println("/// Call this to close the sending connection. Blocking.")
     println("func closeSend() throws")

+ 5 - 0
Sources/protoc-gen-swiftgrpc/Generator-Methods.swift

@@ -24,4 +24,9 @@ extension Generator {
     println("/// Call this to wait for a result. Nonblocking.")
     println("func receive(completion: @escaping (ResultOrRPCError<\(receivedType)?>) -> Void) throws")
   }
+  
+  func printStreamSendMethods(sentType: String) {
+    println("/// Call this to send each message in the request stream. Nonblocking.")
+    println("func send(_ message: \(sentType), completion: @escaping (Error?) -> Void) throws")
+  }
 }

+ 2 - 4
Sources/protoc-gen-swiftgrpc/Generator-Server.swift

@@ -162,8 +162,7 @@ extension Generator {
   private func printServerMethodServerStreaming() {
     println("\(access) protocol \(methodSessionName): ServerSessionServerStreaming {")
     indent()
-    println("/// Send a message. Nonblocking.")
-    println("func send(_ response: \(methodOutputName), completion: ((Error?) -> Void)?) throws")
+    printStreamSendMethods(sentType: methodOutputName)
     outdent()
     println("}")
     println()
@@ -179,8 +178,7 @@ extension Generator {
     indent()
     printStreamReceiveMethods(receivedType: methodInputName)
     println()
-    println("/// Send a message. Nonblocking.")
-    println("func send(_ response: \(methodOutputName), completion: ((Error?) -> Void)?) throws")
+    printStreamSendMethods(sentType: methodOutputName)
     println()
     println("/// Close a connection. Blocks until the connection is closed.")
     println("func close() throws")