diff seobeo/s_linux_network.c @ 36:84672efec192

[Zenbu] WIP fixing issues regarding to using edge only. I think there is a problem where socket closes before sending back the info.
author MrJuneJune <me@mrjunejune.com>
date Sun, 09 Nov 2025 06:25:16 -0800
parents c0f6c8c7829f
children ea9ef388ab97
line wrap: on
line diff
--- a/seobeo/s_linux_network.c	Thu Oct 30 09:53:22 2025 -0700
+++ b/seobeo/s_linux_network.c	Sun Nov 09 06:25:16 2025 -0800
@@ -37,9 +37,13 @@
   }
 
   if (listen(socket_fd, 16) != 0)
-  { perror("listen"); close(socket_fd); return NULL; }
+  { 
+    printf("Closing: %d", socket_fd);
+    perror("listen"); close(socket_fd); return NULL; 
+  }
 
-	if(fcntl(socket_fd, F_SETFL, O_NONBLOCK) != 0) { perror("fcntl"); return NULL; }
+  int flags = fcntl(socket_fd, F_GETFL, 0);
+	if(fcntl(socket_fd, F_SETFL, flags | O_NONBLOCK) != 0) { perror("fcntl"); return NULL; }
   freeaddrinfo(server_infos);
 
   p_handle = malloc(sizeof(*p_handle));
@@ -62,9 +66,6 @@
   p_handle->write_buffer_capacity = INITIAL_BUFFER_CAPACITY;
   p_handle->write_buffer_len = 0;
 
-  p_handle->file = NULL;
-  p_handle->text_copy = NULL;
-  p_handle->file_name = NULL;
   p_handle->destroyed = false;
 
   return p_handle;
@@ -135,9 +136,6 @@
   p_handle->write_buffer_capacity = INITIAL_BUFFER_CAPACITY;
   p_handle->write_buffer_len = 0;
 
-  p_handle->file = NULL;
-  p_handle->text_copy = NULL;
-  p_handle->file_name = NULL;
   p_handle->destroyed = false;
 
   return p_handle;
@@ -194,14 +192,14 @@
   if (!p_handle) return;
 
   bool expected = false;
+  // Need to check
   if (!atomic_compare_exchange_strong(&p_handle->destroyed, &expected, true))
   {
-    // Already destroyed by another thread
     return;
   }
 
-  if (p_handle->host) free(p_handle->host);
-  if (p_handle->port) free(p_handle->port);
+  if (p_handle->host) Dowa_Free(p_handle->host);
+  if (p_handle->port) Dowa_Free(p_handle->port);
 
   if (p_handle->ssl)
   {
@@ -216,15 +214,15 @@
     p_handle->ssl_ctx = NULL;
   }
 
-  if (p_handle->socket) close(p_handle->socket);
-
-  if (p_handle->read_buffer) free(p_handle->read_buffer);
-  if (p_handle->write_buffer) free(p_handle->write_buffer);
+  if (p_handle->socket) {
+    printf("Closing: %d", p_handle->socket);
+    close(p_handle->socket);
+  }
 
-  if (p_handle->text_copy) free(p_handle->text_copy);
-  if (p_handle->file_name) free(p_handle->file_name);
+  if (p_handle->read_buffer) Dowa_Free(p_handle->read_buffer);
+  if (p_handle->write_buffer) Dowa_Free(p_handle->write_buffer);
 
-  free(p_handle);
+  Dowa_Free(p_handle);
 }
 
 
@@ -254,6 +252,7 @@
       sent += (uint32)n;
     }else
     {
+      printf("socket: %d\n", p_handle->socket);
       ssize_t n = write(
         p_handle->socket,
         p_handle->write_buffer + sent,
@@ -315,12 +314,28 @@
     return 0;
   }
 
+  if (!p_handle) {
+    printf("[ERROR] p_handle is NULL before memcpy\n");
+    return;
+  }
+  
+  if (!p_handle->write_buffer) {
+    printf("[ERROR] p_handle->write_buffer is NULL (len=%zu, size=%zu)\n",
+            p_handle->write_buffer_len, data_size);
+    return;
+  }
+  
+  printf("[DEBUG] memcpy -> dest=%p (write_buffer=%p + offset=%zu), src=%p, size=%zu\n",
+          p_handle->write_buffer + p_handle->write_buffer_len,
+          p_handle->write_buffer,
+          p_handle->write_buffer_len,
+          data,
+          data_size); 
+
   memcpy(p_handle->write_buffer + p_handle->write_buffer_len,
          data,
          data_size);
   p_handle->write_buffer_len += data_size;
-  // DEBUG
-  printf("\nheader data_size: %d\n\n", data_size);
   return 0;
 }