Jorge Canizales | 142acc9 | 2015-05-15 18:43:34 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2015 gRPC authors. |
Jorge Canizales | 142acc9 | 2015-05-15 18:43:34 -0700 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
Jorge Canizales | 142acc9 | 2015-05-15 18:43:34 -0700 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Jorge Canizales | 142acc9 | 2015-05-15 18:43:34 -0700 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
Jorge Canizales | 142acc9 | 2015-05-15 18:43:34 -0700 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
| 19 | #import "GRXBufferedPipe.h" |
| 20 | |
Muxi Yan | d6545bb | 2017-06-05 09:22:15 -0700 | [diff] [blame] | 21 | @interface GRXBufferedPipe () |
Muxi Yan | d7d6a2e | 2017-06-06 14:39:15 -0700 | [diff] [blame] | 22 | @property(atomic) id<GRXWriteable> writeable; |
Muxi Yan | d6545bb | 2017-06-05 09:22:15 -0700 | [diff] [blame] | 23 | @end |
| 24 | |
Jorge Canizales | 142acc9 | 2015-05-15 18:43:34 -0700 | [diff] [blame] | 25 | @implementation GRXBufferedPipe { |
Muxi Yan | d7d6a2e | 2017-06-06 14:39:15 -0700 | [diff] [blame] | 26 | NSError *_errorOrNil; |
Muxi Yan | 91d7bb0 | 2017-05-10 14:26:40 -0700 | [diff] [blame] | 27 | dispatch_queue_t _writeQueue; |
Jorge Canizales | 142acc9 | 2015-05-15 18:43:34 -0700 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | @synthesize state = _state; |
| 31 | |
| 32 | + (instancetype)pipe { |
| 33 | return [[self alloc] init]; |
| 34 | } |
| 35 | |
| 36 | - (instancetype)init { |
| 37 | if (self = [super init]) { |
Jorge Canizales | 142acc9 | 2015-05-15 18:43:34 -0700 | [diff] [blame] | 38 | _state = GRXWriterStateNotStarted; |
Muxi Yan | 91d7bb0 | 2017-05-10 14:26:40 -0700 | [diff] [blame] | 39 | _writeQueue = dispatch_queue_create(NULL, DISPATCH_QUEUE_SERIAL); |
Muxi Yan | ec8e825 | 2017-05-15 14:59:07 -0700 | [diff] [blame] | 40 | dispatch_suspend(_writeQueue); |
Jorge Canizales | 142acc9 | 2015-05-15 18:43:34 -0700 | [diff] [blame] | 41 | } |
| 42 | return self; |
| 43 | } |
| 44 | |
Jorge Canizales | 142acc9 | 2015-05-15 18:43:34 -0700 | [diff] [blame] | 45 | #pragma mark GRXWriteable implementation |
| 46 | |
Jorge Canizales | a90a9c3 | 2015-05-18 17:12:41 -0700 | [diff] [blame] | 47 | - (void)writeValue:(id)value { |
Muxi Yan | 91d7bb0 | 2017-05-10 14:26:40 -0700 | [diff] [blame] | 48 | if ([value respondsToSelector:@selector(copy)]) { |
Muxi Yan | ec8e825 | 2017-05-15 14:59:07 -0700 | [diff] [blame] | 49 | // Even if we're paused and with enqueued values, we can't excert back-pressure to our writer. |
| 50 | // So just buffer the new value. |
| 51 | // We need a copy, so that it doesn't mutate before it's written at the other end of the pipe. |
Muxi Yan | 91d7bb0 | 2017-05-10 14:26:40 -0700 | [diff] [blame] | 52 | value = [value copy]; |
Jorge Canizales | 142acc9 | 2015-05-15 18:43:34 -0700 | [diff] [blame] | 53 | } |
Muxi Yan | ec8e825 | 2017-05-15 14:59:07 -0700 | [diff] [blame] | 54 | __weak GRXBufferedPipe *weakSelf = self; |
Muxi Yan | 91d7bb0 | 2017-05-10 14:26:40 -0700 | [diff] [blame] | 55 | dispatch_async(_writeQueue, ^(void) { |
Muxi Yan | d4792e9 | 2017-06-13 10:00:53 -0700 | [diff] [blame] | 56 | [weakSelf.writeable writeValue:value]; |
Muxi Yan | 91d7bb0 | 2017-05-10 14:26:40 -0700 | [diff] [blame] | 57 | }); |
Jorge Canizales | 142acc9 | 2015-05-15 18:43:34 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Jorge Canizales | b2c300c | 2015-05-18 17:19:16 -0700 | [diff] [blame] | 60 | - (void)writesFinishedWithError:(NSError *)errorOrNil { |
Muxi Yan | d6cee15 | 2017-06-26 17:09:09 -0700 | [diff] [blame] | 61 | __weak GRXBufferedPipe *weakSelf = self; |
| 62 | dispatch_async(_writeQueue, ^{ |
Muxi Yan | c05d1b4 | 2017-06-26 17:25:01 -0700 | [diff] [blame] | 63 | [weakSelf finishWithError:errorOrNil]; |
Muxi Yan | d6cee15 | 2017-06-26 17:09:09 -0700 | [diff] [blame] | 64 | }); |
Jorge Canizales | 142acc9 | 2015-05-15 18:43:34 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | #pragma mark GRXWriter implementation |
| 68 | |
| 69 | - (void)setState:(GRXWriterState)newState { |
Muxi Yan | c92d90a | 2018-04-11 18:10:02 -0700 | [diff] [blame] | 70 | @synchronized(self) { |
Muxi Yan | 033db46 | 2017-05-23 17:17:20 -0700 | [diff] [blame] | 71 | // Manual transitions are only allowed from the started or paused states. |
| 72 | if (_state == GRXWriterStateNotStarted || _state == GRXWriterStateFinished) { |
| 73 | return; |
| 74 | } |
Jorge Canizales | 142acc9 | 2015-05-15 18:43:34 -0700 | [diff] [blame] | 75 | |
Muxi Yan | 033db46 | 2017-05-23 17:17:20 -0700 | [diff] [blame] | 76 | switch (newState) { |
| 77 | case GRXWriterStateFinished: |
Muxi Yan | 08fef09 | 2017-06-26 12:19:27 -0700 | [diff] [blame] | 78 | self.writeable = nil; |
Muxi Yan | 033db46 | 2017-05-23 17:17:20 -0700 | [diff] [blame] | 79 | if (_state == GRXWriterStatePaused) { |
Muxi Yan | d6cee15 | 2017-06-26 17:09:09 -0700 | [diff] [blame] | 80 | dispatch_resume(_writeQueue); |
Muxi Yan | 033db46 | 2017-05-23 17:17:20 -0700 | [diff] [blame] | 81 | } |
Jorge Canizales | 142acc9 | 2015-05-15 18:43:34 -0700 | [diff] [blame] | 82 | _state = newState; |
Muxi Yan | 033db46 | 2017-05-23 17:17:20 -0700 | [diff] [blame] | 83 | return; |
| 84 | case GRXWriterStatePaused: |
| 85 | if (_state == GRXWriterStateStarted) { |
| 86 | _state = newState; |
| 87 | dispatch_suspend(_writeQueue); |
| 88 | } |
| 89 | return; |
| 90 | case GRXWriterStateStarted: |
Muxi Yan | c05d1b4 | 2017-06-26 17:25:01 -0700 | [diff] [blame] | 91 | if (_state == GRXWriterStatePaused) { |
Muxi Yan | d6545bb | 2017-06-05 09:22:15 -0700 | [diff] [blame] | 92 | _state = newState; |
Muxi Yan | 033db46 | 2017-05-23 17:17:20 -0700 | [diff] [blame] | 93 | dispatch_resume(_writeQueue); |
| 94 | } |
| 95 | return; |
| 96 | case GRXWriterStateNotStarted: |
| 97 | return; |
| 98 | } |
Jorge Canizales | 142acc9 | 2015-05-15 18:43:34 -0700 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
| 102 | - (void)startWithWriteable:(id<GRXWriteable>)writeable { |
Muxi Yan | 08fef09 | 2017-06-26 12:19:27 -0700 | [diff] [blame] | 103 | self.writeable = writeable; |
Muxi Yan | c05d1b4 | 2017-06-26 17:25:01 -0700 | [diff] [blame] | 104 | _state = GRXWriterStateStarted; |
| 105 | dispatch_resume(_writeQueue); |
Jorge Canizales | 142acc9 | 2015-05-15 18:43:34 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | - (void)finishWithError:(NSError *)errorOrNil { |
Muxi Yan | d6cee15 | 2017-06-26 17:09:09 -0700 | [diff] [blame] | 109 | [self.writeable writesFinishedWithError:errorOrNil]; |
Jorge Canizales | 142acc9 | 2015-05-15 18:43:34 -0700 | [diff] [blame] | 110 | self.state = GRXWriterStateFinished; |
Jorge Canizales | 142acc9 | 2015-05-15 18:43:34 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Muxi Yan | 860b1da | 2017-07-29 12:05:19 -0700 | [diff] [blame] | 113 | - (void)dealloc { |
| 114 | GRXWriterState state = self.state; |
Muxi Yan | c92d90a | 2018-04-11 18:10:02 -0700 | [diff] [blame] | 115 | if (state == GRXWriterStateNotStarted || state == GRXWriterStatePaused) { |
Muxi Yan | 860b1da | 2017-07-29 12:05:19 -0700 | [diff] [blame] | 116 | dispatch_resume(_writeQueue); |
| 117 | } |
| 118 | } |
| 119 | |
Jorge Canizales | 142acc9 | 2015-05-15 18:43:34 -0700 | [diff] [blame] | 120 | @end |