diff 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
line wrap: on
line diff
--- a/seobeo/s_network.c	Tue Aug 04 16:49:01 2026 -0700
+++ b/seobeo/s_network.c	Tue Aug 04 16:49:11 2026 -0700
@@ -146,6 +146,7 @@
   p_handle->write_buffer_len = 0;
 
   p_handle->destroyed = FALSE;
+  p_handle->is_sse = FALSE;
 
   if (!p_handle->host ||
       !p_handle->port ||
@@ -224,6 +225,7 @@
   p_handle->write_buffer_len = 0;
 
   p_handle->destroyed = FALSE;
+  p_handle->is_sse = FALSE;
 
   return p_handle;
 }
@@ -277,6 +279,7 @@
   p_client_handle->text_copy             = NULL;
   p_client_handle->file_name             = NULL;
   p_client_handle->destroyed             = FALSE;
+  p_client_handle->is_sse                = FALSE;
 
   return p_client_handle;
 }
@@ -292,6 +295,8 @@
     return;
   }
 
+  Seobeo_SSE_Server_Detach_Handle(p_handle);
+
   if (p_handle->host) Dowa_Free(p_handle->host);
   if (p_handle->port) Dowa_Free(p_handle->port);
 
@@ -311,6 +316,8 @@
 
 int32 Seobeo_Handle_Flush(Seobeo_Handle *p_handle)
 {
+  if (!p_handle || !p_handle->write_buffer)
+    return -1;
   uint32 total = p_handle->write_buffer_len;
   uint32 sent  = 0;
 
@@ -321,8 +328,30 @@
     if (p_handle->ssl)
     {
       int n = Seobeo_SSL_Write(p_handle, p_handle->write_buffer + sent, total - sent);
-      if (n < 0) return -1;
-      if (n == 0) return 0;  // would block
+      if (n < 0)
+      {
+        if (sent)
+        {
+          memmove(
+              p_handle->write_buffer,
+              p_handle->write_buffer + sent,
+              total - sent);
+          p_handle->write_buffer_len = total - sent;
+        }
+        return -1;
+      }
+      if (n == 0)
+      {
+        if (sent)
+        {
+          memmove(
+              p_handle->write_buffer,
+              p_handle->write_buffer + sent,
+              total - sent);
+          p_handle->write_buffer_len = total - sent;
+        }
+        return 1;
+      }
       sent += (uint32)n;
     }else
     {
@@ -334,7 +363,26 @@
       );
       if (n < 0) {
         if (errno == EINTR)  continue;
-        if (errno == EAGAIN) return 1;
+        if (errno == EAGAIN || errno == EWOULDBLOCK)
+        {
+          if (sent)
+          {
+            memmove(
+                p_handle->write_buffer,
+                p_handle->write_buffer + sent,
+                total - sent);
+            p_handle->write_buffer_len = total - sent;
+          }
+          return 1;
+        }
+        if (sent)
+        {
+          memmove(
+              p_handle->write_buffer,
+              p_handle->write_buffer + sent,
+              total - sent);
+          p_handle->write_buffer_len = total - sent;
+        }
         return -1;
       }
       sent += (uint32)n;