1 /**
2 A simple interface to create a peer-to-peer mesh network composed of $(D WebRTCPeerConnection) that is compatible with the $(D MultiplayerAPI).
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.webrtcmultiplayer;
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.networkedmultiplayerpeer;
25 import godot.packetpeer;
26 import godot.reference;
27 import godot.webrtcpeerconnection;
28 /**
29 A simple interface to create a peer-to-peer mesh network composed of $(D WebRTCPeerConnection) that is compatible with the $(D MultiplayerAPI).
30 
31 This class constructs a full mesh of $(D WebRTCPeerConnection) (one connection for each peer) that can be used as a $(D MultiplayerAPI.networkPeer).
32 You can add each $(D WebRTCPeerConnection) via $(D addPeer) or remove them via $(D removePeer). Peers must be added in $(D constant WebRTCPeerConnection.STATE_NEW) state to allow it to create the appropriate channels. This class will not create offers nor set descriptions, it will only poll them, and notify connections and disconnections.
33 $(D NetworkedMultiplayerPeer.connectionSucceeded) and $(D NetworkedMultiplayerPeer.serverDisconnected) will not be emitted unless `server_compatibility` is `true` in $(D initialize). Beside that data transfer works like in a $(D NetworkedMultiplayerPeer).
34 */
35 @GodotBaseClass struct WebRTCMultiplayer
36 {
37 	package(godot) enum string _GODOT_internal_name = "WebRTCMultiplayer";
38 public:
39 @nogc nothrow:
40 	union { /** */ godot_object _godot_object; /** */ NetworkedMultiplayerPeer _GODOT_base; }
41 	alias _GODOT_base this;
42 	alias BaseClasses = AliasSeq!(typeof(_GODOT_base), typeof(_GODOT_base).BaseClasses);
43 	package(godot) __gshared bool _classBindingInitialized = false;
44 	package(godot) static struct GDNativeClassBinding
45 	{
46 		__gshared:
47 		@GodotName("add_peer") GodotMethod!(GodotError, WebRTCPeerConnection, long, long) addPeer;
48 		@GodotName("close") GodotMethod!(void) close;
49 		@GodotName("get_peer") GodotMethod!(Dictionary, long) getPeer;
50 		@GodotName("get_peers") GodotMethod!(Dictionary) getPeers;
51 		@GodotName("has_peer") GodotMethod!(bool, long) hasPeer;
52 		@GodotName("initialize") GodotMethod!(GodotError, long, bool) initialize;
53 		@GodotName("remove_peer") GodotMethod!(void, long) removePeer;
54 	}
55 	/// 
56 	pragma(inline, true) bool opEquals(in WebRTCMultiplayer other) const
57 	{ return _godot_object.ptr is other._godot_object.ptr; }
58 	/// 
59 	pragma(inline, true) typeof(null) opAssign(typeof(null) n)
60 	{ _godot_object.ptr = n; return null; }
61 	/// 
62 	pragma(inline, true) bool opEquals(typeof(null) n) const
63 	{ return _godot_object.ptr is n; }
64 	/// 
65 	size_t toHash() const @trusted { return cast(size_t)_godot_object.ptr; }
66 	mixin baseCasts;
67 	/// Construct a new instance of WebRTCMultiplayer.
68 	/// Note: use `memnew!WebRTCMultiplayer` instead.
69 	static WebRTCMultiplayer _new()
70 	{
71 		static godot_class_constructor constructor;
72 		if(constructor is null) constructor = _godot_api.godot_get_class_constructor("WebRTCMultiplayer");
73 		if(constructor is null) return typeof(this).init;
74 		return cast(WebRTCMultiplayer)(constructor());
75 	}
76 	@disable new(size_t s);
77 	/**
78 	Add a new peer to the mesh with the given `peer_id`. The $(D WebRTCPeerConnection) must be in state $(D constant WebRTCPeerConnection.STATE_NEW).
79 	Three channels will be created for reliable, unreliable, and ordered transport. The value of `unreliable_lifetime` will be passed to the `maxPacketLifetime` option when creating unreliable and ordered channels (see $(D WebRTCPeerConnection.createDataChannel)).
80 	*/
81 	GodotError addPeer(WebRTCPeerConnection peer, in long peer_id, in long unreliable_lifetime = 1)
82 	{
83 		checkClassBinding!(typeof(this))();
84 		return ptrcall!(GodotError)(GDNativeClassBinding.addPeer, _godot_object, peer, peer_id, unreliable_lifetime);
85 	}
86 	/**
87 	Close all the add peer connections and channels, freeing all resources.
88 	*/
89 	void close()
90 	{
91 		checkClassBinding!(typeof(this))();
92 		ptrcall!(void)(GDNativeClassBinding.close, _godot_object);
93 	}
94 	/**
95 	Return a dictionary representation of the peer with given `peer_id` with three keys. `connection` containing the $(D WebRTCPeerConnection) to this peer, `channels` an array of three $(D WebRTCDataChannel), and `connected` a boolean representing if the peer connection is currently connected (all three channels are open).
96 	*/
97 	Dictionary getPeer(in long peer_id)
98 	{
99 		checkClassBinding!(typeof(this))();
100 		return ptrcall!(Dictionary)(GDNativeClassBinding.getPeer, _godot_object, peer_id);
101 	}
102 	/**
103 	Returns a dictionary which keys are the peer ids and values the peer representation as in $(D getPeer).
104 	*/
105 	Dictionary getPeers()
106 	{
107 		checkClassBinding!(typeof(this))();
108 		return ptrcall!(Dictionary)(GDNativeClassBinding.getPeers, _godot_object);
109 	}
110 	/**
111 	Returns `true` if the given `peer_id` is in the peers map (it might not be connected though).
112 	*/
113 	bool hasPeer(in long peer_id)
114 	{
115 		checkClassBinding!(typeof(this))();
116 		return ptrcall!(bool)(GDNativeClassBinding.hasPeer, _godot_object, peer_id);
117 	}
118 	/**
119 	Initialize the multiplayer peer with the given `peer_id` (must be between 1 and 2147483647).
120 	If `server_compatibilty` is `false` (default), the multiplayer peer will be immediately in state $(D constant NetworkedMultiplayerPeer.CONNECTION_CONNECTED) and $(D NetworkedMultiplayerPeer.connectionSucceeded) will not be emitted.
121 	If `server_compatibilty` is `true` the peer will suppress all $(D NetworkedMultiplayerPeer.peerConnected) signals until a peer with id $(D constant NetworkedMultiplayerPeer.TARGET_PEER_SERVER) connects and then emit $(D NetworkedMultiplayerPeer.connectionSucceeded). After that the signal $(D NetworkedMultiplayerPeer.peerConnected) will be emitted for every already connected peer, and any new peer that might connect. If the server peer disconnects after that, signal $(D NetworkedMultiplayerPeer.serverDisconnected) will be emitted and state will become $(D constant NetworkedMultiplayerPeer.CONNECTION_CONNECTED).
122 	*/
123 	GodotError initialize(in long peer_id, in bool server_compatibility = false)
124 	{
125 		checkClassBinding!(typeof(this))();
126 		return ptrcall!(GodotError)(GDNativeClassBinding.initialize, _godot_object, peer_id, server_compatibility);
127 	}
128 	/**
129 	Remove the peer with given `peer_id` from the mesh. If the peer was connected, and $(D NetworkedMultiplayerPeer.peerConnected) was emitted for it, then $(D NetworkedMultiplayerPeer.peerDisconnected) will be emitted.
130 	*/
131 	void removePeer(in long peer_id)
132 	{
133 		checkClassBinding!(typeof(this))();
134 		ptrcall!(void)(GDNativeClassBinding.removePeer, _godot_object, peer_id);
135 	}
136 }