Skip to content
0.2.0-dev Download ZIP
168 lines ·7.6 KB Raw
1<?xml version="1.0" encoding="UTF-8" ?>
2<class name="PacketPeerUDP" inherits="PacketPeer" api_type="core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
3 <brief_description>
4 UDP packet peer.
5 </brief_description>
6 <description>
7 UDP packet peer. Can be used to send and receive raw UDP packets as well as [Variant]s.
8 [b]Example:[/b] Send a packet:
9 [codeblock]
10 var peer = PacketPeerUDP.new()
11
12 # Optionally, you can select the local port used to send the packet.
13 peer.bind(4444)
14
15 peer.set_dest_address("1.1.1.1", 4433)
16 peer.put_packet("hello".to_utf8_buffer())
17 [/codeblock]
18 [b]Example:[/b] Listen for packets:
19 [codeblock]
20 var peer
21
22 func _ready():
23 peer = PacketPeerUDP.new()
24 peer.bind(4433)
25
26
27 func _process(_delta):
28 if peer.get_available_packet_count() &gt; 0:
29 var array_bytes = peer.get_packet()
30 var packet_string = array_bytes.get_string_from_ascii()
31 print("Received message: ", packet_string)
32 [/codeblock]
33 [b]Note:[/b] When exporting to Android, make sure to enable the [code]INTERNET[/code] permission in the Android export preset before exporting the project or using remote deploy. Otherwise, network communication of any kind will be blocked by Android.
34 </description>
35 <tutorials>
36 </tutorials>
37 <methods>
38 <method name="bind">
39 <return type="int" enum="Error" />
40 <param index="0" name="port" type="int" />
41 <param index="1" name="bind_address" type="String" default="&quot;*&quot;" />
42 <param index="2" name="recv_buf_size" type="int" default="65536" />
43 <description>
44 Binds this [PacketPeerUDP] to the specified [param port] and [param bind_address] with a buffer size [param recv_buf_size], allowing it to receive incoming packets.
45 If [param bind_address] is set to [code]"*"[/code] (default), the peer will be bound on all available addresses (both IPv4 and IPv6).
46 If [param bind_address] is set to [code]"0.0.0.0"[/code] (for IPv4) or [code]"::"[/code] (for IPv6), the peer will be bound to all available addresses matching that IP type.
47 If [param bind_address] is set to any valid address (e.g. [code]"192.168.1.101"[/code], [code]"::1"[/code], etc.), the peer will only be bound to the interface with that address (or fail if no interface with the given address exists).
48 </description>
49 </method>
50 <method name="close">
51 <return type="void" />
52 <description>
53 Closes the [PacketPeerUDP]'s underlying UDP socket.
54 </description>
55 </method>
56 <method name="connect_to_host">
57 <return type="int" enum="Error" />
58 <param index="0" name="host" type="String" />
59 <param index="1" name="port" type="int" />
60 <description>
61 Calling this method connects this UDP peer to the given [param host]/[param port] pair. UDP is in reality connectionless, so this option only means that incoming packets from different addresses are automatically discarded, and that outgoing packets are always sent to the connected address (future calls to [method set_dest_address] are not allowed). This method does not send any data to the remote peer, to do that, use [method PacketPeer.put_var] or [method PacketPeer.put_packet] as usual. See also [UDPServer].
62 [b]Note:[/b] Connecting to the remote peer does not help to protect from malicious attacks like IP spoofing, etc. Think about using an encryption technique like TLS or DTLS if you feel like your application is transferring sensitive information.
63 </description>
64 </method>
65 <method name="get_local_port" qualifiers="const">
66 <return type="int" />
67 <description>
68 Returns the local port to which this peer is bound.
69 </description>
70 </method>
71 <method name="get_packet_ip" qualifiers="const">
72 <return type="String" />
73 <description>
74 Returns the IP of the remote peer that sent the last packet(that was received with [method PacketPeer.get_packet] or [method PacketPeer.get_var]).
75 </description>
76 </method>
77 <method name="get_packet_port" qualifiers="const">
78 <return type="int" />
79 <description>
80 Returns the port of the remote peer that sent the last packet(that was received with [method PacketPeer.get_packet] or [method PacketPeer.get_var]).
81 </description>
82 </method>
83 <method name="is_bound" qualifiers="const">
84 <return type="bool" />
85 <description>
86 Returns whether this [PacketPeerUDP] is bound to an address and can receive packets.
87 </description>
88 </method>
89 <method name="is_socket_connected" qualifiers="const">
90 <return type="bool" />
91 <description>
92 Returns [code]true[/code] if the UDP socket is open and has been connected to a remote address. See [method connect_to_host].
93 </description>
94 </method>
95 <method name="join_multicast_group">
96 <return type="int" enum="Error" />
97 <param index="0" name="multicast_address" type="String" />
98 <param index="1" name="interface_name" type="String" />
99 <description>
100 Joins the multicast group specified by [param multicast_address] using the interface identified by [param interface_name].
101 You can join the same multicast group with multiple interfaces. Use [method IP.get_local_interfaces] to know which are available.
102 [b]Note:[/b] Some Android devices might require the [code]CHANGE_WIFI_MULTICAST_STATE[/code] permission for multicast to work.
103 </description>
104 </method>
105 <method name="leave_multicast_group">
106 <return type="int" enum="Error" />
107 <param index="0" name="multicast_address" type="String" />
108 <param index="1" name="interface_name" type="String" />
109 <description>
110 Removes the interface identified by [param interface_name] from the multicast group specified by [param multicast_address].
111 </description>
112 </method>
113 <method name="set_broadcast_enabled">
114 <return type="void" />
115 <param index="0" name="enabled" type="bool" />
116 <description>
117 Enable or disable sending of broadcast packets (e.g. [code]set_dest_address("255.255.255.255", 4343)[/code]. This option is disabled by default.
118 [b]Note:[/b] Some Android devices might require the [code]CHANGE_WIFI_MULTICAST_STATE[/code] permission and this option to be enabled to receive broadcast packets too.
119 </description>
120 </method>
121 <method name="set_dest_address">
122 <return type="int" enum="Error" />
123 <param index="0" name="host" type="String" />
124 <param index="1" name="port" type="int" />
125 <description>
126 Sets the destination address and port for sending packets and variables. A hostname will be resolved using DNS if needed.
127 [b]Note:[/b] [method set_broadcast_enabled] must be enabled before sending packets to a broadcast address (e.g. [code]255.255.255.255[/code]).
128 </description>
129 </method>
130 <method name="wait">
131 <return type="int" enum="Error" />
132 <description>
133 Waits for a packet to arrive on the bound address. See [method bind].
134 [b]Note:[/b] [method wait] can't be interrupted once it has been called. This can be worked around by allowing the other party to send a specific "death pill" packet like this:
135 [codeblocks]
136 [gdscript]
137 socket = PacketPeerUDP.new()
138 # Server
139 socket.set_dest_address("127.0.0.1", 789)
140 socket.put_packet("Time to stop".to_ascii_buffer())
141
142 # Client
143 while socket.wait() == OK:
144 var data = socket.get_packet().get_string_from_ascii()
145 if data == "Time to stop":
146 return
147 [/gdscript]
148 [csharp]
149 var socket = new PacketPeerUdp();
150 // Server
151 socket.SetDestAddress("127.0.0.1", 789);
152 socket.PutPacket("Time to stop".ToAsciiBuffer());
153
154 // Client
155 while (socket.Wait() == OK)
156 {
157 string data = socket.GetPacket().GetStringFromASCII();
158 if (data == "Time to stop")
159 {
160 return;
161 }
162 }
163 [/csharp]
164 [/codeblocks]
165 </description>
166 </method>
167 </methods>
168</class>