comparison seobeo/s_network.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 117c4d53c9a4
children
comparison
equal deleted inserted replaced
256:30c2196d03d4 257:609d3c6aff4e
144 p_handle->write_buffer = malloc(sizeof(*p_handle->read_buffer) * INITIAL_BUFFER_CAPACITY); 144 p_handle->write_buffer = malloc(sizeof(*p_handle->read_buffer) * INITIAL_BUFFER_CAPACITY);
145 p_handle->write_buffer_capacity = INITIAL_BUFFER_CAPACITY; 145 p_handle->write_buffer_capacity = INITIAL_BUFFER_CAPACITY;
146 p_handle->write_buffer_len = 0; 146 p_handle->write_buffer_len = 0;
147 147
148 p_handle->destroyed = FALSE; 148 p_handle->destroyed = FALSE;
149 p_handle->is_sse = FALSE;
149 150
150 if (!p_handle->host || 151 if (!p_handle->host ||
151 !p_handle->port || 152 !p_handle->port ||
152 !p_handle->read_buffer || 153 !p_handle->read_buffer ||
153 !p_handle->write_buffer) 154 !p_handle->write_buffer)
222 p_handle->write_buffer = malloc(sizeof(*p_handle->read_buffer) * INITIAL_BUFFER_CAPACITY); 223 p_handle->write_buffer = malloc(sizeof(*p_handle->read_buffer) * INITIAL_BUFFER_CAPACITY);
223 p_handle->write_buffer_capacity = INITIAL_BUFFER_CAPACITY; 224 p_handle->write_buffer_capacity = INITIAL_BUFFER_CAPACITY;
224 p_handle->write_buffer_len = 0; 225 p_handle->write_buffer_len = 0;
225 226
226 p_handle->destroyed = FALSE; 227 p_handle->destroyed = FALSE;
228 p_handle->is_sse = FALSE;
227 229
228 return p_handle; 230 return p_handle;
229 } 231 }
230 232
231 Seobeo_Handle *Seobeo_Stream_Handle_Server_Accept(Seobeo_Handle *p_server_handle) 233 Seobeo_Handle *Seobeo_Stream_Handle_Server_Accept(Seobeo_Handle *p_server_handle)
275 p_client_handle->read_buffer_used = 0; 277 p_client_handle->read_buffer_used = 0;
276 p_client_handle->file = NULL; 278 p_client_handle->file = NULL;
277 p_client_handle->text_copy = NULL; 279 p_client_handle->text_copy = NULL;
278 p_client_handle->file_name = NULL; 280 p_client_handle->file_name = NULL;
279 p_client_handle->destroyed = FALSE; 281 p_client_handle->destroyed = FALSE;
282 p_client_handle->is_sse = FALSE;
280 283
281 return p_client_handle; 284 return p_client_handle;
282 } 285 }
283 286
284 void Seobeo_Handle_Destroy(Seobeo_Handle *p_handle) 287 void Seobeo_Handle_Destroy(Seobeo_Handle *p_handle)
290 if (!atomic_compare_exchange_strong(&p_handle->destroyed, &expected, TRUE)) 293 if (!atomic_compare_exchange_strong(&p_handle->destroyed, &expected, TRUE))
291 { 294 {
292 return; 295 return;
293 } 296 }
294 297
298 Seobeo_SSE_Server_Detach_Handle(p_handle);
299
295 if (p_handle->host) Dowa_Free(p_handle->host); 300 if (p_handle->host) Dowa_Free(p_handle->host);
296 if (p_handle->port) Dowa_Free(p_handle->port); 301 if (p_handle->port) Dowa_Free(p_handle->port);
297 302
298 Seobeo_SSL_Cleanup(p_handle); 303 Seobeo_SSL_Cleanup(p_handle);
299 304
309 } 314 }
310 315
311 316
312 int32 Seobeo_Handle_Flush(Seobeo_Handle *p_handle) 317 int32 Seobeo_Handle_Flush(Seobeo_Handle *p_handle)
313 { 318 {
319 if (!p_handle || !p_handle->write_buffer)
320 return -1;
314 uint32 total = p_handle->write_buffer_len; 321 uint32 total = p_handle->write_buffer_len;
315 uint32 sent = 0; 322 uint32 sent = 0;
316 323
317 Seobeo_Log(SEOBEO_DEBUG, "Write buffer total: %d\n", p_handle->write_buffer_len); 324 Seobeo_Log(SEOBEO_DEBUG, "Write buffer total: %d\n", p_handle->write_buffer_len);
318 325
319 while (sent < total) 326 while (sent < total)
320 { 327 {
321 if (p_handle->ssl) 328 if (p_handle->ssl)
322 { 329 {
323 int n = Seobeo_SSL_Write(p_handle, p_handle->write_buffer + sent, total - sent); 330 int n = Seobeo_SSL_Write(p_handle, p_handle->write_buffer + sent, total - sent);
324 if (n < 0) return -1; 331 if (n < 0)
325 if (n == 0) return 0; // would block 332 {
333 if (sent)
334 {
335 memmove(
336 p_handle->write_buffer,
337 p_handle->write_buffer + sent,
338 total - sent);
339 p_handle->write_buffer_len = total - sent;
340 }
341 return -1;
342 }
343 if (n == 0)
344 {
345 if (sent)
346 {
347 memmove(
348 p_handle->write_buffer,
349 p_handle->write_buffer + sent,
350 total - sent);
351 p_handle->write_buffer_len = total - sent;
352 }
353 return 1;
354 }
326 sent += (uint32)n; 355 sent += (uint32)n;
327 }else 356 }else
328 { 357 {
329 Seobeo_Log(SEOBEO_DEBUG, "Flushing socket: %d\n", p_handle->socket); 358 Seobeo_Log(SEOBEO_DEBUG, "Flushing socket: %d\n", p_handle->socket);
330 ssize_t n = Seobeo_Socket_Write( 359 ssize_t n = Seobeo_Socket_Write(
332 p_handle->write_buffer + sent, 361 p_handle->write_buffer + sent,
333 total - sent 362 total - sent
334 ); 363 );
335 if (n < 0) { 364 if (n < 0) {
336 if (errno == EINTR) continue; 365 if (errno == EINTR) continue;
337 if (errno == EAGAIN) return 1; 366 if (errno == EAGAIN || errno == EWOULDBLOCK)
367 {
368 if (sent)
369 {
370 memmove(
371 p_handle->write_buffer,
372 p_handle->write_buffer + sent,
373 total - sent);
374 p_handle->write_buffer_len = total - sent;
375 }
376 return 1;
377 }
378 if (sent)
379 {
380 memmove(
381 p_handle->write_buffer,
382 p_handle->write_buffer + sent,
383 total - sent);
384 p_handle->write_buffer_len = total - sent;
385 }
338 return -1; 386 return -1;
339 } 387 }
340 sent += (uint32)n; 388 sent += (uint32)n;
341 } 389 }
342 } 390 }