From 8b5160375fbb1089331e35d9c32f34e883a6f273 Mon Sep 17 00:00:00 2001 From: NishiOwO Date: Sat, 20 Dec 2025 13:06:17 +0900 Subject: [PATCH] update --- client/gl.c | 12 ++++++++++ client/net.c | 21 ++++++++++++++-- external/libfishsoup | 2 +- include/af_client.h | 1 + include/af_common.h | 1 + server/net.c | 57 ++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 91 insertions(+), 3 deletions(-) diff --git a/client/gl.c b/client/gl.c index 6dadf51..5f56f95 100644 --- a/client/gl.c +++ b/client/gl.c @@ -13,6 +13,8 @@ gl_scene_t gl_scenes[] = { {NULL, NULL, NULL} /**/ }; +static char gl_status[256]; + void gl_resize(int width, int height) { glViewport(0, 0, width, height); } @@ -48,6 +50,8 @@ void gl_init(void) { glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); gl_font_init(); + + gl_status[0] = 0; } void gl_cube(double x, double y, double z, double xs, double ys, double zs, double xr, double yr, double zr) { @@ -91,6 +95,8 @@ static void gl_common(void) { sprintf(buf, "%s", AF_COPYRIGHT); gl_font_text(buf, MwGetInteger(opengl, MwNwidth) - gl_font_width(buf), MwGetInteger(opengl, MwNheight) - gl_font_height(buf), 1); + + gl_font_text(gl_status, 0, 0, 1); } void gl_render(void) { @@ -117,6 +123,12 @@ void gl_render(void) { void gl_scene_change(void) { if(gl_scenes[gl_scene].changed != NULL) gl_scenes[gl_scene].changed(); + + gl_status[0] = 0; +} + +void gl_set_status(const char* status) { + strcpy(gl_status, status); } static void cross(double* out, double v00, double v01, double v02, double v10, double v11, double v12) { diff --git a/client/net.c b/client/net.c index 3ee6b8d..9c8543d 100644 --- a/client/net.c +++ b/client/net.c @@ -4,7 +4,7 @@ static fishsoup_client_t* client = NULL; enum OPAQUE_STATES { OPAQUE_WAITING_HELLO = 0, - OPAQUE_SENT_JOIN + OPAQUE_JOINING }; typedef struct opaque { @@ -41,13 +41,28 @@ static void on_packet(fishsoup_client_t* self, fishsoup_packet_t* pkt) { fishsoup_packet_send(self->fd, &p); - o->state = OPAQUE_SENT_JOIN; + o->state = OPAQUE_JOINING; + + gl_set_status("Joining"); return; } af_common_log_info("Server version is incompatible with this client\n"); MwAddUserHandler(ui_message("Server version is incompatible with this client"), MwNactivateHandler, net_return, NULL); + fishsoup_client_destroy(self); + client = NULL; + } else if(pkt->header.type == AF_PACKET_OK && pkt->header.length == sizeof(af_packet_ok_t) && o->state == OPAQUE_JOINING) { + af_packet_ok_t* p_ok = pkt->data; + + af_common_log_info("OK Packet: Joined successfully\n"); + + gl_set_status("Downloading data"); + } else if(pkt->header.type == AF_PACKET_ERROR && pkt->header.length == sizeof(af_packet_error_t) && o->state == OPAQUE_JOINING) { + af_packet_error_t* p_error = pkt->data; + + af_common_log_info("ERROR Packet: Failed to join\n"); + fishsoup_client_destroy(self); client = NULL; } @@ -74,6 +89,8 @@ void net_connect(const char* username, const char* hostname, int port) { fishsoup_client_on_packet(client, on_packet); fishsoup_client_on_disconnect(client, on_disconnect); + + gl_set_status("Waiting for server to be ready"); } void net_poll(void) { diff --git a/external/libfishsoup b/external/libfishsoup index 6b9eb5d..3d41cf0 160000 --- a/external/libfishsoup +++ b/external/libfishsoup @@ -1 +1 @@ -Subproject commit 6b9eb5de20afd9899aec561d43791f9c7aaac534 +Subproject commit 3d41cf0afb5aa6b5a33da415a75b59b1b1bb82b0 diff --git a/include/af_client.h b/include/af_client.h index e95c306..237bdbf 100644 --- a/include/af_client.h +++ b/include/af_client.h @@ -40,6 +40,7 @@ void gl_resize(int width, int height); void gl_scene_change(void); void gl_calc_normal3(double* out, double v00, double v01, double v02, double v10, double v11, double v12, double v20, double v21, double v22); void gl_calc_normal4(double* out, double v00, double v01, double v02, double v10, double v11, double v12, double v20, double v21, double v22, double v30, double v31, double v32); +void gl_set_status(const char* status); /* gl_font.c */ void gl_font_init(void); diff --git a/include/af_common.h b/include/af_common.h index 3c9d5f1..9996739 100644 --- a/include/af_common.h +++ b/include/af_common.h @@ -5,6 +5,7 @@ #include #include #include +#include #include diff --git a/server/net.c b/server/net.c index 6c28f30..46a3633 100644 --- a/server/net.c +++ b/server/net.c @@ -1,5 +1,7 @@ #include +#include + static fishsoup_server_t* server; typedef struct opaque { @@ -11,11 +13,48 @@ static void on_packet(fishsoup_client_t* self, fishsoup_packet_t* pkt) { if(pkt->header.type == AF_PACKET_JOIN && pkt->header.length == sizeof(af_packet_join_t)) { af_packet_join_t* p_join = pkt->data; + int i; o->username[256] = 0; memcpy(o->username, p_join->username, 256); af_common_log_info("Client %d: JOIN packet: Username \"%s\"\n", self->fd, o->username); + + for(i = 0; i < arrlen(server->clients); i++) { + if(self->fd != server->clients[i].fd && strcmp(o->username, ((opaque_t*)server->clients[i].user)->username) == 0) { + break; + } + } + + if(i == arrlen(server->clients)) { + fishsoup_packet_t p; + af_packet_ok_t p_ok; + + p.header.type = AF_PACKET_OK; + p.header.length = sizeof(p_ok); + p.data = &p_ok; + + memset(p_ok.reason, 0, 256); + + fishsoup_packet_send(self->fd, &p); + + af_common_log_info("Client %d: JOIN packet: Username is unused\n", self->fd); + } else { + fishsoup_packet_t p; + af_packet_error_t p_error; + + p.header.type = AF_PACKET_ERROR; + p.header.length = sizeof(p_error); + p.data = &p_error; + + memset(p_error.reason, 0, 256); + strcpy(p_error.reason, "Username is already used"); + + fishsoup_packet_send(self->fd, &p); + + af_common_log_info("Client %d: JOIN packet: Username is already used\n", self->fd); + memset(o->username, 0, 256 + 1); + } } } @@ -47,16 +86,34 @@ static void on_client(fishsoup_server_t* self, fishsoup_client_t* client) { fishsoup_packet_send(client->fd, &pkt); } +static struct timespec c; +static void on_tick(fishsoup_server_t* self) { + struct timespec n; + unsigned long t1, t2, d; + + clock_gettime(CLOCK_MONOTONIC, &n); + + t1 = n.tv_sec * 1000 + n.tv_nsec / 1000000; + t2 = c.tv_sec * 1000 + c.tv_nsec / 1000000; + + d = t1 - t2; /* elapsed time */ + af_common_log_info("Tick: %dms\n", d); + + c = n; +} + void net_start(int port) { if(port == 0) port = AF_DEFAULT_PORT; if((server = fishsoup_server(port)) == NULL) return; fishsoup_server_on_client(server, on_client); + fishsoup_server_on_tick(server, on_tick); } void net_loop(void) { if(server == NULL) return; + clock_gettime(CLOCK_MONOTONIC, &c); fishsoup_server_loop(server); }