Standard setup for writing C inspired by Casey Muratori, Ryan Fleury, Mr. 4th Programmer, and others in the handmade community.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

119 lignes
2.6 KiB

  1. #ifndef OS_H
  2. #define OS_H
  3. #include "core.h"
  4. // ### Memory ###
  5. void *os_alloc(uint64 capacity);
  6. void os_reserve(void *ptr);
  7. void os_decommit(void *ptr);
  8. void os_free(void *ptr, uint64 freeSize);
  9. // ### File IO ###
  10. string os_readEntireFile(Arena *arena, string filename);
  11. bool os_writeEntireFile(Arena *arena, string filename, const byte *contents, uint64 contentsLength);
  12. bool os_fileAppend(Arena *arena, string filename, const byte *contents, uint64 contentsLength);
  13. // ### Standard IO ###
  14. void os_print(StdStream target, const char *fmt, va_list argList);
  15. void os_println(StdStream target, const char *fmt, va_list argList);
  16. // ### Multithreading ###
  17. typedef struct OS_Thread OS_Thread;
  18. struct OS_Thread {
  19. uint64 id;
  20. };
  21. OS_Thread os_createThread(void *(*entry)(void *ctx), void *ctx);
  22. // ### Network I/O ###
  23. typedef struct Address Address;
  24. struct Address;
  25. typedef struct SocketHandle SocketHandle;
  26. struct SocketHandle;
  27. typedef struct ServerEvents ServerEvents;
  28. struct ServerEvents;
  29. typedef struct Socket Socket;
  30. struct Socket {
  31. const Address *address;
  32. SocketHandle *handle;
  33. bool closed;
  34. };
  35. DefineList(Socket, Socket);
  36. typedef struct Server Server;
  37. struct Server {
  38. Arena *arena;
  39. Address *address;
  40. uint32 port;
  41. SocketHandle *handle;
  42. SocketList clients;
  43. bool listening;
  44. ServerEvents *events;
  45. };
  46. typedef struct ServerInitInfo ServerInitInfo;
  47. struct ServerInitInfo {
  48. int16 port;
  49. int32 concurrentClients;
  50. int64 memory;
  51. int32 maxEvents;
  52. };
  53. typedef struct SocketConnectInfo SocketConnectInfo;
  54. struct SocketConnectInfo {
  55. string address;
  56. uint16 port;
  57. bool blocking;
  58. };
  59. // Server/Client interface
  60. Server serverInit(ServerInitInfo info);
  61. void serverListen(Server *s);
  62. Socket *serverAccept(Server *s);
  63. void serverClose(Server *s);
  64. enum ServerEventType {
  65. ServerEventType_AcceptClient,
  66. ServerEventType_ClientMessage,
  67. ServerEventType_None,
  68. ServerEventType_COUNT,
  69. };
  70. typedef struct ServerEvent ServerEvent;
  71. struct ServerEvent {
  72. enum ServerEventType type;
  73. union {
  74. struct {} tAcceptClient;
  75. struct {
  76. int32 clientId;
  77. Socket *client;
  78. } tClientMessage;
  79. };
  80. };
  81. ServerEvent *serverGetNextEvent(Server *s);
  82. // Generic socket interface
  83. Socket socketConnect(Arena *arena, SocketConnectInfo info);
  84. int64 socketRead(Socket *s, byte *dest, uint64 numBytes);
  85. DefineResult(string, String);
  86. StringResult socketReadStr(Arena *arena, Socket *s);
  87. int64 socketWrite(Socket *s, byte *source, uint64 numBytes);
  88. int64 socketWriteStr(Socket *socket, string data);
  89. void socketClose(Socket *s);
  90. #endif