You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
862 B

4 months ago
  1. /*
  2. * IXUdpSocket.h
  3. * Author: Benjamin Sergeant
  4. * Copyright (c) 2020 Machine Zone, Inc. All rights reserved.
  5. */
  6. #pragma once
  7. #include <atomic>
  8. #include <memory>
  9. #include <string>
  10. #ifdef _WIN32
  11. #include <basetsd.h>
  12. #ifdef _MSC_VER
  13. typedef SSIZE_T ssize_t;
  14. #endif
  15. #endif
  16. #include "IXNetSystem.h"
  17. namespace ix
  18. {
  19. class UdpSocket
  20. {
  21. public:
  22. UdpSocket(int fd = -1);
  23. ~UdpSocket();
  24. // Virtual methods
  25. bool init(const std::string& host, int port, std::string& errMsg);
  26. ssize_t sendto(const std::string& buffer);
  27. ssize_t recvfrom(char* buffer, size_t length);
  28. void close();
  29. static int getErrno();
  30. static bool isWaitNeeded();
  31. static void closeSocket(int fd);
  32. private:
  33. std::atomic<int> _sockfd;
  34. struct sockaddr_in _server;
  35. };
  36. } // namespace ix