tcp.h 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. #pragma once
  2. #if defined(_WIN32)
  3. # include <WinSock2.h>
  4. # include <WS2tcpip.h>
  5. # define SOCKET_INVALID INVALID_SOCKET
  6. # define SOCKET_CLOSE(s) do {closesocket(s); (s) = SOCKET_INVALID;} while(0)
  7. # define SOCKET_SOCKOPT_REUSEADDR_CAST(opt) (char *)(&(opt))
  8. # define poll WSAPoll
  9. typedef SOCKET socket_t;
  10. typedef BOOL sockopt_reuseaddr_t;
  11. #else
  12. # include <unistd.h>
  13. # include <sys/socket.h>
  14. # include <sys/poll.h>
  15. # include <netinet/in.h>
  16. # include <arpa/inet.h>
  17. # include <netdb.h>
  18. # define SOCKET_INVALID (-1)
  19. # define SOCKET_ERROR (-1)
  20. # define SOCKET_CLOSE(s) do {close(s); (s) = SOCKET_INVALID;} while(0)
  21. # define SOCKET_SOCKOPT_REUSEADDR_CAST(opt) (&(opt))
  22. typedef int socket_t;
  23. typedef int sockopt_reuseaddr_t;
  24. #endif
  25. class TCP {
  26. public:
  27. static bool Start();
  28. static void Stop();
  29. static void StoreError(char *dst, unsigned int size);
  30. static bool GetIPAddressString(struct sockaddr_storage *addr, char *host, unsigned int host_size, char *port, unsigned int port_size);
  31. };