# HG changeset patch # User June Park # Date 1758730280 25200 # Node ID adcfad6e86fb4a0b051513fc34ab026687c082b6 # Parent 5695ef413be07d5a0b6b3b7e83061d10666be71c Updated naming and separated out some logic within seobeo. diff -r 5695ef413be0 -r adcfad6e86fb dowa/BUILD --- /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"], +) diff -r 5695ef413be0 -r adcfad6e86fb dowa/d_memory.c --- /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); + } +} diff -r 5695ef413be0 -r adcfad6e86fb dowa/d_string.c --- /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; +} + diff -r 5695ef413be0 -r adcfad6e86fb dowa/dowa.h --- /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 +#include // 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 diff -r 5695ef413be0 -r adcfad6e86fb dowa/dowa_internal.h --- /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 diff -r 5695ef413be0 -r adcfad6e86fb hello_world/BUILD --- 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", - ] -) diff -r 5695ef413be0 -r adcfad6e86fb hello_world/main.c --- 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 -#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; -} diff -r 5695ef413be0 -r adcfad6e86fb helper/BUILD --- 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"], -) diff -r 5695ef413be0 -r adcfad6e86fb helper/helper.c --- 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; -} diff -r 5695ef413be0 -r adcfad6e86fb helper/helper.h --- 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 // 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 diff -r 5695ef413be0 -r adcfad6e86fb helper/helper_internal.h --- 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 diff -r 5695ef413be0 -r adcfad6e86fb playground/BUILD --- /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 = [], +) diff -r 5695ef413be0 -r adcfad6e86fb playground/main Binary file playground/main has changed diff -r 5695ef413be0 -r adcfad6e86fb playground/main.c --- /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 +#include +#include +#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); +} diff -r 5695ef413be0 -r adcfad6e86fb raylib_examples/BUILD --- /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", + ] +) diff -r 5695ef413be0 -r adcfad6e86fb raylib_examples/main.c --- /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 +#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; +} diff -r 5695ef413be0 -r adcfad6e86fb seobeo/BUILD --- 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", + ], +) diff -r 5695ef413be0 -r adcfad6e86fb seobeo/include/server.h --- 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 diff -r 5695ef413be0 -r adcfad6e86fb seobeo/index.html --- /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 @@ + + + + +

June

+ + diff -r 5695ef413be0 -r adcfad6e86fb seobeo/main.c --- 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 -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#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); } diff -r 5695ef413be0 -r adcfad6e86fb seobeo/s_linux_network.c --- /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; +} + diff -r 5695ef413be0 -r adcfad6e86fb seobeo/seobeo.h --- /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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "dowa/dowa.h" + + +extern int Seobeo_CreateSocket(int32 stream, char* port, int32 backlog); + +#endif diff -r 5695ef413be0 -r adcfad6e86fb seobeo/seobeo_internal.h diff -r 5695ef413be0 -r adcfad6e86fb seobeo/src/server.c --- 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); - } -}