diff seobeo/s_linux_network.c @ 17:d97ec3ded2ae

[Seobeo] Few changes... - Fixed seobeo edge for macos - Updated so that socket creation can be used for both client and server - Started on a cutelient library for making connection to the server.
author June Park <parkjune1995@gmail.com>
date Sat, 04 Oct 2025 07:53:12 -0700
parents fb2cff495a60
children fa2b8af609d9
line wrap: on
line diff
--- a/seobeo/s_linux_network.c	Fri Oct 03 09:55:51 2025 -0700
+++ b/seobeo/s_linux_network.c	Sat Oct 04 07:53:12 2025 -0700
@@ -1,6 +1,6 @@
 #include "seobeo/seobeo.h"
 
-int Seobeo_CreateSocket(int32 stream, const char *host,  const char* port, int32 backlog)
+int Seobeo_CreateSocket(int32 stream, const char *host,  const char* port)
 {
   struct addrinfo hints = {0}, *server_infos, *free_server_info;
   int32 sock_fd, yes = 1;  // Need this for setsockopt 
@@ -41,24 +41,34 @@
        continue;
      }
 
-     if (bind(sock_fd, free_server_info->ai_addr, free_server_info->ai_addrlen) == -1)
+     if (host != NULL)
      {
-       close(sock_fd);
-       perror("setsockopt");
-       continue;
-     }
+       if (connect(sock_fd, free_server_info->ai_addr, free_server_info->ai_addrlen) == -1)
+       {
+         perror("connect");
+         continue;
+       }
 
-     // UDP should be non blocking
-     if(!stream)
+     }
+     else
      {
-       if (fcntl(sock_fd, F_SETFL, O_NONBLOCK) != 0)
+       if (bind(sock_fd, free_server_info->ai_addr, free_server_info->ai_addrlen) == -1)
        {
-         close(sock_fd);
          perror("v_network: Couldn't make socket non-blocking\n");
-         return -1;
+         continue;
+       }
+
+       // UDP should be non blocking
+       if(!stream)
+       {
+         if (fcntl(sock_fd, F_SETFL, O_NONBLOCK) != 0)
+         {
+           close(sock_fd);
+           perror("v_network: Couldn't make socket non-blocking\n");
+           return -1;
+         }
        }
      }
-
      // binded to a open server infos;
      break;
   }
@@ -72,10 +82,9 @@
     return -1;
   }
 
-  // TCP listen
-  if(stream)
+  if (host == NULL)
   {
-    if (listen(sock_fd, backlog) != 0)
+    if (listen(sock_fd, 16) != 0)
     {
       perror("listen");
       close(sock_fd);
@@ -91,7 +100,11 @@
   Seobeo_PHandle p_handle;
   p_handle = malloc(sizeof(*p_handle));
 
-  p_handle->socket = Seobeo_CreateSocket(1, host,  port, 10); // socke fd
+  p_handle->socket = Seobeo_CreateSocket(1, host,  port); // socke fd
+  if (!p_handle->socket)
+  {
+    perror("Seobeo_CreateSocket");
+  }
   p_handle->host = host != NULL ? strdup(host) : "localhost";
   p_handle->port = strdup(port);
 
@@ -192,7 +205,7 @@
   return 0;
 }
 
-int Seobeo_Handle_Queue(Seobeo_PHandle p_handle, const uint8_t *data, uint32_t data_size)
+int Seobeo_Handle_Queue(Seobeo_PHandle p_handle, const uint8 *data, uint32 data_size)
 {
   if (p_handle->write_buffer_len + data_size > p_handle->write_buffer_capacity)
   {
@@ -203,7 +216,7 @@
 
   if (data_size > p_handle->write_buffer_capacity)
   {
-    uint32_t offset = 0;
+    uint32 offset = 0;
     while (offset < data_size)
     {
       ssize_t n = write(p_handle->socket,
@@ -215,7 +228,7 @@
         if (errno == EAGAIN) return 1;
         return -1;
       }
-      offset += (uint32_t)n;
+      offset += (uint32)n;
     }
     return 0;
   }