sockets.h 2.0 KB

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