| 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/io/net_socket.h" |
| 35 |
#include "core/io/packet_peer.h" |
| 36 |
|
| 37 |
class UDPServer; |
| 38 |
|
| 39 |
class PacketPeerUDP : public PacketPeer { |
| 40 |
GDCLASS(PacketPeerUDP, PacketPeer); |
| 41 |
|
| 42 |
protected: |
| 43 |
enum { |
| 44 |
PACKET_BUFFER_SIZE = 65536 |
| 45 |
}; |
| 46 |
|
| 47 |
RingBuffer<uint8_t> rb; |
| 48 |
uint8_t recv_buffer[PACKET_BUFFER_SIZE]; |
| 49 |
uint8_t packet_buffer[PACKET_BUFFER_SIZE]; |
| 50 |
IPAddress packet_ip; |
| 51 |
int packet_port = 0; |
| 52 |
int queue_count = 0; |
| 53 |
|
| 54 |
IPAddress peer_addr; |
| 55 |
int peer_port = 0; |
| 56 |
bool connected = false; |
| 57 |
bool blocking = true; |
| 58 |
bool broadcast = false; |
| 59 |
UDPServer *udp_server = nullptr; |
| 60 |
Ref<NetSocket> _sock; |
| 61 |
|
| 62 |
static void _bind_methods(); |
| 63 |
|
| 64 |
String _get_packet_ip() const; |
| 65 |
|
| 66 |
Error _set_dest_address(const String &p_address, int p_port); |
| 67 |
Error _poll(); |
| 68 |
|
| 69 |
public: |
| 70 |
void set_blocking_mode(bool p_enable); |
| 71 |
|
| 72 |
Error bind(int p_port, const IPAddress &p_bind_address = IPAddress("*"), int p_recv_buffer_size = 65536); |
| 73 |
void close(); |
| 74 |
Error wait(); |
| 75 |
bool is_bound() const; |
| 76 |
|
| 77 |
Error connect_shared_socket(Ref<NetSocket> p_sock, IPAddress p_ip, uint16_t p_port, UDPServer *ref); |
| 78 |
void disconnect_shared_socket(); |
| 79 |
Error store_packet(IPAddress p_ip, uint32_t p_port, uint8_t *p_buf, int p_buf_size); |
| 80 |
Error connect_to_host(const IPAddress &p_host, int p_port); |
| 81 |
bool is_socket_connected() const; |
| 82 |
|
| 83 |
IPAddress get_packet_address() const; |
| 84 |
int get_packet_port() const; |
| 85 |
int get_local_port() const; |
| 86 |
void set_dest_address(const IPAddress &p_address, int p_port); |
| 87 |
|
| 88 |
Error put_packet(const uint8_t *p_buffer, int p_buffer_size) override; |
| 89 |
Error get_packet(const uint8_t **r_buffer, int &r_buffer_size) override; |
| 90 |
int get_available_packet_count() const override; |
| 91 |
int get_max_packet_size() const override; |
| 92 |
void set_broadcast_enabled(bool p_enabled); |
| 93 |
Error join_multicast_group(IPAddress p_multi_address, const String &p_if_name); |
| 94 |
Error leave_multicast_group(IPAddress p_multi_address, const String &p_if_name); |
| 95 |
|
| 96 |
PacketPeerUDP(); |
| 97 |
~PacketPeerUDP(); |
| 98 |
}; |