sockets.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright 2020 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <memory>
  6. #include <utility>
  7. #if defined(_WIN32)
  8. #elif !YUZU_UNIX
  9. #error "Platform not implemented"
  10. #endif
  11. #include "common/common_types.h"
  12. #include "core/network/network.h"
  13. // TODO: C++20 Replace std::vector usages with std::span
  14. namespace Network {
  15. class Socket {
  16. public:
  17. struct AcceptResult {
  18. std::unique_ptr<Socket> socket;
  19. SockAddrIn sockaddr_in;
  20. };
  21. explicit Socket() = default;
  22. ~Socket();
  23. Socket(const Socket&) = delete;
  24. Socket& operator=(const Socket&) = delete;
  25. Socket(Socket&& rhs) noexcept;
  26. // Avoid closing sockets implicitly
  27. Socket& operator=(Socket&&) noexcept = delete;
  28. Errno Initialize(Domain domain, Type type, Protocol protocol);
  29. Errno Close();
  30. std::pair<AcceptResult, Errno> Accept();
  31. Errno Connect(SockAddrIn addr_in);
  32. std::pair<SockAddrIn, Errno> GetPeerName();
  33. std::pair<SockAddrIn, Errno> GetSockName();
  34. Errno Bind(SockAddrIn addr);
  35. Errno Listen(s32 backlog);
  36. Errno Shutdown(ShutdownHow how);
  37. std::pair<s32, Errno> Recv(int flags, std::vector<u8>& message);
  38. std::pair<s32, Errno> RecvFrom(int flags, std::vector<u8>& message, SockAddrIn* addr);
  39. std::pair<s32, Errno> Send(const std::vector<u8>& message, int flags);
  40. std::pair<s32, Errno> SendTo(u32 flags, const std::vector<u8>& message, const SockAddrIn* addr);
  41. Errno SetLinger(bool enable, u32 linger);
  42. Errno SetReuseAddr(bool enable);
  43. Errno SetBroadcast(bool enable);
  44. Errno SetSndBuf(u32 value);
  45. Errno SetRcvBuf(u32 value);
  46. Errno SetSndTimeo(u32 value);
  47. Errno SetRcvTimeo(u32 value);
  48. Errno SetNonBlock(bool enable);
  49. bool IsOpened() const;
  50. #if defined(_WIN32)
  51. SOCKET fd = INVALID_SOCKET;
  52. #elif YUZU_UNIX
  53. int fd = -1;
  54. #endif
  55. };
  56. std::pair<s32, Errno> Poll(std::vector<PollFD>& poll_fds, s32 timeout);
  57. } // namespace Network