1 /**
2 Abstraction and base class for packet-based protocols.
3 
4 Copyright:
5 Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur.  
6 Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md)  
7 Copyright (c) 2017-2018 Godot-D contributors  
8 
9 License: $(LINK2 https://opensource.org/licenses/MIT, MIT License)
10 
11 
12 */
13 module godot.packetpeer;
14 import std.meta : AliasSeq, staticIndexOf;
15 import std.traits : Unqual;
16 import godot.d.traits;
17 import godot.core;
18 import godot.c;
19 import godot.d.bind;
20 import godot.d.reference;
21 import godot.globalenums;
22 import godot.object;
23 import godot.reference;
24 /**
25 Abstraction and base class for packet-based protocols.
26 
27 PacketPeer is an abstraction and base class for packet-based protocols (such as UDP). It provides an API for sending and receiving packets both as raw data or variables. This makes it easy to transfer data over a protocol, without having to encode data as low-level bytes or having to worry about network ordering.
28 */
29 @GodotBaseClass struct PacketPeer
30 {
31 	package(godot) enum string _GODOT_internal_name = "PacketPeer";
32 public:
33 @nogc nothrow:
34 	union { /** */ godot_object _godot_object; /** */ Reference _GODOT_base; }
35 	alias _GODOT_base this;
36 	alias BaseClasses = AliasSeq!(typeof(_GODOT_base), typeof(_GODOT_base).BaseClasses);
37 	package(godot) __gshared bool _classBindingInitialized = false;
38 	package(godot) static struct GDNativeClassBinding
39 	{
40 		__gshared:
41 		@GodotName("get_available_packet_count") GodotMethod!(long) getAvailablePacketCount;
42 		@GodotName("get_encode_buffer_max_size") GodotMethod!(long) getEncodeBufferMaxSize;
43 		@GodotName("get_packet") GodotMethod!(PoolByteArray) getPacket;
44 		@GodotName("get_packet_error") GodotMethod!(GodotError) getPacketError;
45 		@GodotName("get_var") GodotMethod!(Variant, bool) getVar;
46 		@GodotName("is_object_decoding_allowed") GodotMethod!(bool) isObjectDecodingAllowed;
47 		@GodotName("put_packet") GodotMethod!(GodotError, PoolByteArray) putPacket;
48 		@GodotName("put_var") GodotMethod!(GodotError, Variant, bool) putVar;
49 		@GodotName("set_allow_object_decoding") GodotMethod!(void, bool) setAllowObjectDecoding;
50 		@GodotName("set_encode_buffer_max_size") GodotMethod!(void, long) setEncodeBufferMaxSize;
51 	}
52 	/// 
53 	pragma(inline, true) bool opEquals(in PacketPeer other) const
54 	{ return _godot_object.ptr is other._godot_object.ptr; }
55 	/// 
56 	pragma(inline, true) typeof(null) opAssign(typeof(null) n)
57 	{ _godot_object.ptr = n; return null; }
58 	/// 
59 	pragma(inline, true) bool opEquals(typeof(null) n) const
60 	{ return _godot_object.ptr is n; }
61 	/// 
62 	size_t toHash() const @trusted { return cast(size_t)_godot_object.ptr; }
63 	mixin baseCasts;
64 	/// Construct a new instance of PacketPeer.
65 	/// Note: use `memnew!PacketPeer` instead.
66 	static PacketPeer _new()
67 	{
68 		static godot_class_constructor constructor;
69 		if(constructor is null) constructor = _godot_api.godot_get_class_constructor("PacketPeer");
70 		if(constructor is null) return typeof(this).init;
71 		return cast(PacketPeer)(constructor());
72 	}
73 	@disable new(size_t s);
74 	/**
75 	Returns the number of packets currently available in the ring-buffer.
76 	*/
77 	long getAvailablePacketCount() const
78 	{
79 		checkClassBinding!(typeof(this))();
80 		return ptrcall!(long)(GDNativeClassBinding.getAvailablePacketCount, _godot_object);
81 	}
82 	/**
83 	
84 	*/
85 	long getEncodeBufferMaxSize() const
86 	{
87 		checkClassBinding!(typeof(this))();
88 		return ptrcall!(long)(GDNativeClassBinding.getEncodeBufferMaxSize, _godot_object);
89 	}
90 	/**
91 	Gets a raw packet.
92 	*/
93 	PoolByteArray getPacket()
94 	{
95 		checkClassBinding!(typeof(this))();
96 		return ptrcall!(PoolByteArray)(GDNativeClassBinding.getPacket, _godot_object);
97 	}
98 	/**
99 	Returns the error state of the last packet received (via $(D getPacket) and $(D getVar)).
100 	*/
101 	GodotError getPacketError() const
102 	{
103 		checkClassBinding!(typeof(this))();
104 		return ptrcall!(GodotError)(GDNativeClassBinding.getPacketError, _godot_object);
105 	}
106 	/**
107 	Gets a Variant. If `allow_objects` (or $(D allowObjectDecoding)) is `true`, decoding objects is allowed.
108 	$(B Warning:) Deserialized objects can contain code which gets executed. Do not use this option if the serialized object comes from untrusted sources to avoid potential security threats such as remote code execution.
109 	*/
110 	Variant getVar(in bool allow_objects = false)
111 	{
112 		checkClassBinding!(typeof(this))();
113 		return ptrcall!(Variant)(GDNativeClassBinding.getVar, _godot_object, allow_objects);
114 	}
115 	/**
116 	
117 	*/
118 	bool isObjectDecodingAllowed() const
119 	{
120 		checkClassBinding!(typeof(this))();
121 		return ptrcall!(bool)(GDNativeClassBinding.isObjectDecodingAllowed, _godot_object);
122 	}
123 	/**
124 	Sends a raw packet.
125 	*/
126 	GodotError putPacket(in PoolByteArray buffer)
127 	{
128 		checkClassBinding!(typeof(this))();
129 		return ptrcall!(GodotError)(GDNativeClassBinding.putPacket, _godot_object, buffer);
130 	}
131 	/**
132 	Sends a $(D Variant) as a packet. If `full_objects` (or $(D allowObjectDecoding)) is `true`, encoding objects is allowed (and can potentially include code).
133 	*/
134 	GodotError putVar(VariantArg0)(in VariantArg0 var, in bool full_objects = false)
135 	{
136 		checkClassBinding!(typeof(this))();
137 		return ptrcall!(GodotError)(GDNativeClassBinding.putVar, _godot_object, var, full_objects);
138 	}
139 	/**
140 	
141 	*/
142 	void setAllowObjectDecoding(in bool enable)
143 	{
144 		checkClassBinding!(typeof(this))();
145 		ptrcall!(void)(GDNativeClassBinding.setAllowObjectDecoding, _godot_object, enable);
146 	}
147 	/**
148 	
149 	*/
150 	void setEncodeBufferMaxSize(in long max_size)
151 	{
152 		checkClassBinding!(typeof(this))();
153 		ptrcall!(void)(GDNativeClassBinding.setEncodeBufferMaxSize, _godot_object, max_size);
154 	}
155 	/**
156 	$(I Deprecated.) Use `get_var` and `put_var` parameters instead.
157 	If `true`, the PacketPeer will allow encoding and decoding of object via $(D getVar) and $(D putVar).
158 	$(B Warning:) Deserialized objects can contain code which gets executed. Do not use this option if the serialized object comes from untrusted sources to avoid potential security threats such as remote code execution.
159 	*/
160 	@property bool allowObjectDecoding()
161 	{
162 		return isObjectDecodingAllowed();
163 	}
164 	/// ditto
165 	@property void allowObjectDecoding(bool v)
166 	{
167 		setAllowObjectDecoding(v);
168 	}
169 	/**
170 	Maximum buffer size allowed when encoding $(D Variant)s. Raise this value to support heavier memory allocations.
171 	The $(D putVar) method allocates memory on the stack, and the buffer used will grow automatically to the closest power of two to match the size of the $(D Variant). If the $(D Variant) is bigger than `encode_buffer_max_size`, the method will error out with $(D constant ERR_OUT_OF_MEMORY).
172 	*/
173 	@property long encodeBufferMaxSize()
174 	{
175 		return getEncodeBufferMaxSize();
176 	}
177 	/// ditto
178 	@property void encodeBufferMaxSize(long v)
179 	{
180 		setEncodeBufferMaxSize(v);
181 	}
182 }