فهرست منبع

Add error handler argument to send methods to better handle errors that occur when sending.

Tim Burks 8 سال پیش
والد
کامیت
1785d231c5

+ 5 - 5
Examples/Echo/Generated/echo.client.pb.swift

@@ -65,7 +65,7 @@ public class Echo_EchoGetCall {
     let sem = DispatchSemaphore(value: 0)
     var returnCallResult : CallResult!
     var returnResponse : Echo_EchoResponse?
-    try start(request:request, metadata:metadata) {response, callResult in
+    _ = try start(request:request, metadata:metadata) {response, callResult in
       returnResponse = response
       returnCallResult = callResult
       sem.signal()
@@ -176,9 +176,9 @@ public class Echo_EchoCollectCall {
   }
 
   /// Call this to send each message in the request stream. Nonblocking.
-  public func send(_ message: Echo_EchoRequest) throws {
+  public func send(_ message:Echo_EchoRequest, errorHandler:@escaping (Error)->()) throws {
     let messageData = try message.serializeProtobuf()
-    try call.sendMessage(data:messageData)
+    try call.sendMessage(data:messageData, errorHandler:errorHandler)
   }
 
   /// Call this to close the connection and wait for a response. Blocking.
@@ -274,9 +274,9 @@ public class Echo_EchoUpdateCall {
   }
 
   /// Call this to send each message in the request stream.
-  public func send(_ message:Echo_EchoRequest) throws {
+  public func send(_ message:Echo_EchoRequest, errorHandler:@escaping (Error)->()) throws {
     let messageData = try message.serializeProtobuf()
-    try call.sendMessage(data:messageData)
+    try call.sendMessage(data:messageData, errorHandler:errorHandler)
   }
 
   /// Call this to close the sending connection. Blocking.

+ 47 - 0
Examples/Echo/Xcode/Echo.xcodeproj/project.pbxproj

@@ -7,6 +7,10 @@
 	objects = {
 
 /* Begin PBXBuildFile section */
+		D30625D01E61331600C92C6A /* BoringSSL.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D363FEDF1E5F751500D8F135 /* BoringSSL.framework */; };
+		D30625D11E61331600C92C6A /* CgRPC.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D363FEE11E5F751500D8F135 /* CgRPC.framework */; };
+		D30625D21E61331600C92C6A /* gRPC.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D363FEE31E5F751500D8F135 /* gRPC.framework */; };
+		D30625D31E61331D00C92C6A /* SwiftProtobuf.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D3F7791E1E380C140002CB64 /* SwiftProtobuf.framework */; };
 		D35358201E219963007FA223 /* echo.client.pb.swift in Sources */ = {isa = PBXBuildFile; fileRef = D353581D1E219963007FA223 /* echo.client.pb.swift */; };
 		D35358211E219963007FA223 /* echo.pb.swift in Sources */ = {isa = PBXBuildFile; fileRef = D353581E1E219963007FA223 /* echo.pb.swift */; };
 		D35358221E219963007FA223 /* echo.server.pb.swift in Sources */ = {isa = PBXBuildFile; fileRef = D353581F1E219963007FA223 /* echo.server.pb.swift */; };
@@ -20,6 +24,27 @@
 /* End PBXBuildFile section */
 
 /* Begin PBXContainerItemProxy section */
+		D30625C31E6132AA00C92C6A /* PBXContainerItemProxy */ = {
+			isa = PBXContainerItemProxy;
+			containerPortal = D363FED71E5F751500D8F135 /* SwiftGRPC.xcodeproj */;
+			proxyType = 1;
+			remoteGlobalIDString = OBJ_782;
+			remoteInfo = BoringSSL;
+		};
+		D30625C51E6132AA00C92C6A /* PBXContainerItemProxy */ = {
+			isa = PBXContainerItemProxy;
+			containerPortal = D363FED71E5F751500D8F135 /* SwiftGRPC.xcodeproj */;
+			proxyType = 1;
+			remoteGlobalIDString = OBJ_1086;
+			remoteInfo = CgRPC;
+		};
+		D30625C71E6132AA00C92C6A /* PBXContainerItemProxy */ = {
+			isa = PBXContainerItemProxy;
+			containerPortal = D363FED71E5F751500D8F135 /* SwiftGRPC.xcodeproj */;
+			proxyType = 1;
+			remoteGlobalIDString = OBJ_1366;
+			remoteInfo = gRPC;
+		};
 		D363FEDE1E5F751500D8F135 /* PBXContainerItemProxy */ = {
 			isa = PBXContainerItemProxy;
 			containerPortal = D363FED71E5F751500D8F135 /* SwiftGRPC.xcodeproj */;
@@ -107,6 +132,10 @@
 			isa = PBXFrameworksBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
+				D30625D31E61331D00C92C6A /* SwiftProtobuf.framework in Frameworks */,
+				D30625D01E61331600C92C6A /* BoringSSL.framework in Frameworks */,
+				D30625D11E61331600C92C6A /* CgRPC.framework in Frameworks */,
+				D30625D21E61331600C92C6A /* gRPC.framework in Frameworks */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};
@@ -193,6 +222,9 @@
 			buildRules = (
 			);
 			dependencies = (
+				D30625C41E6132AA00C92C6A /* PBXTargetDependency */,
+				D30625C61E6132AA00C92C6A /* PBXTargetDependency */,
+				D30625C81E6132AA00C92C6A /* PBXTargetDependency */,
 				D3F779261E380C310002CB64 /* PBXTargetDependency */,
 				D38E00371E315DB700CE7B16 /* PBXTargetDependency */,
 				D38E00391E315DB700CE7B16 /* PBXTargetDependency */,
@@ -338,6 +370,21 @@
 /* End PBXSourcesBuildPhase section */
 
 /* Begin PBXTargetDependency section */
+		D30625C41E6132AA00C92C6A /* PBXTargetDependency */ = {
+			isa = PBXTargetDependency;
+			name = BoringSSL;
+			targetProxy = D30625C31E6132AA00C92C6A /* PBXContainerItemProxy */;
+		};
+		D30625C61E6132AA00C92C6A /* PBXTargetDependency */ = {
+			isa = PBXTargetDependency;
+			name = CgRPC;
+			targetProxy = D30625C51E6132AA00C92C6A /* PBXContainerItemProxy */;
+		};
+		D30625C81E6132AA00C92C6A /* PBXTargetDependency */ = {
+			isa = PBXTargetDependency;
+			name = gRPC;
+			targetProxy = D30625C71E6132AA00C92C6A /* PBXContainerItemProxy */;
+		};
 		D38E00371E315DB700CE7B16 /* PBXTargetDependency */ = {
 			isa = PBXTargetDependency;
 			name = BoringSSL;

+ 2 - 2
Examples/Echo/Xcode/Echo/EchoViewController.swift

@@ -232,7 +232,7 @@ class EchoViewController : NSViewController, NSTextFieldDelegate {
     if let collectCall = collectCall {
       let requestMessage = Echo_EchoRequest(text:self.messageField.stringValue)
       self.displayMessageSent(requestMessage.text)
-      try collectCall.send(requestMessage)
+      try collectCall.send(requestMessage) {error in print(error)}
     }
   }
 
@@ -240,7 +240,7 @@ class EchoViewController : NSViewController, NSTextFieldDelegate {
     if let updateCall = updateCall {
       let requestMessage = Echo_EchoRequest(text:self.messageField.stringValue)
       self.displayMessageSent(requestMessage.text)
-      try updateCall.send(requestMessage)
+      try updateCall.send(requestMessage) {error in print(error)}
     }
   }
 

تفاوت فایلی نمایش داده نمی شود زیرا این فایل بسیار بزرگ است
+ 0 - 0
Plugin/Sources/protoc-gen-swiftgrpc/templates.swift


+ 3 - 3
Plugin/Templates/client-call-bidistreaming.swift

@@ -51,9 +51,9 @@ public class {{ .|call:protoFile,service,method }} {
   }
 
   /// Call this to send each message in the request stream.
-  public func send(_ message:{{ method|input }}) throws {
+  public func send(_ message:Echo_EchoRequest, errorHandler:@escaping (Error)->()) throws {
     let messageData = try message.serializeProtobuf()
-    try call.sendMessage(data:messageData)
+    try call.sendMessage(data:messageData, errorHandler:errorHandler)
   }
 
   /// Call this to close the sending connection. Blocking.
@@ -71,4 +71,4 @@ public class {{ .|call:protoFile,service,method }} {
       completion()
     }
   }
-}
+}

+ 2 - 2
Plugin/Templates/client-call-clientstreaming.swift

@@ -15,9 +15,9 @@ public class {{ .|call:protoFile,service,method }} {
   }
 
   /// Call this to send each message in the request stream. Nonblocking.
-  public func send(_ message: {{ method|input }}) throws {
+  public func send(_ message:Echo_EchoRequest, errorHandler:@escaping (Error)->()) throws {
     let messageData = try message.serializeProtobuf()
-    try call.sendMessage(data:messageData)
+    try call.sendMessage(data:messageData, errorHandler:errorHandler)
   }
 
   /// Call this to close the connection and wait for a response. Blocking.

+ 1 - 1
Plugin/Templates/client-call-unary.swift

@@ -13,7 +13,7 @@ public class {{ .|call:protoFile,service,method }} {
     let sem = DispatchSemaphore(value: 0)
     var returnCallResult : CallResult!
     var returnResponse : {{ method|output }}?
-    try start(request:request, metadata:metadata) {response, callResult in
+    _ = try start(request:request, metadata:metadata) {response, callResult in
       returnResponse = response
       returnCallResult = callResult
       sem.signal()

+ 0 - 1
Plugin/Templates/client.pb.swift

@@ -65,7 +65,6 @@ public enum {{ .|clienterror:protoFile,service }} : Error {
 //-{% include "client-call-bidistreaming.swift" %}
 //-{% endif %}
 //-{% endfor %}
-
 /// Call methods of this class to make API calls.
 public class {{ .|serviceclass:protoFile,service }} {
   private var channel: Channel

+ 8 - 6
Sources/gRPC/Call.swift

@@ -257,7 +257,7 @@ public class Call {
   /// Sends a message over a streaming connection.
   ///
   /// Parameter data: the message data to send
-  public func sendMessage(data: Data) throws {
+  public func sendMessage(data: Data, errorHandler:@escaping (Error)->()) throws {
     self.sendMutex.lock()
     defer {self.sendMutex.unlock()}
     if self.writing {
@@ -267,12 +267,13 @@ public class Call {
       self.pendingMessages.append(data) 
     } else {
       self.writing = true
-      try self.sendWithoutBlocking(data: data)
+      try self.sendWithoutBlocking(data: data, errorHandler:errorHandler)
     }
   }
 
   /// helper for sending queued messages
-  private func sendWithoutBlocking(data: Data) throws -> Void {
+  private func sendWithoutBlocking(data: Data, errorHandler:@escaping (Error)->())
+    throws -> Void {
     try self.perform(OperationGroup(call:self,
                                     operations:[.sendMessage(ByteBuffer(data:data))])
     {(operationGroup) in
@@ -284,9 +285,9 @@ public class Call {
               let nextMessage = self.pendingMessages.first!
               self.pendingMessages.removeFirst()
               do {
-                try self.sendWithoutBlocking(data: nextMessage)
+                try self.sendWithoutBlocking(data: nextMessage, errorHandler:errorHandler)
               } catch (let callError) {
-                print("Call sendWithoutBlocking: grpc error \(callError)")
+                errorHandler(callError)
               }
             } else {
               // otherwise, we are finished writing
@@ -295,8 +296,9 @@ public class Call {
           }
         }
       } else {
-        // TODO: if the event failed, shut down
+        // if the event failed, shut down
         self.writing = false
+        errorHandler(CallError.unknown)
       }
     })
   }

برخی فایل ها در این مقایسه diff نمایش داده نمی شوند زیرا تعداد فایل ها بسیار زیاد است