Mercurial
comparison seobeo/s_sse.c @ 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 | |
| children | 04fee26ecce0 |
comparison
equal
deleted
inserted
replaced
| 256:30c2196d03d4 | 257:609d3c6aff4e |
|---|---|
| 1 #include "seobeo/seobeo.h" | |
| 2 | |
| 3 #include <stdio.h> | |
| 4 #include <stdlib.h> | |
| 5 #include <string.h> | |
| 6 | |
| 7 static const char *SEOBEO_SSE_HEADERS = | |
| 8 "HTTP/1.1 200 OK\r\n" | |
| 9 "Content-Type: text/event-stream\r\n" | |
| 10 "Cache-Control: no-cache\r\n" | |
| 11 "Connection: keep-alive\r\n" | |
| 12 "X-Accel-Buffering: no\r\n" | |
| 13 "\r\n"; | |
| 14 | |
| 15 static pthread_mutex_t g_sse_mutex = PTHREAD_MUTEX_INITIALIZER; | |
| 16 static Seobeo_SSE_Stream **g_sse_streams = NULL; | |
| 17 | |
| 18 typedef struct { | |
| 19 char *data; | |
| 20 uint32 size; | |
| 21 } Seobeo_SSE_Pending_Frame; | |
| 22 | |
| 23 #define SEOBEO_SSE_MAX_PENDING 32 | |
| 24 | |
| 25 static boolean sse_is_open_unlocked(const Seobeo_SSE_Stream *p_stream) | |
| 26 { | |
| 27 return p_stream && | |
| 28 p_stream->started && | |
| 29 !p_stream->closed && | |
| 30 p_stream->p_handle && | |
| 31 p_stream->p_handle->socket >= 0 && | |
| 32 !atomic_load(&p_stream->p_handle->destroyed); | |
| 33 } | |
| 34 | |
| 35 static void sse_clear_pending_unlocked(Seobeo_SSE_Stream *p_stream) | |
| 36 { | |
| 37 Seobeo_SSE_Pending_Frame *p_frames = p_stream->p_pending_frames; | |
| 38 size_t count = Dowa_Array_Length(p_frames); | |
| 39 for (size_t i = p_stream->pending_offset; i < count; i++) | |
| 40 Dowa_Free(p_frames[i].data); | |
| 41 Dowa_Array_Free(p_frames); | |
| 42 p_stream->p_pending_frames = NULL; | |
| 43 p_stream->pending_offset = 0; | |
| 44 } | |
| 45 | |
| 46 static void sse_release_unlocked(Seobeo_SSE_Stream *p_stream) | |
| 47 { | |
| 48 if (!p_stream) | |
| 49 return; | |
| 50 if (atomic_fetch_sub(&p_stream->ref_count, 1) != 1) | |
| 51 return; | |
| 52 sse_clear_pending_unlocked(p_stream); | |
| 53 Dowa_Free(p_stream->path); | |
| 54 Dowa_Free(p_stream); | |
| 55 } | |
| 56 | |
| 57 static int32 sse_drain_pending_unlocked(Seobeo_SSE_Stream *p_stream) | |
| 58 { | |
| 59 if (!sse_is_open_unlocked(p_stream)) | |
| 60 return -1; | |
| 61 int32 flush_result = Seobeo_Handle_Flush(p_stream->p_handle); | |
| 62 if (flush_result != 0) | |
| 63 return flush_result; | |
| 64 | |
| 65 Seobeo_SSE_Pending_Frame *p_frames = p_stream->p_pending_frames; | |
| 66 size_t count = Dowa_Array_Length(p_frames); | |
| 67 while (p_stream->pending_offset < count) | |
| 68 { | |
| 69 Seobeo_SSE_Pending_Frame *p_frame = | |
| 70 &p_frames[p_stream->pending_offset]; | |
| 71 int32 queue_result = Seobeo_Handle_Queue( | |
| 72 p_stream->p_handle, | |
| 73 (const uint8 *)p_frame->data, | |
| 74 p_frame->size); | |
| 75 if (queue_result != 0) | |
| 76 return queue_result; | |
| 77 Dowa_Free(p_frame->data); | |
| 78 p_stream->pending_offset++; | |
| 79 flush_result = Seobeo_Handle_Flush(p_stream->p_handle); | |
| 80 if (flush_result != 0) | |
| 81 return flush_result; | |
| 82 } | |
| 83 | |
| 84 Dowa_Array_Free(p_frames); | |
| 85 p_stream->p_pending_frames = NULL; | |
| 86 p_stream->pending_offset = 0; | |
| 87 return 0; | |
| 88 } | |
| 89 | |
| 90 static int32 sse_enqueue_frame_unlocked( | |
| 91 Seobeo_SSE_Stream *p_stream, | |
| 92 char *frame, | |
| 93 uint32 frame_size) | |
| 94 { | |
| 95 Seobeo_SSE_Pending_Frame *p_frames = p_stream->p_pending_frames; | |
| 96 size_t pending = Dowa_Array_Length(p_frames) - p_stream->pending_offset; | |
| 97 if (pending >= SEOBEO_SSE_MAX_PENDING) | |
| 98 return SEOBEO_SSE_BACKPRESSURE; | |
| 99 Seobeo_SSE_Pending_Frame pending_frame = { | |
| 100 .data = frame, | |
| 101 .size = frame_size, | |
| 102 }; | |
| 103 Dowa_Array_Push(p_frames, pending_frame); | |
| 104 p_stream->p_pending_frames = p_frames; | |
| 105 return sse_drain_pending_unlocked(p_stream); | |
| 106 } | |
| 107 | |
| 108 static boolean sse_field_is_safe(const char *value) | |
| 109 { | |
| 110 return !value || (strchr(value, '\r') == NULL && strchr(value, '\n') == NULL); | |
| 111 } | |
| 112 | |
| 113 static size_t sse_multiline_size(const char *prefix, const char *value) | |
| 114 { | |
| 115 const char *line = value ? value : ""; | |
| 116 size_t prefix_length = strlen(prefix); | |
| 117 size_t total = 0; | |
| 118 | |
| 119 while (TRUE) | |
| 120 { | |
| 121 const char *end = line; | |
| 122 while (*end && *end != '\r' && *end != '\n') | |
| 123 end++; | |
| 124 size_t line_length = (size_t)(end - line); | |
| 125 total += prefix_length + (line_length ? 1 : 0) + line_length + 1; | |
| 126 if (*end == '\0') | |
| 127 break; | |
| 128 if (*end == '\r' && end[1] == '\n') | |
| 129 end++; | |
| 130 line = end + 1; | |
| 131 } | |
| 132 return total; | |
| 133 } | |
| 134 | |
| 135 static char *sse_append_multiline( | |
| 136 char *output, | |
| 137 const char *prefix, | |
| 138 const char *value) | |
| 139 { | |
| 140 const char *line = value ? value : ""; | |
| 141 size_t prefix_length = strlen(prefix); | |
| 142 | |
| 143 while (TRUE) | |
| 144 { | |
| 145 const char *end = line; | |
| 146 while (*end && *end != '\r' && *end != '\n') | |
| 147 end++; | |
| 148 size_t line_length = (size_t)(end - line); | |
| 149 memcpy(output, prefix, prefix_length); | |
| 150 output += prefix_length; | |
| 151 if (line_length) | |
| 152 *output++ = ' '; | |
| 153 memcpy(output, line, line_length); | |
| 154 output += line_length; | |
| 155 *output++ = '\n'; | |
| 156 if (*end == '\0') | |
| 157 break; | |
| 158 if (*end == '\r' && end[1] == '\n') | |
| 159 end++; | |
| 160 line = end + 1; | |
| 161 } | |
| 162 return output; | |
| 163 } | |
| 164 | |
| 165 static int32 sse_send_frame( | |
| 166 Seobeo_SSE_Stream *p_stream, | |
| 167 const char *frame, | |
| 168 size_t frame_size) | |
| 169 { | |
| 170 if (!sse_is_open_unlocked(p_stream) || !frame) | |
| 171 return -1; | |
| 172 if (frame_size > p_stream->p_handle->write_buffer_capacity) | |
| 173 return SEOBEO_SSE_EVENT_TOO_LARGE; | |
| 174 | |
| 175 int32 flush_result = Seobeo_Handle_Flush(p_stream->p_handle); | |
| 176 if (flush_result != 0) | |
| 177 return flush_result; | |
| 178 | |
| 179 int32 queue_result = Seobeo_Handle_Queue( | |
| 180 p_stream->p_handle, | |
| 181 (const uint8 *)frame, | |
| 182 (uint32)frame_size); | |
| 183 if (queue_result != 0) | |
| 184 return queue_result; | |
| 185 return Seobeo_Handle_Flush(p_stream->p_handle); | |
| 186 } | |
| 187 | |
| 188 int32 Seobeo_SSE_Start( | |
| 189 Seobeo_SSE_Stream *p_stream, | |
| 190 Seobeo_Handle *p_handle) | |
| 191 { | |
| 192 if (!p_stream || !p_handle || atomic_load(&p_handle->destroyed)) | |
| 193 return -1; | |
| 194 p_stream->p_handle = p_handle; | |
| 195 p_stream->started = TRUE; | |
| 196 p_stream->closed = FALSE; | |
| 197 return sse_send_frame( | |
| 198 p_stream, | |
| 199 SEOBEO_SSE_HEADERS, | |
| 200 strlen(SEOBEO_SSE_HEADERS)); | |
| 201 } | |
| 202 | |
| 203 static int32 sse_send_event_unlocked( | |
| 204 Seobeo_SSE_Stream *p_stream, | |
| 205 const Seobeo_SSE_Event *p_event) | |
| 206 { | |
| 207 if (!sse_is_open_unlocked(p_stream)) | |
| 208 return -1; | |
| 209 if (!p_event || !sse_field_is_safe(p_event->event) || | |
| 210 !sse_field_is_safe(p_event->id) || | |
| 211 p_event->retry_ms < SEOBEO_SSE_RETRY_NONE) | |
| 212 { | |
| 213 return SEOBEO_SSE_INVALID_EVENT; | |
| 214 } | |
| 215 | |
| 216 size_t frame_size = 1 + sse_multiline_size("data:", p_event->data); | |
| 217 if (p_event->id) | |
| 218 frame_size += strlen("id: ") + strlen(p_event->id) + 1; | |
| 219 if (p_event->event) | |
| 220 frame_size += strlen("event: ") + strlen(p_event->event) + 1; | |
| 221 | |
| 222 char retry[32] = {0}; | |
| 223 if (p_event->retry_ms != SEOBEO_SSE_RETRY_NONE) | |
| 224 { | |
| 225 int written = snprintf( | |
| 226 retry, | |
| 227 sizeof(retry), | |
| 228 "retry: %d\n", | |
| 229 p_event->retry_ms); | |
| 230 if (written < 0 || (size_t)written >= sizeof(retry)) | |
| 231 return SEOBEO_SSE_INVALID_EVENT; | |
| 232 frame_size += (size_t)written; | |
| 233 } | |
| 234 | |
| 235 if (frame_size > p_stream->p_handle->write_buffer_capacity) | |
| 236 return SEOBEO_SSE_EVENT_TOO_LARGE; | |
| 237 char *frame = malloc(frame_size); | |
| 238 if (!frame) | |
| 239 return -1; | |
| 240 | |
| 241 char *output = frame; | |
| 242 if (p_event->id) | |
| 243 output += sprintf(output, "id: %s\n", p_event->id); | |
| 244 if (p_event->event) | |
| 245 output += sprintf(output, "event: %s\n", p_event->event); | |
| 246 if (retry[0]) | |
| 247 { | |
| 248 size_t retry_length = strlen(retry); | |
| 249 memcpy(output, retry, retry_length); | |
| 250 output += retry_length; | |
| 251 } | |
| 252 output = sse_append_multiline(output, "data:", p_event->data); | |
| 253 *output++ = '\n'; | |
| 254 | |
| 255 int32 result = sse_enqueue_frame_unlocked( | |
| 256 p_stream, | |
| 257 frame, | |
| 258 (uint32)(output - frame)); | |
| 259 if (result == SEOBEO_SSE_BACKPRESSURE) | |
| 260 Dowa_Free(frame); | |
| 261 return result; | |
| 262 } | |
| 263 | |
| 264 int32 Seobeo_SSE_Send( | |
| 265 Seobeo_SSE_Stream *p_stream, | |
| 266 const Seobeo_SSE_Event *p_event) | |
| 267 { | |
| 268 pthread_mutex_lock(&g_sse_mutex); | |
| 269 int32 result = sse_send_event_unlocked(p_stream, p_event); | |
| 270 pthread_mutex_unlock(&g_sse_mutex); | |
| 271 return result; | |
| 272 } | |
| 273 | |
| 274 int32 Seobeo_SSE_Send_Data( | |
| 275 Seobeo_SSE_Stream *p_stream, | |
| 276 const char *data) | |
| 277 { | |
| 278 Seobeo_SSE_Event event = { | |
| 279 .data = data, | |
| 280 .retry_ms = SEOBEO_SSE_RETRY_NONE, | |
| 281 }; | |
| 282 return Seobeo_SSE_Send(p_stream, &event); | |
| 283 } | |
| 284 | |
| 285 int32 Seobeo_SSE_Send_Comment( | |
| 286 Seobeo_SSE_Stream *p_stream, | |
| 287 const char *comment) | |
| 288 { | |
| 289 pthread_mutex_lock(&g_sse_mutex); | |
| 290 if (!sse_is_open_unlocked(p_stream)) | |
| 291 { | |
| 292 pthread_mutex_unlock(&g_sse_mutex); | |
| 293 return -1; | |
| 294 } | |
| 295 size_t frame_size = sse_multiline_size(":", comment) + 1; | |
| 296 if (frame_size > p_stream->p_handle->write_buffer_capacity) | |
| 297 { | |
| 298 pthread_mutex_unlock(&g_sse_mutex); | |
| 299 return SEOBEO_SSE_EVENT_TOO_LARGE; | |
| 300 } | |
| 301 char *frame = malloc(frame_size); | |
| 302 if (!frame) | |
| 303 { | |
| 304 pthread_mutex_unlock(&g_sse_mutex); | |
| 305 return -1; | |
| 306 } | |
| 307 char *output = sse_append_multiline(frame, ":", comment); | |
| 308 *output++ = '\n'; | |
| 309 int32 result = sse_enqueue_frame_unlocked( | |
| 310 p_stream, | |
| 311 frame, | |
| 312 (uint32)(output - frame)); | |
| 313 if (result == SEOBEO_SSE_BACKPRESSURE) | |
| 314 Dowa_Free(frame); | |
| 315 pthread_mutex_unlock(&g_sse_mutex); | |
| 316 return result; | |
| 317 } | |
| 318 | |
| 319 int32 Seobeo_SSE_Flush(Seobeo_SSE_Stream *p_stream) | |
| 320 { | |
| 321 pthread_mutex_lock(&g_sse_mutex); | |
| 322 int32 result = sse_is_open_unlocked(p_stream) | |
| 323 ? sse_drain_pending_unlocked(p_stream) | |
| 324 : -1; | |
| 325 pthread_mutex_unlock(&g_sse_mutex); | |
| 326 return result; | |
| 327 } | |
| 328 | |
| 329 boolean Seobeo_SSE_Is_Open(const Seobeo_SSE_Stream *p_stream) | |
| 330 { | |
| 331 pthread_mutex_lock(&g_sse_mutex); | |
| 332 boolean open = sse_is_open_unlocked(p_stream); | |
| 333 pthread_mutex_unlock(&g_sse_mutex); | |
| 334 return open; | |
| 335 } | |
| 336 | |
| 337 boolean Seobeo_SSE_Retain(Seobeo_SSE_Stream *p_stream) | |
| 338 { | |
| 339 if (!p_stream) | |
| 340 return FALSE; | |
| 341 pthread_mutex_lock(&g_sse_mutex); | |
| 342 unsigned int references = atomic_load(&p_stream->ref_count); | |
| 343 if (references == 0) | |
| 344 { | |
| 345 pthread_mutex_unlock(&g_sse_mutex); | |
| 346 return FALSE; | |
| 347 } | |
| 348 atomic_fetch_add(&p_stream->ref_count, 1); | |
| 349 pthread_mutex_unlock(&g_sse_mutex); | |
| 350 return TRUE; | |
| 351 } | |
| 352 | |
| 353 void Seobeo_SSE_Release(Seobeo_SSE_Stream *p_stream) | |
| 354 { | |
| 355 if (!p_stream) | |
| 356 return; | |
| 357 pthread_mutex_lock(&g_sse_mutex); | |
| 358 sse_release_unlocked(p_stream); | |
| 359 pthread_mutex_unlock(&g_sse_mutex); | |
| 360 } | |
| 361 | |
| 362 void Seobeo_SSE_Close(Seobeo_SSE_Stream *p_stream) | |
| 363 { | |
| 364 if (!p_stream) | |
| 365 return; | |
| 366 pthread_mutex_lock(&g_sse_mutex); | |
| 367 if (p_stream->started && !p_stream->closed && p_stream->p_handle) | |
| 368 { | |
| 369 sse_drain_pending_unlocked(p_stream); | |
| 370 if (p_stream->managed) | |
| 371 shutdown(p_stream->p_handle->socket, SHUT_RDWR); | |
| 372 } | |
| 373 sse_clear_pending_unlocked(p_stream); | |
| 374 p_stream->closed = TRUE; | |
| 375 pthread_mutex_unlock(&g_sse_mutex); | |
| 376 } | |
| 377 | |
| 378 Seobeo_SSE_Stream *Seobeo_SSE_Server_Attach( | |
| 379 Seobeo_Handle *p_handle, | |
| 380 const char *path) | |
| 381 { | |
| 382 if (!p_handle || !path) | |
| 383 return NULL; | |
| 384 Seobeo_SSE_Stream *p_stream = malloc(sizeof(*p_stream)); | |
| 385 if (!p_stream) | |
| 386 return NULL; | |
| 387 memset(p_stream, 0, sizeof(*p_stream)); | |
| 388 p_stream->path = strdup(path); | |
| 389 if (!p_stream->path) | |
| 390 { | |
| 391 Dowa_Free(p_stream); | |
| 392 return NULL; | |
| 393 } | |
| 394 atomic_init(&p_stream->ref_count, 1); | |
| 395 if (Seobeo_SSE_Start(p_stream, p_handle) < 0) | |
| 396 { | |
| 397 Dowa_Free(p_stream->path); | |
| 398 Dowa_Free(p_stream); | |
| 399 return NULL; | |
| 400 } | |
| 401 p_stream->managed = TRUE; | |
| 402 p_handle->is_sse = TRUE; | |
| 403 | |
| 404 pthread_mutex_lock(&g_sse_mutex); | |
| 405 Dowa_Array_Push(g_sse_streams, p_stream); | |
| 406 pthread_mutex_unlock(&g_sse_mutex); | |
| 407 return p_stream; | |
| 408 } | |
| 409 | |
| 410 void Seobeo_SSE_Server_Detach_Handle(Seobeo_Handle *p_handle) | |
| 411 { | |
| 412 if (!p_handle) | |
| 413 return; | |
| 414 p_handle->is_sse = FALSE; | |
| 415 pthread_mutex_lock(&g_sse_mutex); | |
| 416 size_t count = Dowa_Array_Length(g_sse_streams); | |
| 417 for (size_t i = 0; i < count; i++) | |
| 418 { | |
| 419 Seobeo_SSE_Stream *p_stream = g_sse_streams[i]; | |
| 420 if (p_stream->p_handle != p_handle) | |
| 421 continue; | |
| 422 p_stream->closed = TRUE; | |
| 423 p_stream->p_handle = NULL; | |
| 424 sse_clear_pending_unlocked(p_stream); | |
| 425 g_sse_streams[i] = Dowa_Array_Pop(g_sse_streams); | |
| 426 sse_release_unlocked(p_stream); | |
| 427 break; | |
| 428 } | |
| 429 pthread_mutex_unlock(&g_sse_mutex); | |
| 430 } | |
| 431 | |
| 432 void Seobeo_SSE_Server_Destroy(void) | |
| 433 { | |
| 434 pthread_mutex_lock(&g_sse_mutex); | |
| 435 size_t count = Dowa_Array_Length(g_sse_streams); | |
| 436 for (size_t i = 0; i < count; i++) | |
| 437 { | |
| 438 Seobeo_SSE_Stream *p_stream = g_sse_streams[i]; | |
| 439 if (!p_stream) | |
| 440 continue; | |
| 441 p_stream->closed = TRUE; | |
| 442 if (p_stream->p_handle) | |
| 443 p_stream->p_handle->is_sse = FALSE; | |
| 444 p_stream->p_handle = NULL; | |
| 445 sse_clear_pending_unlocked(p_stream); | |
| 446 sse_release_unlocked(p_stream); | |
| 447 } | |
| 448 Dowa_Array_Free(g_sse_streams); | |
| 449 pthread_mutex_unlock(&g_sse_mutex); | |
| 450 } |