@@ -22,20 +22,20 @@ import Foundation // for String.Encoding
2222public 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 {
0 commit comments