From 66a153c5012f0237bdf67b79b5cc6b2f5f496879 Mon Sep 17 00:00:00 2001 From: Raymaekers Luca Date: Thu, 24 Oct 2024 23:14:18 +0200 Subject: Initial draft for v2 This is a rewrite of v1 using arena's as memory allocators. The other great difference is using wide strings by default (wchar_t) because that will be needed in the future to print pretty UIs. --- v2/common.h | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 v2/common.h (limited to 'v2/common.h') diff --git a/v2/common.h b/v2/common.h new file mode 100644 index 0000000..0f14329 --- /dev/null +++ b/v2/common.h @@ -0,0 +1,55 @@ +#ifndef COMMON_H +#define COMMON_H + +#include +#include +#include +#include +#include +#include +#include + +#define AUTHOR_LEN 13 +#define TIMESTAMP_LEN 9 +// port to listen on +#define PORT 9983 +// buffer size for holding data received from recv() +// TODO: choose a good size +#define STREAM_BUF 256 +// max data received in one recv() call on serverfd +// TODO: choose a good size +#define STREAM_LIMIT 512 +// max message that can be displayed with writef() +#define WRITEF_MAX 256 + + +typedef uint8_t u8; +typedef uint16_t u16; +typedef uint32_t u32; +typedef uint64_t u64; +typedef int8_t s8; +typedef int16_t s16; +typedef int32_t s32; +typedef int64_t s64; + +struct Message { + u8 author[AUTHOR_LEN]; + u8 timestamp[TIMESTAMP_LEN]; + // includes null terminator + u16 text_len; + wchar_t *text; +} typedef Message; + +#define MESSAGELEN(m) (AUTHOR_LEN + TIMESTAMP_LEN + sizeof(m.text_len)*sizeof(wchar_t) + m.text_len) +#define MESSAGELENP(m) (AUTHOR_LEN + TIMESTAMP_LEN + sizeof(m->text_len) + m->text_len*(sizeof(wchar_t))) + +void message_timestamp(u8 str[TIMESTAMP_LEN]) +{ + time_t now; + struct tm *ltime; + time(&now); + ltime = localtime(&now); + strftime((char *)str, TIMESTAMP_LEN, "%H:%M:%S", ltime); +} + +#endif -- cgit v1.2.3