From baa781df4fa510e4ae43e4d445d00a033b85449b Mon Sep 17 00:00:00 2001 From: mallocnull Date: Wed, 18 Oct 2017 21:59:50 +0000 Subject: [PATCH] i might get four goobahs --- protocol | 44 +++++++++++++++++++++++++++++++++----------- src/flimit.c | 18 ++++++++++++++++++ src/flimit.h | 13 +++++++++++++ src/server/server.c | 14 +++++++++++--- src/server/server.h | 3 +++ src/sock.c | 4 ++-- 6 files changed, 80 insertions(+), 16 deletions(-) create mode 100644 src/flimit.c create mode 100644 src/flimit.h diff --git a/protocol b/protocol index 8d60279..371e9c6 100644 --- a/protocol +++ b/protocol @@ -3,28 +3,50 @@ PROTOCOL this is almost a carbon copy of every other tcp/ip protocol i write don't care enough to make it fancy +all numbers are packed unless stated otherwise packed values are msb -byte n is b.sub n +byte n is b.n +region n is r.n --- byte the zero: packet id byte the one: region count -for 0 <= n < b.sub 1 - byte the byte after the last byte byte: - region length < 128: one byte +for 0 <= n < b.1 + byte the byte after the previous byte byte: + region length < 128: one byte region length >= 128: two bytes, highest bit on msb always 1 -byte the bytes after the header octet bytes: +byte the bytes after the header octet bytes bytes: raw body data shoved next to each other with no separation --- - -server -> client - - ---- client -> server - \ No newline at end of file + id 0: registration attempt + r.0 - username - string + r.1 - pin - ushort (max 9999) + + id 1: login attempt + r.0 - username - string + r.1 - pin - ushort (max 9999) + + id 2: ctx change response + r.0 - ack - bool + r.1 - ctx id - ushort + +--- + +server -> client + id 0: registration response + r.0 - succeeded - bool + r.1 - status - string + + id 1: login response + r.0 - succeeded - bool + r.1 -> status - string (if r.0 false) + -> user id - ushort (if r.0 true) + + id 2: ctx change request + r.0 - ctx id - ushort \ No newline at end of file diff --git a/src/flimit.c b/src/flimit.c new file mode 100644 index 0000000..5a0c611 --- /dev/null +++ b/src/flimit.c @@ -0,0 +1,18 @@ +#include "flimit.h" + +void frame_limit_tick(flimit_t *fld) { + time(fld); +} + +void frame_limit_wait(flimit_t *fld, unsigned int fps) { + double fr = 1.0 / fps, dt, wait; + struct timespec swait; + + time_t now = time(NULL); + dt = difftime(now, *fld); + + if(dt < fr) { + wait = fr - dt; + usleep((unsigned int)(wait * 1000000)); + } +} \ No newline at end of file diff --git a/src/flimit.h b/src/flimit.h new file mode 100644 index 0000000..1c4aee4 --- /dev/null +++ b/src/flimit.h @@ -0,0 +1,13 @@ +#ifndef FLIMIT_H +#define FLIMIT_H + +#include +#include +#include + +typedef time_t flimit_t; + +void frame_limit_tick(flimit_t*); +void frame_limit_wait(flimit_t*, unsigned int); + +#endif \ No newline at end of file diff --git a/src/server/server.c b/src/server/server.c index 06562e1..b8222b9 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -1,12 +1,20 @@ #include "server.h" -void server() { +void server() { server_context_start(); + socket_t *sock = sock_server_init("6770"); sock_start(sock); - - sock_set_timeout_us(sock, 0, 100); + sock_set_nonblocking(sock); + flimit_t vsync; + for(;;) { + frame_limit_tick(&vsync); + + + + frame_limit_wait(&vsync, 1000); + } printf("awaiting connection...\r\n"); socket_t *conn = sock_accept(sock); diff --git a/src/server/server.h b/src/server/server.h index 21446e7..54d9341 100644 --- a/src/server/server.h +++ b/src/server/server.h @@ -4,6 +4,9 @@ #include #include "sock.h" #include "queue.h" +#include "flimit.h" + +#define MAX_CONNS 100 void server(); diff --git a/src/sock.c b/src/sock.c index 0bf46ca..dd35cdc 100644 --- a/src/sock.c +++ b/src/sock.c @@ -5,14 +5,14 @@ static socket_t* sock_init() { } socket_t* sock_client_init(char *addr, char *port) { - socket_t* sock = sock_init(); + socket_t *sock = sock_init(); sock->type = SOCK_TYPE_CLIENT; sock->port = port; sock->addr = addr; } socket_t* sock_server_init(char *port) { - socket_t* sock = sock_init(); + socket_t *sock = sock_init(); sock->type = SOCK_TYPE_SERVER; sock->port = port; }