Mercurial
diff seobeo/seobeo.h @ 257:609d3c6aff4e
[seobeo] Add persistent SSE streams
Co-authored-by: Copilot <[email protected]>
| author | MrJuneJune <me@mrjunejune.com> |
|---|---|
| date | Tue, 04 Aug 2026 16:49:11 -0700 |
| parents | 117c4d53c9a4 |
| children | 1f9877b637e9 |
line wrap: on
line diff
--- a/seobeo/seobeo.h Tue Aug 04 16:49:01 2026 -0700 +++ b/seobeo/seobeo.h Tue Aug 04 16:49:11 2026 -0700 @@ -27,6 +27,10 @@ #define INITIAL_BUFFER_CAPACITY 4096 +#define SEOBEO_SSE_RETRY_NONE -1 +#define SEOBEO_SSE_EVENT_TOO_LARGE -2 +#define SEOBEO_SSE_INVALID_EVENT -3 +#define SEOBEO_SSE_BACKPRESSURE -4 // HTTP STATUS CODE #define HTTP_OK 200 @@ -342,6 +346,8 @@ extern void Seobeo_Router_Register(const char *method, const char *path_pattern, Seobeo_Route_Handler handler); /* Register a streaming route handler. Handler receives client handle for direct streaming. */ extern void Seobeo_Router_Register_Stream(const char *method, const char *path_pattern, Seobeo_Stream_Handler handler); +/* Register a GET route that owns an SSE stream until its handler returns. */ +extern void Seobeo_Router_Register_SSE(const char *path_pattern, Seobeo_SSE_Handler handler); /* Clean up router resources */ extern void Seobeo_Router_Destroy(); /* Find matching route handler (internal use) */ @@ -353,16 +359,36 @@ extern char *Seobeo_Web_LoadFile(const char *file_path, size_t *p_file_size); /* Being a proxy and keeping the client open */ extern Seobeo_Stream_Handler Seobeo_Router_Find_Stream_Handler(const char *method, const char *path, Seobeo_Request_Entry **pp_request_map, Dowa_Arena *p_arena); +extern Seobeo_SSE_Handler Seobeo_Router_Find_SSE_Handler(const char *method, const char *path, Seobeo_Request_Entry **pp_request_map, Dowa_Arena *p_arena); + +/* Start an SSE response and send its required HTTP headers. */ +extern int32 Seobeo_SSE_Start(Seobeo_SSE_Stream *p_stream, Seobeo_Handle *p_handle); +/* Send one SSE record. retry_ms may be SEOBEO_SSE_RETRY_NONE. */ +extern int32 Seobeo_SSE_Send(Seobeo_SSE_Stream *p_stream, const Seobeo_SSE_Event *p_event); +/* Send a data-only SSE record. */ +extern int32 Seobeo_SSE_Send_Data(Seobeo_SSE_Stream *p_stream, const char *data); +/* Send a comment/heartbeat record. */ +extern int32 Seobeo_SSE_Send_Comment(Seobeo_SSE_Stream *p_stream, const char *comment); +/* Flush queued SSE bytes. Returns 1 when the socket would block. */ +extern int32 Seobeo_SSE_Flush(Seobeo_SSE_Stream *p_stream); +/* Return TRUE while the stream can still accept records. */ +extern boolean Seobeo_SSE_Is_Open(const Seobeo_SSE_Stream *p_stream); +/* Retain a stream before storing it beyond the connection callback. */ +extern boolean Seobeo_SSE_Retain(Seobeo_SSE_Stream *p_stream); +/* Release a previously retained stream. */ +extern void Seobeo_SSE_Release(Seobeo_SSE_Stream *p_stream); +/* Stop accepting records. The router retains ownership of the handle. */ +extern void Seobeo_SSE_Close(Seobeo_SSE_Stream *p_stream); // --- Helper functions --- // /* Destroy handle. It will handle all NULL poointers. */ extern void Seobeo_Handle_Destroy(Seobeo_Handle *p_handle); /* Write to socket from write_buffer in the handle. */ -extern int Seobeo_Handle_Flush(Seobeo_Handle *p_handle); +extern int32 Seobeo_Handle_Flush(Seobeo_Handle *p_handle); /* Write to socket with given data source, if the data source is bigger than the write buffer for handle then we just directly write from the data source. */ -extern int Seobeo_Handle_Queue(Seobeo_Handle *p_handle, const uint8_t *data, uint32_t data_size); +extern int32 Seobeo_Handle_Queue(Seobeo_Handle *p_handle, const uint8 *data, uint32 data_size); /* Read to socket from read_buffer in the handle. */ -extern int Seobeo_Handle_Read(Seobeo_Handle *p_handle); +extern int32 Seobeo_Handle_Read(Seobeo_Handle *p_handle); /* Move to read_buffer to front and we already consumed the given amount in the handle. */ extern void Seobeo_Handle_Consume(Seobeo_Handle *p_handle, uint32 consumed); /* Assign IP4 or IP6 to sockaddr. TODO: Maybe create my own struct for this? */