socket.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #pragma once
  2. /*
  3. * Networking functions for TCP and UDP sockets
  4. * Dependencies: bzbutil, winsock2 (Windows)
  5. */
  6. #include <string>
  7. #include <vector>
  8. namespace bzbnet {
  9. constexpr int DEFAULT_BACKLOG = 10;
  10. constexpr unsigned int DEFAULT_PACKETSIZE = 512;
  11. /*
  12. * Initializes the system library for networking
  13. * TCPSocket and UDPSocket call this function automatically.
  14. */
  15. void initialize();
  16. /*
  17. * Terminates the system library for networking
  18. */
  19. void destroy();
  20. /*
  21. * Returns a list of the associated IPs of the given hostname
  22. */
  23. std::vector<std::string> get_host_ip_list(std::string hostname);
  24. /*
  25. * Returns a list of the associated hostnames and IPs of the given hostname
  26. */
  27. std::vector<std::string> get_host_list(std::string hostname);
  28. /*
  29. * Returns the machine's hostname
  30. */
  31. std::string get_hostname();
  32. /*
  33. * Socket class for TCP networking
  34. */
  35. class TCPSocket {
  36. private:
  37. void* fd;
  38. public:
  39. TCPSocket();
  40. TCPSocket(void* socket_fd);
  41. ~TCPSocket();
  42. void connect(std::string hostname, std::string service);
  43. void connect(std::string hostname, uint16_t port);
  44. void bind(std::string hostname, std::string service);
  45. void bind(std::string hostname, uint16_t port);
  46. void bind(std::string service);
  47. void bind(uint16_t port);
  48. void listen(std::string service, int backlog = DEFAULT_BACKLOG);
  49. void listen(uint16_t port);
  50. TCPSocket accept();
  51. std::vector<char> recv(unsigned int packetsize = DEFAULT_PACKETSIZE);
  52. void send(std::string data);
  53. void send(std::vector<char> data);
  54. void close();
  55. bool is_open();
  56. int get_raw_fd();
  57. };
  58. /*
  59. * Socket class for UDP networking
  60. */
  61. class UDPSocket {
  62. private:
  63. void* fd;
  64. public:
  65. UDPSocket();
  66. ~UDPSocket();
  67. void connect(std::string hostname, std::string service);
  68. void connect(std::string hostname, uint16_t port);
  69. void bind(std::string hostname, std::string service);
  70. void bind(std::string hostname, uint16_t port);
  71. void bind(std::string service);
  72. void bind(uint16_t port);
  73. void listen(std::string service, int backlog = DEFAULT_BACKLOG);
  74. void listen(uint16_t port);
  75. std::vector<char> recvfrom();
  76. void sendto(std::string data, std::string hostname, std::string service);
  77. void sendto(std::string data, std::string hostname, uint16_t port);
  78. void sendto(std::vector<char> data, std::string hostname, std::string service);
  79. void sendto(std::vector<char> data, std::string hostname, uint16_t port);
  80. void close();
  81. bool is_open();
  82. int get_raw_fd();
  83. };
  84. }