// // Created by nicol on 2018-03-18. // #ifndef ESC_ESC_H #define ESC_ESC_H #include // bool typedef int bool_t; #define BOOL_TRUE 1 #define BOOL_FALSE 0 // array typedef struct st_array array_t; typedef void (*array_deallocator)(void*); array_t* array_create(size_t n, ...); void array_destroy(array_t* array, array_deallocator destroyer); // uuid #if defined(_WIN32) && defined(UUID_DEFINED) #undef uuid_t #endif typedef struct st_uuid uuid_t; uuid_t* uuid_create(); void uuid_destroy(uuid_t*); // buffer typedef struct st_buffer buffer_t; void buffer_destroy(buffer_t*); buffer_t* buffer_from_string(const char* str); size_t buffer_size(buffer_t* b); // error #define ERROR_MSG_SIZE 1024 typedef struct st_error { const char* file; int line; int code; char message[ERROR_MSG_SIZE]; } error_t; // esc #define ESC_VERSION_NOSTREAM -1 #define ESC_VERSION_EMPTYSTREAM -1 #define ESC_VERSION_ANY -2 typedef struct st_esc_position esc_position_t; typedef struct st_esc_connection_settings esc_connection_settings_t; typedef struct st_esc_connection esc_connection_t; typedef struct st_esc_credentials esc_credentials_t; typedef struct st_esc_recorded_event { uuid_t* event_id; const char* event_type; int64_t event_number; const char *event_stream_id; int64_t created_epoch; buffer_t* data; buffer_t* metadata; } esc_recorded_event_t; typedef struct st_esc_resolved_event { esc_recorded_event_t* event; esc_recorded_event_t* link; esc_position_t* original_position; } esc_resolved_event_t; struct st_all_events_slice { char* read_direction; esc_position_t* from_position; esc_position_t* next_position; size_t n_events; esc_resolved_event_t** events; int is_end_of_stream; }; typedef struct st_all_events_slice esc_all_events_slice_t; typedef struct st_esc_write_result { int64_t next_expected_version; esc_position_t* log_position; } esc_write_result_t; typedef struct st_esc_event_data esc_event_data_t; typedef struct st_esc_subscription esc_subscription_t; typedef void (*esc_event_appeared_t)(void* ctx, esc_subscription_t* s, esc_resolved_event_t* ev); typedef struct st_event_appeared_callback { esc_event_appeared_t fn; void* ctx; } esc_event_appeared_callback_t; typedef void (*esc_subscription_droppped_t)(void* ctx, esc_subscription_t* subscription, const char* reason, error_t* error); typedef struct st_subscription_dropped_callback { esc_subscription_droppped_t fn; void* ctx; } esc_subscription_dropped_callback_t; esc_connection_settings_t* const esc_default_connection_settings; // Connection esc_connection_t* esc_connection_create(esc_connection_settings_t* connection_settings, const char* addr, const char* connection_name); void esc_connection_destroy(esc_connection_t* conn); int esc_connection_connect(esc_connection_t* conn); void esc_connection_close(esc_connection_t* conn); esc_credentials_t* esc_credentials_create(const char* username, const char* password); void esc_credentials_destroy(esc_credentials_t* creds); esc_event_data_t* esc_event_data_create(uuid_t* event_id, const char* event_type, bool_t is_json, buffer_t* data, buffer_t* metadata); void esc_event_data_destroy(esc_event_data_t*); esc_write_result_t* esc_append_to_stream(esc_connection_t* conn, const char* stream, int64_t expected_version, array_t* events); void esc_write_result_destroy(esc_write_result_t* write_result); esc_all_events_slice_t* esc_connection_read_all_forward(esc_connection_t* conn, esc_position_t* last_checkpoint, unsigned int count, esc_credentials_t* credentials); void esc_all_events_slice_destroy(esc_all_events_slice_t* all_events_slice); error_t* esc_connection_last_error(esc_connection_t* conn); // Subscriptions extern esc_event_appeared_callback_t esc_empty_event_appeared_callback; extern esc_subscription_dropped_callback_t esc_empty_subscription_dropped_callback; esc_subscription_t* esc_connection_subscribe_to_all(esc_connection_t* conn, bool_t resolve_link_tos, esc_event_appeared_callback_t event_appeared_callback, esc_subscription_dropped_callback_t subscription_dropped_callback, esc_credentials_t* credentials); void esc_subscription_stop(esc_subscription_t* subscription); void esc_subscription_destroy(esc_subscription_t* subscription); // Formatting const char* uuid_format(uuid_t* uuid, char* buf, size_t buf_size); const char* esc_position_format(esc_position_t* position, char* buffer, size_t buf_size); #endif //ESC_ESC_H