packet_peer.h 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. public:
  40. virtual int get_available_packet_count() const=0;
  41. virtual Error get_packet(const uint8_t **r_buffer,int &r_buffer_size) const=0; ///< buffer is GONE after next get_packet
  42. virtual Error put_packet(const uint8_t *p_buffer,int p_buffer_size)=0;
  43. virtual int get_max_packet_size() const=0;
  44. /* helpers / binders */
  45. virtual Error get_packet_buffer(DVector<uint8_t> &r_buffer) const;
  46. virtual Error put_packet_buffer(const DVector<uint8_t> &p_buffer);
  47. virtual Error get_var(Variant &r_variant) const;
  48. virtual Error put_var(const Variant& p_packet);
  49. PacketPeer();
  50. ~PacketPeer(){}
  51. };
  52. class PacketPeerStream : public PacketPeer {
  53. OBJ_TYPE(PacketPeerStream,PacketPeer);
  54. //the way the buffers work sucks, will change later
  55. mutable Ref<StreamPeer> peer;
  56. mutable RingBuffer<uint8_t> ring_buffer;
  57. mutable Vector<uint8_t> temp_buffer;
  58. Error _poll_buffer() const;
  59. protected:
  60. void _set_stream_peer(REF p_peer);
  61. static void _bind_methods();
  62. public:
  63. virtual int get_available_packet_count() const;
  64. virtual Error get_packet(const uint8_t **r_buffer,int &r_buffer_size) const;
  65. virtual Error put_packet(const uint8_t *p_buffer,int p_buffer_size);
  66. virtual int get_max_packet_size() const;
  67. void set_stream_peer(const Ref<StreamPeer>& p_peer);
  68. void set_input_buffer_max_size(int p_max_size);
  69. PacketPeerStream();
  70. };
  71. #endif // PACKET_STREAM_H