|  | 
| 2 | 2 | #define AIONIC_STREAM_H | 
| 3 | 3 | 
 | 
| 4 | 4 | #include "parser.h" | 
|  | 5 | +#include <pthread.h> | 
|  | 6 | +#include <stdint.h> | 
|  | 7 | +#include <stdbool.h> | 
| 5 | 8 | 
 | 
|  | 9 | +// Stream operation result codes | 
|  | 10 | +typedef enum { | 
|  | 11 | +    STREAM_SUCCESS = 0, | 
|  | 12 | +    STREAM_ERROR_NULL, | 
|  | 13 | +    STREAM_ERROR_INVALID_FD, | 
|  | 14 | +    STREAM_ERROR_MEMORY, | 
|  | 15 | +    STREAM_ERROR_TIMEOUT, | 
|  | 16 | +    STREAM_ERROR_CLOSED, | 
|  | 17 | +    STREAM_ERROR_OVERFLOW | 
|  | 18 | +} StreamResult; | 
|  | 19 | + | 
|  | 20 | +// Stream statistics structure | 
|  | 21 | +typedef struct { | 
|  | 22 | +    uint64_t bytes_sent;        // For network streams: bytes sent | 
|  | 23 | +    uint64_t bytes_received;    // For network streams: bytes received | 
|  | 24 | +    uint64_t chunks_sent;       // For network streams: number of chunks sent | 
|  | 25 | +    uint64_t operations_count;   // For both: number of operations | 
|  | 26 | +    uint64_t total_time_ns;     // For both: total time in nanoseconds | 
|  | 27 | +    uint64_t bytes_written;     // For buffer streams: bytes written | 
|  | 28 | +    uint64_t bytes_read;        // For buffer streams: bytes read | 
|  | 29 | +} StreamStats; | 
|  | 30 | + | 
|  | 31 | +// Stream configuration options | 
|  | 32 | +typedef struct { | 
|  | 33 | +    size_t buffer_size; | 
|  | 34 | +    bool chunked_encoding; | 
|  | 35 | +    uint32_t timeout_ms; | 
|  | 36 | +    bool non_blocking; | 
|  | 37 | +    uint8_t priority; | 
|  | 38 | +} StreamConfig; | 
|  | 39 | + | 
|  | 40 | +// Stream callback function types | 
|  | 41 | +typedef void (*StreamDataCallback)(const char* data, size_t length, void* user_data); | 
|  | 42 | +typedef void (*StreamErrorCallback)(StreamResult error, void* user_data); | 
|  | 43 | + | 
|  | 44 | +// Original StreamData structure with enhancements | 
| 6 | 45 | typedef struct { | 
| 7 | 46 |     int client_fd; | 
| 8 | 47 |     char *buffer; | 
| 9 | 48 |     size_t buffer_size; | 
| 10 | 49 |     size_t buffer_position; | 
| 11 |  | -    int is_active; | 
| 12 |  | -    int chunked_encoding; | 
|  | 50 | +    bool is_active; | 
|  | 51 | +    bool chunked_encoding; | 
|  | 52 | +     | 
|  | 53 | +    // Enhanced features | 
|  | 54 | +    pthread_mutex_t mutex; | 
|  | 55 | +    StreamConfig config; | 
|  | 56 | +    StreamStats stats; | 
|  | 57 | +    StreamDataCallback data_callback; | 
|  | 58 | +    StreamErrorCallback error_callback; | 
|  | 59 | +    void* user_data; | 
|  | 60 | +    uint64_t last_activity_ns; | 
| 13 | 61 | } StreamData; | 
| 14 | 62 | 
 | 
| 15 |  | -// Stream buffer structure for in-memory operations | 
|  | 63 | +// Enhanced StreamBuffer structure | 
| 16 | 64 | typedef struct { | 
| 17 | 65 |     char *data; | 
| 18 | 66 |     size_t size; | 
| 19 | 67 |     size_t pos; | 
| 20 | 68 |     size_t capacity; | 
|  | 69 | +     | 
|  | 70 | +    // Enhanced features | 
|  | 71 | +    pthread_mutex_t mutex; | 
|  | 72 | +    StreamStats stats; | 
|  | 73 | +    bool auto_expand; | 
|  | 74 | +    size_t max_capacity; | 
| 21 | 75 | } StreamBuffer; | 
| 22 | 76 | 
 | 
| 23 |  | -// Stream functions | 
|  | 77 | +// Original stream functions (maintained for compatibility) | 
| 24 | 78 | int stream_init(StreamData *stream, int client_fd); | 
| 25 | 79 | int stream_send_chunk(StreamData *stream, const char *data, size_t length); | 
| 26 | 80 | int stream_end(StreamData *stream); | 
| 27 | 81 | int stream_response(int client_fd, RouteResponse *response); | 
| 28 | 82 | void stream_cleanup(StreamData *stream); | 
| 29 | 83 | 
 | 
| 30 |  | -// Stream buffer functions | 
|  | 84 | +// Enhanced stream functions | 
|  | 85 | +int stream_init_ex(StreamData *stream, int client_fd, const StreamConfig *config); | 
|  | 86 | +int stream_send_chunk_ex(StreamData *stream, const char *data, size_t length, uint32_t timeout_ms); | 
|  | 87 | +StreamResult stream_send_with_callback(StreamData *stream, const char *data, size_t length,  | 
|  | 88 | +                                     StreamDataCallback callback, void* user_data); | 
|  | 89 | +void stream_get_stats(const StreamData *stream, StreamStats *stats); | 
|  | 90 | +void stream_set_callbacks(StreamData *stream, StreamDataCallback data_cb,  | 
|  | 91 | +                         StreamErrorCallback error_cb, void* user_data); | 
|  | 92 | + | 
|  | 93 | +// Original stream buffer functions (maintained for compatibility) | 
| 31 | 94 | int stream_buffer_init(StreamBuffer *stream, size_t initial_size); | 
| 32 | 95 | int stream_buffer_write(StreamBuffer *stream, const void *data, size_t size); | 
| 33 | 96 | int stream_buffer_read(StreamBuffer *stream, void *data, size_t size); | 
| 34 | 97 | void stream_buffer_reset(StreamBuffer *stream); | 
| 35 | 98 | void stream_buffer_cleanup(StreamBuffer *stream); | 
| 36 | 99 | 
 | 
|  | 100 | +// Enhanced stream buffer functions | 
|  | 101 | +int stream_buffer_init_ex(StreamBuffer *stream, size_t initial_size, bool auto_expand, size_t max_capacity); | 
|  | 102 | +StreamResult stream_buffer_write_ex(StreamBuffer *stream, const void *data, size_t size, bool expand); | 
|  | 103 | +StreamResult stream_buffer_read_ex(StreamBuffer *stream, void *data, size_t size, bool advance); | 
|  | 104 | +void stream_buffer_get_stats(const StreamBuffer *stream, StreamStats *stats); | 
|  | 105 | + | 
|  | 106 | +// Utility functions | 
|  | 107 | +const char* stream_result_to_string(StreamResult result); | 
|  | 108 | +void stream_print_stats(const StreamData *stream); | 
|  | 109 | + | 
| 37 | 110 | #endif // AIONIC_STREAM_H | 
0 commit comments