net.h 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #ifndef NET_H
  2. #define NET_H
  3. #include <stdbool.h>
  4. #include <stdint.h>
  5. //#include <SDL2/SDL_platform.h>
  6. #ifdef __WINDOWS__
  7. # include <winsock2.h>
  8. #define SHUT_RD SD_RECEIVE
  9. #define SHUT_WR SD_SEND
  10. #define SHUT_RDWR SD_BOTH
  11. typedef SOCKET socket_t;
  12. #else
  13. # include <sys/socket.h>
  14. # define INVALID_SOCKET -1
  15. typedef int socket_t;
  16. #endif
  17. //#include "config.h"
  18. bool
  19. net_init(void);
  20. void
  21. net_cleanup(void);
  22. socket_t
  23. net_connect(uint32_t addr, uint16_t port);
  24. socket_t
  25. net_listen(uint32_t addr, uint16_t port, int backlog);
  26. socket_t
  27. net_accept(socket_t server_socket);
  28. // the _all versions wait/retry until len bytes have been written/read
  29. ssize_t
  30. net_recv(socket_t socket, void *buf, size_t len);
  31. ssize_t
  32. net_recv_all(socket_t socket, void *buf, size_t len);
  33. ssize_t
  34. net_send(socket_t socket, const void *buf, size_t len);
  35. ssize_t
  36. net_send_all(socket_t socket, const void *buf, size_t len);
  37. // how is SHUT_RD (read), SHUT_WR (write) or SHUT_RDWR (both)
  38. bool
  39. net_shutdown(socket_t socket, int how);
  40. bool
  41. net_close(socket_t socket);
  42. #endif