packet_peer.h
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-2016 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 
32 #include "object.h"
33 #include "io/stream_peer.h"
34 #include "ring_buffer.h"
35 class PacketPeer : public Reference {
36 
37  OBJ_TYPE( PacketPeer, Reference );
38 
39  Variant _bnd_get_var() const;
40  void _bnd_put_var(const Variant& p_var);
41 
42  static void _bind_methods();
43 
44 
45  Error _put_packet(const DVector<uint8_t> &p_buffer);
46  DVector<uint8_t> _get_packet() const;
47  Error _get_packet_error() const;
48 
49 
50  mutable Error last_get_error;
51 
52 public:
53 
54  virtual int get_available_packet_count() const=0;
55  virtual Error get_packet(const uint8_t **r_buffer,int &r_buffer_size) const=0;
56  virtual Error put_packet(const uint8_t *p_buffer,int p_buffer_size)=0;
57 
58  virtual int get_max_packet_size() const=0;
59 
60  /* helpers / binders */
61 
62  virtual Error get_packet_buffer(DVector<uint8_t> &r_buffer) const;
63  virtual Error put_packet_buffer(const DVector<uint8_t> &p_buffer);
64 
65  virtual Error get_var(Variant &r_variant) const;
66  virtual Error put_var(const Variant& p_packet);
67 
68  PacketPeer();
69  ~PacketPeer(){}
70 };
71 
72 class PacketPeerStream : public PacketPeer {
73 
74  OBJ_TYPE(PacketPeerStream,PacketPeer);
75 
76  //the way the buffers work sucks, will change later
77 
78  mutable Ref<StreamPeer> peer;
79  mutable RingBuffer<uint8_t> ring_buffer;
80  mutable Vector<uint8_t> temp_buffer;
81 
82  Error _poll_buffer() const;
83 protected:
84 
85  void _set_stream_peer(REF p_peer);
86  static void _bind_methods();
87 public:
88 
89  virtual int get_available_packet_count() const;
90  virtual Error get_packet(const uint8_t **r_buffer,int &r_buffer_size) const;
91  virtual Error put_packet(const uint8_t *p_buffer,int p_buffer_size);
92 
93  virtual int get_max_packet_size() const;
94 
95  void set_stream_peer(const Ref<StreamPeer>& p_peer);
96  void set_input_buffer_max_size(int p_max_size);
98 
99 };
100 
101 
102 #endif // PACKET_STREAM_H
virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size) const =0
buffer is GONE after next get_packet
Definition: variant.h:74
Definition: reference.h:40
Definition: packet_peer.h:72
Definition: packet_peer.h:35