SocketImpl.h 5.7 KB

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