packet_peer_udp_posix.h 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*************************************************************************/
  2. /* packet_peer_udp_posix.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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. #ifndef PACKET_PEER_UDP_POSIX_H
  31. #define PACKET_PEER_UDP_POSIX_H
  32. #ifdef UNIX_ENABLED
  33. #include "io/packet_peer_udp.h"
  34. #include "ring_buffer.h"
  35. class PacketPeerUDPPosix : public PacketPeerUDP {
  36. enum {
  37. PACKET_BUFFER_SIZE = 65536
  38. };
  39. mutable RingBuffer<uint8_t> rb;
  40. uint8_t recv_buffer[PACKET_BUFFER_SIZE];
  41. mutable uint8_t packet_buffer[PACKET_BUFFER_SIZE];
  42. mutable IP_Address packet_ip;
  43. mutable int packet_port;
  44. mutable int queue_count;
  45. int sockfd;
  46. bool sock_blocking;
  47. IP::Type sock_type;
  48. IP_Address peer_addr;
  49. int peer_port;
  50. _FORCE_INLINE_ int _get_socket();
  51. static PacketPeerUDP *_create();
  52. void _set_sock_blocking(bool p_blocking);
  53. virtual Error _poll(bool p_block);
  54. public:
  55. virtual int get_available_packet_count() const;
  56. virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size) const;
  57. virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size);
  58. virtual int get_max_packet_size() const;
  59. virtual Error listen(int p_port, const IP_Address &p_bind_address = IP_Address("*"), int p_recv_buffer_size = 65536);
  60. virtual void close();
  61. virtual Error wait();
  62. virtual bool is_listening() const;
  63. virtual IP_Address get_packet_address() const;
  64. virtual int get_packet_port() const;
  65. virtual void set_dest_address(const IP_Address &p_address, int p_port);
  66. static void make_default();
  67. PacketPeerUDPPosix();
  68. ~PacketPeerUDPPosix();
  69. };
  70. #endif // PACKET_PEER_UDP_POSIX_H
  71. #endif