| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef COMPONENTS_CRONET_IOS_CRONET_C_FOR_GRPC_H_ |
| 6 #define COMPONENTS_CRONET_IOS_CRONET_C_FOR_GRPC_H_ |
| 7 |
| 8 #define CRONET_EXPORT __attribute__((visibility("default"))) |
| 9 |
| 10 #ifdef __cplusplus |
| 11 extern "C" { |
| 12 #endif |
| 13 |
| 14 #include <stddef.h> |
| 15 |
| 16 /* Cronet Engine API. */ |
| 17 |
| 18 /* Opaque object representing Cronet Engine. Created and configured outside |
| 19 * of this API to facilitate sharing with other components */ |
| 20 typedef struct cronet_engine { |
| 21 void* obj; |
| 22 void* annotation; |
| 23 } cronet_engine; |
| 24 |
| 25 /* Cronet Bidirectional Stream API */ |
| 26 |
| 27 /* Opaque object representing Cronet Bidirectional Stream. */ |
| 28 typedef struct cronet_bidirectional_stream { |
| 29 void* obj; |
| 30 void* annotation; |
| 31 } cronet_bidirectional_stream; |
| 32 |
| 33 /* A single request or response header element. */ |
| 34 typedef struct cronet_bidirectional_stream_header { |
| 35 const char* key; |
| 36 const char* value; |
| 37 } cronet_bidirectional_stream_header; |
| 38 |
| 39 /* Array of request or response headers or trailers. */ |
| 40 typedef struct cronet_bidirectional_stream_header_array { |
| 41 size_t count; |
| 42 size_t capacity; |
| 43 cronet_bidirectional_stream_header* headers; |
| 44 } cronet_bidirectional_stream_header_array; |
| 45 |
| 46 /* Set of callbacks used to receive callbacks from bidirectional stream. */ |
| 47 typedef struct cronet_bidirectional_stream_callback { |
| 48 /* Invoked when the stream is ready for reading and writing. |
| 49 * Consumer may call cronet_bidirectional_stream_read() to start reading data. |
| 50 * Consumer may call cronet_bidirectional_stream_write() to start writing |
| 51 * data. |
| 52 */ |
| 53 void (*on_stream_ready)(cronet_bidirectional_stream* stream); |
| 54 |
| 55 /* Invoked when initial response headers are received. |
| 56 * Consumer must call cronet_bidirectional_stream_read() to start reading. |
| 57 * Consumer may call cronet_bidirectional_stream_write() to start writing or |
| 58 * close the stream. Contents of |headers| is valid for duration of the call. |
| 59 */ |
| 60 void (*on_response_headers_received)( |
| 61 cronet_bidirectional_stream* stream, |
| 62 const cronet_bidirectional_stream_header_array* headers, |
| 63 const char* negotiated_protocol); |
| 64 |
| 65 /* Invoked when data is read into the buffer passed to |
| 66 * cronet_bidirectional_stream_read(). Only part of the buffer may be |
| 67 * populated. To continue reading, call cronet_bidirectional_stream_read(). |
| 68 * It may be invoked after on_response_trailers_received()}, if there was |
| 69 * pending read data before trailers were received. |
| 70 * |
| 71 * If |bytes_read| is 0, it means the remote side has signaled that it will |
| 72 * send no more data; future calls to cronet_bidirectional_stream_read() |
| 73 * will result in the on_data_read() callback or on_succeded() callback if |
| 74 * cronet_bidirectional_stream_write() was invoked with end_of_stream set to |
| 75 * true. |
| 76 */ |
| 77 void (*on_read_completed)(cronet_bidirectional_stream* stream, |
| 78 char* data, |
| 79 int bytes_read); |
| 80 |
| 81 /** |
| 82 * Invoked when all data passed to cronet_bidirectional_stream_write() is |
| 83 * sent. To continue writing, call cronet_bidirectional_stream_write(). |
| 84 */ |
| 85 void (*on_write_completed)(cronet_bidirectional_stream* stream, |
| 86 const char* data); |
| 87 |
| 88 /* Invoked when trailers are received before closing the stream. Only invoked |
| 89 * when server sends trailers, which it may not. May be invoked while there is |
| 90 * read data remaining in local buffer. Contents of |trailers| is valid for |
| 91 * duration of the call. |
| 92 */ |
| 93 void (*on_response_trailers_received)( |
| 94 cronet_bidirectional_stream* stream, |
| 95 const cronet_bidirectional_stream_header_array* trailers); |
| 96 |
| 97 /** |
| 98 * Invoked when there is no data to be read or written and the stream is |
| 99 * closed successfully remotely and locally. Once invoked, no further callback |
| 100 * methods will be invoked. |
| 101 */ |
| 102 void (*on_succeded)(cronet_bidirectional_stream* stream); |
| 103 |
| 104 /** |
| 105 * Invoked if the stream failed for any reason after |
| 106 * cronet_bidirectional_stream_start(). HTTP/2 error codes are |
| 107 * mapped to chrome net error codes. Once invoked, no further callback methods |
| 108 * will be invoked. |
| 109 */ |
| 110 void (*on_failed)(cronet_bidirectional_stream* stream, int net_error); |
| 111 |
| 112 /** |
| 113 * Invoked if the stream was canceled via |
| 114 * cronet_bidirectional_stream_cancel(). Once invoked, no further callback |
| 115 * methods will be invoked. |
| 116 */ |
| 117 void (*on_canceled)(cronet_bidirectional_stream* stream); |
| 118 } cronet_bidirectional_stream_callback; |
| 119 |
| 120 /* Creates a new stream object that uses |engine| and |callback|. All stream |
| 121 * tasks are performed asynchronously on the |engine| network thread. |callback| |
| 122 * methods are invoked synchronously on the |engine| network thread, but must |
| 123 * not run tasks on the current thread to prevent blocking networking operations |
| 124 * and causing exceptions during shutdown. The |annotation| is stored in |
| 125 * bidirectional stream for arbitrary use by application. |
| 126 * |
| 127 * Returned |cronet_bidirectional_stream*| is owned by the caller, and must be |
| 128 * destroyed using |cronet_bidirectional_stream_destroy|. |
| 129 * |
| 130 * Both |calback| and |engine| must remain valid until stream is destroyed. |
| 131 */ |
| 132 CRONET_EXPORT |
| 133 cronet_bidirectional_stream* cronet_bidirectional_stream_create( |
| 134 cronet_engine* engine, |
| 135 void* annotation, |
| 136 cronet_bidirectional_stream_callback* callback); |
| 137 |
| 138 /* TBD: The following methods return int. Should it be a custom type? */ |
| 139 |
| 140 /* Destroys stream object. Destroy could be called from any thread, including |
| 141 * network thread, but is posted, so |stream| is valid until calling task is |
| 142 * complete. |
| 143 */ |
| 144 CRONET_EXPORT |
| 145 int cronet_bidirectional_stream_destroy(cronet_bidirectional_stream* stream); |
| 146 |
| 147 /** |
| 148 * Disables or enables auto flush. By default, data is flushed after |
| 149 * every cronet_bidirectional_stream_write(). If the auto flush is disabled, |
| 150 * the client should explicitly call cronet_bidirectional_stream_flush to flush |
| 151 * the data. |
| 152 */ |
| 153 CRONET_EXPORT void cronet_bidirectional_stream_disable_auto_flush( |
| 154 cronet_bidirectional_stream* stream, |
| 155 bool disable_auto_flush); |
| 156 |
| 157 /** |
| 158 * Delays sending request headers until cronet_bidirectional_stream_flush() |
| 159 * is called. This flag is currently only respected when QUIC is negotiated. |
| 160 * When true, QUIC will send request header frame along with data frame(s) |
| 161 * as a single packet when possible. |
| 162 */ |
| 163 CRONET_EXPORT |
| 164 void cronet_bidirectional_stream_delay_request_headers_until_flush( |
| 165 cronet_bidirectional_stream* stream, |
| 166 bool delay_headers_until_flush); |
| 167 |
| 168 /* Starts the stream by sending request to |url| using |method| and |headers|. |
| 169 * If |end_of_stream| is true, then no data is expected to be written. The |
| 170 * |method| is HTTP verb, with PUT having a special meaning to mark idempotent |
| 171 * request, which could use QUIC 0-RTT. |
| 172 */ |
| 173 CRONET_EXPORT |
| 174 int cronet_bidirectional_stream_start( |
| 175 cronet_bidirectional_stream* stream, |
| 176 const char* url, |
| 177 int priority, |
| 178 const char* method, |
| 179 const cronet_bidirectional_stream_header_array* headers, |
| 180 bool end_of_stream); |
| 181 |
| 182 /* Reads response data into |buffer| of |capacity| length. Must only be called |
| 183 * at most once in response to each invocation of the |
| 184 * on_stream_ready()/on_response_headers_received() and on_read_completed() |
| 185 * methods of the cronet_bidirectional_stream_callback. |
| 186 * Each call will result in an invocation of the callback's |
| 187 * on_read_completed() method if data is read, or its on_failed() method if |
| 188 * there's an error. The callback's on_succeeded() method is also invoked if |
| 189 * there is no more data to read and |end_of_stream| was previously sent. |
| 190 */ |
| 191 CRONET_EXPORT |
| 192 int cronet_bidirectional_stream_read(cronet_bidirectional_stream* stream, |
| 193 char* buffer, |
| 194 int capacity); |
| 195 |
| 196 /* Writes request data from |buffer| of |buffer_length| length. If auto flush is |
| 197 * disabled, data will be sent only after cronet_bidirectional_stream_flush() is |
| 198 * called. |
| 199 * Each call will result in an invocation the callback's on_write_completed() |
| 200 * method if data is sent, or its on_failed() method if there's an error. |
| 201 * The callback's on_succeeded() method is also invoked if |end_of_stream| is |
| 202 * set and all response data has been read. |
| 203 */ |
| 204 CRONET_EXPORT |
| 205 int cronet_bidirectional_stream_write(cronet_bidirectional_stream* stream, |
| 206 const char* buffer, |
| 207 int buffer_length, |
| 208 bool end_of_stream); |
| 209 |
| 210 /** |
| 211 * Flushes pending writes. This method should not be called before invocation of |
| 212 * on_stream_ready() method of the cronet_bidirectional_stream_callback. |
| 213 * For each previously called cronet_bidirectional_stream_write() |
| 214 * a corresponding on_write_completed() callback will be invoked when the buffer |
| 215 * is sent. |
| 216 */ |
| 217 CRONET_EXPORT |
| 218 void cronet_bidirectional_stream_flush(cronet_bidirectional_stream* stream); |
| 219 |
| 220 /* Cancels the stream. Can be called at any time after |
| 221 * cronet_bidirectional_stream_start(). The on_canceled() method of |
| 222 * cronet_bidirectional_stream_callback will be invoked when cancelation |
| 223 * is complete and no further callback methods will be invoked. If the |
| 224 * stream has completed or has not started, calling |
| 225 * cronet_bidirectional_stream_cancel() has no effect and on_canceled() will not |
| 226 * be invoked. At most one callback method may be invoked after |
| 227 * cronet_bidirectional_stream_cancel() has completed. |
| 228 */ |
| 229 CRONET_EXPORT |
| 230 void cronet_bidirectional_stream_cancel(cronet_bidirectional_stream* stream); |
| 231 |
| 232 /* Returns true if the |stream| was successfully started and is now done |
| 233 * (succeeded, canceled, or failed). |
| 234 * Returns false if the |stream| stream is not yet started or is in progress. |
| 235 */ |
| 236 CRONET_EXPORT |
| 237 bool cronet_bidirectional_stream_is_done(cronet_bidirectional_stream* stream); |
| 238 |
| 239 #ifdef __cplusplus |
| 240 } |
| 241 #endif |
| 242 |
| 243 #endif // COMPONENTS_CRONET_IOS_CRONET_C_FOR_GRPC_H_ |
| OLD | NEW |