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.meta;
17 import godot.core;
18 import godot.c;
19 import godot.d.bind;
20 import godot.d.reference;
21 import godot.object;
22 import godot.reference;
23 /**
24 Abstraction and base class for packet-based protocols.
25 
26 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.
27 */
28 @GodotBaseClass struct PacketPeer
29 {
30 	enum string _GODOT_internal_name = "PacketPeer";
31 public:
32 @nogc nothrow:
33 	union { godot_object _godot_object; Reference _GODOT_base; }
34 	alias _GODOT_base this;
35 	alias BaseClasses = AliasSeq!(typeof(_GODOT_base), typeof(_GODOT_base).BaseClasses);
36 	package(godot) __gshared bool _classBindingInitialized = false;
37 	package(godot) static struct _classBinding
38 	{
39 		__gshared:
40 		@GodotName("get_var") GodotMethod!(Variant) getVar;
41 		@GodotName("put_var") GodotMethod!(GodotError, Variant) putVar;
42 		@GodotName("get_packet") GodotMethod!(PoolByteArray) getPacket;
43 		@GodotName("put_packet") GodotMethod!(GodotError, PoolByteArray) putPacket;
44 		@GodotName("get_packet_error") GodotMethod!(GodotError) getPacketError;
45 		@GodotName("get_available_packet_count") GodotMethod!(long) getAvailablePacketCount;
46 		@GodotName("set_allow_object_decoding") GodotMethod!(void, bool) setAllowObjectDecoding;
47 		@GodotName("is_object_decoding_allowed") GodotMethod!(bool) isObjectDecodingAllowed;
48 	}
49 	bool opEquals(in PacketPeer other) const { return _godot_object.ptr is other._godot_object.ptr; }
50 	PacketPeer opAssign(T : typeof(null))(T n) { _godot_object.ptr = null; }
51 	bool opEquals(typeof(null) n) const { return _godot_object.ptr is null; }
52 	mixin baseCasts;
53 	static PacketPeer _new()
54 	{
55 		static godot_class_constructor constructor;
56 		if(constructor is null) constructor = _godot_api.godot_get_class_constructor("PacketPeer");
57 		if(constructor is null) return typeof(this).init;
58 		return cast(PacketPeer)(constructor());
59 	}
60 	@disable new(size_t s);
61 	/**
62 	Get a Variant.
63 	*/
64 	Variant getVar()
65 	{
66 		checkClassBinding!(typeof(this))();
67 		return ptrcall!(Variant)(_classBinding.getVar, _godot_object);
68 	}
69 	/**
70 	Send a Variant as a packet.
71 	*/
72 	GodotError putVar(VariantArg0)(in VariantArg0 var)
73 	{
74 		checkClassBinding!(typeof(this))();
75 		return ptrcall!(GodotError)(_classBinding.putVar, _godot_object, var);
76 	}
77 	/**
78 	Get a raw packet.
79 	*/
80 	PoolByteArray getPacket()
81 	{
82 		checkClassBinding!(typeof(this))();
83 		return ptrcall!(PoolByteArray)(_classBinding.getPacket, _godot_object);
84 	}
85 	/**
86 	Send a raw packet.
87 	*/
88 	GodotError putPacket(in PoolByteArray buffer)
89 	{
90 		checkClassBinding!(typeof(this))();
91 		return ptrcall!(GodotError)(_classBinding.putPacket, _godot_object, buffer);
92 	}
93 	/**
94 	Return the error state of the last packet received (via $(D getPacket) and $(D getVar)).
95 	*/
96 	GodotError getPacketError() const
97 	{
98 		checkClassBinding!(typeof(this))();
99 		return ptrcall!(GodotError)(_classBinding.getPacketError, _godot_object);
100 	}
101 	/**
102 	Return the number of packets currently available in the ring-buffer.
103 	*/
104 	long getAvailablePacketCount() const
105 	{
106 		checkClassBinding!(typeof(this))();
107 		return ptrcall!(long)(_classBinding.getAvailablePacketCount, _godot_object);
108 	}
109 	/**
110 	
111 	*/
112 	void setAllowObjectDecoding(in bool enable)
113 	{
114 		checkClassBinding!(typeof(this))();
115 		ptrcall!(void)(_classBinding.setAllowObjectDecoding, _godot_object, enable);
116 	}
117 	/**
118 	
119 	*/
120 	bool isObjectDecodingAllowed() const
121 	{
122 		checkClassBinding!(typeof(this))();
123 		return ptrcall!(bool)(_classBinding.isObjectDecodingAllowed, _godot_object);
124 	}
125 	/**
126 	
127 	*/
128 	@property bool allowObjectDecoding()
129 	{
130 		return isObjectDecodingAllowed();
131 	}
132 	/// ditto
133 	@property void allowObjectDecoding(bool v)
134 	{
135 		setAllowObjectDecoding(v);
136 	}
137 }