changeset 1:adcfad6e86fb

Updated naming and separated out some logic within seobeo.
author June Park <parkjune1995@gmail.com>
date Wed, 24 Sep 2025 09:11:20 -0700
parents 5695ef413be0
children 8a43dedbe530
files dowa/BUILD dowa/d_memory.c dowa/d_string.c dowa/dowa.h dowa/dowa_internal.h hello_world/BUILD hello_world/main.c helper/BUILD helper/helper.c helper/helper.h helper/helper_internal.h playground/BUILD playground/main playground/main.c raylib_examples/BUILD raylib_examples/main.c seobeo/BUILD seobeo/include/server.h seobeo/index.html seobeo/main.c seobeo/s_linux_network.c seobeo/seobeo.h seobeo/seobeo_internal.h seobeo/src/server.c
diffstat 23 files changed, 368 insertions(+), 203 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dowa/BUILD	Wed Sep 24 09:11:20 2025 -0700
@@ -0,0 +1,14 @@
+load("@rules_cc//cc:cc_library.bzl", "cc_library")
+
+cc_library(
+  name = "dowa",
+    srcs = [
+      "d_string.c",
+      "d_memory.c",
+    ],
+  hdrs = [
+    "dowa.h",
+    "dowa_internal.h"
+  ],
+  visibility = ["//visibility:public"],
+)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dowa/d_memory.c	Wed Sep 24 09:11:20 2025 -0700
@@ -0,0 +1,28 @@
+#include "dowa.h"
+
+// --- Arena --- //
+void Dowa_ArenaIntialize(PArena p_arena, size_t capacity)
+{
+  p_arena = malloc(capacity);
+  p_arena->offset = 0;
+  p_arena->capacity = capacity;
+}
+
+void *Dowa_ArenaAllocate(PArena p_arena, size_t size)
+{
+  if (p_arena->offset + size > p_arena->capacity)
+  {
+    return NULL;
+  }
+  void *currnet_ptr = p_arena->buffer + p_arena->offset;
+  p_arena->offset += size;
+  return currnet_ptr;
+}
+
+void Dowa_ArenaFree(PArena p_arena)
+{
+  if (p_arena) {
+    free(p_arena->buffer);
+    free(p_arena);
+  }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dowa/d_string.c	Wed Sep 24 09:11:20 2025 -0700
@@ -0,0 +1,8 @@
+#include "dowa.h"
+
+char *Dowa_Int32ToString(uint32 int32, char *buffer)
+{
+  sprintf(buffer, "%d", int32);
+  return buffer;
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dowa/dowa.h	Wed Sep 24 09:11:20 2025 -0700
@@ -0,0 +1,30 @@
+#ifndef DOWA
+#define DOWA
+
+#include <stdio.h>
+#include <stdlib.h> // only for malloc, free, stuff
+#include "dowa_internal.h"
+
+typedef unsigned int uint32;
+typedef int int32;
+typedef unsigned short uint16;
+typedef short int16;
+typedef unsigned char uint8;
+typedef char int8;
+typedef char boolean;
+
+// --- Misc --- //
+char *Dowa_Int32ToString(uint32 int32, char *buffer);
+
+// --- Arena --- //
+typedef struct {
+  char   *buffer;
+  size_t offset;
+  size_t capacity;
+} Areana, *PArena;
+
+void  Dowa_ArenaIntialize(PArena p_arena, size_t capacity);
+void *Dowa_ArenaAllocate(PArena p_arena, size_t size);
+void Dowa_ArenaFree(PArena p_arena);
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dowa/dowa_internal.h	Wed Sep 24 09:11:20 2025 -0700
@@ -0,0 +1,4 @@
+#ifndef JUNE_HELPER_INTERNAL
+#define JUNE_HELPER_INTERNAL
+
+#endif
--- a/hello_world/BUILD	Tue Sep 23 10:05:25 2025 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,20 +0,0 @@
-load("@rules_cc//cc:cc_binary.bzl", "cc_binary")
-
-alias(
-  name = "hello_world",
-  actual = select({
-    "@platforms//os:osx": ":hello_world_osx",
-  })
-)
-
-cc_binary(
-  name = "hello_world_osx",
-  srcs = ["main.c"],
-  deps = [
-    "//helper:helper",
-    "//third_party/raylib/raylib-5.5_macos:raylib_mac_os",
-  ],
-  linkopts = [
-    "-framework CoreVideo -framework IOKit -framework Cocoa -framework GLUT -framework OpenGL",
-  ]
-)
--- a/hello_world/main.c	Tue Sep 23 10:05:25 2025 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,17 +0,0 @@
-#include <stdio.h>
-#include "helper/helper.h"
-#include "third_party/raylib/raylib-5.5_macos/include/raylib.h" 
-
-int main()
-{
-  InitWindow(800, 450, "raylib [core] example - basic window");
-  while (!WindowShouldClose())
-  {
-      BeginDrawing();
-          ClearBackground(RAYWHITE);
-          DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY);
-      EndDrawing();
-  }
-  CloseWindow();
-  return 0;
-}
--- a/helper/BUILD	Tue Sep 23 10:05:25 2025 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,13 +0,0 @@
-load("@rules_cc//cc:cc_library.bzl", "cc_library")
-
-cc_library(
-  name = "helper",
-    srcs = [
-        "helper.c",
-    ],
-  hdrs = [
-    "helper.h",
-    "helper_internal.h"
-  ],
-  visibility = ["//visibility:public"],
-)
--- a/helper/helper.c	Tue Sep 23 10:05:25 2025 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,28 +0,0 @@
-#include "helper.h"
-
-void ArenaIntialize(PArena p_arena, size_t capacity)
-{
-  p_arena = malloc(capacity);
-  p_arena->offset = 0;
-  p_arena->capacity = capacity;
-}
-
-void *ArenaAllocate(PArena p_arena, size_t size)
-{
-  if (p_arena->offset + size > p_arena->capacity)
-  {
-    return NULL;
-  }
-  void *currnet_ptr = p_arena->buffer + p_arena->offset;
-  p_arena->offset += size;
-  return currnet_ptr;
-}
-
-void *ArenaFree(PArena p_arena)
-{
-  if (p_arena) {
-    free(p_arena->buffer);
-    free(p_arena);
-  }
-  return;
-}
--- a/helper/helper.h	Tue Sep 23 10:05:25 2025 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,17 +0,0 @@
-#ifndef JUNE_HELPER
-#define JUNE_HELPER 
-
-#include <stdlib.h> // only for malloc, free, stuff
-#include "./helper_internal.h"
-
-typedef struct {
-  char   *buffer;
-  size_t offset;
-  size_t capacity;
-} Areana, *PArena;
-
-void ArenaIntialize(PArena p_arena, size_t capacity);
-void *ArenaAllocate(PArena p_arena, size_t size);
-void *ArenaFree(PArena p_arena);
-
-#endif
--- a/helper/helper_internal.h	Tue Sep 23 10:05:25 2025 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,4 +0,0 @@
-#ifndef JUNE_HELPER_INTERNAL
-#define JUNE_HELPER_INTERNAL
-
-#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/playground/BUILD	Wed Sep 24 09:11:20 2025 -0700
@@ -0,0 +1,14 @@
+load("@rules_cc//cc:cc_binary.bzl", "cc_binary")
+
+alias(
+  name = "playground",
+  actual = select({
+    "@platforms//os:osx": ":playground_osx",
+  })
+)
+
+cc_binary(
+  name = "playground_osx",
+  srcs = ["main.c"],
+  deps = [],
+)
Binary file playground/main has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/playground/main.c	Wed Sep 24 09:11:20 2025 -0700
@@ -0,0 +1,32 @@
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#define NUM_THREADS     5
+
+void *PrintHello(void *threadid)
+{
+   long tid;
+   tid = (long)threadid;
+   printf("Hello World! It's me, thread #%ld!\n", tid);
+   pthread_exit(NULL);
+}
+
+int main (int argc, char *argv[])
+{
+   pthread_t threads[NUM_THREADS];
+   int rc;
+   long t;
+   for(t = 0; t < NUM_THREADS; t++)
+   {
+      printf("In main: creating thread %ld\n", t);
+      rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
+      if (rc)
+      {
+         printf("ERROR; return code from pthread_create() is %d\n", rc);
+         exit(-1);
+      }
+   }
+
+   /* Last thing that main() should do */
+   pthread_exit(NULL);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/raylib_examples/BUILD	Wed Sep 24 09:11:20 2025 -0700
@@ -0,0 +1,20 @@
+load("@rules_cc//cc:cc_binary.bzl", "cc_binary")
+
+alias(
+  name = "hello_world",
+  actual = select({
+    "@platforms//os:osx": ":hello_world_osx",
+  })
+)
+
+cc_binary(
+  name = "hello_world_osx",
+  srcs = ["main.c"],
+  deps = [
+    "//helper:helper",
+    "//third_party/raylib/raylib-5.5_macos:raylib_mac_os",
+  ],
+  linkopts = [
+    "-framework CoreVideo -framework IOKit -framework Cocoa -framework GLUT -framework OpenGL",
+  ]
+)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/raylib_examples/main.c	Wed Sep 24 09:11:20 2025 -0700
@@ -0,0 +1,17 @@
+#include <stdio.h>
+#include "helper/helper.h"
+#include "third_party/raylib/raylib-5.5_macos/include/raylib.h" 
+
+int main()
+{
+  InitWindow(800, 450, "raylib [core] example - basic window");
+  while (!WindowShouldClose())
+  {
+      BeginDrawing();
+          ClearBackground(RAYWHITE);
+          DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY);
+      EndDrawing();
+  }
+  CloseWindow();
+  return 0;
+}
--- a/seobeo/BUILD	Tue Sep 23 10:05:25 2025 -0700
+++ b/seobeo/BUILD	Wed Sep 24 09:11:20 2025 -0700
@@ -1,17 +1,36 @@
 load("@rules_cc//cc:cc_binary.bzl", "cc_binary")
+load("@rules_cc//cc:cc_library.bzl", "cc_library")
 
 alias(
   name = "seobeo",
   actual = select({
-    "@platforms//os:osx": ":seobeo_mac",
+    "@platforms//os:osx": ":seobeo_example_mac",
   })
 )
 
 cc_binary(
-  name = "seobeo_mac",
+  name = "seobeo_example_mac",
   srcs = ["main.c"],
-  deps = [],
+  deps = [":seobeo_non_window"],
+  data = [
+    "index.html"
+  ],
   target_compatible_with = [
       "@platforms//os:osx",
   ],
 )
+
+cc_library(
+  name = "seobeo_non_window",
+  srcs = [
+    "s_linux_network.c",
+  ],
+  deps = ["//dowa:dowa"],
+  hdrs = [
+    "seobeo.h",
+    "seobeo_internal.h"
+  ],
+  target_compatible_with = [
+      "@platforms//os:osx",
+  ],
+)
--- a/seobeo/include/server.h	Tue Sep 23 10:05:25 2025 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,7 +0,0 @@
-#ifndef SEOBEO_SERVER_H
-#define SEOBEO_SERVER_H
-
-void CreateSocket(int *server_fd);
-
-
-#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/seobeo/index.html	Wed Sep 24 09:11:20 2025 -0700
@@ -0,0 +1,7 @@
+<HTML>
+  <head>
+  </head>
+  <body>
+    <h1> June </h1>
+  </body>
+</HTML>
--- a/seobeo/main.c	Tue Sep 23 10:05:25 2025 -0700
+++ b/seobeo/main.c	Wed Sep 24 09:11:20 2025 -0700
@@ -2,22 +2,7 @@
 ** server.c -- a stream socket server demo
 */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <errno.h>
-#include <string.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <netdb.h>
-#include <arpa/inet.h>
-#include <sys/wait.h>
-#include <signal.h>
-
-#define PORT "6969" 
-
-#define BACKLOG 10   // how many pending connections queue will hold
+#include "seobeo/seobeo.h"
 
 void SigchildHandler(int s)
 {
@@ -42,12 +27,48 @@
   return &(((struct sockaddr_in6*)sa)->sin6_addr);
 }
 
+void HandleClientRequest(int client_fd)
+{
+  FILE *file = fopen("seobeo/index.html", "rb");
+  if (!file) {
+    perror("fopen");
+    return;
+  }
+
+  fseek(file, 0, SEEK_END);
+  size_t size = ftell(file);
+  fseek(file, 0, SEEK_SET);
+
+  char *data = malloc(size);
+  fread(data, 1, size, file);
+  fclose(file);
+
+  char *header = malloc(100);
+  sprintf(
+    header,
+    "HTTP/1.1 200 OK\r\n"
+    "Content-Type: text/html\r\n"
+    "Content-Length: %zu\r\n"
+    "Connection: close\r\n"
+    "\r\n",
+    size
+  );
+
+  send(client_fd, header, strlen(header), 0);
+  ssize_t total_sent = 0;
+  while (total_sent < size)
+  {
+    ssize_t sent = send(client_fd, data + total_sent, size - total_sent, 0);
+    if (sent <= 0) break;
+    total_sent += sent;
+  }
+
+  close(client_fd);
+}
+
 int main(void)
 {
-  int sock_fd, client_fd;
-  struct addrinfo hints, *server_infos, *free_server_info;
-  int yes = 1; // boolean
-
+  int32 client_fd;
   struct sockaddr_storage client_addr; 
   socklen_t sin_size;
 
@@ -55,56 +76,6 @@
 
   struct sigaction sa;
 
-  memset(&hints, 0, sizeof(hints));
-  hints.ai_family = AF_INET;
-  hints.ai_socktype = SOCK_STREAM;
-  hints.ai_flags = AI_PASSIVE;
-
-  if (getaddrinfo(NULL, PORT, &hints, &server_infos) == 1)
-  {
-    perror("getaddrinfo");
-    return 1;
-  }
-
-  for (free_server_info = server_infos; free_server_info != NULL; free_server_info = free_server_info->ai_next)
-  {
-    if ((sock_fd = socket(free_server_info->ai_family, free_server_info->ai_socktype, free_server_info->ai_protocol)) == -1)
-    {
-      perror("socket");
-      continue;
-    }
-    
-     if (setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) == -1)
-     {
-       perror("setsockopt");
-       continue;
-     }
-
-     if (bind(sock_fd, free_server_info->ai_addr, free_server_info->ai_addrlen) == -1)
-     {
-       close(sock_fd);
-       perror("setsockopt");
-       continue;
-     }
-
-     // binded to a open server infos;
-     break;
-  }
-
-  freeaddrinfo(server_infos);
-
-  if (free_server_info == NULL)
-  {
-    perror("No free server");
-    return 1;
-  }
-
-  if (listen(sock_fd, BACKLOG) != 0)
-  {
-    perror("listen");
-    return 1;
-  }
-
   sa.sa_handler = SigchildHandler; // reap all dead processes
   sigemptyset(&sa.sa_mask);
   sa.sa_flags = SA_RESTART;
@@ -113,15 +84,9 @@
     exit(1);
   }
   
