GRPCCall.m 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. *
  3. * Copyright 2015 gRPC authors.
  4. *
  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
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  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.
  16. *
  17. */
  18. #import "GRPCCall.h"
  19. #import "GRPCCall+Interceptor.h"
  20. #import "GRPCCallOptions.h"
  21. #import "GRPCInterceptor.h"
  22. #import "GRPCTransport.h"
  23. #import "private/GRPCTransport+Private.h"
  24. NSString *const kGRPCHeadersKey = @"io.grpc.HeadersKey";
  25. NSString *const kGRPCTrailersKey = @"io.grpc.TrailersKey";
  26. NSString *const kGRPCErrorDomain = @"io.grpc";
  27. /**
  28. * The response dispatcher creates its own serial dispatch queue and target the queue to the
  29. * dispatch queue of a user provided response handler. It removes the requirement of having to use
  30. * serial dispatch queue in the user provided response handler.
  31. */
  32. @interface GRPCResponseDispatcher : NSObject <GRPCResponseHandler>
  33. - (nullable instancetype)initWithResponseHandler:(id<GRPCResponseHandler>)responseHandler;
  34. @end
  35. @implementation GRPCResponseDispatcher {
  36. id<GRPCResponseHandler> _responseHandler;
  37. dispatch_queue_t _dispatchQueue;
  38. }
  39. - (instancetype)initWithResponseHandler:(id<GRPCResponseHandler>)responseHandler {
  40. if ((self = [super init])) {
  41. _responseHandler = responseHandler;
  42. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 || __MAC_OS_X_VERSION_MAX_ALLOWED >= 101300
  43. if (@available(iOS 8.0, macOS 10.10, *)) {
  44. _dispatchQueue = dispatch_queue_create(
  45. NULL,
  46. dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_DEFAULT, 0));
  47. } else {
  48. #else
  49. {
  50. #endif
  51. _dispatchQueue = dispatch_queue_create(NULL, DISPATCH_QUEUE_SERIAL);
  52. }
  53. dispatch_set_target_queue(_dispatchQueue, _responseHandler.dispatchQueue);
  54. }
  55. return self;
  56. }
  57. - (dispatch_queue_t)dispatchQueue {
  58. return _dispatchQueue;
  59. }
  60. - (void)didReceiveInitialMetadata:(nullable NSDictionary *)initialMetadata {
  61. if ([_responseHandler respondsToSelector:@selector(didReceiveInitialMetadata:)]) {
  62. [_responseHandler didReceiveInitialMetadata:initialMetadata];
  63. }
  64. }
  65. - (void)didReceiveData:(id)data {
  66. // For backwards compatibility with didReceiveRawMessage, if the user provided a response handler
  67. // that handles didReceiveRawMesssage, we issue to that method instead
  68. if ([_responseHandler respondsToSelector:@selector(didReceiveRawMessage:)]) {
  69. [_responseHandler didReceiveRawMessage:data];
  70. } else if ([_responseHandler respondsToSelector:@selector(didReceiveData:)]) {
  71. [_responseHandler didReceiveData:data];
  72. }
  73. }
  74. - (void)didCloseWithTrailingMetadata:(nullable NSDictionary *)trailingMetadata
  75. error:(nullable NSError *)error {
  76. if ([_responseHandler respondsToSelector:@selector(didCloseWithTrailingMetadata:error:)]) {
  77. [_responseHandler didCloseWithTrailingMetadata:trailingMetadata error:error];
  78. }
  79. }
  80. - (void)didWriteData {
  81. if ([_responseHandler respondsToSelector:@selector(didWriteData)]) {
  82. [_responseHandler didWriteData];
  83. }
  84. }
  85. @end
  86. @implementation GRPCRequestOptions
  87. - (instancetype)initWithHost:(NSString *)host path:(NSString *)path safety:(GRPCCallSafety)safety {
  88. NSAssert(host.length != 0 && path.length != 0, @"host and path cannot be empty");
  89. if (host.length == 0 || path.length == 0) {
  90. return nil;
  91. }
  92. if ((self = [super init])) {
  93. _host = [host copy];
  94. _path = [path copy];
  95. _safety = safety;
  96. }
  97. return self;
  98. }
  99. - (id)copyWithZone:(NSZone *)zone {
  100. GRPCRequestOptions *request = [[GRPCRequestOptions alloc] initWithHost:_host
  101. path:_path
  102. safety:_safety];
  103. return request;
  104. }
  105. @end
  106. /**
  107. * This class acts as a wrapper for interceptors
  108. */
  109. @implementation GRPCCall2 {
  110. /** The handler of responses. */
  111. id<GRPCResponseHandler> _responseHandler;
  112. /**
  113. * Points to the first interceptor in the interceptor chain.
  114. */
  115. id<GRPCInterceptorInterface> _firstInterceptor;
  116. /**
  117. * The actual call options being used by this call. It is different from the user-provided
  118. * call options when the user provided a NULL call options object.
  119. */
  120. GRPCCallOptions *_actualCallOptions;
  121. }
  122. - (instancetype)initWithRequestOptions:(GRPCRequestOptions *)requestOptions
  123. responseHandler:(id<GRPCResponseHandler>)responseHandler
  124. callOptions:(GRPCCallOptions *)callOptions {
  125. NSAssert(requestOptions.host.length != 0 && requestOptions.path.length != 0,
  126. @"Neither host nor path can be nil.");
  127. NSAssert(requestOptions.safety <= GRPCCallSafetyCacheableRequest, @"Invalid call safety value.");
  128. NSAssert(responseHandler != nil, @"Response handler required.");
  129. if (requestOptions.host.length == 0 || requestOptions.path.length == 0) {
  130. return nil;
  131. }
  132. if (requestOptions.safety > GRPCCallSafetyCacheableRequest) {
  133. return nil;
  134. }
  135. if (responseHandler == nil) {
  136. return nil;
  137. }
  138. if ((self = [super init])) {
  139. _requestOptions = [requestOptions copy];
  140. _callOptions = [callOptions copy];
  141. if (!_callOptions) {
  142. _actualCallOptions = [[GRPCCallOptions alloc] init];
  143. } else {
  144. _actualCallOptions = [callOptions copy];
  145. }
  146. _responseHandler = responseHandler;
  147. GRPCResponseDispatcher *dispatcher =
  148. [[GRPCResponseDispatcher alloc] initWithResponseHandler:_responseHandler];
  149. NSMutableArray<id<GRPCInterceptorFactory>> *interceptorFactories;
  150. if (_actualCallOptions.interceptorFactories != nil) {
  151. interceptorFactories =
  152. [NSMutableArray arrayWithArray:_actualCallOptions.interceptorFactories];
  153. } else {
  154. interceptorFactories = [NSMutableArray array];
  155. }
  156. id<GRPCInterceptorFactory> globalInterceptorFactory = [GRPCCall2 globalInterceptorFactory];
  157. if (globalInterceptorFactory != nil) {
  158. [interceptorFactories addObject:globalInterceptorFactory];
  159. }
  160. if (_actualCallOptions.transport != NULL) {
  161. id<GRPCTransportFactory> transportFactory = [[GRPCTransportRegistry sharedInstance]
  162. getTransportFactoryWithID:_actualCallOptions.transport];
  163. NSArray<id<GRPCInterceptorFactory>> *transportInterceptorFactories =
  164. transportFactory.transportInterceptorFactories;
  165. if (transportInterceptorFactories != nil) {
  166. [interceptorFactories addObjectsFromArray:transportInterceptorFactories];
  167. }
  168. }
  169. // continuously create interceptor until one is successfully created
  170. while (_firstInterceptor == nil) {
  171. if (interceptorFactories.count == 0) {
  172. _firstInterceptor =
  173. [[GRPCTransportManager alloc] initWithTransportID:_actualCallOptions.transport
  174. previousInterceptor:dispatcher];
  175. break;
  176. } else {
  177. _firstInterceptor =
  178. [[GRPCInterceptorManager alloc] initWithFactories:interceptorFactories
  179. previousInterceptor:dispatcher
  180. transportID:_actualCallOptions.transport];
  181. if (_firstInterceptor == nil) {
  182. [interceptorFactories removeObjectAtIndex:0];
  183. }
  184. }
  185. }
  186. NSAssert(_firstInterceptor != nil, @"Failed to create interceptor or transport.");
  187. if (_firstInterceptor == nil) {
  188. NSLog(@"Failed to create interceptor or transport.");
  189. }
  190. }
  191. return self;
  192. }
  193. - (instancetype)initWithRequestOptions:(GRPCRequestOptions *)requestOptions
  194. responseHandler:(id<GRPCResponseHandler>)responseHandler {
  195. return [self initWithRequestOptions:requestOptions
  196. responseHandler:responseHandler
  197. callOptions:nil];
  198. }
  199. - (void)start {
  200. id<GRPCInterceptorInterface> copiedFirstInterceptor = _firstInterceptor;
  201. GRPCRequestOptions *requestOptions = _requestOptions;
  202. GRPCCallOptions *callOptions = _actualCallOptions;
  203. dispatch_async(copiedFirstInterceptor.dispatchQueue, ^{
  204. [copiedFirstInterceptor startWithRequestOptions:requestOptions callOptions:callOptions];
  205. });
  206. }
  207. - (void)cancel {
  208. id<GRPCInterceptorInterface> copiedFirstInterceptor = _firstInterceptor;
  209. if (copiedFirstInterceptor != nil) {
  210. dispatch_async(copiedFirstInterceptor.dispatchQueue, ^{
  211. [copiedFirstInterceptor cancel];
  212. });
  213. }
  214. }
  215. - (void)writeData:(id)data {
  216. id<GRPCInterceptorInterface> copiedFirstInterceptor = _firstInterceptor;
  217. dispatch_async(copiedFirstInterceptor.dispatchQueue, ^{
  218. [copiedFirstInterceptor writeData:data];
  219. });
  220. }
  221. - (void)finish {
  222. id<GRPCInterceptorInterface> copiedFirstInterceptor = _firstInterceptor;
  223. dispatch_async(copiedFirstInterceptor.dispatchQueue, ^{
  224. [copiedFirstInterceptor finish];
  225. });
  226. }
  227. - (void)receiveNextMessages:(NSUInteger)numberOfMessages {
  228. id<GRPCInterceptorInterface> copiedFirstInterceptor = _firstInterceptor;
  229. dispatch_async(copiedFirstInterceptor.dispatchQueue, ^{
  230. [copiedFirstInterceptor receiveNextMessages:numberOfMessages];
  231. });
  232. }
  233. @end