OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015, Google Inc. |
| 4 * 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 |
| 34 /** |
| 35 * The gRPC protocol is an RPC protocol on top of HTTP2. |
| 36 * |
| 37 * While the most common type of RPC receives only one request message and retur
ns only one response |
| 38 * message, the protocol also supports RPCs that return multiple individual mess
ages in a streaming |
| 39 * fashion, RPCs that accept a stream of request messages, or RPCs with both str
eaming requests and |
| 40 * responses. |
| 41 * |
| 42 * Conceptually, each gRPC call consists of a bidirectional stream of binary mes
sages, with RPCs of |
| 43 * the "non-streaming type" sending only one message in the corresponding direct
ion (the protocol |
| 44 * doesn't make any distinction). |
| 45 * |
| 46 * Each RPC uses a different HTTP2 stream, and thus multiple simultaneous RPCs c
an be multiplexed |
| 47 * transparently on the same TCP connection. |
| 48 */ |
| 49 |
| 50 #import <Foundation/Foundation.h> |
| 51 #import <RxLibrary/GRXWriter.h> |
| 52 |
| 53 #include <AvailabilityMacros.h> |
| 54 |
| 55 #pragma mark gRPC errors |
| 56 |
| 57 /** Domain of NSError objects produced by gRPC. */ |
| 58 extern NSString *const kGRPCErrorDomain; |
| 59 |
| 60 /** |
| 61 * gRPC error codes. |
| 62 * Note that a few of these are never produced by the gRPC libraries, but are of
general utility for |
| 63 * server applications to produce. |
| 64 */ |
| 65 typedef NS_ENUM(NSUInteger, GRPCErrorCode) { |
| 66 /** The operation was cancelled (typically by the caller). */ |
| 67 GRPCErrorCodeCancelled = 1, |
| 68 |
| 69 /** |
| 70 * Unknown error. Errors raised by APIs that do not return enough error inform
ation may be |
| 71 * converted to this error. |
| 72 */ |
| 73 GRPCErrorCodeUnknown = 2, |
| 74 |
| 75 /** |
| 76 * The client specified an invalid argument. Note that this differs from FAILE
D_PRECONDITION. |
| 77 * INVALID_ARGUMENT indicates arguments that are problematic regardless of the
state of the |
| 78 * server (e.g., a malformed file name). |
| 79 */ |
| 80 GRPCErrorCodeInvalidArgument = 3, |
| 81 |
| 82 /** |
| 83 * Deadline expired before operation could complete. For operations that chang
e the state of the |
| 84 * server, this error may be returned even if the operation has completed succ
essfully. For |
| 85 * example, a successful response from the server could have been delayed long
enough for the |
| 86 * deadline to expire. |
| 87 */ |
| 88 GRPCErrorCodeDeadlineExceeded = 4, |
| 89 |
| 90 /** Some requested entity (e.g., file or directory) was not found. */ |
| 91 GRPCErrorCodeNotFound = 5, |
| 92 |
| 93 /** Some entity that we attempted to create (e.g., file or directory) already
exists. */ |
| 94 GRPCErrorCodeAlreadyExists = 6, |
| 95 |
| 96 /** |
| 97 * The caller does not have permission to execute the specified operation. PER
MISSION_DENIED isn't |
| 98 * used for rejections caused by exhausting some resource (RESOURCE_EXHAUSTED
is used instead for |
| 99 * those errors). PERMISSION_DENIED doesn't indicate a failure to identify the
caller |
| 100 * (UNAUTHENTICATED is used instead for those errors). |
| 101 */ |
| 102 GRPCErrorCodePermissionDenied = 7, |
| 103 |
| 104 /** |
| 105 * The request does not have valid authentication credentials for the operatio
n (e.g. the caller's |
| 106 * identity can't be verified). |
| 107 */ |
| 108 GRPCErrorCodeUnauthenticated = 16, |
| 109 |
| 110 /** Some resource has been exhausted, perhaps a per-user quota. */ |
| 111 GRPCErrorCodeResourceExhausted = 8, |
| 112 |
| 113 /** |
| 114 * The RPC was rejected because the server is not in a state required for the
procedure's |
| 115 * execution. For example, a directory to be deleted may be non-empty, etc. |
| 116 * The client should not retry until the server state has been explicitly fixe
d (e.g. by |
| 117 * performing another RPC). The details depend on the service being called, an
d should be found in |
| 118 * the NSError's userInfo. |
| 119 */ |
| 120 GRPCErrorCodeFailedPrecondition = 9, |
| 121 |
| 122 /** |
| 123 * The RPC was aborted, typically due to a concurrency issue like sequencer ch
eck failures, |
| 124 * transaction aborts, etc. The client should retry at a higher-level (e.g., r
estarting a read- |
| 125 * modify-write sequence). |
| 126 */ |
| 127 GRPCErrorCodeAborted = 10, |
| 128 |
| 129 /** |
| 130 * The RPC was attempted past the valid range. E.g., enumerating past the end
of a list. |
| 131 * Unlike INVALID_ARGUMENT, this error indicates a problem that may be fixed i
f the system state |
| 132 * changes. For example, an RPC to get elements of a list will generate INVALI
D_ARGUMENT if asked |
| 133 * to return the element at a negative index, but it will generate OUT_OF_RANG
E if asked to return |
| 134 * the element at an index past the current size of the list. |
| 135 */ |
| 136 GRPCErrorCodeOutOfRange = 11, |
| 137 |
| 138 /** The procedure is not implemented or not supported/enabled in this server.
*/ |
| 139 GRPCErrorCodeUnimplemented = 12, |
| 140 |
| 141 /** |
| 142 * Internal error. Means some invariant expected by the server application or
the gRPC library has |
| 143 * been broken. |
| 144 */ |
| 145 GRPCErrorCodeInternal = 13, |
| 146 |
| 147 /** |
| 148 * The server is currently unavailable. This is most likely a transient condit
ion and may be |
| 149 * corrected by retrying with a backoff. |
| 150 */ |
| 151 GRPCErrorCodeUnavailable = 14, |
| 152 |
| 153 /** Unrecoverable data loss or corruption. */ |
| 154 GRPCErrorCodeDataLoss = 15, |
| 155 }; |
| 156 |
| 157 /** |
| 158 * Keys used in |NSError|'s |userInfo| dictionary to store the response headers
and trailers sent by |
| 159 * the server. |
| 160 */ |
| 161 extern id const kGRPCHeadersKey; |
| 162 extern id const kGRPCTrailersKey; |
| 163 |
| 164 #pragma mark GRPCCall |
| 165 |
| 166 /** Represents a single gRPC remote call. */ |
| 167 @interface GRPCCall : GRXWriter |
| 168 |
| 169 /** |
| 170 * The container of the request headers of an RPC conforms to this protocol, whi
ch is a subset of |
| 171 * NSMutableDictionary's interface. It will become a NSMutableDictionary later o
n. |
| 172 * The keys of this container are the header names, which per the HTTP standard
are case- |
| 173 * insensitive. They are stored in lowercase (which is how HTTP/2 mandates them
on the wire), and |
| 174 * can only consist of ASCII characters. |
| 175 * A header value is a NSString object (with only ASCII characters), unless the
header name has the |
| 176 * suffix "-bin", in which case the value has to be a NSData object. |
| 177 */ |
| 178 /** |
| 179 * These HTTP headers will be passed to the server as part of this call. Each HT
TP header is a |
| 180 * name-value pair with string names and either string or binary values. |
| 181 * |
| 182 * The passed dictionary has to use NSString keys, corresponding to the header n
ames. The value |
| 183 * associated to each can be a NSString object or a NSData object. E.g.: |
| 184 * |
| 185 * call.requestHeaders = @{@"authorization": @"Bearer ..."}; |
| 186 * |
| 187 * call.requestHeaders[@"my-header-bin"] = someData; |
| 188 * |
| 189 * After the call is started, trying to modify this property is an error. |
| 190 * |
| 191 * The property is initialized to an empty NSMutableDictionary. |
| 192 */ |
| 193 @property(atomic, readonly) NSMutableDictionary *requestHeaders; |
| 194 |
| 195 /** |
| 196 * This dictionary is populated with the HTTP headers received from the server.
This happens before |
| 197 * any response message is received from the server. It has the same structure a
s the request |
| 198 * headers dictionary: Keys are NSString header names; names ending with the suf
fix "-bin" have a |
| 199 * NSData value; the others have a NSString value. |
| 200 * |
| 201 * The value of this property is nil until all response headers are received, an
d will change before |
| 202 * any of -writeValue: or -writesFinishedWithError: are sent to the writeable. |
| 203 */ |
| 204 @property(atomic, readonly) NSDictionary *responseHeaders; |
| 205 |
| 206 /** |
| 207 * Same as responseHeaders, but populated with the HTTP trailers received from t
he server before the |
| 208 * call finishes. |
| 209 * |
| 210 * The value of this property is nil until all response trailers are received, a
nd will change |
| 211 * before -writesFinishedWithError: is sent to the writeable. |
| 212 */ |
| 213 @property(atomic, readonly) NSDictionary *responseTrailers; |
| 214 |
| 215 /** |
| 216 * The request writer has to write NSData objects into the provided Writeable. T
he server will |
| 217 * receive each of those separately and in order as distinct messages. |
| 218 * A gRPC call might not complete until the request writer finishes. On the othe
r hand, the request |
| 219 * finishing doesn't necessarily make the call to finish, as the server might co
ntinue sending |
| 220 * messages to the response side of the call indefinitely (depending on the sema
ntics of the |
| 221 * specific remote method called). |
| 222 * To finish a call right away, invoke cancel. |
| 223 */ |
| 224 - (instancetype)initWithHost:(NSString *)host |
| 225 path:(NSString *)path |
| 226 requestsWriter:(GRXWriter *)requestsWriter NS_DESIGNATED_INITIALIZ
ER; |
| 227 |
| 228 /** |
| 229 * Finishes the request side of this call, notifies the server that the RPC shou
ld be cancelled, and |
| 230 * finishes the response side of the call with an error of code CANCELED. |
| 231 */ |
| 232 - (void)cancel; |
| 233 |
| 234 // TODO(jcanizales): Let specify a deadline. As a category of GRXWriter? |
| 235 @end |
| 236 |
| 237 #pragma mark Backwards compatibiity |
| 238 |
| 239 /** This protocol is kept for backwards compatibility with existing code. */ |
| 240 DEPRECATED_MSG_ATTRIBUTE("Use NSDictionary or NSMutableDictionary instead.") |
| 241 @protocol GRPCRequestHeaders <NSObject> |
| 242 @property(nonatomic, readonly) NSUInteger count; |
| 243 |
| 244 - (id)objectForKeyedSubscript:(id)key; |
| 245 - (void)setObject:(id)obj forKeyedSubscript:(id)key; |
| 246 |
| 247 - (void)removeAllObjects; |
| 248 - (void)removeObjectForKey:(id)key; |
| 249 @end |
| 250 |
| 251 #pragma clang diagnostic push |
| 252 #pragma clang diagnostic ignored "-Wdeprecated" |
| 253 /** This is only needed for backwards-compatibility. */ |
| 254 @interface NSMutableDictionary (GRPCRequestHeaders) <GRPCRequestHeaders> |
| 255 @end |
| 256 #pragma clang diagnostic pop |
OLD | NEW |