improving sockets, fixed some bugs

This commit is contained in:
Daniel Ledda
2025-11-28 15:29:24 +01:00
parent 9b772e2046
commit 45d3f28546
5 changed files with 225 additions and 81 deletions

50
os.h
View File

@@ -4,18 +4,19 @@
#include "core.h"
// ### Memory ###
void *os_alloc(size_t capacity);
void *os_alloc(uint64 capacity);
void os_reserve(void *ptr);
void os_decommit(void *ptr);
void os_free(void *ptr, size_t freeSize);
void os_free(void *ptr, uint64 freeSize);
// ### File IO ###
string os_readEntireFile(Arena *arena, string filename);
bool os_writeEntireFile(Arena *arena, string filename, const byte *contents, size_t contentsLength);
bool os_fileAppend(Arena *arena, string filename, const byte *contents, size_t contentsLength);
bool os_writeEntireFile(Arena *arena, string filename, const byte *contents, uint64 contentsLength);
bool os_fileAppend(Arena *arena, string filename, const byte *contents, uint64 contentsLength);
// ### Standard IO ###
void os_print(StdStream target, const char *fmt, va_list argList);
void os_println(StdStream target, const char *fmt, va_list argList);
// ### Multithreading ###
typedef struct OS_Thread OS_Thread;
@@ -29,23 +30,24 @@ OS_Thread os_createThread(void *(*entry)(void *ctx), void *ctx);
typedef struct Address Address;
struct Address;
typedef struct Socket Socket;
struct Socket;
typedef struct SocketHandle SocketHandle;
struct SocketHandle;
typedef struct Client Client;
struct Client {
Address *clientAddressData;
Socket *socket;
typedef struct Socket Socket;
struct Socket {
const Address *address;
SocketHandle *handle;
bool closed;
};
DefineList(Client, Client);
DefineList(Socket, Socket);
typedef struct Server Server;
struct Server {
Arena *arena;
Address *serverAddressData;
uint32 serverPort;
Socket *socket;
ClientList clients;
Address *address;
uint32 port;
SocketHandle *handle;
SocketList clients;
bool listening;
};
@@ -56,13 +58,23 @@ struct ServerInitInfo {
uint64 memory;
};
typedef struct SocketConnectInfo SocketConnectInfo;
struct SocketConnectInfo {
string address;
uint16 port;
};
// Server/Client interface
Server serverInit(ServerInitInfo info);
void serverListen(Server *s);
Client *serverAccept(Server *s);
Socket *serverAccept(Server *s);
void serverClose(Server *s);
uint64 clientRead(Client *client, void *dest, size_t bytes);
void clientWrite(Client *client);
void clientClose(Client *client);
// Generic socket interface
Socket socketConnect(Arena *arena, SocketConnectInfo info);
int64 socketRead(Socket *s, byte *dest, uint64 numBytes);
int64 socketWrite(Socket *s, byte *source, uint64 numBytes);
void socketClose(Socket *s);
#endif