119 lines
2.6 KiB
C
119 lines
2.6 KiB
C
#ifndef OS_H
|
|
#define OS_H
|
|
|
|
#include "core.h"
|
|
|
|
// ### Memory ###
|
|
void *os_alloc(uint64 capacity);
|
|
void os_reserve(void *ptr);
|
|
void os_decommit(void *ptr);
|
|
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, 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;
|
|
struct OS_Thread {
|
|
uint64 id;
|
|
};
|
|
|
|
OS_Thread os_createThread(void *(*entry)(void *ctx), void *ctx);
|
|
|
|
// ### Network I/O ###
|
|
typedef struct Address Address;
|
|
struct Address;
|
|
|
|
typedef struct SocketHandle SocketHandle;
|
|
struct SocketHandle;
|
|
|
|
typedef struct ServerEvents ServerEvents;
|
|
struct ServerEvents;
|
|
|
|
typedef struct Socket Socket;
|
|
struct Socket {
|
|
const Address *address;
|
|
SocketHandle *handle;
|
|
bool closed;
|
|
};
|
|
DefineList(Socket, Socket);
|
|
|
|
typedef struct Server Server;
|
|
struct Server {
|
|
Arena *arena;
|
|
Address *address;
|
|
uint32 port;
|
|
SocketHandle *handle;
|
|
SocketList clients;
|
|
bool listening;
|
|
ServerEvents *events;
|
|
};
|
|
|
|
typedef struct ServerInitInfo ServerInitInfo;
|
|
struct ServerInitInfo {
|
|
int16 port;
|
|
int32 concurrentClients;
|
|
int64 memory;
|
|
int32 maxEvents;
|
|
};
|
|
|
|
typedef struct SocketConnectInfo SocketConnectInfo;
|
|
struct SocketConnectInfo {
|
|
string address;
|
|
uint16 port;
|
|
bool blocking;
|
|
};
|
|
|
|
|
|
// Server/Client interface
|
|
Server serverInit(ServerInitInfo info);
|
|
|
|
void serverListen(Server *s);
|
|
|
|
Socket *serverAccept(Server *s);
|
|
|
|
void serverClose(Server *s);
|
|
|
|
enum ServerEventType {
|
|
ServerEventType_AcceptClient,
|
|
ServerEventType_ClientMessage,
|
|
ServerEventType_None,
|
|
ServerEventType_COUNT,
|
|
};
|
|
|
|
typedef struct ServerEvent ServerEvent;
|
|
struct ServerEvent {
|
|
enum ServerEventType type;
|
|
union {
|
|
struct {} tAcceptClient;
|
|
struct {
|
|
int32 clientId;
|
|
Socket *client;
|
|
} tClientMessage;
|
|
};
|
|
};
|
|
|
|
ServerEvent *serverGetNextEvent(Server *s);
|
|
|
|
// Generic socket interface
|
|
Socket socketConnect(Arena *arena, SocketConnectInfo info);
|
|
|
|
int64 socketRead(Socket *s, byte *dest, uint64 numBytes);
|
|
|
|
DefineResult(string, String);
|
|
StringResult socketReadStr(Arena *arena, Socket *s);
|
|
|
|
int64 socketWrite(Socket *s, byte *source, uint64 numBytes);
|
|
|
|
int64 socketWriteStr(Socket *socket, string data);
|
|
|
|
void socketClose(Socket *s);
|
|
|
|
#endif
|