Skip to content

Commit 55077ac

Browse files
committed
Improve StatusCode usage and handling of unknown messages in generated servers.
1 parent 9a47c86 commit 55077ac

File tree

9 files changed

+78
-41
lines changed

9 files changed

+78
-41
lines changed

Examples/Echo/Generated/echo.grpc.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,12 @@ internal class Echo_EchoServer {
635635
case "/echo.Echo/Update":
636636
try Echo_EchoUpdateSession(handler:handler, provider:provider).run(queue:queue)
637637
default:
638-
break // handle unknown requests
638+
// handle unknown requests
639+
try handler.receiveMessage(initialMetadata:Metadata()) {(requestData) in
640+
try handler.sendResponse(statusCode:12,
641+
statusMessage:"unimplemented " + handler.method,
642+
trailingMetadata:Metadata())
643+
}
639644
}
640645
} catch (let error) {
641646
print("Server error: \(error)")

Plugin/Sources/protoc-gen-swiftgrpc/templates.swift

Lines changed: 10 additions & 10 deletions
Large diffs are not rendered by default.

Plugin/Templates/server.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
fileprivate var handler : gRPC.Handler
2929
{{ access }} var requestMetadata : Metadata { return handler.requestMetadata }
3030

31-
{{ access }} var statusCode : Int = 0
31+
{{ access }} var statusCode : StatusCode = .ok
3232
{{ access }} var statusMessage : String = "OK"
3333
{{ access }} var initialMetadata : Metadata = Metadata()
3434
{{ access }} var trailingMetadata : Metadata = Metadata()
@@ -103,7 +103,12 @@
103103
try {{ .|session:file,service,method }}(handler:handler, provider:provider).run(queue:queue)
104104
//-{% endfor %}
105105
default:
106-
break // handle unknown requests
106+
// handle unknown requests
107+
try handler.receiveMessage(initialMetadata:Metadata()) {(requestData) in
108+
try handler.sendResponse(statusCode:.unimplemented,
109+
statusMessage:"unknown method " + handler.method,
110+
trailingMetadata:Metadata())
111+
}
107112
}
108113
} catch (let error) {
109114
print("Server error: \(error)")

Sources/gRPC/Call.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,25 +86,29 @@ public enum CallError : Error {
8686
}
8787

8888
public struct CallResult : CustomStringConvertible {
89-
public var statusCode : Int
89+
public var statusCode : StatusCode
9090
public var statusMessage : String?
9191
public var resultData : Data?
9292
public var initialMetadata : Metadata?
9393
public var trailingMetadata : Metadata?
9494

9595
fileprivate init(_ op : OperationGroup) {
9696
if (op.success) {
97-
if let statusCode = op.receivedStatusCode() {
98-
self.statusCode = statusCode
97+
if let statusCodeRawValue = op.receivedStatusCode() {
98+
if let statusCode = StatusCode(rawValue:statusCodeRawValue) {
99+
self.statusCode = statusCode
100+
} else {
101+
self.statusCode = .unknown
102+
}
99103
} else {
100-
self.statusCode = 0
104+
self.statusCode = .ok
101105
}
102106
self.statusMessage = op.receivedStatusMessage()
103107
self.resultData = op.receivedMessage()?.data()
104108
self.initialMetadata = op.receivedInitialMetadata()
105109
self.trailingMetadata = op.receivedTrailingMetadata()
106110
} else {
107-
self.statusCode = 0
111+
self.statusCode = .ok
108112
self.statusMessage = nil
109113
self.resultData = nil
110114
self.initialMetadata = nil

Sources/gRPC/Handler.swift

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,20 @@ import Foundation // for String.Encoding
2222
public class Handler {
2323
/// Pointer to underlying C representation
2424
private var underlyingHandler: UnsafeMutableRawPointer
25-
25+
2626
/// Completion queue for handler response operations
2727
internal var completionQueue: CompletionQueue
28-
28+
2929
/// Metadata received with the request
3030
public var requestMetadata: Metadata
31-
31+
3232
/// A Call object that can be used to respond to the request
3333
internal lazy var call: Call = {
3434
return Call(underlyingCall: cgrpc_handler_get_call(self.underlyingHandler),
3535
owned: false,
3636
completionQueue: self.completionQueue)
3737
}()
38-
38+
3939
/// The host name sent with the request
4040
public lazy var host: String = {
4141
if let string = cgrpc_handler_copy_host(self.underlyingHandler) {
@@ -47,7 +47,7 @@ public class Handler {
4747
return ""
4848
}
4949
}()
50-
50+
5151
/// The method name sent with the request
5252
public lazy var method: String = {
5353
if let string = cgrpc_handler_copy_method(self.underlyingHandler) {
@@ -59,13 +59,13 @@ public class Handler {
5959
return ""
6060
}
6161
}()
62-
62+
6363
/// The caller address associated with the request
6464
public lazy var caller: String = {
6565
return String(cString:cgrpc_handler_call_peer(self.underlyingHandler),
6666
encoding:.utf8)!;
6767
}()
68-
68+
6969
/// Initializes a Handler
7070
///
7171
/// - Parameter underlyingServer: the underlying C representation of the associated server
@@ -76,11 +76,11 @@ public class Handler {
7676
underlyingCompletionQueue:cgrpc_handler_get_completion_queue(underlyingHandler))
7777
completionQueue.name = "Handler"
7878
}
79-
79+
8080
deinit {
8181
cgrpc_handler_destroy(self.underlyingHandler)
8282
}
83-
83+
8484
/// Requests a call for the handler
8585
///
8686
/// Fills the handler properties with information about the received request
@@ -91,7 +91,7 @@ public class Handler {
9191
throw CallError.callError(grpcCallError: error)
9292
}
9393
}
94-
94+
9595
/// Receive the message sent with a call
9696
///
9797
public func receiveMessage(initialMetadata: Metadata,
@@ -110,13 +110,15 @@ public class Handler {
110110
}
111111
try call.perform(operations)
112112
}
113-
113+
114114
/// Sends the response to a request
115115
///
116116
/// - Parameter message: the message to send
117+
/// - Parameter statusCode: status code to send
118+
/// - Parameter statusMessage: status message to send
117119
/// - Parameter trailingMetadata: trailing metadata to send
118120
public func sendResponse(message: Data,
119-
statusCode: Int,
121+
statusCode: StatusCode,
120122
statusMessage: String,
121123
trailingMetadata: Metadata) throws -> Void {
122124
let messageBuffer = ByteBuffer(data:message)
@@ -133,12 +135,33 @@ public class Handler {
133135
}
134136
try call.perform(operations)
135137
}
136-
138+
139+
/// Sends the response to a request
140+
///
141+
/// - Parameter statusCode: status code to send
142+
/// - Parameter statusMessage: status message to send
143+
/// - Parameter trailingMetadata: trailing metadata to send
144+
public func sendResponse(statusCode: StatusCode,
145+
statusMessage: String,
146+
trailingMetadata: Metadata) throws -> Void {
147+
let operations = OperationGroup(
148+
call:call,
149+
operations:[
150+
.receiveCloseOnServer,
151+
.sendStatusFromServer(statusCode, statusMessage, trailingMetadata)])
152+
{(operationGroup) in
153+
if operationGroup.success {
154+
self.shutdown()
155+
}
156+
}
157+
try call.perform(operations)
158+
}
159+
137160
/// Shuts down the handler's completion queue
138161
public func shutdown() {
139162
completionQueue.shutdown()
140163
}
141-
164+
142165
/// Send initial metadata in response to a connection
143166
///
144167
/// - Parameter initialMetadata: initial metadata to send
@@ -156,7 +179,7 @@ public class Handler {
156179
}
157180
try call.perform(operations)
158181
}
159-
182+
160183
/// Receive the message sent with a call
161184
///
162185
/// - Parameter completion: a completion handler to call after the message has been received
@@ -176,7 +199,7 @@ public class Handler {
176199
}
177200
try call.perform(operations)
178201
}
179-
202+
180203
/// Sends the response to a request
181204
///
182205
/// - Parameter message: the message to send
@@ -192,7 +215,7 @@ public class Handler {
192215
}
193216
try call.perform(operations)
194217
}
195-
218+
196219
/// Recognize when the client has closed a request
197220
///
198221
/// - Parameter completion: a completion handler to call after request has been closed
@@ -206,14 +229,14 @@ public class Handler {
206229
}
207230
try call.perform(operations)
208231
}
209-
232+
210233
/// Send final status to the client
211234
///
212235
/// - Parameter statusCode: status code to send
213236
/// - Parameter statusMessage: status message to send
214237
/// - Parameter trailingMetadata: trailing metadata to send
215238
/// - Parameter completion: a completion handler to call after the status has been sent
216-
public func sendStatus(statusCode: Int,
239+
public func sendStatus(statusCode: StatusCode,
217240
statusMessage: String,
218241
trailingMetadata: Metadata,
219242
completion:@escaping (() -> Void)) throws -> Void {

Sources/gRPC/Operation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ enum Operation {
2121
case sendInitialMetadata(Metadata)
2222
case sendMessage(ByteBuffer)
2323
case sendCloseFromClient
24-
case sendStatusFromServer(Int, String, Metadata)
24+
case sendStatusFromServer(StatusCode, String, Metadata)
2525
case receiveInitialMetadata
2626
case receiveMessage
2727
case receiveStatusOnClient

Sources/gRPC/OperationGroup.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ internal class OperationGroup {
6363
underlyingObserver = cgrpc_observer_create_send_close_from_client()!
6464
case .sendStatusFromServer(let statusCode, let statusMessage, let metadata):
6565
underlyingObserver = cgrpc_observer_create_send_status_from_server(metadata.underlyingArray)!
66-
cgrpc_observer_send_status_from_server_set_status(underlyingObserver, Int32(statusCode))
66+
cgrpc_observer_send_status_from_server_set_status(underlyingObserver, Int32(statusCode.rawValue))
6767
cgrpc_observer_send_status_from_server_set_status_details(underlyingObserver, statusMessage)
6868
case .receiveInitialMetadata:
6969
underlyingObserver = cgrpc_observer_create_recv_initial_metadata()!

Sources/gRPC/gRPC.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public func gStandsFor() -> String {
4343
}
4444

4545
/// Status codes for gRPC operations (replicated from status_code_enum.h)
46-
enum StatusCode: Int {
46+
public enum StatusCode: Int {
4747
/// Not an error; returned on success.
4848
case ok = 0
4949

Tests/gRPCTests/GRPCTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
"2": "two"]
5353
let steps = 10
5454
let hello = "/hello"
55-
let statusCode = 0
55+
let statusCode = StatusCode.ok
5656
let statusMessage = "OK"
5757

5858
func runTest(useSSL: Bool) {

0 commit comments

Comments
 (0)