SocketImpl.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #pragma once
  2. #if IL2CPP_TARGET_WINDOWS
  3. #include <string>
  4. #include <vector>
  5. #include <stdint.h>
  6. #include "os/Socket.h"
  7. #include "os/ErrorCodes.h"
  8. #include "os/WaitStatus.h"
  9. #include "utils/NonCopyable.h"
  10. struct sockaddr;
  11. namespace il2cpp
  12. {
  13. namespace os
  14. {
  15. class SocketImpl : public il2cpp::utils::NonCopyable
  16. {
  17. public:
  18. /// Handle for socket. Must be large enough to hold a pointer.
  19. typedef uint64_t SocketDescriptor;
  20. SocketImpl(ThreadStatusCallback thread_status_callback);
  21. ~SocketImpl();
  22. inline SocketDescriptor GetDescriptor()
  23. {
  24. return _fd;
  25. }
  26. ErrorCode GetLastError() const;
  27. WaitStatus Create(SocketDescriptor fd, int32_t family, int32_t type, int32_t protocol);
  28. WaitStatus Create(AddressFamily family, SocketType type, ProtocolType protocol);
  29. WaitStatus Close();
  30. bool IsClosed() { return (_fd == -1); }
  31. WaitStatus SetBlocking(bool blocking);
  32. WaitStatus Listen(int32_t blacklog);
  33. WaitStatus Bind(const char *path);
  34. WaitStatus Bind(const char *address, uint16_t port);
  35. WaitStatus Bind(uint32_t address, uint16_t port);
  36. WaitStatus Bind(uint8_t address[ipv6AddressSize], uint32_t scope, uint16_t port);
  37. WaitStatus Connect(const char *path);
  38. WaitStatus Connect(uint32_t address, uint16_t port);
  39. WaitStatus Connect(uint8_t address[ipv6AddressSize], uint32_t scope, uint16_t port);
  40. WaitStatus Disconnect(bool reuse);
  41. WaitStatus Shutdown(int32_t how);
  42. WaitStatus GetLocalEndPointInfo(EndPointInfo &info);
  43. WaitStatus GetRemoteEndPointInfo(EndPointInfo &info);
  44. WaitStatus Receive(const uint8_t *data, int32_t count, os::SocketFlags flags, int32_t *len);
  45. WaitStatus ReceiveFromInternal(const uint8_t *data, size_t count, int32_t flags, int32_t *len, struct sockaddr *from, int32_t *fromlen);
  46. WaitStatus Send(const uint8_t *data, int32_t count, os::SocketFlags flags, int32_t *len);
  47. WaitStatus SendArray(WSABuf *wsabufs, int32_t count, int32_t *sent, SocketFlags c_flags);
  48. WaitStatus ReceiveArray(WSABuf *wsabufs, int32_t count, int32_t *len, SocketFlags c_flags);
  49. WaitStatus SendTo(uint32_t address, uint16_t port, const uint8_t *data, int32_t count, os::SocketFlags flags, int32_t *len);
  50. WaitStatus SendTo(const char *path, const uint8_t *data, int32_t count, os::SocketFlags flags, int32_t *len);
  51. WaitStatus SendTo(uint8_t address[ipv6AddressSize], uint32_t scope, uint16_t port, const uint8_t *data, int32_t count, os::SocketFlags flags, int32_t *len);
  52. WaitStatus RecvFrom(uint32_t address, uint16_t port, const uint8_t *data, int32_t count, os::SocketFlags flags, int32_t *len, os::EndPointInfo &ep);
  53. WaitStatus RecvFrom(const char *path, const uint8_t *data, int32_t count, os::SocketFlags flags, int32_t *len, os::EndPointInfo &ep);
  54. WaitStatus RecvFrom(uint8_t address[ipv6AddressSize], uint32_t scope, uint16_t port, const uint8_t *data, int32_t count, os::SocketFlags flags, int32_t *len, os::EndPointInfo &ep);
  55. WaitStatus Available(int32_t *amount);
  56. WaitStatus Accept(os::Socket **socket);
  57. WaitStatus Ioctl(int32_t command, const uint8_t *in_data, int32_t in_len, uint8_t *out_data, int32_t out_len, int32_t *written);
  58. WaitStatus GetSocketOption(SocketOptionLevel level, SocketOptionName name, uint8_t *buffer, int32_t *length);
  59. WaitStatus GetSocketOptionFull(SocketOptionLevel level, SocketOptionName name, int32_t *first, int32_t *second);
  60. WaitStatus SetSocketOption(SocketOptionLevel level, SocketOptionName name, int32_t value);
  61. WaitStatus SetSocketOptionLinger(SocketOptionLevel level, SocketOptionName name, bool enabled, int32_t seconds);
  62. WaitStatus SetSocketOptionArray(SocketOptionLevel level, SocketOptionName name, const uint8_t *buffer, int32_t length);
  63. WaitStatus SetSocketOptionMembership(SocketOptionLevel level, SocketOptionName name, uint32_t group_address, uint32_t local_address);
  64. #if IL2CPP_SUPPORT_IPV6
  65. WaitStatus SetSocketOptionMembership(SocketOptionLevel level, SocketOptionName name, IPv6Address ipv6, uint64_t interfaceOffset);
  66. #endif
  67. WaitStatus SendFile(const char *filename, TransmitFileBuffers *buffers, TransmitFileOptions options);
  68. static WaitStatus Poll(std::vector<PollRequest> &requests, int32_t count, int32_t timeout, int32_t *result, int32_t *error);
  69. static WaitStatus Poll(std::vector<PollRequest> &requests, int32_t timeout, int32_t *result, int32_t *error);
  70. static WaitStatus Poll(PollRequest& request, int32_t timeout, int32_t *result, int32_t *error);
  71. static WaitStatus GetHostName(std::string &name);
  72. static WaitStatus GetHostByName(const std::string &host, std::string &name, std::vector<std::string> &aliases, std::vector<std::string> &addr_list);
  73. static WaitStatus GetHostByAddr(const std::string &address, std::string &name, std::vector<std::string> &aliases, std::vector<std::string> &addr_list);
  74. static void Startup();
  75. static void Cleanup();
  76. private:
  77. bool _is_valid;
  78. SocketDescriptor _fd;
  79. int32_t _domain;
  80. int32_t _type;
  81. int32_t _protocol;
  82. ErrorCode _saved_error;
  83. int32_t _still_readable;
  84. ThreadStatusCallback _thread_status_callback;
  85. void StoreLastError();
  86. void StoreLastError(int32_t error_no);
  87. WaitStatus ConnectInternal(struct sockaddr *sa, int32_t sa_size);
  88. WaitStatus SetSocketOptionInternal(int32_t level, int32_t name, const void *value, int32_t len);
  89. };
  90. }
  91. }
  92. #endif