Browse Source

Add completion callback to nonblocking send() in server generated code.

Tim Burks 8 years ago
parent
commit
a26794eaf0

+ 7 - 2
Examples/Echo/EchoProvider.swift

@@ -14,6 +14,7 @@
  * limitations under the License.
  */
 import Foundation
+import Dispatch
 
 class EchoProvider : Echo_EchoProvider {
 
@@ -31,7 +32,9 @@ class EchoProvider : Echo_EchoProvider {
     for part in parts {
       var response = Echo_EchoResponse()
       response.text = "Swift echo expand (\(i)): \(part)"
-      try session.send(response)
+      let sem = DispatchSemaphore(value: 0)
+      try session.send(response) {sem.signal()}
+      _ = sem.wait(timeout: DispatchTime.distantFuture)
       i += 1
       sleep(1)
     }
@@ -64,7 +67,9 @@ class EchoProvider : Echo_EchoProvider {
         count += 1
         var response = Echo_EchoResponse()
         response.text = "Swift echo update (\(count)): \(request.text)"
-        try session.send(response)
+        let sem = DispatchSemaphore(value: 0)
+        try session.send(response) {sem.signal()}
+        _ = sem.wait(timeout: DispatchTime.distantFuture)
       } catch Echo_EchoServerError.endOfStream {
         break
       } catch (let error) {

+ 4 - 4
Examples/Echo/Generated/echo.server.pb.swift

@@ -90,8 +90,8 @@ internal class Echo_EchoExpandSession : Echo_EchoSession {
   }
 
   /// Send a message. Nonblocking.
-  internal func send(_ response: Echo_EchoResponse) throws {
-    try handler.sendResponse(message:response.serializedData()) {}
+  internal func send(_ response: Echo_EchoResponse, completion: @escaping ()->()) throws {
+    try handler.sendResponse(message:response.serializedData()) {completion()}
   }
 
   /// Run the session. Internal.
@@ -203,8 +203,8 @@ internal class Echo_EchoUpdateSession : Echo_EchoSession {
   }
 
   /// Send a message. Nonblocking.
-  internal func send(_ response: Echo_EchoResponse) throws {
-    try handler.sendResponse(message:response.serializedData()) {}
+  internal func send(_ response: Echo_EchoResponse, completion: @escaping ()->()) throws {
+    try handler.sendResponse(message:response.serializedData()) {completion()}
   }
 
   /// Close a connection. Blocks until the connection is closed.

+ 1 - 1
Examples/Echo/PackageManager/Sources/main.swift

@@ -157,7 +157,7 @@ if client != "" {
         } catch Echo_EchoClientError.endOfStream {
           print("update closed")
           latch.signal()
-          break
+          running = false
         } catch (let error) {
           print("error: \(error)")
         }

+ 1 - 1
Plugin/Makefile

@@ -18,7 +18,7 @@ deploy:
 	cp echo.server.pb.swift ../Examples/Echo/Generated/echo.server.pb.swift 
 
 clear : 
-	rm -f echo.client.pb.swift echo.server.pb.swift swiftgrpc.log
+	rm -f echo.pb.swift echo.client.pb.swift echo.server.pb.swift swiftgrpc.log
 
 clean : clear
 	rm -rf protoc-gen-swiftgrpc Packages .build protoc-gen-swift Package.pins

File diff suppressed because it is too large
+ 0 - 0
Plugin/Sources/protoc-gen-swiftgrpc/templates.swift


+ 2 - 2
Plugin/Templates/server-session-bidistreaming.swift

@@ -31,8 +31,8 @@
   }
 
   /// Send a message. Nonblocking.
-  {{ access }} func send(_ response: {{ method|output }}) throws {
-    try handler.sendResponse(message:response.serializedData()) {}
+  {{ access }} func send(_ response: {{ method|output }}, completion: @escaping ()->()) throws {
+    try handler.sendResponse(message:response.serializedData()) {completion()}
   }
 
   /// Close a connection. Blocks until the connection is closed.

+ 2 - 2
Plugin/Templates/server-session-serverstreaming.swift

@@ -9,8 +9,8 @@
   }
 
   /// Send a message. Nonblocking.
-  {{ access }} func send(_ response: {{ method|output }}) throws {
-    try handler.sendResponse(message:response.serializedData()) {}
+  {{ access }} func send(_ response: {{ method|output }}, completion: @escaping ()->()) throws {
+    try handler.sendResponse(message:response.serializedData()) {completion()}
   }
 
   /// Run the session. Internal.

Some files were not shown because too many files changed in this diff