beginning drafting of tcp server

This commit is contained in:
2025-08-18 20:34:25 -05:00
parent 8b5fac0f3f
commit a9efa2dd52
9 changed files with 192 additions and 25 deletions

View File

@@ -80,7 +80,7 @@ void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
glm::vec4 k = camera_t[2];
float d = yoffset;
camera_loc += k * (d/10 * std::max(glm::length(camera_loc), 1.0f));
camera_loc += k * (d/10 * glm::max(glm::length(camera_loc), 1.0f));
camera_t = glm::lookAt(camera_loc, focal_point, up);
}
@@ -163,8 +163,8 @@ int main() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
set_uniform(shader, "camera_t", camera_t);
set_uniform(shader, "projection_t", projection_t);
draw_body(b);
draw_body(b);
draw_body(b2);
glfwSwapBuffers(window);

102
src/tcp_server.cpp Normal file
View File

@@ -0,0 +1,102 @@
#include <WS2tcpip.h>
#include <cstring>
#pragma comment(lib, "pthreadVC3d.lib")
// I hate windows. I hate windows. I hate windows.
#include <WinSock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <pthread.h>
#include "tcp_server.hpp"
static WSADATA wsadata;
static bool winsock_initialized = false; // First tcp server created will initialize this
static int init_result;
void _server_loop(void* args);
void _conn_loop(void* args);
void _start_conn_loop(SOCKET client_s);
bool create_server(tcpserver* server_out, const char* hostname, int port, int max_connections = 1) {
if (!winsock_initialized && (init_result = WSAStartup(MAKEWORD(2, 2), &wsadata)) != 0) {
printf("WSAStartup failed: %d\n", init_result);
winsock_initialized = true;
return false;
}
// ********** Addr Info Creation **********
addrinfo *result, *ptr, hints;
char port_str[16];
_itoa_s(port, port_str, 10);
// TODO: Ensure hostname is null-terminated
int status = getaddrinfo(hostname, port_str, &hints, result);
if (status != 0) {
printf("Error at getaddrinfo(): %d\n", status);
goto fail_cleanup;
}
// *********** Socket Creation ************
SOCKET sock = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (sock == INVALID_SOCKET) {
printf("Error at socket(): %d\n", WSAGetLastError());
freeaddrinfo(result);
goto fail_cleanup;
}
// *********** Socket Binding ************
status = bind(sock, result->ai_addr, (int)result->ai_addrlen);
if (status == SOCKET_ERROR) {
printf("Error at bind(): %d\n", WSAGetLastError());
freeaddrinfo(result);
goto fail_cleanup;
}
return true;
fail_cleanup:
WSACleanup();
winsock_initialized = false;
return false;
}
void start_server(tcpserver& s) { }
void stop_server(tcpserver& s) { }
void send_data(tcpserver& s, SOCKET client, array<char> data) { }
void register_cb(tcpserver& s, data_received_cb_t cb) { append(s.cbs, cb); }
void _server_loop(void* args) {
tcpserver* s = static_cast<tcpserver*>(args);
int num_connections = 0;
int slots[s->max_connections];
array<int> open_slots
= { .data = slots, .len = (size_t)s->max_connections, .cap = (size_t)s->max_connections };
for (int i = 0; i < open_slots.len; i++) {
open_slots[i] = open_slots.len - (i + 1);
}
SOCKET clients[s->max_connections];
memset(clients, INVALID_SOCKET, sizeof(SOCKET));
while (true) {
while (num_connections < s->max_connections) {
int slot = pop(open_slots);
clients[slot] = accept(s->sock, NULL, NULL);
// TODO: More robust handling of bad client sockets
if (clients[slot] == INVALID_SOCKET) {
printf("Error at accept(): %d\n", WSAGetLastError());
stop_server(*s);
return;
}
_start_conn_loop(clients[slot]);
}
}
}
void _start_conn_loop(SOCKET client_s) {
}
void _conn_loop(void* args) { }

View File

@@ -1,9 +1,33 @@
#include <cstdio>
#include <assert.h>
#include <cstring>
#include <stdlib.h>
#include "util.hpp"
template<class T> void append(array<T>& a, T el) {
if (a.len == a.cap) {
resize(a, a.cap*2);
}
a[a.len] = el;
a.len++;
}
template<class T> T pop(array<T>& a) {
assert(a.len >= 1);
a.len--;
return a.data[a.len+1];
}
template<class T> void resize(array<T> &a, size_t new_cap) {
T* new_data = (T*)malloc(new_cap);
memcpy(new_data, a.data, min(a.len, new_cap));
free(a.data);
a.len = min(a.len, new_cap);
a.cap = new_cap;
}
bool read_file(array<char>* out, const char* filepath) {
FILE* fp = NULL;
if (fopen_s(&fp, filepath, "rb") != 0) {
@@ -15,7 +39,7 @@ bool read_file(array<char>* out, const char* filepath) {
size_t sz = ftell(fp);
fseek(fp, 0, SEEK_SET);
char *data = (char*)malloc(sizeof(char)*sz);
char* data = (char*)malloc(sizeof(char) * sz);
fread(data, sizeof(char), sz, fp);
fclose(fp);
out->data = data;