1 /** 2 A high-level network interface to simplify multiplayer interactions. 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.networkedmultiplayerpeer; 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.packetpeer; 24 /** 25 A high-level network interface to simplify multiplayer interactions. 26 27 Manages the connection to network peers. Assigns unique IDs to each client connected to the server. See also $(D MultiplayerAPI). 28 $(B Note:) The high-level multiplayer API protocol is an implementation detail and isn't meant to be used by non-Godot servers. It may change without notice. 29 */ 30 @GodotBaseClass struct NetworkedMultiplayerPeer 31 { 32 package(godot) enum string _GODOT_internal_name = "NetworkedMultiplayerPeer"; 33 public: 34 @nogc nothrow: 35 union { /** */ godot_object _godot_object; /** */ PacketPeer _GODOT_base; } 36 alias _GODOT_base this; 37 alias BaseClasses = AliasSeq!(typeof(_GODOT_base), typeof(_GODOT_base).BaseClasses); 38 package(godot) __gshared bool _classBindingInitialized = false; 39 package(godot) static struct GDNativeClassBinding 40 { 41 __gshared: 42 @GodotName("get_connection_status") GodotMethod!(NetworkedMultiplayerPeer.ConnectionStatus) getConnectionStatus; 43 @GodotName("get_packet_peer") GodotMethod!(long) getPacketPeer; 44 @GodotName("get_transfer_mode") GodotMethod!(NetworkedMultiplayerPeer.TransferMode) getTransferMode; 45 @GodotName("get_unique_id") GodotMethod!(long) getUniqueId; 46 @GodotName("is_refusing_new_connections") GodotMethod!(bool) isRefusingNewConnections; 47 @GodotName("poll") GodotMethod!(void) poll; 48 @GodotName("set_refuse_new_connections") GodotMethod!(void, bool) setRefuseNewConnections; 49 @GodotName("set_target_peer") GodotMethod!(void, long) setTargetPeer; 50 @GodotName("set_transfer_mode") GodotMethod!(void, long) setTransferMode; 51 } 52 /// 53 pragma(inline, true) bool opEquals(in NetworkedMultiplayerPeer 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 NetworkedMultiplayerPeer. 65 /// Note: use `memnew!NetworkedMultiplayerPeer` instead. 66 static NetworkedMultiplayerPeer _new() 67 { 68 static godot_class_constructor constructor; 69 if(constructor is null) constructor = _godot_api.godot_get_class_constructor("NetworkedMultiplayerPeer"); 70 if(constructor is null) return typeof(this).init; 71 return cast(NetworkedMultiplayerPeer)(constructor()); 72 } 73 @disable new(size_t s); 74 /// 75 enum ConnectionStatus : int 76 { 77 /** 78 The ongoing connection disconnected. 79 */ 80 connectionDisconnected = 0, 81 /** 82 A connection attempt is ongoing. 83 */ 84 connectionConnecting = 1, 85 /** 86 The connection attempt succeeded. 87 */ 88 connectionConnected = 2, 89 } 90 /// 91 enum TransferMode : int 92 { 93 /** 94 Packets are not acknowledged, no resend attempts are made for lost packets. Packets may arrive in any order. Potentially faster than $(D constant TRANSFER_MODE_UNRELIABLE_ORDERED). Use for non-critical data, and always consider whether the order matters. 95 */ 96 transferModeUnreliable = 0, 97 /** 98 Packets are not acknowledged, no resend attempts are made for lost packets. Packets are received in the order they were sent in. Potentially faster than $(D constant TRANSFER_MODE_RELIABLE). Use for non-critical data or data that would be outdated if received late due to resend attempt(s) anyway, for example movement and positional data. 99 */ 100 transferModeUnreliableOrdered = 1, 101 /** 102 Packets must be received and resend attempts should be made until the packets are acknowledged. Packets must be received in the order they were sent in. Most reliable transfer mode, but potentially the slowest due to the overhead. Use for critical data that must be transmitted and arrive in order, for example an ability being triggered or a chat message. Consider carefully if the information really is critical, and use sparingly. 103 */ 104 transferModeReliable = 2, 105 } 106 /// 107 enum Constants : int 108 { 109 connectionDisconnected = 0, 110 /** 111 Packets are sent to the server and then redistributed to other peers. 112 */ 113 targetPeerBroadcast = 0, 114 transferModeUnreliable = 0, 115 connectionConnecting = 1, 116 /** 117 Packets are sent to the server alone. 118 */ 119 targetPeerServer = 1, 120 transferModeUnreliableOrdered = 1, 121 connectionConnected = 2, 122 transferModeReliable = 2, 123 } 124 /** 125 Returns the current state of the connection. See $(D connectionstatus). 126 */ 127 NetworkedMultiplayerPeer.ConnectionStatus getConnectionStatus() const 128 { 129 checkClassBinding!(typeof(this))(); 130 return ptrcall!(NetworkedMultiplayerPeer.ConnectionStatus)(GDNativeClassBinding.getConnectionStatus, _godot_object); 131 } 132 /** 133 Returns the ID of the $(D NetworkedMultiplayerPeer) who sent the most recent packet. 134 */ 135 long getPacketPeer() const 136 { 137 checkClassBinding!(typeof(this))(); 138 return ptrcall!(long)(GDNativeClassBinding.getPacketPeer, _godot_object); 139 } 140 /** 141 142 */ 143 NetworkedMultiplayerPeer.TransferMode getTransferMode() const 144 { 145 checkClassBinding!(typeof(this))(); 146 return ptrcall!(NetworkedMultiplayerPeer.TransferMode)(GDNativeClassBinding.getTransferMode, _godot_object); 147 } 148 /** 149 Returns the ID of this $(D NetworkedMultiplayerPeer). 150 */ 151 long getUniqueId() const 152 { 153 checkClassBinding!(typeof(this))(); 154 return ptrcall!(long)(GDNativeClassBinding.getUniqueId, _godot_object); 155 } 156 /** 157 158 */ 159 bool isRefusingNewConnections() const 160 { 161 checkClassBinding!(typeof(this))(); 162 return ptrcall!(bool)(GDNativeClassBinding.isRefusingNewConnections, _godot_object); 163 } 164 /** 165 Waits up to 1 second to receive a new network event. 166 */ 167 void poll() 168 { 169 checkClassBinding!(typeof(this))(); 170 ptrcall!(void)(GDNativeClassBinding.poll, _godot_object); 171 } 172 /** 173 174 */ 175 void setRefuseNewConnections(in bool enable) 176 { 177 checkClassBinding!(typeof(this))(); 178 ptrcall!(void)(GDNativeClassBinding.setRefuseNewConnections, _godot_object, enable); 179 } 180 /** 181 Sets the peer to which packets will be sent. 182 The `id` can be one of: $(D constant TARGET_PEER_BROADCAST) to send to all connected peers, $(D constant TARGET_PEER_SERVER) to send to the peer acting as server, a valid peer ID to send to that specific peer, a negative peer ID to send to all peers except that one. By default, the target peer is $(D constant TARGET_PEER_BROADCAST). 183 */ 184 void setTargetPeer(in long id) 185 { 186 checkClassBinding!(typeof(this))(); 187 ptrcall!(void)(GDNativeClassBinding.setTargetPeer, _godot_object, id); 188 } 189 /** 190 191 */ 192 void setTransferMode(in long mode) 193 { 194 checkClassBinding!(typeof(this))(); 195 ptrcall!(void)(GDNativeClassBinding.setTransferMode, _godot_object, mode); 196 } 197 /** 198 If `true`, this $(D NetworkedMultiplayerPeer) refuses new connections. 199 */ 200 @property bool refuseNewConnections() 201 { 202 return isRefusingNewConnections(); 203 } 204 /// ditto 205 @property void refuseNewConnections(bool v) 206 { 207 setRefuseNewConnections(v); 208 } 209 /** 210 The manner in which to send packets to the `target_peer`. See $(D transfermode). 211 */ 212 @property NetworkedMultiplayerPeer.TransferMode transferMode() 213 { 214 return getTransferMode(); 215 } 216 /// ditto 217 @property void transferMode(long v) 218 { 219 setTransferMode(v); 220 } 221 }