Chromium Code Reviews| 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 #ifdef __cplusplus | |
| 9 extern "C" { | |
| 10 #endif | |
| 11 | |
| 12 #include <stddef.h> | |
| 13 | |
| 14 /* Cronet Engine API. */ | |
| 15 | |
| 16 /* Opaque object representing Cronet Engine. Created and configured outside | |
| 17 * of this API to facilitate sharing with other components */ | |
| 18 typedef struct cronet_engine { | |
| 19 void* obj; | |
| 20 void* annotation; | |
| 21 } cronet_engine; | |
| 22 | |
| 23 /* Cronet Bidirectional Stream API */ | |
| 24 | |
| 25 /* Opaque object representing Cronet Bidirectional Stream. */ | |
| 26 typedef struct cronet_bidirectional_stream { | |
| 27 void* obj; | |
| 28 void* annotation; | |
| 29 } cronet_bidirectional_stream; | |
| 30 | |
| 31 /* A single request or response header element. */ | |
| 32 typedef struct cronet_bidirectional_stream_header { | |
| 33 const char* key; | |
| 34 const char* value; | |
| 35 } cronet_bidirectional_stream_header; | |
| 36 | |
| 37 /* Array of request or response headers or trailers. */ | |
| 38 typedef struct cronet_bidirectional_stream_header_array { | |
| 39 size_t count; | |
| 40 size_t capacity; | |
| 41 cronet_bidirectional_stream_header* headers; | |
| 42 } cronet_bidirectional_stream_header_array; | |
| 43 | |
| 44 /* Set of callbacks used to receive callbacks from bidirectional stream. */ | |
| 45 typedef struct cronet_bidirectional_stream_callback { | |
| 46 /* Invoked when request headers are sent. Indicates that stream has initiated | |
| 47 * the request. Consumer may call cronet_bidirectional_stream_write() to start | |
| 48 * writing data. | |
| 49 */ | |
| 50 void (*on_request_headers_sent)(cronet_bidirectional_stream* stream); | |
| 51 | |
| 52 /* Invoked when initial response headers are received. | |
| 53 * Consumer must call cronet_bidirectional_stream_read() to start reading. | |
| 54 * Consumer may call cronet_bidirectional_stream_write() to start writing or | |
| 55 * close the stream. Contents of |headers| is valid for duration of the call. | |
| 56 */ | |
| 57 void (*on_response_headers_received)( | |
| 58 cronet_bidirectional_stream* stream, | |
| 59 const cronet_bidirectional_stream_header_array* headers, | |
| 60 const char* negotiated_protocol); | |
| 61 | |
| 62 /* Invoked when data is read into the buffer passed to | |
| 63 * cronet_bidirectional_stream_read(). Only part of the buffer may be | |
| 64 * populated. To continue reading, call cronet_bidirectional_stream_read(). | |
| 65 * It may be invoked after on_response_trailers_received()}, if there was | |
| 66 * pending read data before trailers were received. | |
| 67 * | |
| 68 * If |bytes_read| is 0, it means the remote side has signaled that it will | |
| 69 * send no more data; future calls to cronet_bidirectional_stream_read() | |
| 70 * will result in the on_data_read() callback or on_succeded() callback if | |
| 71 * cronet_bidirectional_stream_write() was invoked with end_of_stream set to | |
| 72 * true. | |
| 73 */ | |
| 74 void (*on_read_completed)(cronet_bidirectional_stream* stream, | |
| 75 char* data, | |
| 76 int bytes_read); | |
| 77 | |
| 78 /** | |
| 79 * Invoked when all data passed to cronet_bidirectional_stream_write() is | |
| 80 * sent. To continue writing, call cronet_bidirectional_stream_write(). | |
| 81 */ | |
| 82 void (*on_write_completed)(cronet_bidirectional_stream* stream, | |
| 83 const char* data); | |
| 84 | |
| 85 /* Invoked when trailers are received before closing the stream. Only invoked | |
| 86 * when server sends trailers, which it may not. May be invoked while there is | |
| 87 * read data remaining in local buffer. Contents of |trailers| is valid for | |
| 88 * duration of the call. | |
| 89 */ | |
| 90 void (*on_response_trailers_received)( | |
| 91 cronet_bidirectional_stream* stream, | |
| 92 const cronet_bidirectional_stream_header_array* trailers); | |
| 93 | |
| 94 /** | |
| 95 * Invoked when there is no data to be read or written and the stream is | |
| 96 * closed successfully remotely and locally. Once invoked, no further callback | |
| 97 * methods will be invoked. | |
| 98 */ | |
| 99 void (*on_succeded)(cronet_bidirectional_stream* stream); | |
| 100 | |
| 101 /** | |
| 102 * Invoked if the stream failed for any reason after | |
| 103 * cronet_bidirectional_stream_start(). HTTP/2 error codes are | |
| 104 * mapped to chrome net error codes. Once invoked, no further callback methods | |
| 105 * will be invoked. | |
| 106 */ | |
| 107 void (*on_failed)(cronet_bidirectional_stream* stream, int net_error); | |
| 108 | |
| 109 /** | |
| 110 * Invoked if the stream was canceled via | |
| 111 * cronet_bidirectional_stream_cancel(). Once invoked, no further callback | |
| 112 * methods will be invoked. | |
| 113 */ | |
| 114 void (*on_canceled)(cronet_bidirectional_stream* stream); | |
| 115 } cronet_bidirectional_stream_callback; | |
| 116 | |
| 117 /* Create a new stream object that uses |engine| and |callback|. All stream | |
| 118 * tasks are performed asynchronously on the |engine| network thread. |callback| | |
| 119 * methods are invoked synchronously on the |engine| network thread, but must | |
| 120 * not run tasks on the current thread to prevent blocking networking operations | |
| 121 * and causing exceptions during shutdown. The |annotation| is stored in | |
| 122 * bidirectional stream for arbitrary use by application. | |
| 123 * | |
| 124 * Returned |cronet_bidirectional_stream*| is owned by the caller, and must be | |
| 125 * destroyed using |cronet_bidirectional_stream_destroy|. | |
| 126 * | |
| 127 * Both |calback| and |engine| must remain valid until stream is destroyed. | |
| 128 */ | |
| 129 cronet_bidirectional_stream* cronet_bidirectional_stream_create( | |
| 130 cronet_engine* engine, | |
| 131 void* annotation, | |
| 132 cronet_bidirectional_stream_callback* callback); | |
| 133 | |
| 134 /* TBD: The following methods return int. Should it be a custom type? */ | |
| 135 | |
| 136 /* Destroy stream object. Destroy could be called from any thread, including | |
| 137 * network thread, but is posted, so |stream| is valid until calling task is | |
| 138 * complete. | |
| 139 */ | |
| 140 int cronet_bidirectional_stream_destroy(cronet_bidirectional_stream* stream); | |
| 141 | |
| 142 /* Start the stream by sending request to |url| using |method| and |headers|. If | |
| 143 * |end_of_stream| is true, then no data is expected to be written. The |method| | |
| 144 * is HTTP verb, with PUT having a special meaning to mark idempotent request, | |
| 145 * which could use QUIC 0-RTT. | |
| 146 */ | |
| 147 int cronet_bidirectional_stream_start( | |
| 148 cronet_bidirectional_stream* stream, | |
| 149 const char* url, | |
| 150 int priority, | |
| 151 const char* method, | |
| 152 const cronet_bidirectional_stream_header_array* headers, | |
| 153 bool end_of_stream); | |
| 154 | |
| 155 /* Read response data into |buffer| of |capacity| length. Must only be called at | |
| 156 * most once in response to each invocation of the | |
| 157 * on_response_headers_received() and on_read_completed() methods of the | |
| 158 * cronet_bidirectional_stream_callback. | |
| 159 * Each call will result in an invocation of the callback's | |
| 160 * on_read_completed() method if data is read, or its on_failed() method if | |
| 161 * there's an error. The callback's on_succeeded() method is also invoked if | |
| 162 * there is no more data to read and |end_of_stream| was previously sent. | |
| 163 */ | |
| 164 int cronet_bidirectional_stream_read(cronet_bidirectional_stream* stream, | |
| 165 char* buffer, | |
| 166 int capacity); | |
| 167 | |
| 168 /* Write request data from |buffer| of |count| length. Must only be called at | |
| 169 * most once in response to each invocation of the | |
| 170 * on_request_headers_sent() and on_write_completed() methods of the | |
| 171 * cronet_bidirectional_stream_callback. | |
| 172 * Each call will result in an invocation the callback's on_write_completed() | |
| 173 * method if data is sent, or its on_failed() method if there's an error. | |
| 174 * The callback's on_succeeded() method is also invoked if |end_of_stream| is | |
| 175 * set and all response data has been read. | |
| 176 */ | |
| 177 int cronet_bidirectional_stream_write(cronet_bidirectional_stream* stream, | |
| 178 const char* buffer, | |
| 179 int count, | |
|
xunjieli
2016/04/14 15:43:39
nit: Maybe use |buffer_length|, which is a little
mef
2016/04/14 21:17:36
Done.
| |
| 180 bool end_of_stream); | |
| 181 | |
| 182 /* Cancels the stream. Can be called at any time after | |
| 183 * cronet_bidirectional_stream_start(). The on_canceled() method of | |
| 184 * cronet_bidirectional_stream_callback will be invoked when cancelation | |
| 185 * is complete and no further callback methods will be invoked. If the | |
| 186 * stream has completed or has not started, calling | |
| 187 * cronet_bidirectional_stream_cancel() has no effect and on_canceled() will not | |
| 188 * be invoked. At most one callback method may be invoked after | |
| 189 * cronet_bidirectional_stream_cancel() has completed. | |
| 190 */ | |
| 191 int cronet_bidirectional_stream_cancel(cronet_bidirectional_stream* stream); | |
| 192 | |
| 193 /* Returns true if the |stream| was successfully started and is now done | |
| 194 * (succeeded, canceled, or failed). | |
| 195 * Returns false if the |stream| stream is not yet started or is in progress. | |
| 196 */ | |
| 197 bool cronet_bidirectional_stream_is_done(cronet_bidirectional_stream* stream); | |
| 198 | |
| 199 #ifdef __cplusplus | |
| 200 } | |
| 201 #endif | |
| 202 | |
| 203 #endif // COMPONENTS_CRONET_IOS_CRONET_C_FOR_GRPC_H_ | |
| OLD | NEW |