Skip to content

Commit b450ad3

Browse files
committed
Add generators for asynchronous methods, demonstrate in Echo Mac app.
1 parent b5aa1d9 commit b450ad3

File tree

19 files changed

+4598
-210
lines changed

19 files changed

+4598
-210
lines changed

Examples/Echo/Generated/echo.client.pb.swift

Lines changed: 128 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public enum Echo_EchoClientError : Error {
4949
case invalidMessageReceived
5050
case error(c: CallResult)
5151
}
52+
5253
/// Get (Unary)
5354
public class Echo_EchoGetCall {
5455
private var call : Call
@@ -82,6 +83,27 @@ public class Echo_EchoGetCall {
8283
throw Echo_EchoClientError.error(c: callResult)
8384
}
8485
}
86+
87+
/// Start the call. Nonblocking.
88+
fileprivate func start(request: Echo_EchoRequest,
89+
metadata: Metadata,
90+
completion: @escaping (Echo_EchoResponse?, CallResult)->())
91+
throws -> Echo_EchoGetCall {
92+
93+
let requestData = try request.serializeProtobuf()
94+
try call.start(.unary,
95+
metadata:metadata,
96+
message:requestData)
97+
{(callResult) in
98+
if let responseData = callResult.resultData,
99+
let response = try? Echo_EchoResponse(protobuf:responseData) {
100+
completion(response, callResult)
101+
} else {
102+
completion(nil, callResult)
103+
}
104+
}
105+
return self
106+
}
85107
}
86108

87109
/// Expand (Server Streaming)
@@ -94,20 +116,19 @@ public class Echo_EchoExpandCall {
94116
}
95117

96118
/// Call this once with the message to send.
97-
fileprivate func run(request: Echo_EchoRequest, metadata: Metadata) throws -> Echo_EchoExpandCall {
98-
let requestData = try request.serializeProtobuf()
99-
let sem = DispatchSemaphore(value: 0)
100-
try call.start(.serverStreaming,
101-
metadata:metadata,
102-
message:requestData)
103-
{callResult in
104-
sem.signal()
105-
}
106-
_ = sem.wait(timeout: DispatchTime.distantFuture)
107-
return self
119+
fileprivate func start(request: Echo_EchoRequest,
120+
metadata: Metadata,
121+
completion: @escaping (CallResult) -> ())
122+
throws -> Echo_EchoExpandCall {
123+
let requestData = try request.serializeProtobuf()
124+
try call.start(.serverStreaming,
125+
metadata:metadata,
126+
message:requestData,
127+
completion:completion)
128+
return self
108129
}
109130

110-
/// Call this to wait for a result. Blocks.
131+
/// Call this to wait for a result. Blocking.
111132
public func receive() throws -> Echo_EchoResponse {
112133
var returnError : Echo_EchoClientError?
113134
var response : Echo_EchoResponse!
@@ -131,6 +152,23 @@ public class Echo_EchoExpandCall {
131152
}
132153
return response
133154
}
155+
156+
/// Call this to wait for a result. Nonblocking.
157+
public func receive(completion:@escaping (Echo_EchoResponse?, Echo_EchoClientError?)->()) throws {
158+
do {
159+
try call.receiveMessage() {(responseData) in
160+
if let responseData = responseData {
161+
if let response = try? Echo_EchoResponse(protobuf:responseData) {
162+
completion(response, nil)
163+
} else {
164+
completion(nil, Echo_EchoClientError.invalidMessageReceived)
165+
}
166+
} else {
167+
completion(nil, Echo_EchoClientError.endOfStream)
168+
}
169+
}
170+
}
171+
}
134172
}
135173

136174
/// Collect (Client Streaming)
@@ -143,15 +181,10 @@ public class Echo_EchoCollectCall {
143181
}
144182

145183
/// Call this to start a call.
146-
fileprivate func run(metadata:Metadata) throws -> Echo_EchoCollectCall {
147-
let sem = DispatchSemaphore(value: 0)
148-
try self.call.start(.clientStreaming,
149-
metadata:metadata)
150-
{callResult in
151-
sem.signal()
152-
}
153-
_ = sem.wait(timeout: DispatchTime.distantFuture)
154-
return self
184+
fileprivate func start(metadata:Metadata, completion:@escaping (CallResult)->())
185+
throws -> Echo_EchoCollectCall {
186+
try self.call.start(.clientStreaming, metadata:metadata, completion:completion)
187+
return self
155188
}
156189

157190
/// Call this to send each message in the request stream.
@@ -160,7 +193,7 @@ public class Echo_EchoCollectCall {
160193
try call.sendMessage(data:messageData)
161194
}
162195

163-
/// Call this to close the connection and wait for a response. Blocks.
196+
/// Call this to close the connection and wait for a response. Blocking.
164197
public func closeAndReceive() throws -> Echo_EchoResponse {
165198
var returnError : Echo_EchoClientError?
166199
var returnResponse : Echo_EchoResponse!
@@ -185,6 +218,24 @@ public class Echo_EchoCollectCall {
185218
}
186219
return returnResponse
187220
}
221+
222+
/// Call this to close the connection and wait for a response. Nonblocking.
223+
public func closeAndReceive(completion:@escaping (Echo_EchoResponse?, Echo_EchoClientError?)->())
224+
throws {
225+
do {
226+
try call.receiveMessage() {(responseData) in
227+
if let responseData = responseData,
228+
let response = try? Echo_EchoResponse(protobuf:responseData) {
229+
completion(response, nil)
230+
} else {
231+
completion(nil, Echo_EchoClientError.invalidMessageReceived)
232+
}
233+
}
234+
try call.close(completion:{})
235+
} catch (let error) {
236+
throw error
237+
}
238+
}
188239
}
189240

