1 /**
2 UDP packet peer.
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.packetpeerudp;
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.classdb;
24 import godot.packetpeer;
25 import godot.reference;
26 /**
27 UDP packet peer.
28 
29 Can be used to send raw UDP packets as well as $(D Variant)s.
30 */
31 @GodotBaseClass struct PacketPeerUDP
32 {
33 	package(godot) enum string _GODOT_internal_name = "PacketPeerUDP";
34 public:
35 @nogc nothrow:
36 	union { /** */ godot_object _godot_object; /** */ PacketPeer _GODOT_base; }
37 	alias _GODOT_base this;
38 	alias BaseClasses = AliasSeq!(typeof(_GODOT_base), typeof(_GODOT_base).BaseClasses);
39 	package(godot) __gshared bool _classBindingInitialized = false;
40 	package(godot) static struct GDNativeClassBinding
41 	{
42 		__gshared:
43 		@GodotName("close") GodotMethod!(void) close;
44 		@GodotName("connect_to_host") GodotMethod!(GodotError, String, long) connectToHost;
45 		@GodotName("get_packet_ip") GodotMethod!(String) getPacketIp;
46 		@GodotName("get_packet_port") GodotMethod!(long) getPacketPort;
47 		@GodotName("is_connected_to_host") GodotMethod!(bool) isConnectedToHost;
48 		@GodotName("is_listening") GodotMethod!(bool) isListening;
49 		@GodotName("join_multicast_group") GodotMethod!(GodotError, String, String) joinMulticastGroup;
50 		@GodotName("leave_multicast_group") GodotMethod!(GodotError, String, String) leaveMulticastGroup;
51 		@GodotName("listen") GodotMethod!(GodotError, long, String, long) listen;
52 		@GodotName("set_broadcast_enabled") GodotMethod!(void, bool) setBroadcastEnabled;
53 		@GodotName("set_dest_address") GodotMethod!(GodotError, String, long) setDestAddress;
54 		@GodotName("wait") GodotMethod!(GodotError) wait;
55 	}
56 	/// 
57 	pragma(inline, true) bool opEquals(in PacketPeerUDP other) const
58 	{ return _godot_object.ptr is other._godot_object.ptr; }
59 	/// 
60 	pragma(inline, true) typeof(null) opAssign(typeof(null) n)
61 	{ _godot_object.ptr = n; return null; }
62 	/// 
63 	pragma(inline, true) bool opEquals(typeof(null) n) const
64 	{ return _godot_object.ptr is n; }
65 	/// 
66 	size_t toHash() const @trusted { return cast(size_t)_godot_object.ptr; }
67 	mixin baseCasts;
68 	/// Construct a new instance of PacketPeerUDP.
69 	/// Note: use `memnew!PacketPeerUDP` instead.
70 	static PacketPeerUDP _new()
71 	{
72 		static godot_class_constructor constructor;
73 		if(constructor is null) constructor = _godot_api.godot_get_class_constructor("PacketPeerUDP");
74 		if(constructor is null) return typeof(this).init;
75 		return cast(PacketPeerUDP)(constructor());
76 	}
77 	@disable new(size_t s);
78 	/**
79 	Closes the UDP socket the $(D PacketPeerUDP) is currently listening on.
80 	*/
81 	void close()
82 	{
83 		checkClassBinding!(typeof(this))();
84 		ptrcall!(void)(GDNativeClassBinding.close, _godot_object);
85 	}
86 	/**
87 	Calling this method connects this UDP peer to the given `host`/`port` pair. UDP is in reality connectionless, so this option only means that incoming packets from different addresses are automatically discarded, and that outgoing packets are always sent to the connected address (future calls to $(D setDestAddress) are not allowed). This method does not send any data to the remote peer, to do that, use $(D PacketPeer.putVar) or $(D PacketPeer.putPacket) as usual. See also $(D UDPServer).
88 	$(B Note:) Connecting to the remote peer does not help to protect from malicious attacks like IP spoofing, etc. Think about using an encryption technique like SSL or DTLS if you feel like your application is transferring sensitive information.
89 	*/
90 	GodotError connectToHost(in String host, in long port)
91 	{
92 		checkClassBinding!(typeof(this))();
93 		return ptrcall!(GodotError)(GDNativeClassBinding.connectToHost, _godot_object, host, port);
94 	}
95 	/**
96 	Returns the IP of the remote peer that sent the last packet(that was received with $(D PacketPeer.getPacket) or $(D PacketPeer.getVar)).
97 	*/
98 	String getPacketIp() const
99 	{
100 		checkClassBinding!(typeof(this))();
101 		return ptrcall!(String)(GDNativeClassBinding.getPacketIp, _godot_object);
102 	}
103 	/**
104 	Returns the port of the remote peer that sent the last packet(that was received with $(D PacketPeer.getPacket) or $(D PacketPeer.getVar)).
105 	*/
106 	long getPacketPort() const
107 	{
108 		checkClassBinding!(typeof(this))();
109 		return ptrcall!(long)(GDNativeClassBinding.getPacketPort, _godot_object);
110 	}
111 	/**
112 	Returns `true` if the UDP socket is open and has been connected to a remote address. See $(D connectToHost).
113 	*/
114 	bool isConnectedToHost() const
115 	{
116 		checkClassBinding!(typeof(this))();
117 		return ptrcall!(bool)(GDNativeClassBinding.isConnectedToHost, _godot_object);
118 	}
119 	/**
120 	Returns whether this $(D PacketPeerUDP) is listening.
121 	*/
122 	bool isListening() const
123 	{
124 		checkClassBinding!(typeof(this))();
125 		return ptrcall!(bool)(GDNativeClassBinding.isListening, _godot_object);
126 	}
127 	/**
128 	Joins the multicast group specified by `multicast_address` using the interface identified by `interface_name`.
129 	You can join the same multicast group with multiple interfaces. Use $(D IP.getLocalInterfaces) to know which are available.
130 	Note: Some Android devices might require the `CHANGE_WIFI_MULTICAST_STATE` permission for multicast to work.
131 	*/
132 	GodotError joinMulticastGroup(in String multicast_address, in String interface_name)
133 	{
134 		checkClassBinding!(typeof(this))();
135 		return ptrcall!(GodotError)(GDNativeClassBinding.joinMulticastGroup, _godot_object, multicast_address, interface_name);
136 	}
137 	/**
138 	Removes the interface identified by `interface_name` from the multicast group specified by `multicast_address`.
139 	*/
140 	GodotError leaveMulticastGroup(in String multicast_address, in String interface_name)
141 	{
142 		checkClassBinding!(typeof(this))();
143 		return ptrcall!(GodotError)(GDNativeClassBinding.leaveMulticastGroup, _godot_object, multicast_address, interface_name);
144 	}
145 	/**
146 	Makes this $(D PacketPeerUDP) listen on the `port` binding to `bind_address` with a buffer size `recv_buf_size`.
147 	If `bind_address` is set to `"*"` (default), the peer will listen on all available addresses (both IPv4 and IPv6).
148 	If `bind_address` is set to `"0.0.0.0"` (for IPv4) or `"::"` (for IPv6), the peer will listen on all available addresses matching that IP type.
149 	If `bind_address` is set to any valid address (e.g. `"192.168.1.101"`, `"::1"`, etc), the peer will only listen on the interface with that addresses (or fail if no interface with the given address exists).
150 	*/
151 	GodotError listen(in long port, in String bind_address = gs!"*", in long recv_buf_size = 65536)
152 	{
153 		checkClassBinding!(typeof(this))();
154 		return ptrcall!(GodotError)(GDNativeClassBinding.listen, _godot_object, port, bind_address, recv_buf_size);
155 	}
156 	/**
157 	Enable or disable sending of broadcast packets (e.g. `set_dest_address("255.255.255.255", 4343)`. This option is disabled by default.
158 	Note: Some Android devices might require the `CHANGE_WIFI_MULTICAST_STATE` permission and this option to be enabled to receive broadcast packets too.
159 	*/
160 	void setBroadcastEnabled(in bool enabled)
161 	{
162 		checkClassBinding!(typeof(this))();
163 		ptrcall!(void)(GDNativeClassBinding.setBroadcastEnabled, _godot_object, enabled);
164 	}
165 	/**
166 	Sets the destination address and port for sending packets and variables. A hostname will be resolved using DNS if needed.
167 	Note: $(D setBroadcastEnabled) must be enabled before sending packets to a broadcast address (e.g. `255.255.255.255`).
168 	*/
169 	GodotError setDestAddress(in String host, in long port)
170 	{
171 		checkClassBinding!(typeof(this))();
172 		return ptrcall!(GodotError)(GDNativeClassBinding.setDestAddress, _godot_object, host, port);
173 	}
174 	/**
175 	Waits for a packet to arrive on the listening port. See $(D listen).
176 	$(B Note:) $(D wait) can't be interrupted once it has been called. This can be worked around by allowing the other party to send a specific "death pill" packet like this:
177 	
178 	
179 	# Server
180 	socket.set_dest_address("127.0.0.1", 789)
181 	socket.put_packet("Time to stop".to_ascii())
182 	
183 	# Client
184 	while socket.wait() == OK:
185 	    var data = socket.get_packet().get_string_from_ascii()
186 	    if data == "Time to stop":
187 	        return
188 	
189 	
190 	*/
191 	GodotError wait()
192 	{
193 		checkClassBinding!(typeof(this))();
194 		return ptrcall!(GodotError)(GDNativeClassBinding.wait, _godot_object);
195 	}
196 }