udp_server.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. #ifndef UDP_SERVER_H
  31. #define UDP_SERVER_H
  32. #include "core/io/net_socket.h"
  33. #include "core/io/packet_peer_udp.h"
  34. class UDPServer : public Reference {
  35. GDCLASS(UDPServer, Reference);
  36. protected:
  37. enum {
  38. PACKET_BUFFER_SIZE = 65536
  39. };
  40. struct Peer {
  41. PacketPeerUDP *peer;
  42. IP_Address ip;
  43. uint16_t port = 0;
  44. bool operator==(const Peer &p_other) const {
  45. return (ip == p_other.ip && port == p_other.port);
  46. }
  47. };
  48. uint8_t recv_buffer[PACKET_BUFFER_SIZE];
  49. int bind_port = 0;
  50. IP_Address bind_address;
  51. List<Peer> peers;
  52. List<Peer> pending;
  53. int max_pending_connections = 16;
  54. Ref<NetSocket> _sock;
  55. static void _bind_methods();
  56. public:
  57. void remove_peer(IP_Address p_ip, int p_port);
  58. Error listen(uint16_t p_port, const IP_Address &p_bind_address = IP_Address("*"));
  59. Error poll();
  60. bool is_listening() const;
  61. bool is_connection_available() const;
  62. void set_max_pending_connections(int p_max);
  63. int get_max_pending_connections() const;
  64. Ref<PacketPeerUDP> take_connection();
  65. void stop();
  66. UDPServer();
  67. ~UDPServer();
  68. };
  69. #endif // UDP_SERVER_H