-  printf("server: waiting for connections...\n");
+  int sock_fd = Seobeo_CreateSocket(1, "6969", 10);
 
-  char *response = 
-    "HTTP/1.1 OK %s\r\n"
-    "Content-Type: text/txt\r\n"
-    "Content-Length: 13\r\n"
-    "Connection: close\r\n"
-    "\r\n"
-    "Hello, world!";
+  printf("server: waiting for connections...\n");
 
   while (1) 
   {
@@ -131,7 +96,7 @@
     if (client_fd == -1)
     {
       perror("accept");
-      continue;
+      break;
     }
 
     inet_ntop(
@@ -145,12 +110,7 @@
     if (!fork())
     {
       close(sock_fd);
-      if (send(client_fd, response, strlen(response), 0) == -1)
-      {
-        perror("send");
-        continue; 
-      }
-      close(client_fd);
+      HandleClientRequest(client_fd);
       exit(0);
     }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/seobeo/s_linux_network.c	Wed Sep 24 09:11:20 2025 -0700
@@ -0,0 +1,100 @@
+#include "seobeo/seobeo.h"
+
+int Seobeo_CreateSocket(int32 stream, char* port, int32 backlog)
+{
+  int32 sock_fd;
+  struct addrinfo hints, *server_infos, *free_server_info;
+  int32 yes = 1; // Need this for setsockopt 
+
+  memset(&hints, 0, sizeof(hints));
+  if (stream)
+  {
+    hints.ai_family = AF_INET;
+    hints.ai_socktype = SOCK_STREAM;
+    hints.ai_protocol = IPPROTO_TCP; 
+    hints.ai_flags = AI_PASSIVE;
+  }
+  else
+  {
+    hints.ai_family = AF_INET;
+    hints.ai_socktype = SOCK_DGRAM;
+    hints.ai_protocol = IPPROTO_UDP; 
+    hints.ai_flags = AI_PASSIVE;
+  }
+
+
+  if (getaddrinfo(NULL, port, &hints, &server_infos) != 0)
+  {
+    perror("getaddrinfo");
+    return -1;
+  }
+
+  for
+  (
+    free_server_info = server_infos;
+    free_server_info != NULL;
+    free_server_info = free_server_info->ai_next
+  )
+  {
+    if
+    (
+      (sock_fd = socket(
+        free_server_info->ai_family, free_server_info->ai_socktype,
+        free_server_info->ai_protocol
+      )) == -1
+    )
+    {
+      perror("socket");
+      continue;
+    }
+    
+     if (setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) == -1)
+     {
+       perror("setsockopt");
+       continue;
+     }
+
+     if (bind(sock_fd, free_server_info->ai_addr, free_server_info->ai_addrlen) == -1)
+     {
+       close(sock_fd);
+       perror("setsockopt");
+       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;
+  }
+
+  // No longer need these values
+  freeaddrinfo(server_infos);
+
+  if (free_server_info == NULL)
+  {
+    perror("No free server");
+    return -1;
+  }
+
+  // TCP listen
+	if(stream)
+  {
+    if (listen(sock_fd, backlog) != 0)
+    {
+      perror("listen");
+      return -1;
+    }
+  }
+
+  return sock_fd;
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/seobeo/seobeo.h	Wed Sep 24 09:11:20 2025 -0700
@@ -0,0 +1,27 @@
+#ifndef SEOBEO_SERVER_H
+#define SEOBEO_SERVER_H
+
+// --- Seobeo --- //
+// Library for creating sockets, binding sockets, and listening to a sockets.
+// Sending and unpacking bytes of data. 
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netdb.h>
+#include <arpa/inet.h>
+#include <sys/wait.h>
+#include <signal.h>
+#include <fcntl.h>
+
+#include "dowa/dowa.h"
+
+
+extern int Seobeo_CreateSocket(int32 stream, char* port, int32 backlog);
+
+#endif
--- a/seobeo/src/server.c	Tue Sep 23 10:05:25 2025 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,9 +0,0 @@
-void CreateSocket(int *server_fd)
-{
-  *server_fd = socket(PF_INET, SOCK_STREAM, 0);
-  if (*server_fd == -1)
-  {
-    perror("socket failed");
-    exit(EXIT_FAILURE);
-  }
-}