packet_peer_udp.cpp 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*************************************************************************/
  2. /* packet_peer_udp.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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. #include "packet_peer_udp.h"
  31. #include "io/ip.h"
  32. PacketPeerUDP *(*PacketPeerUDP::_create)() = NULL;
  33. void PacketPeerUDP::set_blocking_mode(bool p_enable) {
  34. blocking = p_enable;
  35. }
  36. String PacketPeerUDP::_get_packet_ip() const {
  37. return get_packet_address();
  38. }
  39. Error PacketPeerUDP::_set_send_address(const String &p_address, int p_port) {
  40. IP_Address ip;
  41. if (p_address.is_valid_ip_address()) {
  42. ip = p_address;
  43. } else {
  44. ip = IP::get_singleton()->resolve_hostname(p_address);
  45. if (!ip.is_valid())
  46. return ERR_CANT_RESOLVE;
  47. }
  48. set_send_address(ip, p_port);
  49. return OK;
  50. }
  51. void PacketPeerUDP::_bind_methods() {
  52. ObjectTypeDB::bind_method(_MD("listen:Error", "port", "bind_address", "recv_buf_size"), &PacketPeerUDP::listen, DEFVAL("*"), DEFVAL(65536));
  53. ObjectTypeDB::bind_method(_MD("close"), &PacketPeerUDP::close);
  54. ObjectTypeDB::bind_method(_MD("wait:Error"), &PacketPeerUDP::wait);
  55. ObjectTypeDB::bind_method(_MD("is_listening"), &PacketPeerUDP::is_listening);
  56. ObjectTypeDB::bind_method(_MD("get_packet_ip"), &PacketPeerUDP::_get_packet_ip);
  57. //ObjectTypeDB::bind_method(_MD("get_packet_address"),&PacketPeerUDP::_get_packet_address);
  58. ObjectTypeDB::bind_method(_MD("get_packet_port"), &PacketPeerUDP::get_packet_port);
  59. ObjectTypeDB::bind_method(_MD("set_send_address", "host", "port"), &PacketPeerUDP::_set_send_address);
  60. }
  61. Ref<PacketPeerUDP> PacketPeerUDP::create_ref() {
  62. if (!_create)
  63. return Ref<PacketPeerUDP>();
  64. return Ref<PacketPeerUDP>(_create());
  65. }
  66. PacketPeerUDP *PacketPeerUDP::create() {
  67. if (!_create)
  68. return NULL;
  69. return _create();
  70. }
  71. PacketPeerUDP::PacketPeerUDP() {
  72. blocking = true;
  73. }