190241
/// Update (Bidirectional Streaming)
@@ -196,16 +247,11 @@ public class Echo_EchoUpdateCall {
196247
self.call = channel.makeCall("/echo.Echo/Update")
197248
}
198249

199-
/// Call this to start a call.
200-
fileprivate func run(metadata:Metadata) throws -> Echo_EchoUpdateCall {
201-
let sem = DispatchSemaphore(value: 0)
202-
try self.call.start(.bidiStreaming,
203-
metadata:metadata)
204-
{callResult in
205-
sem.signal()
206-
}
207-
_ = sem.wait(timeout: DispatchTime.distantFuture)
208-
return self
250+
/// Call this to start a call. Nonblocking.
251+
fileprivate func start(metadata:Metadata, completion:@escaping (CallResult)->())
252+
throws -> Echo_EchoUpdateCall {
253+
try self.call.start(.bidiStreaming, metadata:metadata, completion:completion)
254+
return self
209255
}
210256

211257
/// Call this to wait for a result. Blocks.
@@ -233,22 +279,45 @@ public class Echo_EchoUpdateCall {
233279
return returnMessage
234280
}
235281

282+
/// Call this to wait for a result. Nonblocking.
283+
public func receive(completion:@escaping (Echo_EchoResponse?, Echo_EchoClientError?)->()) throws {
284+
do {
285+
try call.receiveMessage() {(data) in
286+
if let data = data {
287+
if let returnMessage = try? Echo_EchoResponse(protobuf:data) {
288+
completion(returnMessage, nil)
289+
} else {
290+
completion(nil, Echo_EchoClientError.invalidMessageReceived)
291+
}
292+
} else {
293+
completion(nil, Echo_EchoClientError.endOfStream)
294+
}
295+
}
296+
}
297+
}
298+
236299
/// Call this to send each message in the request stream.
237300
public func send(_ message:Echo_EchoRequest) throws {
238301
let messageData = try message.serializeProtobuf()
239302
try call.sendMessage(data:messageData)
240303
}
241304

242-
/// Call this to close the sending connection.
305+
/// Call this to close the sending connection. Blocking
243306
public func closeSend() throws {
244307
let sem = DispatchSemaphore(value: 0)
245308
try call.close() {
246309
sem.signal()
247310
}
248311
_ = sem.wait(timeout: DispatchTime.distantFuture)
249312
}
250-
}
251313

314+
/// Call this to close the sending connection. Nonblocking
315+
public func closeSend(completion:@escaping ()->()) throws {
316+
try call.close() {
317+
completion()
318+
}
319+
}
320+
}
252321

253322
/// Call methods of this class to make API calls.
254323
public class Echo_EchoService {
@@ -284,25 +353,42 @@ public class Echo_EchoService {
284353
}
285354

286355
/// Synchronous. Unary.
287-
public func get(_ request: Echo_EchoRequest) throws -> Echo_EchoResponse {
288-
return try Echo_EchoGetCall(channel).run(request:request, metadata:metadata)
356+
public func get(_ request: Echo_EchoRequest)
357+
throws
358+
-> Echo_EchoResponse {
359+
return try Echo_EchoGetCall(channel).run(request:request, metadata:metadata)
360+
}
361+
/// Asynchronous. Unary.
362+
public func get(_ request: Echo_EchoRequest,
363+
completion: @escaping (Echo_EchoResponse?, CallResult)->())
364+
throws
365+
-> Echo_EchoGetCall {
366+
return try Echo_EchoGetCall(channel).start(request:request,
367+
metadata:metadata,
368+
completion:completion)
289369
}
290370
/// Asynchronous. Server-streaming.
291371
/// Send the initial message.
292372
/// Use methods on the returned object to get streamed responses.
293-
public func expand(_ request: Echo_EchoRequest) throws -> Echo_EchoExpandCall {
294-
return try Echo_EchoExpandCall(channel).run(request:request, metadata:metadata)
373+
public func expand(_ request: Echo_EchoRequest, completion: @escaping (CallResult)->())
374+
throws
375+
-> Echo_EchoExpandCall {
376+
return try Echo_EchoExpandCall(channel).start(request:request, metadata:metadata, completion:completion)
295377
}
296378
/// Asynchronous. Client-streaming.
297379
/// Use methods on the returned object to stream messages and
298380
/// to close the connection and wait for a final response.
299-
public func collect() throws -> Echo_EchoCollectCall {
300-
return try Echo_EchoCollectCall(channel).run(metadata:metadata)
381+
public func collect(completion: @escaping (CallResult)->())
382+
throws
383+
-> Echo_EchoCollectCall {
384+
return try Echo_EchoCollectCall(channel).start(metadata:metadata, completion:completion)
301385
}
302386
/// Asynchronous. Bidirectional-streaming.
303387
/// Use methods on the returned object to stream messages,
304388
/// to wait for replies, and to close the connection.
305-
public func update() throws -> Echo_EchoUpdateCall {
306-
return try Echo_EchoUpdateCall(channel).run(metadata:metadata)
389+
public func update(completion: @escaping (CallResult)->())
390+
throws
391+
-> Echo_EchoUpdateCall {
392+
return try Echo_EchoUpdateCall(channel).start(metadata:metadata, completion:completion)
307393
}
308394
}

0 commit comments

Comments
 (0)