0.2.0-dev Download ZIP
178 lines ·6.3 KB Raw
| 1 | <?xml version="1.0" encoding="UTF-8" ?> |
| 2 | <class name="UDPServer" inherits="RefCounted" api_type="core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd"> |
| 3 | <brief_description> |
| 4 | Helper class to implement a UDP server. |
| 5 | </brief_description> |
| 6 | <description> |
| 7 | A simple server that opens a UDP socket and returns connected [PacketPeerUDP] upon receiving new packets. See also [method PacketPeerUDP.connect_to_host]. |
| 8 | After starting the server ([method listen]), you will need to [method poll] it at regular intervals (e.g. inside [method Node._process]) for it to process new packets, delivering them to the appropriate [PacketPeerUDP], and taking new connections. |
| 9 | Below a small example of how it can be used: |
| 10 | [codeblocks] |
| 11 | [gdscript] |
| 12 | # server_node.gd |
| 13 | class_name ServerNode |
| 14 | extends Node |
| 15 | |
| 16 | var server = UDPServer.new() |
| 17 | var peers = [] |
| 18 | |
| 19 | func _ready(): |
| 20 | server.listen(4242) |
| 21 | |
| 22 | func _process(delta): |
| 23 | server.poll() # Important! |
| 24 | if server.is_connection_available(): |
| 25 | var peer = server.take_connection() |
| 26 | var packet = peer.get_packet() |
| 27 | print("Accepted peer: %s:%s" % [peer.get_packet_ip(), peer.get_packet_port()]) |
| 28 | print("Received data: %s" % [packet.get_string_from_utf8()]) |
| 29 | # Reply so it knows we received the message. |
| 30 | peer.put_packet(packet) |
| 31 | # Keep a reference so we can keep contacting the remote peer. |
| 32 | peers.append(peer) |
| 33 | |
| 34 | for i in range(0, peers.size()): |
| 35 | pass # Do something with the connected peers. |
| 36 | [/gdscript] |
| 37 | [csharp] |
| 38 | // ServerNode.cs |
| 39 | using Godot; |
| 40 | using System.Collections.Generic; |
| 41 | |
| 42 | public partial class ServerNode : Node |
| 43 | { |
| 44 | private UdpServer _server = new UdpServer(); |
| 45 | private List<PacketPeerUdp> _peers = new List<PacketPeerUdp>(); |
| 46 | |
| 47 | public override void _Ready() |
| 48 | { |
| 49 | _server.Listen(4242); |
| 50 | } |
| 51 | |
| 52 | public override void _Process(double delta) |
| 53 | { |
| 54 | _server.Poll(); // Important! |
| 55 | if (_server.IsConnectionAvailable()) |
| 56 | { |
| 57 | PacketPeerUdp peer = _server.TakeConnection(); |
| 58 | byte[] packet = peer.GetPacket(); |
| 59 | GD.Print($"Accepted Peer: {peer.GetPacketIP()}:{peer.GetPacketPort()}"); |
| 60 | GD.Print($"Received Data: {packet.GetStringFromUtf8()}"); |
| 61 | // Reply so it knows we received the message. |
| 62 | peer.PutPacket(packet); |
| 63 | // Keep a reference so we can keep contacting the remote peer. |
| 64 | _peers.Add(peer); |
| 65 | } |
| 66 | foreach (var peer in _peers) |
| 67 | { |
| 68 | // Do something with the peers. |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | [/csharp] |
| 73 | [/codeblocks] |
| 74 | [codeblocks] |
| 75 | [gdscript] |
| 76 | # client_node.gd |
| 77 | class_name ClientNode |
| 78 | extends Node |
| 79 | |
| 80 | var udp = PacketPeerUDP.new() |
| 81 | var connected = false |
| 82 | |
| 83 | func _ready(): |
| 84 | udp.connect_to_host("127.0.0.1", 4242) |
| 85 | |
| 86 | func _process(delta): |
| 87 | if !connected: |
| 88 | # Try to contact server |
| 89 | udp.put_packet("The answer is... 42!".to_utf8_buffer()) |
| 90 | if udp.get_available_packet_count() > 0: |
| 91 | print("Connected: %s" % udp.get_packet().get_string_from_utf8()) |
| 92 | connected = true |
| 93 | [/gdscript] |
| 94 | [csharp] |
| 95 | // ClientNode.cs |
| 96 | using Godot; |
| 97 | |
| 98 | public partial class ClientNode : Node |
| 99 | { |
| 100 | private PacketPeerUdp _udp = new PacketPeerUdp(); |
| 101 | private bool _connected = false; |
| 102 | |
| 103 | public override void _Ready() |
| 104 | { |
| 105 | _udp.ConnectToHost("127.0.0.1", 4242); |
| 106 | } |
| 107 | |
| 108 | public override void _Process(double delta) |
| 109 | { |
| 110 | if (!_connected) |
| 111 | { |
| 112 | // Try to contact server |
| 113 | _udp.PutPacket("The Answer Is..42!".ToUtf8Buffer()); |
| 114 | } |
| 115 | if (_udp.GetAvailablePacketCount() > 0) |
| 116 | { |
| 117 | GD.Print($"Connected: {_udp.GetPacket().GetStringFromUtf8()}"); |
| 118 | _connected = true; |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | [/csharp] |
| 123 | [/codeblocks] |
| 124 | </description> |
| 125 | <tutorials> |
| 126 | </tutorials> |
| 127 | <methods> |
| 128 | <method name="get_local_port" qualifiers="const"> |
| 129 | <return type="int" /> |
| 130 | <description> |
| 131 | Returns the local port this server is listening to. |
| 132 | </description> |
| 133 | </method> |
| 134 | <method name="is_connection_available" qualifiers="const"> |
| 135 | <return type="bool" /> |
| 136 | <description> |
| 137 | Returns [code]true[/code] if a packet with a new address/port combination was received on the socket. |
| 138 | </description> |
| 139 | </method> |
| 140 | <method name="is_listening" qualifiers="const"> |
| 141 | <return type="bool" /> |
| 142 | <description> |
| 143 | Returns [code]true[/code] if the socket is open and listening on a port. |
| 144 | </description> |
| 145 | </method> |
| 146 | <method name="listen"> |
| 147 | <return type="int" enum="Error" /> |
| 148 | <param index="0" name="port" type="int" /> |
| 149 | <param index="1" name="bind_address" type="String" default=""*"" /> |
| 150 | <description> |
| 151 | Starts the server by opening a UDP socket listening on the given [param port]. You can optionally specify a [param bind_address] to only listen for packets sent to that address. See also [method PacketPeerUDP.bind]. |
| 152 | </description> |
| 153 | </method> |
| 154 | <method name="poll"> |
| 155 | <return type="int" enum="Error" /> |
| 156 | <description> |
| 157 | Call this method at regular intervals (e.g. inside [method Node._process]) to process new packets. Any packet from a known address/port pair will be delivered to the appropriate [PacketPeerUDP], while any packet received from an unknown address/port pair will be added as a pending connection (see [method is_connection_available] and [method take_connection]). The maximum number of pending connections is defined via [member max_pending_connections]. |
| 158 | </description> |
| 159 | </method> |
| 160 | <method name="stop"> |
| 161 | <return type="void" /> |
| 162 | <description> |
| 163 | Stops the server, closing the UDP socket if open. Will close all connected [PacketPeerUDP] accepted via [method take_connection] (remote peers will not be notified). |
| 164 | </description> |
| 165 | </method> |
| 166 | <method name="take_connection"> |
| 167 | <return type="PacketPeerUDP" /> |
| 168 | <description> |
| 169 | Returns the first pending connection (connected to the appropriate address/port). Will return [code]null[/code] if no new connection is available. See also [method is_connection_available], [method PacketPeerUDP.connect_to_host]. |
| 170 | </description> |
| 171 | </method> |
| 172 | </methods> |
| 173 | <members> |
| 174 | <member name="max_pending_connections" type="int" setter="set_max_pending_connections" getter="get_max_pending_connections" default="16"> |
| 175 | Define the maximum number of pending connections, during [method poll], any new pending connection exceeding that value will be automatically dropped. Setting this value to [code]0[/code] effectively prevents any new pending connection to be accepted (e.g. when all your players have connected). |
| 176 | </member> |
| 177 | </members> |
| 178 | </class> |