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