Skip to content

Commit 38dd33e

Browse files
committed
Update code generator to implement blocking API calls by calling nonblocking calls.
1 parent 78efca2 commit 38dd33e

File tree

6 files changed

+56
-102
lines changed

6 files changed

+56
-102
lines changed

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

Lines changed: 26 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -63,24 +63,18 @@ public class Echo_EchoGetCall {
6363
fileprivate func run(request: Echo_EchoRequest,
6464
metadata: Metadata) throws -> Echo_EchoResponse {
6565
let sem = DispatchSemaphore(value: 0)
66-
var callResult : CallResult!
67-
var response : Echo_EchoResponse?
68-
let requestData = try request.serializeProtobuf()
69-
try call.start(.unary,
70-
metadata:metadata,
71-
message:requestData)
72-
{(_callResult) in
73-
callResult = _callResult
74-
if let responseData = callResult.resultData {
75-
response = try? Echo_EchoResponse(protobuf:responseData)
76-
}
66+
var returnCallResult : CallResult!
67+
var returnResponse : Echo_EchoResponse?
68+
try start(request:request, metadata:metadata) {response, callResult in
69+
returnResponse = response
70+
returnCallResult = callResult
7771
sem.signal()
7872
}
7973
_ = sem.wait(timeout: DispatchTime.distantFuture)
80-
if let response = response {
81-
return response
74+
if let returnResponse = returnResponse {
75+
return returnResponse
8276
} else {
83-
throw Echo_EchoClientError.error(c: callResult)
77+
throw Echo_EchoClientError.error(c: returnCallResult)
8478
}
8579
}
8680

@@ -115,7 +109,7 @@ public class Echo_EchoExpandCall {
115109
self.call = channel.makeCall("/echo.Echo/Expand")
116110
}
117111

118-
/// Call this once with the message to send.
112+
/// Call this once with the message to send. Nonblocking.
119113
fileprivate func start(request: Echo_EchoRequest,
120114
metadata: Metadata,
121115
completion: @escaping (CallResult) -> ())
@@ -131,26 +125,20 @@ public class Echo_EchoExpandCall {
131125
/// Call this to wait for a result. Blocking.
132126
public func receive() throws -> Echo_EchoResponse {
133127
var returnError : Echo_EchoClientError?
134-
var response : Echo_EchoResponse!
128+
var returnResponse : Echo_EchoResponse!
135129
let sem = DispatchSemaphore(value: 0)
136130
do {
137-
try call.receiveMessage() {(responseData) in
138-
if let responseData = responseData {
139-
response = try? Echo_EchoResponse(protobuf:responseData)
140-
if response == nil {
141-
returnError = Echo_EchoClientError.invalidMessageReceived
142-
}
143-
} else {
144-
returnError = Echo_EchoClientError.endOfStream
145-
}
131+
try receive() {response, error in
132+
returnResponse = response
133+
returnError = error
146134
sem.signal()
147135
}
148136
_ = sem.wait(timeout: DispatchTime.distantFuture)
149137
}
150138
if let returnError = returnError {
151139
throw returnError
152140
}
153-
return response
141+
return returnResponse
154142
}
155143

156144
/// Call this to wait for a result. Nonblocking.
@@ -180,14 +168,14 @@ public class Echo_EchoCollectCall {
180168
self.call = channel.makeCall("/echo.Echo/Collect")
181169
}
182170

183-
/// Call this to start a call.
171+
/// Call this to start a call. Nonblocking.
184172
fileprivate func start(metadata:Metadata, completion:@escaping (CallResult)->())
185173
throws -> Echo_EchoCollectCall {
186174
try self.call.start(.clientStreaming, metadata:metadata, completion:completion)
187175
return self
188176
}
189177

190-
/// Call this to send each message in the request stream.
178+
/// Call this to send each message in the request stream. Nonblocking.
191179
public func send(_ message: Echo_EchoRequest) throws {
192180
let messageData = try message.serializeProtobuf()
193181
try call.sendMessage(data:messageData)
@@ -199,16 +187,11 @@ public class Echo_EchoCollectCall {
199187
var returnResponse : Echo_EchoResponse!
200188
let sem = DispatchSemaphore(value: 0)
201189
do {
202-
try call.receiveMessage() {(responseData) in
203-
if let responseData = responseData,
204-
let response = try? Echo_EchoResponse(protobuf:responseData) {
205-
returnResponse = response
206-
} else {
207-
returnError = Echo_EchoClientError.invalidMessageReceived
208-
}
190+
try closeAndReceive() {response, error in
191+
returnResponse = response
192+
returnError = error
209193
sem.signal()
210194
}
211-
try call.close(completion:{})
212195
_ = sem.wait(timeout: DispatchTime.distantFuture)
213196
} catch (let error) {
214197
throw error
@@ -254,21 +237,15 @@ public class Echo_EchoUpdateCall {
254237
return self
255238
}
256239

257-
/// Call this to wait for a result. Blocks.
240+
/// Call this to wait for a result. Blocking.
258241
public func receive() throws -> Echo_EchoResponse {
259242
var returnError : Echo_EchoClientError?
260243
var returnMessage : Echo_EchoResponse!
261244
let sem = DispatchSemaphore(value: 0)
262245
do {
263-
try call.receiveMessage() {(data) in
264-
if let data = data {
265-
returnMessage = try? Echo_EchoResponse(protobuf:data)
266-
if returnMessage == nil {
267-
returnError = Echo_EchoClientError.invalidMessageReceived
268-
}
269-
} else {
270-
returnError = Echo_EchoClientError.endOfStream
271-
}
246+
try receive() {response, error in
247+
returnMessage = response
248+
returnError = error
272249
sem.signal()
273250
}
274251
_ = sem.wait(timeout: DispatchTime.distantFuture)
@@ -302,16 +279,16 @@ public class Echo_EchoUpdateCall {
302279
try call.sendMessage(data:messageData)
303280
}
304281

305-
/// Call this to close the sending connection. Blocking
282+
/// Call this to close the sending connection. Blocking.
306283
public func closeSend() throws {
307284
let sem = DispatchSemaphore(value: 0)
308-
try call.close() {
285+
try closeSend() {
309286
sem.signal()
310287
}
311288
_ = sem.wait(timeout: DispatchTime.distantFuture)
312289
}
313290

314-
/// Call this to close the sending connection. Nonblocking
291+
/// Call this to close the sending connection. Nonblocking.
315292
public func closeSend(completion:@escaping ()->()) throws {
316293
try call.close() {
317294
completion()

0 commit comments

Comments
 (0)