1/**************************************************************************/
2/* udp_server.h */
3/**************************************************************************/
4/* This file is part of: */
5/* GODOT ENGINE */
6/* https://godotengine.org */
7/**************************************************************************/
8/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10/* */
11/* Permission is hereby granted, free of charge, to any person obtaining */
12/* a copy of this software and associated documentation files (the */
13/* "Software"), to deal in the Software without restriction, including */
14/* without limitation the rights to use, copy, modify, merge, publish, */
15/* distribute, sublicense, and/or sell copies of the Software, and to */
16/* permit persons to whom the Software is furnished to do so, subject to */
17/* the following conditions: */
18/* */
19/* The above copyright notice and this permission notice shall be */
20/* included in all copies or substantial portions of the Software. */
21/* */
22/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29/**************************************************************************/
30
31#ifndef UDP_SERVER_H
32#define UDP_SERVER_H
33
34#include "core/io/net_socket.h"
35#include "core/io/packet_peer_udp.h"
36
37class UDPServer : public RefCounted {
38 GDCLASS(UDPServer, RefCounted);
39
40protected:
41 enum {
42 PACKET_BUFFER_SIZE = 65536
43 };
44
45 struct Peer {
46 PacketPeerUDP *peer = nullptr;
47 IPAddress ip;
48 uint16_t port = 0;
49
50 bool operator==(const Peer &p_other) const {
51 return (ip == p_other.ip && port == p_other.port);
52 }
53 };
54 uint8_t recv_buffer[PACKET_BUFFER_SIZE];
55
56 List<Peer> peers;
57 List<Peer> pending;
58 int max_pending_connections = 16;
59
60 Ref<NetSocket> _sock;
61 static void _bind_methods();
62
63public:
64 void remove_peer(IPAddress p_ip, int p_port);
65 Error listen(uint16_t p_port, const IPAddress &p_bind_address = IPAddress("*"));
66 Error poll();
67 int get_local_port() const;
68 bool is_listening() const;
69 bool is_connection_available() const;
70 void set_max_pending_connections(int p_max);
71 int get_max_pending_connections() const;
72 Ref<PacketPeerUDP> take_connection();
73
74 void stop();
75
76 UDPServer();
77 ~UDPServer();
78};
79
80#endif // UDP_SERVER_H
81