Lesson 35 - Get Compute Auth Token Working
This commit is contained in:
@@ -0,0 +1,680 @@
|
||||
#ifndef AWS_EVENT_STREAM_H_
|
||||
#define AWS_EVENT_STREAM_H_
|
||||
|
||||
/**
|
||||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0.
|
||||
*/
|
||||
|
||||
#include <aws/common/array_list.h>
|
||||
#include <aws/common/byte_buf.h>
|
||||
#include <aws/common/logging.h>
|
||||
#include <aws/event-stream/event_stream_exports.h>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
AWS_PUSH_SANE_WARNING_LEVEL
|
||||
|
||||
#define AWS_C_EVENT_STREAM_PACKAGE_ID 4
|
||||
/* max message size is 16MB */
|
||||
#define AWS_EVENT_STREAM_MAX_MESSAGE_SIZE (16 * 1024 * 1024)
|
||||
|
||||
/* max header size is 128kb */
|
||||
#define AWS_EVENT_STREAM_MAX_HEADERS_SIZE (128 * 1024)
|
||||
|
||||
/* Max header name length is 127 bytes */
|
||||
#define AWS_EVENT_STREAM_HEADER_NAME_LEN_MAX (INT8_MAX)
|
||||
|
||||
/* Max header static value length is 16 bytes */
|
||||
#define AWS_EVENT_STREAM_HEADER_STATIC_VALUE_LEN_MAX (16)
|
||||
|
||||
enum aws_event_stream_errors {
|
||||
AWS_ERROR_EVENT_STREAM_BUFFER_LENGTH_MISMATCH = AWS_ERROR_ENUM_BEGIN_RANGE(AWS_C_EVENT_STREAM_PACKAGE_ID),
|
||||
AWS_ERROR_EVENT_STREAM_INSUFFICIENT_BUFFER_LEN,
|
||||
AWS_ERROR_EVENT_STREAM_MESSAGE_FIELD_SIZE_EXCEEDED,
|
||||
AWS_ERROR_EVENT_STREAM_PRELUDE_CHECKSUM_FAILURE,
|
||||
AWS_ERROR_EVENT_STREAM_MESSAGE_CHECKSUM_FAILURE,
|
||||
AWS_ERROR_EVENT_STREAM_MESSAGE_INVALID_HEADERS_LEN,
|
||||
AWS_ERROR_EVENT_STREAM_MESSAGE_UNKNOWN_HEADER_TYPE,
|
||||
AWS_ERROR_EVENT_STREAM_MESSAGE_PARSER_ILLEGAL_STATE,
|
||||
AWS_ERROR_EVENT_STREAM_RPC_CONNECTION_CLOSED,
|
||||
AWS_ERROR_EVENT_STREAM_RPC_PROTOCOL_ERROR,
|
||||
AWS_ERROR_EVENT_STREAM_RPC_STREAM_CLOSED,
|
||||
AWS_ERROR_EVENT_STREAM_RPC_STREAM_NOT_ACTIVATED,
|
||||
|
||||
AWS_ERROR_EVENT_STREAM_END_RANGE = AWS_ERROR_ENUM_END_RANGE(AWS_C_EVENT_STREAM_PACKAGE_ID),
|
||||
};
|
||||
|
||||
enum aws_event_stream_log_subject {
|
||||
AWS_LS_EVENT_STREAM_GENERAL = AWS_LOG_SUBJECT_BEGIN_RANGE(AWS_C_EVENT_STREAM_PACKAGE_ID),
|
||||
AWS_LS_EVENT_STREAM_CHANNEL_HANDLER,
|
||||
AWS_LS_EVENT_STREAM_RPC_SERVER,
|
||||
AWS_LS_EVENT_STREAM_RPC_CLIENT,
|
||||
|
||||
AWS_LS_EVENT_STREAM_LAST = AWS_LOG_SUBJECT_END_RANGE(AWS_C_EVENT_STREAM_PACKAGE_ID),
|
||||
};
|
||||
|
||||
struct aws_event_stream_message_prelude {
|
||||
uint32_t total_len;
|
||||
uint32_t headers_len;
|
||||
uint32_t prelude_crc;
|
||||
};
|
||||
|
||||
struct aws_event_stream_message {
|
||||
struct aws_allocator *alloc;
|
||||
struct aws_byte_buf message_buffer;
|
||||
};
|
||||
|
||||
/* Prelude (12-bytes) = Total Byte Length (4-bytes) + Headers Byte Length (4-bytes) + Prelude CRC (4-bytes) */
|
||||
#define AWS_EVENT_STREAM_PRELUDE_LENGTH (uint32_t)(12)
|
||||
|
||||
/* Trailer (4-bytes) = Message CRC (4-bytes) */
|
||||
#define AWS_EVENT_STREAM_TRAILER_LENGTH (uint32_t)(4)
|
||||
|
||||
enum aws_event_stream_header_value_type {
|
||||
AWS_EVENT_STREAM_HEADER_BOOL_TRUE = 0,
|
||||
AWS_EVENT_STREAM_HEADER_BOOL_FALSE,
|
||||
AWS_EVENT_STREAM_HEADER_BYTE,
|
||||
AWS_EVENT_STREAM_HEADER_INT16,
|
||||
AWS_EVENT_STREAM_HEADER_INT32,
|
||||
AWS_EVENT_STREAM_HEADER_INT64,
|
||||
AWS_EVENT_STREAM_HEADER_BYTE_BUF,
|
||||
AWS_EVENT_STREAM_HEADER_STRING,
|
||||
/* 64 bit integer (millis since epoch) */
|
||||
AWS_EVENT_STREAM_HEADER_TIMESTAMP,
|
||||
AWS_EVENT_STREAM_HEADER_UUID
|
||||
};
|
||||
|
||||
struct aws_event_stream_header_value_pair {
|
||||
uint8_t header_name_len;
|
||||
char header_name[INT8_MAX];
|
||||
enum aws_event_stream_header_value_type header_value_type;
|
||||
union {
|
||||
uint8_t *variable_len_val;
|
||||
uint8_t static_val[AWS_EVENT_STREAM_HEADER_STATIC_VALUE_LEN_MAX];
|
||||
} header_value;
|
||||
|
||||
uint16_t header_value_len;
|
||||
int8_t value_owned;
|
||||
};
|
||||
|
||||
struct aws_event_stream_streaming_decoder;
|
||||
typedef int(aws_event_stream_process_state_fn)(
|
||||
struct aws_event_stream_streaming_decoder *decoder,
|
||||
const uint8_t *data,
|
||||
size_t len,
|
||||
size_t *processed);
|
||||
|
||||
/**
|
||||
* Called by aws_aws_event_stream_streaming_decoder when payload data has been received.
|
||||
* 'data' doesn't belong to you, so copy the data if it is needed beyond the scope of your callback.
|
||||
* final_segment == 1 indicates the current data is the last payload buffer for that message.
|
||||
*/
|
||||
typedef void(aws_event_stream_process_on_payload_segment_fn)(
|
||||
struct aws_event_stream_streaming_decoder *decoder,
|
||||
struct aws_byte_buf *payload,
|
||||
int8_t final_segment,
|
||||
void *user_data);
|
||||
|
||||
/**
|
||||
* Called by aws_aws_event_stream_streaming_decoder when a new message has arrived. The prelude will contain metadata
|
||||
* about the message. At this point no headers or payload have been received. prelude is copyable.
|
||||
*/
|
||||
typedef void(aws_event_stream_prelude_received_fn)(
|
||||
struct aws_event_stream_streaming_decoder *decoder,
|
||||
struct aws_event_stream_message_prelude *prelude,
|
||||
void *user_data);
|
||||
|
||||
/**
|
||||
* Called by aws_aws_event_stream_streaming_decoder when a header is encountered. 'header' is not yours. Copy the data
|
||||
* you want from it if your scope extends beyond your callback.
|
||||
*/
|
||||
typedef void(aws_event_stream_header_received_fn)(
|
||||
struct aws_event_stream_streaming_decoder *decoder,
|
||||
struct aws_event_stream_message_prelude *prelude,
|
||||
struct aws_event_stream_header_value_pair *header,
|
||||
void *user_data);
|
||||
|
||||
/**
|
||||
* Called by aws_aws_event_stream_streaming_decoder when a message decoding is complete
|
||||
* and crc is verified.
|
||||
*/
|
||||
typedef void(aws_event_stream_on_complete_fn)(
|
||||
struct aws_event_stream_streaming_decoder *decoder,
|
||||
uint32_t message_crc,
|
||||
void *user_data);
|
||||
|
||||
/**
|
||||
* Called by aws_aws_event_stream_streaming_decoder when an error is encountered. The decoder is not in a good state for
|
||||
* usage after this callback.
|
||||
*/
|
||||
typedef void(aws_event_stream_on_error_fn)(
|
||||
struct aws_event_stream_streaming_decoder *decoder,
|
||||
struct aws_event_stream_message_prelude *prelude,
|
||||
int error_code,
|
||||
const char *message,
|
||||
void *user_data);
|
||||
|
||||
struct aws_event_stream_streaming_decoder {
|
||||
struct aws_allocator *alloc;
|
||||
uint8_t working_buffer[AWS_EVENT_STREAM_PRELUDE_LENGTH];
|
||||
size_t message_pos;
|
||||
uint32_t running_crc;
|
||||
size_t current_header_name_offset;
|
||||
size_t current_header_value_offset;
|
||||
struct aws_event_stream_header_value_pair current_header;
|
||||
struct aws_event_stream_message_prelude prelude;
|
||||
aws_event_stream_process_state_fn *state;
|
||||
aws_event_stream_process_on_payload_segment_fn *on_payload;
|
||||
aws_event_stream_prelude_received_fn *on_prelude;
|
||||
aws_event_stream_header_received_fn *on_header;
|
||||
aws_event_stream_on_complete_fn *on_complete;
|
||||
aws_event_stream_on_error_fn *on_error;
|
||||
void *user_context;
|
||||
};
|
||||
|
||||
struct aws_event_stream_streaming_decoder_options {
|
||||
/**
|
||||
* (Required)
|
||||
* Invoked repeatedly as payload segment are received.
|
||||
* See `aws_event_stream_process_on_payload_segment_fn`.
|
||||
*/
|
||||
aws_event_stream_process_on_payload_segment_fn *on_payload_segment;
|
||||
/**
|
||||
* (Required)
|
||||
* Invoked when when a new message has arrived. The prelude will contain metadata about the message.
|
||||
* See `aws_event_stream_prelude_received_fn`.
|
||||
*/
|
||||
aws_event_stream_prelude_received_fn *on_prelude;
|
||||
/**
|
||||
* (Required)
|
||||
* Invoked repeatedly as headers are received.
|
||||
* See `aws_event_stream_header_received_fn`.
|
||||
*/
|
||||
aws_event_stream_header_received_fn *on_header;
|
||||
/**
|
||||
* (Optional)
|
||||
* Invoked if a message is decoded successfully.
|
||||
* See `aws_event_stream_on_complete_fn`.
|
||||
*/
|
||||
aws_event_stream_on_complete_fn *on_complete;
|
||||
/**
|
||||
* (Required)
|
||||
* Invoked when an error is encountered. The decoder is not in a good state for usage after this callback.
|
||||
* See `aws_event_stream_on_error_fn`.
|
||||
*/
|
||||
aws_event_stream_on_error_fn *on_error;
|
||||
/**
|
||||
* (Optional)
|
||||
* user_data passed to callbacks.
|
||||
*/
|
||||
void *user_data;
|
||||
};
|
||||
AWS_EXTERN_C_BEGIN
|
||||
|
||||
/**
|
||||
* Initializes with a list of headers, the payload, and a payload length. CRCs will be computed for you.
|
||||
* If headers or payload is NULL, then the fields will be appropriately set to indicate no headers and/or no payload.
|
||||
* Both payload and headers will result in an allocation.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API int aws_event_stream_message_init(
|
||||
struct aws_event_stream_message *message,
|
||||
struct aws_allocator *alloc,
|
||||
const struct aws_array_list *headers,
|
||||
const struct aws_byte_buf *payload);
|
||||
|
||||
/**
|
||||
* Zero allocation, Zero copy. The message will simply wrap the buffer. The message functions are only useful as long as
|
||||
* buffer is referencable memory.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API int aws_event_stream_message_from_buffer(
|
||||
struct aws_event_stream_message *message,
|
||||
struct aws_allocator *alloc,
|
||||
struct aws_byte_buf *buffer);
|
||||
|
||||
/**
|
||||
* Allocates memory and copies buffer. Otherwise the same as aws_aws_event_stream_message_from_buffer. This is slower,
|
||||
* but possibly safer.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API int aws_event_stream_message_from_buffer_copy(
|
||||
struct aws_event_stream_message *message,
|
||||
struct aws_allocator *alloc,
|
||||
const struct aws_byte_buf *buffer);
|
||||
|
||||
/**
|
||||
* Cleans up any internally allocated memory. Always call this for API compatibility reasons, even if you only used the
|
||||
* aws_aws_event_stream_message_from_buffer function.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API void aws_event_stream_message_clean_up(struct aws_event_stream_message *message);
|
||||
|
||||
/**
|
||||
* Returns the total length of the message (including the length field).
|
||||
*/
|
||||
AWS_EVENT_STREAM_API uint32_t aws_event_stream_message_total_length(const struct aws_event_stream_message *message);
|
||||
|
||||
/**
|
||||
* Returns the length of the headers portion of the message.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API uint32_t aws_event_stream_message_headers_len(const struct aws_event_stream_message *message);
|
||||
|
||||
/**
|
||||
* Returns the prelude crc (crc32)
|
||||
*/
|
||||
AWS_EVENT_STREAM_API uint32_t aws_event_stream_message_prelude_crc(const struct aws_event_stream_message *message);
|
||||
|
||||
/**
|
||||
* Writes the message to fd in json format. All strings and binary fields are base64 encoded.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API int aws_event_stream_message_to_debug_str(
|
||||
FILE *fd,
|
||||
const struct aws_event_stream_message *message);
|
||||
|
||||
/**
|
||||
* Adds the headers for the message to list. The memory in each header is owned as part of the message, do not free it
|
||||
* or pass its address around.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API int aws_event_stream_message_headers(
|
||||
const struct aws_event_stream_message *message,
|
||||
struct aws_array_list *headers);
|
||||
|
||||
/**
|
||||
* Returns a pointer to the beginning of the message payload.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API const uint8_t *aws_event_stream_message_payload(const struct aws_event_stream_message *message);
|
||||
|
||||
/**
|
||||
* Returns the length of the message payload.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API uint32_t aws_event_stream_message_payload_len(const struct aws_event_stream_message *message);
|
||||
|
||||
/**
|
||||
* Returns the checksum of the entire message (crc32)
|
||||
*/
|
||||
AWS_EVENT_STREAM_API uint32_t aws_event_stream_message_message_crc(const struct aws_event_stream_message *message);
|
||||
|
||||
/**
|
||||
* Returns the message as a buffer ready for transport.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API const uint8_t *aws_event_stream_message_buffer(const struct aws_event_stream_message *message);
|
||||
|
||||
AWS_EVENT_STREAM_API uint32_t
|
||||
aws_event_stream_compute_headers_required_buffer_len(const struct aws_array_list *headers);
|
||||
|
||||
/**
|
||||
* Writes headers to buf assuming buf is large enough to hold the data. Prefer this function over the unsafe variant
|
||||
* 'aws_event_stream_write_headers_to_buffer'.
|
||||
*
|
||||
* Returns AWS_OP_SUCCESS if the headers were successfully and completely written and AWS_OP_ERR otherwise.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API int aws_event_stream_write_headers_to_buffer_safe(
|
||||
const struct aws_array_list *headers,
|
||||
struct aws_byte_buf *buf);
|
||||
|
||||
/**
|
||||
* Deprecated in favor of 'aws_event_stream_write_headers_to_buffer_safe' as this API is unsafe.
|
||||
*
|
||||
* Writes headers to buffer and returns the length of bytes written to buffer. Assumes buffer is large enough to
|
||||
* store the headers.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API size_t
|
||||
aws_event_stream_write_headers_to_buffer(const struct aws_array_list *headers, uint8_t *buffer);
|
||||
|
||||
/** Get the headers from the buffer, store them in the headers list.
|
||||
* the user's responsibility to cleanup the list when they are finished with it.
|
||||
* no buffer copies happen here, the lifetime of the buffer, must outlive the usage of the headers.
|
||||
* returns error codes defined in the public interface.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API int aws_event_stream_read_headers_from_buffer(
|
||||
struct aws_array_list *headers,
|
||||
const uint8_t *buffer,
|
||||
size_t headers_len);
|
||||
|
||||
/**
|
||||
* Initialize a streaming decoder for messages with callbacks for usage
|
||||
* and an optional user context pointer.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API
|
||||
void aws_event_stream_streaming_decoder_init_from_options(
|
||||
struct aws_event_stream_streaming_decoder *decoder,
|
||||
struct aws_allocator *allocator,
|
||||
const struct aws_event_stream_streaming_decoder_options *options);
|
||||
|
||||
/**
|
||||
* Deprecated. Use aws_event_stream_streaming_decoder_init_from_options instead.
|
||||
* Initialize a streaming decoder for messages with callbacks for usage and an optional user context pointer.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API void aws_event_stream_streaming_decoder_init(
|
||||
struct aws_event_stream_streaming_decoder *decoder,
|
||||
struct aws_allocator *alloc,
|
||||
aws_event_stream_process_on_payload_segment_fn *on_payload_segment,
|
||||
aws_event_stream_prelude_received_fn *on_prelude,
|
||||
aws_event_stream_header_received_fn *on_header,
|
||||
aws_event_stream_on_error_fn *on_error,
|
||||
void *user_data);
|
||||
|
||||
/**
|
||||
* Currently, no memory is allocated inside aws_aws_event_stream_streaming_decoder, but for future API compatibility,
|
||||
* you should call this when finished with it.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API void aws_event_stream_streaming_decoder_clean_up(
|
||||
struct aws_event_stream_streaming_decoder *decoder);
|
||||
|
||||
/**
|
||||
* initializes a headers list for you. It will default to a capacity of 4 in dynamic mode.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API int aws_event_stream_headers_list_init(
|
||||
struct aws_array_list *headers,
|
||||
struct aws_allocator *allocator);
|
||||
|
||||
/**
|
||||
* Cleans up the headers list. Also deallocates any headers that were the result of a copy flag for strings or buffer.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API void aws_event_stream_headers_list_cleanup(struct aws_array_list *headers);
|
||||
|
||||
/**
|
||||
* Adds a string header to the list of headers. If copy is set to true, this will result in an allocation for the header
|
||||
* value. Otherwise, the value will be set to the memory address of 'value'.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API int aws_event_stream_add_string_header(
|
||||
struct aws_array_list *headers,
|
||||
const char *name,
|
||||
uint8_t name_len,
|
||||
const char *value,
|
||||
uint16_t value_len,
|
||||
int8_t copy);
|
||||
|
||||
AWS_EVENT_STREAM_API struct aws_event_stream_header_value_pair aws_event_stream_create_string_header(
|
||||
struct aws_byte_cursor name,
|
||||
struct aws_byte_cursor value);
|
||||
|
||||
AWS_EVENT_STREAM_API struct aws_event_stream_header_value_pair aws_event_stream_create_int32_header(
|
||||
struct aws_byte_cursor name,
|
||||
int32_t value);
|
||||
|
||||
/**
|
||||
* Adds a byte header to the list of headers.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API int aws_event_stream_add_byte_header(
|
||||
struct aws_array_list *headers,
|
||||
const char *name,
|
||||
uint8_t name_len,
|
||||
int8_t value);
|
||||
|
||||
/**
|
||||
* Adds a bool header to the list of headers.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API int aws_event_stream_add_bool_header(
|
||||
struct aws_array_list *headers,
|
||||
const char *name,
|
||||
uint8_t name_len,
|
||||
int8_t value);
|
||||
|
||||
/**
|
||||
* adds a 16 bit integer to the list of headers.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API int aws_event_stream_add_int16_header(
|
||||
struct aws_array_list *headers,
|
||||
const char *name,
|
||||
uint8_t name_len,
|
||||
int16_t value);
|
||||
|
||||
/**
|
||||
* adds a 32 bit integer to the list of headers.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API int aws_event_stream_add_int32_header(
|
||||
struct aws_array_list *headers,
|
||||
const char *name,
|
||||
uint8_t name_len,
|
||||
int32_t value);
|
||||
|
||||
/**
|
||||
* adds a 64 bit integer to the list of headers.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API int aws_event_stream_add_int64_header(
|
||||
struct aws_array_list *headers,
|
||||
const char *name,
|
||||
uint8_t name_len,
|
||||
int64_t value);
|
||||
|
||||
/**
|
||||
* Adds a byte-buffer header to the list of headers. If copy is set to true, this will result in an allocation for the
|
||||
* header value. Otherwise, the value will be set to the memory address of 'value'.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API int aws_event_stream_add_bytebuf_header(
|
||||
struct aws_array_list *headers,
|
||||
const char *name,
|
||||
uint8_t name_len,
|
||||
uint8_t *value,
|
||||
uint16_t value_len,
|
||||
int8_t copy);
|
||||
|
||||
/**
|
||||
* adds a 64 bit integer representing milliseconds since unix epoch to the list of headers.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API int aws_event_stream_add_timestamp_header(
|
||||
struct aws_array_list *headers,
|
||||
const char *name,
|
||||
uint8_t name_len,
|
||||
int64_t value);
|
||||
|
||||
/**
|
||||
* adds a uuid buffer to the list of headers. Value must always be 16 bytes long.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API int aws_event_stream_add_uuid_header(
|
||||
struct aws_array_list *headers,
|
||||
const char *name,
|
||||
uint8_t name_len,
|
||||
const uint8_t *value);
|
||||
|
||||
/**
|
||||
* Adds a generic header to the list of headers.
|
||||
* Makes a copy of the underlaying data.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API int aws_event_stream_add_header(
|
||||
struct aws_array_list *headers,
|
||||
const struct aws_event_stream_header_value_pair *header);
|
||||
|
||||
/* Cursor-based header APIs */
|
||||
|
||||
/**
|
||||
* Adds a boolean-valued header to a header list
|
||||
*
|
||||
* @param headers header list to add to
|
||||
* @param name name of the header to add
|
||||
* @param value value of the header to add
|
||||
* @return AWS_OP_SUCCESS on success, AWS_OP_ERR on failure
|
||||
*/
|
||||
AWS_EVENT_STREAM_API int aws_event_stream_add_bool_header_by_cursor(
|
||||
struct aws_array_list *headers,
|
||||
struct aws_byte_cursor name,
|
||||
bool value);
|
||||
|
||||
/**
|
||||
* Adds a byte-valued header to a header list
|
||||
*
|
||||
* @param headers header list to add to
|
||||
* @param name name of the header to add
|
||||
* @param value value of the header to add
|
||||
* @return AWS_OP_SUCCESS on success, AWS_OP_ERR on failure
|
||||
*/
|
||||
AWS_EVENT_STREAM_API int aws_event_stream_add_byte_header_by_cursor(
|
||||
struct aws_array_list *headers,
|
||||
struct aws_byte_cursor name,
|
||||
int8_t value);
|
||||
|
||||
/**
|
||||
* Adds a int16-valued header to a header list
|
||||
*
|
||||
* @param headers header list to add to
|
||||
* @param name name of the header to add
|
||||
* @param value value of the header to add
|
||||
* @return AWS_OP_SUCCESS on success, AWS_OP_ERR on failure
|
||||
*/
|
||||
AWS_EVENT_STREAM_API int aws_event_stream_add_int16_header_by_cursor(
|
||||
struct aws_array_list *headers,
|
||||
struct aws_byte_cursor name,
|
||||
int16_t value);
|
||||
|
||||
/**
|
||||
* Adds a int32-valued header to a header list
|
||||
*
|
||||
* @param headers header list to add to
|
||||
* @param name name of the header to add
|
||||
* @param value value of the header to add
|
||||
* @return AWS_OP_SUCCESS on success, AWS_OP_ERR on failure
|
||||
*/
|
||||
AWS_EVENT_STREAM_API int aws_event_stream_add_int32_header_by_cursor(
|
||||
struct aws_array_list *headers,
|
||||
struct aws_byte_cursor name,
|
||||
int32_t value);
|
||||
|
||||
/**
|
||||
* Adds a int64-valued header to a header list
|
||||
*
|
||||
* @param headers header list to add to
|
||||
* @param name name of the header to add
|
||||
* @param value value of the header to add
|
||||
* @return AWS_OP_SUCCESS on success, AWS_OP_ERR on failure
|
||||
*/
|
||||
AWS_EVENT_STREAM_API int aws_event_stream_add_int64_header_by_cursor(
|
||||
struct aws_array_list *headers,
|
||||
struct aws_byte_cursor name,
|
||||
int64_t value);
|
||||
|
||||
/**
|
||||
* Adds a string-valued header to a header list
|
||||
*
|
||||
* @param headers header list to add to
|
||||
* @param name name of the header to add
|
||||
* @param value value of the header to add
|
||||
* @return AWS_OP_SUCCESS on success, AWS_OP_ERR on failure
|
||||
*/
|
||||
AWS_EVENT_STREAM_API int aws_event_stream_add_string_header_by_cursor(
|
||||
struct aws_array_list *headers,
|
||||
struct aws_byte_cursor name,
|
||||
struct aws_byte_cursor value);
|
||||
|
||||
/**
|
||||
* Adds a byte_buf-valued header to a header list
|
||||
*
|
||||
* @param headers header list to add to
|
||||
* @param name name of the header to add
|
||||
* @param value value of the header to add
|
||||
* @return AWS_OP_SUCCESS on success, AWS_OP_ERR on failure
|
||||
*/
|
||||
AWS_EVENT_STREAM_API int aws_event_stream_add_byte_buf_header_by_cursor(
|
||||
struct aws_array_list *headers,
|
||||
struct aws_byte_cursor name,
|
||||
struct aws_byte_cursor value);
|
||||
|
||||
/**
|
||||
* Adds a timestamp-valued header to a header list
|
||||
*
|
||||
* @param headers header list to add to
|
||||
* @param name name of the header to add
|
||||
* @param value value of the header to add
|
||||
* @return AWS_OP_SUCCESS on success, AWS_OP_ERR on failure
|
||||
*/
|
||||
AWS_EVENT_STREAM_API int aws_event_stream_add_timestamp_header_by_cursor(
|
||||
struct aws_array_list *headers,
|
||||
struct aws_byte_cursor name,
|
||||
int64_t value);
|
||||
|
||||
/**
|
||||
* Adds a uuid-valued header to a header list
|
||||
*
|
||||
* @param headers header list to add to
|
||||
* @param name name of the header to add
|
||||
* @param value value of the header to add
|
||||
* @return AWS_OP_SUCCESS on success, AWS_OP_ERR on failure
|
||||
*/
|
||||
AWS_EVENT_STREAM_API int aws_event_stream_add_uuid_header_by_cursor(
|
||||
struct aws_array_list *headers,
|
||||
struct aws_byte_cursor name,
|
||||
struct aws_byte_cursor value);
|
||||
|
||||
/**
|
||||
* Returns the header name. Note: this value is not null terminated
|
||||
*/
|
||||
AWS_EVENT_STREAM_API struct aws_byte_buf aws_event_stream_header_name(
|
||||
struct aws_event_stream_header_value_pair *header);
|
||||
|
||||
/**
|
||||
* Returns the header value as a string. Note: this value is not null terminated.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API struct aws_byte_buf aws_event_stream_header_value_as_string(
|
||||
struct aws_event_stream_header_value_pair *header);
|
||||
|
||||
/**
|
||||
* Returns the header value as a byte
|
||||
*/
|
||||
AWS_EVENT_STREAM_API int8_t aws_event_stream_header_value_as_byte(struct aws_event_stream_header_value_pair *header);
|
||||
|
||||
/**
|
||||
* Returns the header value as a boolean value.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API int8_t aws_event_stream_header_value_as_bool(struct aws_event_stream_header_value_pair *header);
|
||||
|
||||
/**
|
||||
* Returns the header value as a 16 bit integer.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API int16_t aws_event_stream_header_value_as_int16(struct aws_event_stream_header_value_pair *header);
|
||||
|
||||
/**
|
||||
* Returns the header value as a 32 bit integer.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API int32_t aws_event_stream_header_value_as_int32(struct aws_event_stream_header_value_pair *header);
|
||||
|
||||
/**
|
||||
* Returns the header value as a 64 bit integer.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API int64_t aws_event_stream_header_value_as_int64(struct aws_event_stream_header_value_pair *header);
|
||||
|
||||
/**
|
||||
* Returns the header value as a pointer to a byte buffer, call aws_event_stream_header_value_length to determine
|
||||
* the length of the buffer.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API struct aws_byte_buf aws_event_stream_header_value_as_bytebuf(
|
||||
struct aws_event_stream_header_value_pair *header);
|
||||
|
||||
/**
|
||||
* Returns the header value as a 64 bit integer representing milliseconds since unix epoch.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API int64_t
|
||||
aws_event_stream_header_value_as_timestamp(struct aws_event_stream_header_value_pair *header);
|
||||
|
||||
/**
|
||||
* Returns the header value a byte buffer which is 16 bytes long. Represents a UUID.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API struct aws_byte_buf aws_event_stream_header_value_as_uuid(
|
||||
struct aws_event_stream_header_value_pair *header);
|
||||
|
||||
/**
|
||||
* Returns the length of the header value buffer. This is mostly intended for string and byte buffer types.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API uint16_t aws_event_stream_header_value_length(struct aws_event_stream_header_value_pair *header);
|
||||
|
||||
/**
|
||||
* The main driver of the decoder. Pass data that should be decoded with its length. A likely use-case here is in
|
||||
* response to a read event from an io-device
|
||||
*/
|
||||
AWS_EVENT_STREAM_API int aws_event_stream_streaming_decoder_pump(
|
||||
struct aws_event_stream_streaming_decoder *decoder,
|
||||
const struct aws_byte_buf *data);
|
||||
|
||||
/**
|
||||
* Initializes internal datastructures used by aws-c-event-stream.
|
||||
* Must be called before using any functionality in aws-c-event-stream.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API void aws_event_stream_library_init(struct aws_allocator *allocator);
|
||||
|
||||
/**
|
||||
* Clean up internal datastructures used by aws-c-event-stream.
|
||||
* Must not be called until application is done using functionality in aws-c-event-stream.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API void aws_event_stream_library_clean_up(void);
|
||||
|
||||
AWS_EXTERN_C_END
|
||||
AWS_POP_SANE_WARNING_LEVEL
|
||||
|
||||
#endif /* AWS_EVENT_STREAM_H_ */
|
||||
@@ -0,0 +1,85 @@
|
||||
#ifndef AWS_EVENT_STREAM_CHANNEL_HANDLER_H
|
||||
#define AWS_EVENT_STREAM_CHANNEL_HANDLER_H
|
||||
/**
|
||||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0.
|
||||
*/
|
||||
|
||||
#include <aws/event-stream/event_stream.h>
|
||||
|
||||
AWS_PUSH_SANE_WARNING_LEVEL
|
||||
|
||||
struct aws_event_stream_channel_handler;
|
||||
struct aws_channel_handler;
|
||||
|
||||
/**
|
||||
* Invoked when an aws_event_stream_message is encountered. If the message
|
||||
* parsed successfully, message will be non-null and error_code will be AWS_ERROR_SUCCESS.
|
||||
* Otherwise message will be null and error_code will represent the error that was encountered.
|
||||
* Note that any case that error_code was not AWS_OP_SUCCESS, the channel also shuts down.
|
||||
*/
|
||||
typedef void(aws_event_stream_channel_handler_on_message_received_fn)(
|
||||
struct aws_event_stream_message *message,
|
||||
int error_code,
|
||||
void *user_data);
|
||||
|
||||
/**
|
||||
* Invoked when an aws_event_stream_message is flushed to the IO interface. When error_code is AWS_ERROR_SUCCESS the
|
||||
* write happened successfuly. Regardless, message is held from the aws_event_stream_channel_handler_write_message()
|
||||
* call and should likely be freed in this callback. If error_code is non-zero, the channel will be shutdown immediately
|
||||
* after this callback returns.
|
||||
*/
|
||||
typedef void(aws_event_stream_channel_handler_on_message_written_fn)(
|
||||
struct aws_event_stream_message *message,
|
||||
int error_code,
|
||||
void *user_data);
|
||||
|
||||
struct aws_event_stream_channel_handler_options {
|
||||
/** Callback for when messages are received. Can not be null. */
|
||||
aws_event_stream_channel_handler_on_message_received_fn *on_message_received;
|
||||
/** user data passed to message callback. Optional */
|
||||
void *user_data;
|
||||
/** initial window size to use for the channel. If automatic window management is set to true, this value is
|
||||
* ignored. */
|
||||
size_t initial_window_size;
|
||||
/**
|
||||
* if set to false (the default), windowing will be managed automatically for the user.
|
||||
* Otherwise, after any on_message_received, the user must invoke
|
||||
* aws_event_stream_channel_handler_increment_read_window()
|
||||
*/
|
||||
bool manual_window_management;
|
||||
};
|
||||
|
||||
AWS_EXTERN_C_BEGIN
|
||||
/**
|
||||
* Allocates and initializes a new channel handler for processing aws_event_stream_message() events. Handler options
|
||||
* must not be null.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API struct aws_channel_handler *aws_event_stream_channel_handler_new(
|
||||
struct aws_allocator *allocator,
|
||||
const struct aws_event_stream_channel_handler_options *handler_options);
|
||||
|
||||
/**
|
||||
* Writes an aws_event_stream_message() to the channel. Once the channel flushes or an error occurs, on_message_written
|
||||
* will be invoked. message should stay valid until the callback is invoked. If an error an occurs, the channel will
|
||||
* automatically be shutdown.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API int aws_event_stream_channel_handler_write_message(
|
||||
struct aws_channel_handler *handler,
|
||||
struct aws_event_stream_message *message,
|
||||
aws_event_stream_channel_handler_on_message_written_fn *on_message_written,
|
||||
void *user_data);
|
||||
|
||||
/**
|
||||
* Updates the read window for the channel if automatic_window_managemanet was set to false.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API void aws_event_stream_channel_handler_increment_read_window(
|
||||
struct aws_channel_handler *handler,
|
||||
size_t window_update_size);
|
||||
|
||||
AWS_EVENT_STREAM_API void *aws_event_stream_channel_handler_get_user_data(struct aws_channel_handler *handler);
|
||||
|
||||
AWS_EXTERN_C_END
|
||||
AWS_POP_SANE_WARNING_LEVEL
|
||||
|
||||
#endif /* AWS_EVENT_STREAM_CHANNEL_HANDLER_H */
|
||||
@@ -0,0 +1,29 @@
|
||||
#ifndef AWS_EVENT_STREAM_EXPORTS_H_
|
||||
#define AWS_EVENT_STREAM_EXPORTS_H_
|
||||
/**
|
||||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0.
|
||||
*/
|
||||
#if defined(USE_WINDOWS_DLL_SEMANTICS) || defined(WIN32)
|
||||
# ifdef AWS_EVENT_STREAM_USE_IMPORT_EXPORT
|
||||
# ifdef AWS_EVENT_STREAM_EXPORTS
|
||||
# define AWS_EVENT_STREAM_API __declspec(dllexport)
|
||||
# else
|
||||
# define AWS_EVENT_STREAM_API __declspec(dllimport)
|
||||
# endif /* AWS_EVENT_STREAM_EXPORTS */
|
||||
# else
|
||||
# define AWS_EVENT_STREAM_API
|
||||
# endif /* AWS_EVENT_STREAM_USE_IMPORT_EXPORT */
|
||||
|
||||
#else /* defined (USE_WINDOWS_DLL_SEMANTICS) || defined (WIN32) */
|
||||
|
||||
# if ((__GNUC__ >= 4) || defined(__clang__)) && defined(AWS_EVENT_STREAM_USE_IMPORT_EXPORT) && \
|
||||
defined(AWS_EVENT_STREAM_EXPORTS)
|
||||
# define AWS_EVENT_STREAM_API __attribute__((visibility("default")))
|
||||
# else
|
||||
# define AWS_EVENT_STREAM_API
|
||||
# endif /* __GNUC__ >= 4 || defined(__clang__) */
|
||||
|
||||
#endif /* defined (USE_WINDOWS_DLL_SEMANTICS) || defined (WIN32) */
|
||||
|
||||
#endif /* AWS_EVENT_STREAM_EXPORTS_H */
|
||||
@@ -0,0 +1,65 @@
|
||||
#ifndef AWS_EVENT_STREAM_RPC_H
|
||||
#define AWS_EVENT_STREAM_RPC_H
|
||||
/**
|
||||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0.
|
||||
*/
|
||||
#include <aws/event-stream/event_stream.h>
|
||||
|
||||
AWS_PUSH_SANE_WARNING_LEVEL
|
||||
|
||||
/**
|
||||
* :message-type header name
|
||||
*/
|
||||
extern AWS_EVENT_STREAM_API const struct aws_byte_cursor aws_event_stream_rpc_message_type_name;
|
||||
/**
|
||||
* :message-flags header name
|
||||
*/
|
||||
extern AWS_EVENT_STREAM_API const struct aws_byte_cursor aws_event_stream_rpc_message_flags_name;
|
||||
/**
|
||||
* :stream-id header name
|
||||
*/
|
||||
extern AWS_EVENT_STREAM_API const struct aws_byte_cursor aws_event_stream_rpc_stream_id_name;
|
||||
/**
|
||||
* operation header name.
|
||||
*/
|
||||
extern AWS_EVENT_STREAM_API const struct aws_byte_cursor aws_event_stream_rpc_operation_name;
|
||||
|
||||
enum aws_event_stream_rpc_message_type {
|
||||
AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_APPLICATION_MESSAGE,
|
||||
AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_APPLICATION_ERROR,
|
||||
AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_PING,
|
||||
AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_PING_RESPONSE,
|
||||
AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_CONNECT,
|
||||
AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_CONNECT_ACK,
|
||||
AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_PROTOCOL_ERROR,
|
||||
AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_INTERNAL_ERROR,
|
||||
|
||||
AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_COUNT,
|
||||
};
|
||||
|
||||
enum aws_event_stream_rpc_message_flag {
|
||||
AWS_EVENT_STREAM_RPC_MESSAGE_FLAG_CONNECTION_ACCEPTED = 1,
|
||||
AWS_EVENT_STREAM_RPC_MESSAGE_FLAG_TERMINATE_STREAM = 2,
|
||||
};
|
||||
|
||||
struct aws_event_stream_rpc_message_args {
|
||||
/** array of headers for an event-stream message. */
|
||||
struct aws_event_stream_header_value_pair *headers;
|
||||
/** number of headers in the headers array.
|
||||
* headers are copied in aws_event_stream_rpc_*_send_message()
|
||||
* so you can free the memory immediately after calling it if you need to.*/
|
||||
size_t headers_count;
|
||||
/** payload buffer for the message, payload is copied in aws_event_stream_rpc_*_send_message()
|
||||
* so you can free the memory immediately after calling it if you need to. */
|
||||
struct aws_byte_buf *payload;
|
||||
/** message type for the message. This will be added to the headers array
|
||||
* and the ":message-type" header should not be included in headers */
|
||||
enum aws_event_stream_rpc_message_type message_type;
|
||||
/** message flags for the message. This will be added to the headers array
|
||||
* and the ":message-flags" header should not be included in headers */
|
||||
uint32_t message_flags;
|
||||
};
|
||||
AWS_POP_SANE_WARNING_LEVEL
|
||||
|
||||
#endif /* AWS_EVENT_STREAM_RPC_SERVER_H */
|
||||
@@ -0,0 +1,201 @@
|
||||
#ifndef AWS_EVENT_STREAM_RPC_CLIENT_H
|
||||
#define AWS_EVENT_STREAM_RPC_CLIENT_H
|
||||
/**
|
||||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0.
|
||||
*/
|
||||
|
||||
#include <aws/event-stream/event_stream_rpc.h>
|
||||
|
||||
AWS_PUSH_SANE_WARNING_LEVEL
|
||||
|
||||
struct aws_channel;
|
||||
struct aws_event_stream_rpc_client_connection;
|
||||
struct aws_event_stream_rpc_client_continuation_token;
|
||||
|
||||
/**
|
||||
* Invoked when a connection receives a message on an existing stream. message_args contains the
|
||||
* message data.
|
||||
*/
|
||||
typedef void(aws_event_stream_rpc_client_stream_continuation_fn)(
|
||||
struct aws_event_stream_rpc_client_continuation_token *token,
|
||||
const struct aws_event_stream_rpc_message_args *message_args,
|
||||
void *user_data);
|
||||
|
||||
/**
|
||||
* Invoked when a continuation has either been closed with the TERMINATE_STREAM flag, or when the connection
|
||||
* shuts down and deletes the continuation.
|
||||
*/
|
||||
typedef void(aws_event_stream_rpc_client_stream_continuation_closed_fn)(
|
||||
struct aws_event_stream_rpc_client_continuation_token *token,
|
||||
void *user_data);
|
||||
|
||||
struct aws_event_stream_rpc_client_stream_continuation_options {
|
||||
aws_event_stream_rpc_client_stream_continuation_fn *on_continuation;
|
||||
aws_event_stream_rpc_client_stream_continuation_closed_fn *on_continuation_closed;
|
||||
void *user_data;
|
||||
};
|
||||
|
||||
/**
|
||||
* Invoked when a non-stream level message is received on a connection.
|
||||
*/
|
||||
typedef void(aws_event_stream_rpc_client_connection_protocol_message_fn)(
|
||||
struct aws_event_stream_rpc_client_connection *connection,
|
||||
const struct aws_event_stream_rpc_message_args *message_args,
|
||||
void *user_data);
|
||||
|
||||
/**
|
||||
* Invoked when a successfully created connection is shutdown. error_code will indicate the reason for the shutdown.
|
||||
*/
|
||||
typedef void(aws_event_stream_rpc_client_on_connection_shutdown_fn)(
|
||||
struct aws_event_stream_rpc_client_connection *connection,
|
||||
int error_code,
|
||||
void *user_data);
|
||||
|
||||
/**
|
||||
* Invoked when a connection attempt completes.
|
||||
*
|
||||
* If the attempt was unsuccessful, the error_code will be non-zero and the connection pointer will be NULL,
|
||||
* and aws_event_stream_rpc_client_on_connection_shutdown_fn will not be invoked.
|
||||
*
|
||||
* If the attempt was successful, error_code will be 0 and the connection pointer will be valid.
|
||||
* You must call aws_event_stream_rpc_client_connection_acquire()
|
||||
* to prevent the pointer's memory from being destroyed before you are ready.
|
||||
* When you are completely done with the connection pointer you must call
|
||||
* aws_event_stream_rpc_client_connection_release() or its memory will leak.
|
||||
* aws_event_stream_rpc_client_on_connection_shutdown_fn will be invoked
|
||||
* when the network connection has closed. If you are done with the connection,
|
||||
* but it is still open, you must call aws_aws_event_stream_rpc_client_close()
|
||||
* or network connection will remain open, even if you call release().
|
||||
*/
|
||||
typedef void(aws_event_stream_rpc_client_on_connection_setup_fn)(
|
||||
struct aws_event_stream_rpc_client_connection *connection,
|
||||
int error_code,
|
||||
void *user_data);
|
||||
|
||||
/**
|
||||
* Invoked whenever a message has been flushed to the channel.
|
||||
*/
|
||||
typedef void(aws_event_stream_rpc_client_message_flush_fn)(int error_code, void *user_data);
|
||||
|
||||
struct aws_client_bootstrap;
|
||||
|
||||
struct aws_event_stream_rpc_client_connection_options {
|
||||
/** host name to use for the connection. This depends on your socket type. */
|
||||
const char *host_name;
|
||||
/** port to use for your connection, assuming for the appropriate socket type. */
|
||||
uint32_t port;
|
||||
/** socket options for establishing the connection to the RPC server. */
|
||||
const struct aws_socket_options *socket_options;
|
||||
/** optional: tls options for using when establishing your connection. */
|
||||
const struct aws_tls_connection_options *tls_options;
|
||||
struct aws_client_bootstrap *bootstrap;
|
||||
aws_event_stream_rpc_client_on_connection_setup_fn *on_connection_setup;
|
||||
aws_event_stream_rpc_client_connection_protocol_message_fn *on_connection_protocol_message;
|
||||
aws_event_stream_rpc_client_on_connection_shutdown_fn *on_connection_shutdown;
|
||||
void *user_data;
|
||||
};
|
||||
|
||||
AWS_EXTERN_C_BEGIN
|
||||
|
||||
/**
|
||||
* Initiate a new connection. If this function returns AWS_OP_SUCESSS, the
|
||||
* aws_event_stream_rpc_client_connection_options::on_connection_setup is guaranteed to be called exactly once. If that
|
||||
* callback successfully creates a connection, aws_event_stream_rpc_client_connection_options::on_connection_shutdown
|
||||
* will be invoked upon connection closure. However if the connection was never successfully setup,
|
||||
* aws_event_stream_rpc_client_connection_options::on_connection_shutdown will not be invoked later.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API int aws_event_stream_rpc_client_connection_connect(
|
||||
struct aws_allocator *allocator,
|
||||
const struct aws_event_stream_rpc_client_connection_options *conn_options);
|
||||
AWS_EVENT_STREAM_API void aws_event_stream_rpc_client_connection_acquire(
|
||||
const struct aws_event_stream_rpc_client_connection *connection);
|
||||
AWS_EVENT_STREAM_API void aws_event_stream_rpc_client_connection_release(
|
||||
const struct aws_event_stream_rpc_client_connection *connection);
|
||||
|
||||
/**
|
||||
* Closes the connection if it is open and aws_event_stream_rpc_client_connection_options::on_connection_shutdown will
|
||||
* be invoked upon shutdown. shutdown_error_code will indicate the reason for shutdown. For a graceful shutdown pass 0
|
||||
* or AWS_ERROR_SUCCESS.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API void aws_event_stream_rpc_client_connection_close(
|
||||
struct aws_event_stream_rpc_client_connection *connection,
|
||||
int shutdown_error_code);
|
||||
|
||||
/**
|
||||
* Returns true if the connection is open, false otherwise.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API bool aws_event_stream_rpc_client_connection_is_open(
|
||||
const struct aws_event_stream_rpc_client_connection *connection);
|
||||
|
||||
/**
|
||||
* Sends a message on the connection. These must be connection level messages (not application messages).
|
||||
*
|
||||
* flush_fn will be invoked when the message has been successfully writen to the wire or when it fails.
|
||||
*
|
||||
* returns AWS_OP_SUCCESS if the message was successfully created and queued, and in that case flush_fn will always be
|
||||
* invoked. Otherwise, flush_fn will not be invoked.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API int aws_event_stream_rpc_client_connection_send_protocol_message(
|
||||
struct aws_event_stream_rpc_client_connection *connection,
|
||||
const struct aws_event_stream_rpc_message_args *message_args,
|
||||
aws_event_stream_rpc_client_message_flush_fn *flush_fn,
|
||||
void *user_data);
|
||||
|
||||
/**
|
||||
* Create a new stream. continuation_option's callbacks will not be invoked, and nothing will be sent across the wire
|
||||
* until aws_event_stream_rpc_client_continuation_activate() is invoked.
|
||||
*
|
||||
* returns an instance of a aws_event_stream_rpc_client_continuation_token on success with a reference count of 1. You
|
||||
* must call aws_event_stream_rpc_client_continuation_release() when you're finished with it. Returns NULL on failure.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API struct aws_event_stream_rpc_client_continuation_token *
|
||||
aws_event_stream_rpc_client_connection_new_stream(
|
||||
struct aws_event_stream_rpc_client_connection *connection,
|
||||
const struct aws_event_stream_rpc_client_stream_continuation_options *continuation_options);
|
||||
AWS_EVENT_STREAM_API void aws_event_stream_rpc_client_continuation_acquire(
|
||||
const struct aws_event_stream_rpc_client_continuation_token *continuation);
|
||||
AWS_EVENT_STREAM_API void aws_event_stream_rpc_client_continuation_release(
|
||||
const struct aws_event_stream_rpc_client_continuation_token *continuation);
|
||||
|
||||
/**
|
||||
* returns true if the continuation has been closed.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API bool aws_event_stream_rpc_client_continuation_is_closed(
|
||||
const struct aws_event_stream_rpc_client_continuation_token *continuation);
|
||||
|
||||
/**
|
||||
* Actually sends the initial stream to the peer. Callbacks from aws_event_stream_rpc_client_connection_new_stream()
|
||||
* will actually be invoked if this function returns AWS_OP_SUCCESS, otherwise, the stream has not been queued and no
|
||||
* callbacks will be invoked.
|
||||
*
|
||||
* operation_name is the name to identify which logical rpc call you want to kick off with the peer. It must be
|
||||
* non-empty. flush_fn will be invoked once the message has either been written to the wire or it fails.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API int aws_event_stream_rpc_client_continuation_activate(
|
||||
struct aws_event_stream_rpc_client_continuation_token *continuation,
|
||||
struct aws_byte_cursor operation_name,
|
||||
const struct aws_event_stream_rpc_message_args *message_args,
|
||||
aws_event_stream_rpc_client_message_flush_fn *flush_fn,
|
||||
void *user_data);
|
||||
|
||||
AWS_EVENT_STREAM_API void *aws_event_stream_rpc_client_continuation_get_user_data(
|
||||
struct aws_event_stream_rpc_client_continuation_token *continuation);
|
||||
|
||||
/**
|
||||
* Sends a message on the continuation. aws_event_stream_rpc_client_continuation_activate() must be successfully invoked
|
||||
* prior to calling this function.
|
||||
*
|
||||
* If this function returns AWS_OP_SUCCESS, flush_fn will be invoked once the message has either been written to the
|
||||
* wire or it fails.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API int aws_event_stream_rpc_client_continuation_send_message(
|
||||
struct aws_event_stream_rpc_client_continuation_token *continuation,
|
||||
const struct aws_event_stream_rpc_message_args *message_args,
|
||||
aws_event_stream_rpc_client_message_flush_fn *flush_fn,
|
||||
void *user_data);
|
||||
|
||||
AWS_EXTERN_C_END
|
||||
AWS_POP_SANE_WARNING_LEVEL
|
||||
|
||||
#endif /* AWS_EVENT_STREAM_RPC_CLIENT_H */
|
||||
@@ -0,0 +1,213 @@
|
||||
#ifndef AWS_EVENT_STREAM_RPC_SERVER_H
|
||||
#define AWS_EVENT_STREAM_RPC_SERVER_H
|
||||
/**
|
||||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0.
|
||||
*/
|
||||
|
||||
#include <aws/event-stream/event_stream_rpc.h>
|
||||
|
||||
AWS_PUSH_SANE_WARNING_LEVEL
|
||||
|
||||
struct aws_channel;
|
||||
struct aws_event_stream_rpc_server_connection;
|
||||
|
||||
struct aws_event_stream_rpc_server_continuation_token;
|
||||
|
||||
/**
|
||||
* Invoked when a connection receives a message on an existing stream. message_args contains the
|
||||
* message data.
|
||||
*/
|
||||
typedef void(aws_event_stream_rpc_server_stream_continuation_fn)(
|
||||
struct aws_event_stream_rpc_server_continuation_token *token,
|
||||
const struct aws_event_stream_rpc_message_args *message_args,
|
||||
void *user_data);
|
||||
|
||||
/**
|
||||
* Invoked when a continuation has either been closed with the TERMINATE_STREAM flag, or when the connection
|
||||
* shutsdown and deletes the continuation.
|
||||
*/
|
||||
typedef void(aws_event_stream_rpc_server_stream_continuation_closed_fn)(
|
||||
struct aws_event_stream_rpc_server_continuation_token *token,
|
||||
void *user_data);
|
||||
|
||||
struct aws_event_stream_rpc_server_stream_continuation_options {
|
||||
aws_event_stream_rpc_server_stream_continuation_fn *on_continuation;
|
||||
aws_event_stream_rpc_server_stream_continuation_closed_fn *on_continuation_closed;
|
||||
void *user_data;
|
||||
};
|
||||
|
||||
/**
|
||||
* Invoked when a non-stream level message is received on a connection.
|
||||
*/
|
||||
typedef void(aws_event_stream_rpc_server_connection_protocol_message_fn)(
|
||||
struct aws_event_stream_rpc_server_connection *connection,
|
||||
const struct aws_event_stream_rpc_message_args *message_args,
|
||||
void *user_data);
|
||||
|
||||
/**
|
||||
* Invoked when a new stream has been received on the connection. If you return AWS_OP_SUCCESS (0),
|
||||
* You must fill in the fields for continuation options or the program will assert and exit.
|
||||
*
|
||||
* A failure path MUST leave the ref count of the continuation alone.
|
||||
*
|
||||
* A success path should probably take a ref which will leave the continuation (assuming no other interference)
|
||||
* at two AFTER creation is complete: 1 for the connection's continuation table, and one for the callback
|
||||
* recipient which is presumably tracking it as well.
|
||||
*/
|
||||
typedef int(aws_event_stream_rpc_server_on_incoming_stream_fn)(
|
||||
struct aws_event_stream_rpc_server_connection *connection,
|
||||
struct aws_event_stream_rpc_server_continuation_token *token,
|
||||
struct aws_byte_cursor operation_name,
|
||||
struct aws_event_stream_rpc_server_stream_continuation_options *continuation_options,
|
||||
void *user_data);
|
||||
|
||||
struct aws_event_stream_rpc_connection_options {
|
||||
aws_event_stream_rpc_server_on_incoming_stream_fn *on_incoming_stream;
|
||||
aws_event_stream_rpc_server_connection_protocol_message_fn *on_connection_protocol_message;
|
||||
void *user_data;
|
||||
};
|
||||
|
||||
/**
|
||||
* Invoked when a new connection is received on a server listener. If you return AWS_OP_SUCCESS,
|
||||
* You must fill in the fields for connection_options or the program will assert and exit.
|
||||
*
|
||||
* If error_code is non-zero, an error occurred upon setting up the channel and connection will be NULL. Otherwise,
|
||||
* connection is non-null. If you intend to seat a pointer to connection, you MUST call
|
||||
* aws_event_stream_rpc_server_connection_acquire() and when you're finished with the connection you MUST call
|
||||
* aws_event_stream_server_connection_release().
|
||||
*/
|
||||
typedef int(aws_event_stream_rpc_server_on_new_connection_fn)(
|
||||
struct aws_event_stream_rpc_server_connection *connection,
|
||||
int error_code,
|
||||
struct aws_event_stream_rpc_connection_options *connection_options,
|
||||
void *user_data);
|
||||
|
||||
/**
|
||||
* Invoked when a successfully created connection is shutdown. error_code will indicate the reason for the shutdown.
|
||||
*/
|
||||
typedef void(aws_event_stream_rpc_server_on_connection_shutdown_fn)(
|
||||
struct aws_event_stream_rpc_server_connection *connection,
|
||||
int error_code,
|
||||
void *user_data);
|
||||
|
||||
/**
|
||||
* Invoked whenever a message has been flushed to the channel.
|
||||
*/
|
||||
typedef void(aws_event_stream_rpc_server_message_flush_fn)(int error_code, void *user_data);
|
||||
|
||||
struct aws_server_bootstrap;
|
||||
struct aws_event_stream_rpc_server_listener;
|
||||
|
||||
/**
|
||||
* (Optional). Invoked when the listener has been successfully shutdown (after the last ref count release).
|
||||
*/
|
||||
typedef void(aws_event_stream_rpc_server_on_listener_destroy_fn)(
|
||||
struct aws_event_stream_rpc_server_listener *server,
|
||||
void *user_data);
|
||||
|
||||
struct aws_event_stream_rpc_server_listener_options {
|
||||
/** host name to use for the listener. This depends on your socket type. */
|
||||
const char *host_name;
|
||||
/** port to use for your listener, assuming for the appropriate socket type. */
|
||||
uint32_t port;
|
||||
const struct aws_socket_options *socket_options;
|
||||
/** optional: tls options for using when setting up your server. */
|
||||
const struct aws_tls_connection_options *tls_options;
|
||||
struct aws_server_bootstrap *bootstrap;
|
||||
aws_event_stream_rpc_server_on_new_connection_fn *on_new_connection;
|
||||
aws_event_stream_rpc_server_on_connection_shutdown_fn *on_connection_shutdown;
|
||||
aws_event_stream_rpc_server_on_listener_destroy_fn *on_destroy_callback;
|
||||
void *user_data;
|
||||
};
|
||||
|
||||
AWS_EXTERN_C_BEGIN
|
||||
|
||||
/**
|
||||
* Creates a listener with a ref count of 1. You are responsible for calling
|
||||
* aws_event_stream_rpc_server_listener_release() when you're finished with the listener. Returns NULL if an error
|
||||
* occurs.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API struct aws_event_stream_rpc_server_listener *aws_event_stream_rpc_server_new_listener(
|
||||
struct aws_allocator *allocator,
|
||||
struct aws_event_stream_rpc_server_listener_options *options);
|
||||
AWS_EVENT_STREAM_API void aws_event_stream_rpc_server_listener_acquire(
|
||||
struct aws_event_stream_rpc_server_listener *listener);
|
||||
AWS_EVENT_STREAM_API void aws_event_stream_rpc_server_listener_release(
|
||||
struct aws_event_stream_rpc_server_listener *listener);
|
||||
|
||||
/**
|
||||
* Get the local port which the listener's socket is bound to.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API
|
||||
uint32_t aws_event_stream_rpc_server_listener_get_bound_port(
|
||||
const struct aws_event_stream_rpc_server_listener *listener);
|
||||
|
||||
/**
|
||||
* Bypasses server, and creates a connection on an already existing channel. No connection lifetime callbacks will be
|
||||
* invoked on the returned connection. Returns NULL if an error occurs. If and only if, you use this API, the returned
|
||||
* connection is already ref counted and you must call aws_event_stream_rpc_server_connection_release() even if you did
|
||||
* not explictly call aws_event_stream_rpc_server_connection_acquire()
|
||||
*/
|
||||
AWS_EVENT_STREAM_API struct aws_event_stream_rpc_server_connection *
|
||||
aws_event_stream_rpc_server_connection_from_existing_channel(
|
||||
struct aws_event_stream_rpc_server_listener *server,
|
||||
struct aws_channel *channel,
|
||||
const struct aws_event_stream_rpc_connection_options *connection_options);
|
||||
AWS_EVENT_STREAM_API void aws_event_stream_rpc_server_connection_acquire(
|
||||
struct aws_event_stream_rpc_server_connection *connection);
|
||||
AWS_EVENT_STREAM_API void aws_event_stream_rpc_server_connection_release(
|
||||
struct aws_event_stream_rpc_server_connection *connection);
|
||||
|
||||
AWS_EVENT_STREAM_API void *aws_event_stream_rpc_server_connection_get_user_data(
|
||||
struct aws_event_stream_rpc_server_connection *connection);
|
||||
/**
|
||||
* returns true if the connection is open. False otherwise.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API bool aws_event_stream_rpc_server_connection_is_open(
|
||||
struct aws_event_stream_rpc_server_connection *connection);
|
||||
|
||||
/**
|
||||
* Closes the connection (including all continuations on the connection), and releases the connection ref count.
|
||||
* shutdown_error_code is the error code to use when shutting down the channel. Use AWS_ERROR_SUCCESS for non-error
|
||||
* cases.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API void aws_event_stream_rpc_server_connection_close(
|
||||
struct aws_event_stream_rpc_server_connection *connection,
|
||||
int shutdown_error_code);
|
||||
|
||||
/**
|
||||
* Sends a protocol message on the connection (not application data). If the message is valid and successfully written
|
||||
* to the channel, flush_fn will be invoked.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API int aws_event_stream_rpc_server_connection_send_protocol_message(
|
||||
struct aws_event_stream_rpc_server_connection *connection,
|
||||
const struct aws_event_stream_rpc_message_args *message_args,
|
||||
aws_event_stream_rpc_server_message_flush_fn *flush_fn,
|
||||
void *user_data);
|
||||
|
||||
AWS_EVENT_STREAM_API void aws_event_stream_rpc_server_continuation_acquire(
|
||||
struct aws_event_stream_rpc_server_continuation_token *continuation);
|
||||
AWS_EVENT_STREAM_API void aws_event_stream_rpc_server_continuation_release(
|
||||
struct aws_event_stream_rpc_server_continuation_token *continuation);
|
||||
|
||||
/**
|
||||
* returns true if the continuation is still in an open state.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API bool aws_event_stream_rpc_server_continuation_is_closed(
|
||||
struct aws_event_stream_rpc_server_continuation_token *continuation);
|
||||
|
||||
/**
|
||||
* Sends an application message on the continuation. If the message is valid and successfully written
|
||||
* to the channel, flush_fn will be invoked.
|
||||
*/
|
||||
AWS_EVENT_STREAM_API int aws_event_stream_rpc_server_continuation_send_message(
|
||||
struct aws_event_stream_rpc_server_continuation_token *continuation,
|
||||
const struct aws_event_stream_rpc_message_args *message_args,
|
||||
aws_event_stream_rpc_server_message_flush_fn *flush_fn,
|
||||
void *user_data);
|
||||
|
||||
AWS_EXTERN_C_END
|
||||
AWS_POP_SANE_WARNING_LEVEL
|
||||
|
||||
#endif /* AWS_EVENT_STREAM_RPC_SERVER_H */
|
||||
Reference in New Issue
Block a user