packet_peer.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*************************************************************************/
  2. /* packet_peer.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef PACKET_PEER_H
  30. #define PACKET_PEER_H
  31. #include "object.h"
  32. #include "io/stream_peer.h"
  33. #include "ring_buffer.h"
  34. class PacketPeer : public Reference {
  35. OBJ_TYPE( PacketPeer, Reference );
  36. Variant _bnd_get_var() const;
  37. void _bnd_put_var(const Variant& p_var);
  38. static void _bind_methods();
  39. Error _put_packet(const DVector<uint8_t> &p_buffer);
  40. DVector<uint8_t> _get_packet() const;
  41. Error _get_packet_error() const;
  42. mutable Error last_get_error;
  43. public:
  44. virtual int get_available_packet_count() const=0;
  45. virtual Error get_packet(const uint8_t **r_buffer,int &r_buffer_size) const=0; ///< buffer is GONE after next get_packet
  46. virtual Error put_packet(const uint8_t *p_buffer,int p_buffer_size)=0;
  47. virtual int get_max_packet_size() const=0;
  48. /* helpers / binders */
  49. virtual Error get_packet_buffer(DVector<uint8_t> &r_buffer) const;
  50. virtual Error put_packet_buffer(const DVector<uint8_t> &p_buffer);
  51. virtual Error get_var(Variant &r_variant) const;
  52. virtual Error put_var(const Variant& p_packet);
  53. PacketPeer();
  54. ~PacketPeer(){}
  55. };
  56. class PacketPeerStream : public PacketPeer {
  57. OBJ_TYPE(PacketPeerStream,PacketPeer);
  58. //the way the buffers work sucks, will change later
  59. mutable Ref<StreamPeer> peer;
  60. mutable RingBuffer<uint8_t> ring_buffer;
  61. mutable Vector<uint8_t> temp_buffer;
  62. Error _poll_buffer() const;
  63. protected:
  64. void _set_stream_peer(REF p_peer);
  65. static void _bind_methods();
  66. public:
  67. virtual int get_available_packet_count() const;
  68. virtual Error get_packet(const uint8_t **r_buffer,int &r_buffer_size) const;
  69. virtual Error put_packet(const uint8_t *p_buffer,int p_buffer_size);
  70. virtual int get_max_packet_size() const;
  71. void set_stream_peer(const Ref<StreamPeer>& p_peer);
  72. void set_input_buffer_max_size(int p_max_size);
  73. PacketPeerStream();
  74. };
  75. #endif // PACKET_STREAM_H