| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "core/io/ip.h" |
| 34 |
#include "core/object/ref_counted.h" |
| 35 |
|
| 36 |
class NetSocket : public RefCounted { |
| 37 |
GDSOFTCLASS(NetSocket, RefCounted); |
| 38 |
|
| 39 |
protected: |
| 40 |
static NetSocket *(*_create)(); |
| 41 |
|
| 42 |
public: |
| 43 |
static NetSocket *create(); |
| 44 |
|
| 45 |
enum PollType : int32_t { |
| 46 |
POLL_TYPE_IN, |
| 47 |
POLL_TYPE_OUT, |
| 48 |
POLL_TYPE_IN_OUT |
| 49 |
}; |
| 50 |
|
| 51 |
enum Type : int32_t { |
| 52 |
TYPE_NONE, |
| 53 |
TYPE_TCP, |
| 54 |
TYPE_UDP, |
| 55 |
}; |
| 56 |
|
| 57 |
enum class Family { |
| 58 |
NONE, |
| 59 |
INET, |
| 60 |
UNIX, |
| 61 |
}; |
| 62 |
|
| 63 |
class Address { |
| 64 |
Family _family = Family::NONE; |
| 65 |
CharString _path; |
| 66 |
IPAddress _ip; |
| 67 |
uint16_t _port = 0; |
| 68 |
|
| 69 |
public: |
| 70 |
_FORCE_INLINE_ Family get_family() const { return _family; } |
| 71 |
_FORCE_INLINE_ bool is_inet() const { return _family == Family::INET; } |
| 72 |
_FORCE_INLINE_ bool is_unix() const { return _family == Family::UNIX; } |
| 73 |
_FORCE_INLINE_ bool is_valid() const { return is_inet() || is_unix(); } |
| 74 |
|
| 75 |
_FORCE_INLINE_ const IPAddress &ip() const { return _ip; } |
| 76 |
_FORCE_INLINE_ const uint16_t &port() const { return _port; } |
| 77 |
|
| 78 |
_FORCE_INLINE_ const CharString &get_path() const { return _path; } |
| 79 |
|
| 80 |
Address() {} |
| 81 |
|
| 82 |
Address(const IPAddress &p_addr, uint16_t p_port) : |
| 83 |
_family(Family::INET) { |
| 84 |
_ip = p_addr; |
| 85 |
_port = p_port; |
| 86 |
} |
| 87 |
|
| 88 |
Address(const String &p_path) : |
| 89 |
_family(Family::UNIX), _path(p_path.utf8()) { |
| 90 |
} |
| 91 |
|
| 92 |
Address(const CharString &p_path) : |
| 93 |
_family(Family::UNIX), _path(p_path) { |
| 94 |
} |
| 95 |
}; |
| 96 |
|
| 97 |
virtual Error open(Family p_family, Type p_type, IP::Type &r_ip_type) = 0; |
| 98 |
virtual void close() = 0; |
| 99 |
virtual Error bind(Address p_addr) = 0; |
| 100 |
virtual Error listen(int p_max_pending) = 0; |
| 101 |
virtual Error connect_to_host(Address p_addr) = 0; |
| 102 |
virtual Error poll(PollType p_type, int timeout) const = 0; |
| 103 |
virtual Error recv(uint8_t *p_buffer, int p_len, int &r_read) = 0; |
| 104 |
virtual Error recvfrom(uint8_t *p_buffer, int p_len, int &r_read, IPAddress &r_ip, uint16_t &r_port, bool p_peek = false) = 0; |
| 105 |
virtual Error send(const uint8_t *p_buffer, int p_len, int &r_sent) = 0; |
| 106 |
virtual Error sendto(const uint8_t *p_buffer, int p_len, int &r_sent, IPAddress p_ip, uint16_t p_port) = 0; |
| 107 |
virtual Ref<NetSocket> accept(Address &r_addr) = 0; |
| 108 |
|
| 109 |
virtual bool is_open() const = 0; |
| 110 |
virtual int get_available_bytes() const = 0; |
| 111 |
virtual Error get_socket_address(Address *r_addr) const = 0; |
| 112 |
|
| 113 |
virtual Error set_broadcasting_enabled(bool p_enabled) = 0; |
| 114 |
virtual void set_blocking_enabled(bool p_enabled) = 0; |
| 115 |
virtual void set_ipv6_only_enabled(bool p_enabled) = 0; |
| 116 |
virtual void set_tcp_no_delay_enabled(bool p_enabled) = 0; |
| 117 |
virtual void set_reuse_address_enabled(bool p_enabled) = 0; |
| 118 |
virtual Error join_multicast_group(const IPAddress &p_multi_address, const String &p_if_name) = 0; |
| 119 |
virtual Error leave_multicast_group(const IPAddress &p_multi_address, const String &p_if_name) = 0; |
| 120 |
|
| 121 |
virtual ~NetSocket() {} |
| 122 |
}; |