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.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.packetpeer; 23 import godot.reference; 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. 28 */ 29 @GodotBaseClass struct NetworkedMultiplayerPeer 30 { 31 enum string _GODOT_internal_name = "NetworkedMultiplayerPeer"; 32 public: 33 @nogc nothrow: 34 union { godot_object _godot_object; PacketPeer _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 _classBinding 39 { 40 __gshared: 41 @GodotName("set_transfer_mode") GodotMethod!(void, long) setTransferMode; 42 @GodotName("get_transfer_mode") GodotMethod!(NetworkedMultiplayerPeer.TransferMode) getTransferMode; 43 @GodotName("set_target_peer") GodotMethod!(void, long) setTargetPeer; 44 @GodotName("get_packet_peer") GodotMethod!(long) getPacketPeer; 45 @GodotName("poll") GodotMethod!(void) poll; 46 @GodotName("get_connection_status") GodotMethod!(NetworkedMultiplayerPeer.ConnectionStatus) getConnectionStatus; 47 @GodotName("get_unique_id") GodotMethod!(long) getUniqueId; 48 @GodotName("set_refuse_new_connections") GodotMethod!(void, bool) setRefuseNewConnections; 49 @GodotName("is_refusing_new_connections") GodotMethod!(bool) isRefusingNewConnections; 50 } 51 bool opEquals(in NetworkedMultiplayerPeer other) const { return _godot_object.ptr is other._godot_object.ptr; } 52 NetworkedMultiplayerPeer opAssign(T : typeof(null))(T n) { _godot_object.ptr = null; } 53 bool opEquals(typeof(null) n) const { return _godot_object.ptr is null; } 54 mixin baseCasts; 55 static NetworkedMultiplayerPeer _new() 56 { 57 static godot_class_constructor constructor; 58 if(constructor is null) constructor = _godot_api.godot_get_class_constructor("NetworkedMultiplayerPeer"); 59 if(constructor is null) return typeof(this).init; 60 return cast(NetworkedMultiplayerPeer)(constructor()); 61 } 62 @disable new(size_t s); 63 /// 64 enum ConnectionStatus : int 65 { 66 /** 67 The ongoing connection disconnected. 68 */ 69 connectionDisconnected = 0, 70 /** 71 A connection attempt is ongoing. 72 */ 73 connectionConnecting = 1, 74 /** 75 The connection attempt succeeded. 76 */ 77 connectionConnected = 2, 78 } 79 /// 80 enum TransferMode : int 81 { 82 /** 83 Packets are not acknowledged, no resend attempts are made for lost packets. Packets may arrive in any order. Potentially faster than `TRANSFER_MODE_UNRELIABLE_ORDERED`. Use for non-critical data, and always consider whether the order matters. 84 */ 85 transferModeUnreliable = 0, 86 /** 87 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 `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. 88 */ 89 transferModeUnreliableOrdered = 1, 90 /** 91 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 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. 92 */ 93 transferModeReliable = 2, 94 } 95 /// 96 enum Constants : int 97 { 98 connectionDisconnected = 0, 99 /** 100 Packets are sent to the server and then redistributed to other peers. 101 */ 102 targetPeerBroadcast = 0, 103 transferModeUnreliable = 0, 104 connectionConnecting = 1, 105 /** 106 Packets are sent to the server alone. 107 */ 108 targetPeerServer = 1, 109 transferModeUnreliableOrdered = 1, 110 connectionConnected = 2, 111 transferModeReliable = 2, 112 } 113 /** 114 115 */ 116 void setTransferMode(in long mode) 117 { 118 checkClassBinding!(typeof(this))(); 119 ptrcall!(void)(_classBinding.setTransferMode, _godot_object, mode); 120 } 121 /** 122 123 */ 124 NetworkedMultiplayerPeer.TransferMode getTransferMode() const 125 { 126 checkClassBinding!(typeof(this))(); 127 return ptrcall!(NetworkedMultiplayerPeer.TransferMode)(_classBinding.getTransferMode, _godot_object); 128 } 129 /** 130 Sets the peer to which packets will be sent. 131 The `id` can be one of: `TARGET_PEER_BROADCAST` to send to all connected peers, `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. Default: `TARGET_PEER_BROADCAST` 132 */ 133 void setTargetPeer(in long id) 134 { 135 checkClassBinding!(typeof(this))(); 136 ptrcall!(void)(_classBinding.setTargetPeer, _godot_object, id); 137 } 138 /** 139 Returns the ID of the `NetworkedMultiplayerPeer` who sent the most recent packet. 140 */ 141 long getPacketPeer() const 142 { 143 checkClassBinding!(typeof(this))(); 144 return ptrcall!(long)(_classBinding.getPacketPeer, _godot_object); 145 } 146 /** 147 Waits up to 1 second to receive a new network event. 148 */ 149 void poll() 150 { 151 checkClassBinding!(typeof(this))(); 152 ptrcall!(void)(_classBinding.poll, _godot_object); 153 } 154 /** 155 Returns the current state of the connection. See $(D connectionstatus). 156 */ 157 NetworkedMultiplayerPeer.ConnectionStatus getConnectionStatus() const 158 { 159 checkClassBinding!(typeof(this))(); 160 return ptrcall!(NetworkedMultiplayerPeer.ConnectionStatus)(_classBinding.getConnectionStatus, _godot_object); 161 } 162 /** 163 Returns the ID of this `NetworkedMultiplayerPeer`. 164 */ 165 long getUniqueId() const 166 { 167 checkClassBinding!(typeof(this))(); 168 return ptrcall!(long)(_classBinding.getUniqueId, _godot_object); 169 } 170 /** 171 172 */ 173 void setRefuseNewConnections(in bool enable) 174 { 175 checkClassBinding!(typeof(this))(); 176 ptrcall!(void)(_classBinding.setRefuseNewConnections, _godot_object, enable); 177 } 178 /** 179 180 */ 181 bool isRefusingNewConnections() const 182 { 183 checkClassBinding!(typeof(this))(); 184 return ptrcall!(bool)(_classBinding.isRefusingNewConnections, _godot_object); 185 } 186 /** 187 If `true` this `NetworkedMultiplayerPeer` refuses new connections. Default value: `false`. 188 */ 189 @property bool refuseNewConnections() 190 { 191 return isRefusingNewConnections(); 192 } 193 /// ditto 194 @property void refuseNewConnections(bool v) 195 { 196 setRefuseNewConnections(v); 197 } 198 /** 199 The manner in which to send packets to the `target_peer`. See $(D transfermode). 200 */ 201 @property NetworkedMultiplayerPeer.TransferMode transferMode() 202 { 203 return getTransferMode(); 204 } 205 /// ditto 206 @property void transferMode(long v) 207 { 208 setTransferMode(v); 209 } 210 }