blob: 326def7f3933c1c2b616219b0b8cf47a36b4b5f9 [file] [log] [blame]
Jorge Canizales9409ad82015-02-18 16:19:56 -08001/*
2 *
Yang Gao5fc90292015-02-20 09:46:22 -08003 * Copyright 2015, Google Inc.
Jorge Canizales9409ad82015-02-18 16:19:56 -08004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
Jorge Canizales5e0efd92015-02-17 18:23:58 -080034#import "GRPCCall.h"
35
Jorge Canizalesc2d7ecb2015-02-27 01:22:41 -080036#include <grpc/grpc.h>
Jorge Canizales59bb9d72015-06-22 19:04:15 -070037#include <grpc/support/time.h>
Jorge Canizales35f003b2015-07-17 21:14:36 -070038#import <RxLibrary/GRXConcurrentWriteable.h>
Jorge Canizales5e0efd92015-02-17 18:23:58 -080039
murgatroid9969927d62015-04-24 13:32:48 -070040#import "private/GRPCWrappedCall.h"
Jorge Canizales5e0efd92015-02-17 18:23:58 -080041#import "private/NSData+GRPC.h"
42#import "private/NSDictionary+GRPC.h"
43#import "private/NSError+GRPC.h"
44
Jorge Canizalesf3a4f2c2015-06-12 22:12:50 -070045NSString * const kGRPCStatusMetadataKey = @"io.grpc.StatusMetadataKey";
46
Jorge Canizales5e0efd92015-02-17 18:23:58 -080047@interface GRPCCall () <GRXWriteable>
Jorge Canizales0b34c892015-08-12 20:19:20 -070048// Make them read-write.
49@property(atomic, strong) NSDictionary *responseHeaders;
50@property(atomic, strong) NSDictionary *responseTrailers;
Jorge Canizales5e0efd92015-02-17 18:23:58 -080051@end
52
53// The following methods of a C gRPC call object aren't reentrant, and thus
54// calls to them must be serialized:
murgatroid99b5c076f2015-04-27 17:25:36 -070055// - start_batch
Jorge Canizales5e0efd92015-02-17 18:23:58 -080056// - destroy
Jorge Canizales5e0efd92015-02-17 18:23:58 -080057//
murgatroid99b5c076f2015-04-27 17:25:36 -070058// start_batch with a SEND_MESSAGE argument can only be called after the
59// OP_COMPLETE event for any previous write is received. This is achieved by
Jorge Canizales5e0efd92015-02-17 18:23:58 -080060// pausing the requests writer immediately every time it writes a value, and
murgatroid99b5c076f2015-04-27 17:25:36 -070061// resuming it again when OP_COMPLETE is received.
Jorge Canizales5e0efd92015-02-17 18:23:58 -080062//
murgatroid99b5c076f2015-04-27 17:25:36 -070063// Similarly, start_batch with a RECV_MESSAGE argument can only be called after
64// the OP_COMPLETE event for any previous read is received.This is easier to
65// enforce, as we're writing the received messages into the writeable:
66// start_batch is enqueued once upon receiving the OP_COMPLETE event for the
67// RECV_METADATA batch, and then once after receiving each OP_COMPLETE event for
68// each RECV_MESSAGE batch.
Jorge Canizales5e0efd92015-02-17 18:23:58 -080069@implementation GRPCCall {
70 dispatch_queue_t _callQueue;
71
murgatroid9930b7d4e2015-04-24 10:36:43 -070072 GRPCWrappedCall *_wrappedCall;
Jorge Canizales5e0efd92015-02-17 18:23:58 -080073 dispatch_once_t _callAlreadyInvoked;
74
Jorge Canizales5e0efd92015-02-17 18:23:58 -080075 // The C gRPC library has less guarantees on the ordering of events than we
76 // do. Particularly, in the face of errors, there's no ordering guarantee at
77 // all. This wrapper over our actual writeable ensures thread-safety and
78 // correct ordering.
Jorge Canizales35f003b2015-07-17 21:14:36 -070079 GRXConcurrentWriteable *_responseWriteable;
Jorge Canizales238ad782015-08-07 23:11:29 -070080
81 // The network thread wants the requestWriter to resume (when the server is ready for more input),
82 // or to stop (on errors), concurrently with user threads that want to start it, pause it or stop
83 // it. Because a writer isn't thread-safe, we'll synchronize those operations on it.
84 // We don't use a dispatch queue for that purpose, because the writer can call writeValue: or
85 // writesFinishedWithError: on this GRPCCall as part of those operations. We want to be able to
86 // pause the writer immediately on writeValue:, so we need our locking to be recursive.
Jorge Canizales56047122015-07-17 12:18:08 -070087 GRXWriter *_requestWriter;
Jorge Canizales544963e2015-06-12 19:46:27 -070088
Jorge Canizales6531b2b2015-07-18 00:19:14 -070089 // To create a retain cycle when a call is started, up until it finishes. See
Jorge Canizaleseb87b462015-08-08 16:16:43 -070090 // |startWithWriteable:| and |finishWithError:|. This saves users from having to retain a
91 // reference to the call object if all they're interested in is the handler being executed when
92 // the response arrives.
93 GRPCCall *_retainSelf;
Jorge Canizales6531b2b2015-07-18 00:19:14 -070094
Jorge Canizales6512d262015-08-12 17:33:23 -070095 NSMutableDictionary *_requestHeaders;
Jorge Canizales5e0efd92015-02-17 18:23:58 -080096}
97
98@synthesize state = _state;
99
100- (instancetype)init {
Jorge Canizalesbe808e32015-07-04 14:37:58 -0700101 return [self initWithHost:nil path:nil requestsWriter:nil];
Jorge Canizales5e0efd92015-02-17 18:23:58 -0800102}
103
104// Designated initializer
105- (instancetype)initWithHost:(NSString *)host
Jorge Canizalesbe808e32015-07-04 14:37:58 -0700106 path:(NSString *)path
Jorge Canizales56047122015-07-17 12:18:08 -0700107 requestsWriter:(GRXWriter *)requestWriter {
Jorge Canizalesbe808e32015-07-04 14:37:58 -0700108 if (!host || !path) {
Jorge Canizales597ef982015-07-31 23:31:56 -0700109 [NSException raise:NSInvalidArgumentException format:@"Neither host nor path can be nil."];
Jorge Canizales5e0efd92015-02-17 18:23:58 -0800110 }
Jorge Canizales6bbfcc32015-06-17 14:10:52 -0700111 if (requestWriter.state != GRXWriterStateNotStarted) {
Jorge Canizales597ef982015-07-31 23:31:56 -0700112 [NSException raise:NSInvalidArgumentException
113 format:@"The requests writer can't be already started."];
Jorge Canizales6bbfcc32015-06-17 14:10:52 -0700114 }
Jorge Canizales5e0efd92015-02-17 18:23:58 -0800115 if ((self = [super init])) {
Jorge Canizales3a5253e2015-07-31 23:48:56 -0700116 _wrappedCall = [[GRPCWrappedCall alloc] initWithHost:host path:path];
Jorge Canizales8c5318a2015-08-05 18:50:08 -0700117 if (!_wrappedCall) {
118 return nil;
119 }
Jorge Canizales5e0efd92015-02-17 18:23:58 -0800120
121 // Serial queue to invoke the non-reentrant methods of the grpc_call object.
122 _callQueue = dispatch_queue_create("org.grpc.call", NULL);
123
124 _requestWriter = requestWriter;
Jorge Canizales544963e2015-06-12 19:46:27 -0700125
Jorge Canizales6512d262015-08-12 17:33:23 -0700126 _requestHeaders = [NSMutableDictionary dictionary];
Jorge Canizales5e0efd92015-02-17 18:23:58 -0800127 }
128 return self;
129}
130
Jorge Canizales544963e2015-06-12 19:46:27 -0700131#pragma mark Metadata
132
Jorge Canizales6512d262015-08-12 17:33:23 -0700133- (NSMutableDictionary *)requestHeaders {
134 return _requestHeaders;
Jorge Canizales544963e2015-06-12 19:46:27 -0700135}
136
Jorge Canizales6512d262015-08-12 17:33:23 -0700137- (void)setRequestHeaders:(NSDictionary *)requestHeaders {
138 _requestHeaders = [NSMutableDictionary dictionaryWithDictionary:requestHeaders];
Jorge Canizales544963e2015-06-12 19:46:27 -0700139}
140
Jorge Canizales5e0efd92015-02-17 18:23:58 -0800141#pragma mark Finish
142
143- (void)finishWithError:(NSError *)errorOrNil {
Jorge Canizales6531b2b2015-07-18 00:19:14 -0700144 // If the call isn't retained anywhere else, it can be deallocated now.
Jorge Canizaleseb87b462015-08-08 16:16:43 -0700145 _retainSelf = nil;
Jorge Canizales6531b2b2015-07-18 00:19:14 -0700146
147 // If there were still request messages coming, stop them.
Jorge Canizales238ad782015-08-07 23:11:29 -0700148 @synchronized(_requestWriter) {
149 _requestWriter.state = GRXWriterStateFinished;
Jorge Canizales238ad782015-08-07 23:11:29 -0700150 }
Jorge Canizales6531b2b2015-07-18 00:19:14 -0700151
Jorge Canizales5e0efd92015-02-17 18:23:58 -0800152 if (errorOrNil) {
153 [_responseWriteable cancelWithError:errorOrNil];
154 } else {
155 [_responseWriteable enqueueSuccessfulCompletion];
156 }
157}
158
159- (void)cancelCall {
160 // Can be called from any thread, any number of times.
murgatroid99b56609c2015-04-28 16:41:11 -0700161 [_wrappedCall cancel];
Jorge Canizales5e0efd92015-02-17 18:23:58 -0800162}
163
164- (void)cancel {
165 [self finishWithError:[NSError errorWithDomain:kGRPCErrorDomain
166 code:GRPCErrorCodeCancelled
167 userInfo:nil]];
168 [self cancelCall];
169}
170
171- (void)dealloc {
murgatroid996cc46802015-04-28 09:35:48 -0700172 __block GRPCWrappedCall *wrappedCall = _wrappedCall;
Jorge Canizales5e0efd92015-02-17 18:23:58 -0800173 dispatch_async(_callQueue, ^{
murgatroid996cc46802015-04-28 09:35:48 -0700174 wrappedCall = nil;
Jorge Canizales5e0efd92015-02-17 18:23:58 -0800175 });
176}
177
178#pragma mark Read messages
179
180// Only called from the call queue.
181// The handler will be called from the network queue.
murgatroid996cc46802015-04-28 09:35:48 -0700182- (void)startReadWithHandler:(void(^)(grpc_byte_buffer *))handler {
murgatroid99ca38ddb2015-04-29 13:16:42 -0700183 // TODO(jcanizales): Add error handlers for async failures
murgatroid9954e93d42015-04-27 09:29:49 -0700184 [_wrappedCall startBatchWithOperations:@[[[GRPCOpRecvMessage alloc] initWithHandler:handler]]];
Jorge Canizales5e0efd92015-02-17 18:23:58 -0800185}
186
187// Called initially from the network queue once response headers are received,
188// then "recursively" from the responseWriteable queue after each response from the
189// server has been written.
190// If the call is currently paused, this is a noop. Restarting the call will invoke this
191// method.
192// TODO(jcanizales): Rename to readResponseIfNotPaused.
193- (void)startNextRead {
194 if (self.state == GRXWriterStatePaused) {
195 return;
196 }
197 __weak GRPCCall *weakSelf = self;
Jorge Canizales35f003b2015-07-17 21:14:36 -0700198 __weak GRXConcurrentWriteable *weakWriteable = _responseWriteable;
murgatroid9969927d62015-04-24 13:32:48 -0700199
Jorge Canizales5e0efd92015-02-17 18:23:58 -0800200 dispatch_async(_callQueue, ^{
murgatroid996cc46802015-04-28 09:35:48 -0700201 [weakSelf startReadWithHandler:^(grpc_byte_buffer *message) {
202 if (message == NULL) {
murgatroid99b56609c2015-04-28 16:41:11 -0700203 // No more messages from the server
Jorge Canizales5e0efd92015-02-17 18:23:58 -0800204 return;
205 }
murgatroid996cc46802015-04-28 09:35:48 -0700206 NSData *data = [NSData grpc_dataWithByteBuffer:message];
207 grpc_byte_buffer_destroy(message);
Jorge Canizales5e0efd92015-02-17 18:23:58 -0800208 if (!data) {
209 // The app doesn't have enough memory to hold the server response. We
210 // don't want to throw, because the app shouldn't crash for a behavior
211 // that's on the hands of any server to have. Instead we finish and ask
212 // the server to cancel.
213 //
214 // TODO(jcanizales): No canonical code is appropriate for this situation
215 // (because it's just a client problem). Use another domain and an
216 // appropriately-documented code.
217 [weakSelf finishWithError:[NSError errorWithDomain:kGRPCErrorDomain
218 code:GRPCErrorCodeInternal
219 userInfo:nil]];
220 [weakSelf cancelCall];
221 return;
222 }
Jorge Canizales4c6f7782015-07-17 23:13:36 -0700223 [weakWriteable enqueueValue:data completionHandler:^{
Jorge Canizales5e0efd92015-02-17 18:23:58 -0800224 [weakSelf startNextRead];
225 }];
226 }];
227 });
228}
229
230#pragma mark Send headers
231
Jorge Canizales2a246542015-08-12 17:58:38 -0700232- (void)sendHeaders:(NSDictionary *)headers {
murgatroid99ca38ddb2015-04-29 13:16:42 -0700233 // TODO(jcanizales): Add error handlers for async failures
murgatroid996cc46802015-04-28 09:35:48 -0700234 [_wrappedCall startBatchWithOperations:@[[[GRPCOpSendMetadata alloc]
Jorge Canizales2a246542015-08-12 17:58:38 -0700235 initWithMetadata:headers ?: @{} handler:nil]]];
Jorge Canizales5e0efd92015-02-17 18:23:58 -0800236}
237
238#pragma mark GRXWriteable implementation
239
240// Only called from the call queue. The error handler will be called from the
241// network queue if the write didn't succeed.
242- (void)writeMessage:(NSData *)message withErrorHandler:(void (^)())errorHandler {
243
244 __weak GRPCCall *weakSelf = self;
murgatroid9954e93d42015-04-27 09:29:49 -0700245 void(^resumingHandler)(void) = ^{
murgatroid99ca38ddb2015-04-29 13:16:42 -0700246 // Resume the request writer.
Jorge Canizales5e0efd92015-02-17 18:23:58 -0800247 GRPCCall *strongSelf = weakSelf;
248 if (strongSelf) {
Jorge Canizales578ab162015-08-08 17:11:43 -0700249 @synchronized(strongSelf->_requestWriter) {
Jorge Canizales238ad782015-08-07 23:11:29 -0700250 strongSelf->_requestWriter.state = GRXWriterStateStarted;
251 }
Jorge Canizales5e0efd92015-02-17 18:23:58 -0800252 }
253 };
Jorge Canizales238ad782015-08-07 23:11:29 -0700254 [_wrappedCall startBatchWithOperations:@[[[GRPCOpSendMessage alloc] initWithMessage:message
255 handler:resumingHandler]]
256 errorHandler:errorHandler];
Jorge Canizales5e0efd92015-02-17 18:23:58 -0800257}
258
Jorge Canizalesa90a9c32015-05-18 17:12:41 -0700259- (void)writeValue:(id)value {
Jorge Canizales5e0efd92015-02-17 18:23:58 -0800260 // TODO(jcanizales): Throw/assert if value isn't NSData.
261
262 // Pause the input and only resume it when the C layer notifies us that writes
263 // can proceed.
Jorge Canizales238ad782015-08-07 23:11:29 -0700264 @synchronized(_requestWriter) {
265 _requestWriter.state = GRXWriterStatePaused;
266 }
Jorge Canizales5e0efd92015-02-17 18:23:58 -0800267
268 __weak GRPCCall *weakSelf = self;
269 dispatch_async(_callQueue, ^{
270 [weakSelf writeMessage:value withErrorHandler:^{
271 [weakSelf finishWithError:[NSError errorWithDomain:kGRPCErrorDomain
272 code:GRPCErrorCodeInternal
273 userInfo:nil]];
274 }];
275 });
276}
277
278// Only called from the call queue. The error handler will be called from the
279// network queue if the requests stream couldn't be closed successfully.
280- (void)finishRequestWithErrorHandler:(void (^)())errorHandler {
murgatroid996cc46802015-04-28 09:35:48 -0700281 [_wrappedCall startBatchWithOperations:@[[[GRPCOpSendClose alloc] init]]
282 errorHandler:errorHandler];
Jorge Canizales5e0efd92015-02-17 18:23:58 -0800283}
284
Jorge Canizalesb2c300c2015-05-18 17:19:16 -0700285- (void)writesFinishedWithError:(NSError *)errorOrNil {
Jorge Canizales5e0efd92015-02-17 18:23:58 -0800286 if (errorOrNil) {
287 [self cancel];
288 } else {
289 __weak GRPCCall *weakSelf = self;
290 dispatch_async(_callQueue, ^{
291 [weakSelf finishRequestWithErrorHandler:^{
292 [weakSelf finishWithError:[NSError errorWithDomain:kGRPCErrorDomain
293 code:GRPCErrorCodeInternal
294 userInfo:nil]];
295 }];
296 });
297 }
298}
299
300#pragma mark Invoke
301
302// Both handlers will eventually be called, from the network queue. Writes can start immediately
303// after this.
Jorge Canizales0b34c892015-08-12 20:19:20 -0700304// The first one (headersHandler), when the response headers are received.
Jorge Canizales5e0efd92015-02-17 18:23:58 -0800305// The second one (completionHandler), whenever the RPC finishes for any reason.
Jorge Canizales0b34c892015-08-12 20:19:20 -0700306- (void)invokeCallWithHeadersHandler:(void(^)(NSDictionary *))headersHandler
Jorge Canizalesf3a4f2c2015-06-12 22:12:50 -0700307 completionHandler:(void(^)(NSError *, NSDictionary *))completionHandler {
murgatroid99ca38ddb2015-04-29 13:16:42 -0700308 // TODO(jcanizales): Add error handlers for async failures
murgatroid996cc46802015-04-28 09:35:48 -0700309 [_wrappedCall startBatchWithOperations:@[[[GRPCOpRecvMetadata alloc]
Jorge Canizales0b34c892015-08-12 20:19:20 -0700310 initWithHandler:headersHandler]]];
murgatroid996cc46802015-04-28 09:35:48 -0700311 [_wrappedCall startBatchWithOperations:@[[[GRPCOpRecvStatus alloc]
312 initWithHandler:completionHandler]]];
Jorge Canizales5e0efd92015-02-17 18:23:58 -0800313}
314
315- (void)invokeCall {
316 __weak GRPCCall *weakSelf = self;
Jorge Canizales0b34c892015-08-12 20:19:20 -0700317 [self invokeCallWithHeadersHandler:^(NSDictionary *headers) {
Jorge Canizalesf3a4f2c2015-06-12 22:12:50 -0700318 // Response headers received.
Jorge Canizales5e0efd92015-02-17 18:23:58 -0800319 GRPCCall *strongSelf = weakSelf;
320 if (strongSelf) {
Jorge Canizales0b34c892015-08-12 20:19:20 -0700321 strongSelf.responseHeaders = headers;
Jorge Canizales5e0efd92015-02-17 18:23:58 -0800322 [strongSelf startNextRead];
323 }
Jorge Canizalesf3a4f2c2015-06-12 22:12:50 -0700324 } completionHandler:^(NSError *error, NSDictionary *trailers) {
325 GRPCCall *strongSelf = weakSelf;
326 if (strongSelf) {
Jorge Canizales0b34c892015-08-12 20:19:20 -0700327 strongSelf.responseTrailers = trailers;
Jorge Canizalesf3a4f2c2015-06-12 22:12:50 -0700328
Jorge Canizalesc58a1102015-06-15 19:03:41 -0700329 if (error) {
Jorge Canizales0b34c892015-08-12 20:19:20 -0700330 NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
331 if (error.userInfo) {
332 [userInfo addEntriesFromDictionary:error.userInfo];
333 }
334 userInfo[kGRPCStatusMetadataKey] = strongSelf.responseTrailers;
335 // TODO(jcanizales): The C gRPC library doesn't guarantee that the headers block will be
336 // called before this one, so an error might end up with trailers but no headers. We
337 // shouldn't call finishWithError until ater both blocks are called. It is when this is done
338 // that we can provide a merged view of response headers and trailers in a thread-safe way.
Jorge Canizalesc58a1102015-06-15 19:03:41 -0700339 error = [NSError errorWithDomain:error.domain code:error.code userInfo:userInfo];
340 }
Jorge Canizalesf3a4f2c2015-06-12 22:12:50 -0700341 [strongSelf finishWithError:error];
342 }
Jorge Canizales5e0efd92015-02-17 18:23:58 -0800343 }];
344 // Now that the RPC has been initiated, request writes can start.
Jorge Canizales238ad782015-08-07 23:11:29 -0700345 @synchronized(_requestWriter) {
346 [_requestWriter startWithWriteable:self];
347 }
Jorge Canizales5e0efd92015-02-17 18:23:58 -0800348}
349
350#pragma mark GRXWriter implementation
351
352- (void)startWithWriteable:(id<GRXWriteable>)writeable {
Jorge Canizales6531b2b2015-07-18 00:19:14 -0700353 // Create a retain cycle so that this instance lives until the RPC finishes (or is cancelled).
354 // This makes RPCs in which the call isn't externally retained possible (as long as it is started
355 // before being autoreleased).
356 // Care is taken not to retain self strongly in any of the blocks used in this implementation, so
357 // that the life of the instance is determined by this retain cycle.
Jorge Canizaleseb87b462015-08-08 16:16:43 -0700358 _retainSelf = self;
Jorge Canizales6531b2b2015-07-18 00:19:14 -0700359
360 _responseWriteable = [[GRXConcurrentWriteable alloc] initWithWriteable:writeable];
Jorge Canizales6512d262015-08-12 17:33:23 -0700361 [self sendHeaders:_requestHeaders];
Jorge Canizales5e0efd92015-02-17 18:23:58 -0800362 [self invokeCall];
363}
364
365- (void)setState:(GRXWriterState)newState {
366 // Manual transitions are only allowed from the started or paused states.
367 if (_state == GRXWriterStateNotStarted || _state == GRXWriterStateFinished) {
368 return;
369 }
370
371 switch (newState) {
372 case GRXWriterStateFinished:
373 _state = newState;
374 // Per GRXWriter's contract, setting the state to Finished manually
375 // means one doesn't wish the writeable to be messaged anymore.
376 [_responseWriteable cancelSilently];
377 _responseWriteable = nil;
378 return;
379 case GRXWriterStatePaused:
380 _state = newState;
381 return;
382 case GRXWriterStateStarted:
383 if (_state == GRXWriterStatePaused) {
384 _state = newState;
385 [self startNextRead];
386 }
387 return;
388 case GRXWriterStateNotStarted:
389 return;
390 }
391}
392@end