1 /** 2 PacketPeer implementation using the $(D url=http://enet.bespin.org/index.html)ENet$(D /url) library. 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.networkedmultiplayerenet; 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.x509certificate; 26 import godot.cryptokey; 27 /** 28 PacketPeer implementation using the $(D url=http://enet.bespin.org/index.html)ENet$(D /url) library. 29 30 A PacketPeer implementation that should be passed to $(D SceneTree.networkPeer) after being initialized as either a client or server. Events can then be handled by connecting to $(D SceneTree) signals. 31 ENet's purpose is to provide a relatively thin, simple and robust network communication layer on top of UDP (User Datagram Protocol). 32 $(B Note:) ENet only uses UDP, not TCP. When forwarding the server port to make your server accessible on the public Internet, you only need to forward the server port in UDP. You can use the $(D UPNP) class to try to forward the server port automatically when starting the server. 33 */ 34 @GodotBaseClass struct NetworkedMultiplayerENet 35 { 36 package(godot) enum string _GODOT_internal_name = "NetworkedMultiplayerENet"; 37 public: 38 @nogc nothrow: 39 union { /** */ godot_object _godot_object; /** */ NetworkedMultiplayerPeer _GODOT_base; } 40 alias _GODOT_base this; 41 alias BaseClasses = AliasSeq!(typeof(_GODOT_base), typeof(_GODOT_base).BaseClasses); 42 package(godot) __gshared bool _classBindingInitialized = false; 43 package(godot) static struct GDNativeClassBinding 44 { 45 __gshared: 46 @GodotName("close_connection") GodotMethod!(void, long) closeConnection; 47 @GodotName("create_client") GodotMethod!(GodotError, String, long, long, long, long) createClient; 48 @GodotName("create_server") GodotMethod!(GodotError, long, long, long, long) createServer; 49 @GodotName("disconnect_peer") GodotMethod!(void, long, bool) disconnectPeer; 50 @GodotName("get_channel_count") GodotMethod!(long) getChannelCount; 51 @GodotName("get_compression_mode") GodotMethod!(NetworkedMultiplayerENet.CompressionMode) getCompressionMode; 52 @GodotName("get_last_packet_channel") GodotMethod!(long) getLastPacketChannel; 53 @GodotName("get_packet_channel") GodotMethod!(long) getPacketChannel; 54 @GodotName("get_peer_address") GodotMethod!(String, long) getPeerAddress; 55 @GodotName("get_peer_port") GodotMethod!(long, long) getPeerPort; 56 @GodotName("get_transfer_channel") GodotMethod!(long) getTransferChannel; 57 @GodotName("is_always_ordered") GodotMethod!(bool) isAlwaysOrdered; 58 @GodotName("is_dtls_enabled") GodotMethod!(bool) isDtlsEnabled; 59 @GodotName("is_dtls_verify_enabled") GodotMethod!(bool) isDtlsVerifyEnabled; 60 @GodotName("is_server_relay_enabled") GodotMethod!(bool) isServerRelayEnabled; 61 @GodotName("set_always_ordered") GodotMethod!(void, bool) setAlwaysOrdered; 62 @GodotName("set_bind_ip") GodotMethod!(void, String) setBindIp; 63 @GodotName("set_channel_count") GodotMethod!(void, long) setChannelCount; 64 @GodotName("set_compression_mode") GodotMethod!(void, long) setCompressionMode; 65 @GodotName("set_dtls_certificate") GodotMethod!(void, X509Certificate) setDtlsCertificate; 66 @GodotName("set_dtls_enabled") GodotMethod!(void, bool) setDtlsEnabled; 67 @GodotName("set_dtls_key") GodotMethod!(void, CryptoKey) setDtlsKey; 68 @GodotName("set_dtls_verify_enabled") GodotMethod!(void, bool) setDtlsVerifyEnabled; 69 @GodotName("set_peer_timeout") GodotMethod!(void, long, long, long, long) setPeerTimeout; 70 @GodotName("set_server_relay_enabled") GodotMethod!(void, bool) setServerRelayEnabled; 71 @GodotName("set_transfer_channel") GodotMethod!(void, long) setTransferChannel; 72 } 73 /// 74 pragma(inline, true) bool opEquals(in NetworkedMultiplayerENet other) const 75 { return _godot_object.ptr is other._godot_object.ptr; } 76 /// 77 pragma(inline, true) typeof(null) opAssign(typeof(null) n) 78 { _godot_object.ptr = n; return null; } 79 /// 80 pragma(inline, true) bool opEquals(typeof(null) n) const 81 { return _godot_object.ptr is n; } 82 /// 83 size_t toHash() const @trusted { return cast(size_t)_godot_object.ptr; } 84 mixin baseCasts; 85 /// Construct a new instance of NetworkedMultiplayerENet. 86 /// Note: use `memnew!NetworkedMultiplayerENet` instead. 87 static NetworkedMultiplayerENet _new() 88 { 89 static godot_class_constructor constructor; 90 if(constructor is null) constructor = _godot_api.godot_get_class_constructor("NetworkedMultiplayerENet"); 91 if(constructor is null) return typeof(this).init; 92 return cast(NetworkedMultiplayerENet)(constructor()); 93 } 94 @disable new(size_t s); 95 /// 96 enum CompressionMode : int 97 { 98 /** 99 No compression. This uses the most bandwidth, but has the upside of requiring the fewest CPU resources. 100 */ 101 compressNone = 0, 102 /** 103 ENet's built-in range encoding. 104 */ 105 compressRangeCoder = 1, 106 /** 107 $(D url=http://fastlz.org/)FastLZ$(D /url) compression. This option uses less CPU resources compared to $(D constant COMPRESS_ZLIB), at the expense of using more bandwidth. 108 */ 109 compressFastlz = 2, 110 /** 111 $(D url=https://www.zlib.net/)Zlib$(D /url) compression. This option uses less bandwidth compared to $(D constant COMPRESS_FASTLZ), at the expense of using more CPU resources. 112 */ 113 compressZlib = 3, 114 /** 115 $(D url=https://facebook.github.io/zstd/)Zstandard$(D /url) compression. 116 */ 117 compressZstd = 4, 118 } 119 /// 120 enum Constants : int 121 { 122 compressNone = 0, 123 compressRangeCoder = 1, 124 compressFastlz = 2, 125 compressZlib = 3, 126 compressZstd = 4, 127 } 128 /** 129 Closes the connection. Ignored if no connection is currently established. If this is a server it tries to notify all clients before forcibly disconnecting them. If this is a client it simply closes the connection to the server. 130 */ 131 void closeConnection(in long wait_usec = 100) 132 { 133 checkClassBinding!(typeof(this))(); 134 ptrcall!(void)(GDNativeClassBinding.closeConnection, _godot_object, wait_usec); 135 } 136 /** 137 Create client that connects to a server at `address` using specified `port`. The given address needs to be either a fully qualified domain name (e.g. `"www.example.com"`) or an IP address in IPv4 or IPv6 format (e.g. `"192.168.1.1"`). The `port` is the port the server is listening on. The `in_bandwidth` and `out_bandwidth` parameters can be used to limit the incoming and outgoing bandwidth to the given number of bytes per second. The default of 0 means unlimited bandwidth. Note that ENet will strategically drop packets on specific sides of a connection between peers to ensure the peer's bandwidth is not overwhelmed. The bandwidth parameters also determine the window size of a connection which limits the amount of reliable packets that may be in transit at any given time. Returns $(D constant OK) if a client was created, $(D constant ERR_ALREADY_IN_USE) if this NetworkedMultiplayerENet instance already has an open connection (in which case you need to call $(D closeConnection) first) or $(D constant ERR_CANT_CREATE) if the client could not be created. If `client_port` is specified, the client will also listen to the given port; this is useful for some NAT traversal techniques. 138 */ 139 GodotError createClient(in String address, in long port, in long in_bandwidth = 0, in long out_bandwidth = 0, in long client_port = 0) 140 { 141 checkClassBinding!(typeof(this))(); 142 return ptrcall!(GodotError)(GDNativeClassBinding.createClient, _godot_object, address, port, in_bandwidth, out_bandwidth, client_port); 143 } 144 /** 145 Create server that listens to connections via `port`. The port needs to be an available, unused port between 0 and 65535. Note that ports below 1024 are privileged and may require elevated permissions depending on the platform. To change the interface the server listens on, use $(D setBindIp). The default IP is the wildcard `"*"`, which listens on all available interfaces. `max_clients` is the maximum number of clients that are allowed at once, any number up to 4095 may be used, although the achievable number of simultaneous clients may be far lower and depends on the application. For additional details on the bandwidth parameters, see $(D createClient). Returns $(D constant OK) if a server was created, $(D constant ERR_ALREADY_IN_USE) if this NetworkedMultiplayerENet instance already has an open connection (in which case you need to call $(D closeConnection) first) or $(D constant ERR_CANT_CREATE) if the server could not be created. 146 */ 147 GodotError createServer(in long port, in long max_clients = 32, in long in_bandwidth = 0, in long out_bandwidth = 0) 148 { 149 checkClassBinding!(typeof(this))(); 150 return ptrcall!(GodotError)(GDNativeClassBinding.createServer, _godot_object, port, max_clients, in_bandwidth, out_bandwidth); 151 } 152 /** 153 Disconnect the given peer. If "now" is set to `true`, the connection will be closed immediately without flushing queued messages. 154 */ 155 void disconnectPeer(in long id, in bool now = false) 156 { 157 checkClassBinding!(typeof(this))(); 158 ptrcall!(void)(GDNativeClassBinding.disconnectPeer, _godot_object, id, now); 159 } 160 /** 161 162 */ 163 long getChannelCount() const 164 { 165 checkClassBinding!(typeof(this))(); 166 return ptrcall!(long)(GDNativeClassBinding.getChannelCount, _godot_object); 167 } 168 /** 169 170 */ 171 NetworkedMultiplayerENet.CompressionMode getCompressionMode() const 172 { 173 checkClassBinding!(typeof(this))(); 174 return ptrcall!(NetworkedMultiplayerENet.CompressionMode)(GDNativeClassBinding.getCompressionMode, _godot_object); 175 } 176 /** 177 Returns the channel of the last packet fetched via $(D PacketPeer.getPacket). 178 */ 179 long getLastPacketChannel() const 180 { 181 checkClassBinding!(typeof(this))(); 182 return ptrcall!(long)(GDNativeClassBinding.getLastPacketChannel, _godot_object); 183 } 184 /** 185 Returns the channel of the next packet that will be retrieved via $(D PacketPeer.getPacket). 186 */ 187 long getPacketChannel() const 188 { 189 checkClassBinding!(typeof(this))(); 190 return ptrcall!(long)(GDNativeClassBinding.getPacketChannel, _godot_object); 191 } 192 /** 193 Returns the IP address of the given peer. 194 */ 195 String getPeerAddress(in long id) const 196 { 197 checkClassBinding!(typeof(this))(); 198 return ptrcall!(String)(GDNativeClassBinding.getPeerAddress, _godot_object, id); 199 } 200 /** 201 Returns the remote port of the given peer. 202 */ 203 long getPeerPort(in long id) const 204 { 205 checkClassBinding!(typeof(this))(); 206 return ptrcall!(long)(GDNativeClassBinding.getPeerPort, _godot_object, id); 207 } 208 /** 209 210 */ 211 long getTransferChannel() const 212 { 213 checkClassBinding!(typeof(this))(); 214 return ptrcall!(long)(GDNativeClassBinding.getTransferChannel, _godot_object); 215 } 216 /** 217 218 */ 219 bool isAlwaysOrdered() const 220 { 221 checkClassBinding!(typeof(this))(); 222 return ptrcall!(bool)(GDNativeClassBinding.isAlwaysOrdered, _godot_object); 223 } 224 /** 225 226 */ 227 bool isDtlsEnabled() const 228 { 229 checkClassBinding!(typeof(this))(); 230 return ptrcall!(bool)(GDNativeClassBinding.isDtlsEnabled, _godot_object); 231 } 232 /** 233 234 */ 235 bool isDtlsVerifyEnabled() const 236 { 237 checkClassBinding!(typeof(this))(); 238 return ptrcall!(bool)(GDNativeClassBinding.isDtlsVerifyEnabled, _godot_object); 239 } 240 /** 241 242 */ 243 bool isServerRelayEnabled() const 244 { 245 checkClassBinding!(typeof(this))(); 246 return ptrcall!(bool)(GDNativeClassBinding.isServerRelayEnabled, _godot_object); 247 } 248 /** 249 250 */ 251 void setAlwaysOrdered(in bool ordered) 252 { 253 checkClassBinding!(typeof(this))(); 254 ptrcall!(void)(GDNativeClassBinding.setAlwaysOrdered, _godot_object, ordered); 255 } 256 /** 257 The IP used when creating a server. This is set to the wildcard `"*"` by default, which binds to all available interfaces. The given IP needs to be in IPv4 or IPv6 address format, for example: `"192.168.1.1"`. 258 */ 259 void setBindIp(in String ip) 260 { 261 checkClassBinding!(typeof(this))(); 262 ptrcall!(void)(GDNativeClassBinding.setBindIp, _godot_object, ip); 263 } 264 /** 265 266 */ 267 void setChannelCount(in long channels) 268 { 269 checkClassBinding!(typeof(this))(); 270 ptrcall!(void)(GDNativeClassBinding.setChannelCount, _godot_object, channels); 271 } 272 /** 273 274 */ 275 void setCompressionMode(in long mode) 276 { 277 checkClassBinding!(typeof(this))(); 278 ptrcall!(void)(GDNativeClassBinding.setCompressionMode, _godot_object, mode); 279 } 280 /** 281 Configure the $(D X509Certificate) to use when $(D useDtls) is `true`. For servers, you must also setup the $(D CryptoKey) via $(D setDtlsKey). 282 */ 283 void setDtlsCertificate(X509Certificate certificate) 284 { 285 checkClassBinding!(typeof(this))(); 286 ptrcall!(void)(GDNativeClassBinding.setDtlsCertificate, _godot_object, certificate); 287 } 288 /** 289 290 */ 291 void setDtlsEnabled(in bool enabled) 292 { 293 checkClassBinding!(typeof(this))(); 294 ptrcall!(void)(GDNativeClassBinding.setDtlsEnabled, _godot_object, enabled); 295 } 296 /** 297 Configure the $(D CryptoKey) to use when $(D useDtls) is `true`. Remember to also call $(D setDtlsCertificate) to setup your $(D X509Certificate). 298 */ 299 void setDtlsKey(CryptoKey key) 300 { 301 checkClassBinding!(typeof(this))(); 302 ptrcall!(void)(GDNativeClassBinding.setDtlsKey, _godot_object, key); 303 } 304 /** 305 306 */ 307 void setDtlsVerifyEnabled(in bool enabled) 308 { 309 checkClassBinding!(typeof(this))(); 310 ptrcall!(void)(GDNativeClassBinding.setDtlsVerifyEnabled, _godot_object, enabled); 311 } 312 /** 313 Sets the timeout parameters for a peer. The timeout parameters control how and when a peer will timeout from a failure to acknowledge reliable traffic. Timeout values are expressed in milliseconds. 314 The `timeout_limit` is a factor that, multiplied by a value based on the average round trip time, will determine the timeout limit for a reliable packet. When that limit is reached, the timeout will be doubled, and the peer will be disconnected if that limit has reached `timeout_min`. The `timeout_max` parameter, on the other hand, defines a fixed timeout for which any packet must be acknowledged or the peer will be dropped. 315 */ 316 void setPeerTimeout(in long id, in long timeout_limit, in long timeout_min, in long timeout_max) 317 { 318 checkClassBinding!(typeof(this))(); 319 ptrcall!(void)(GDNativeClassBinding.setPeerTimeout, _godot_object, id, timeout_limit, timeout_min, timeout_max); 320 } 321 /** 322 323 */ 324 void setServerRelayEnabled(in bool enabled) 325 { 326 checkClassBinding!(typeof(this))(); 327 ptrcall!(void)(GDNativeClassBinding.setServerRelayEnabled, _godot_object, enabled); 328 } 329 /** 330 331 */ 332 void setTransferChannel(in long channel) 333 { 334 checkClassBinding!(typeof(this))(); 335 ptrcall!(void)(GDNativeClassBinding.setTransferChannel, _godot_object, channel); 336 } 337 /** 338 Enforce ordered packets when using $(D constant NetworkedMultiplayerPeer.TRANSFER_MODE_UNRELIABLE) (thus behaving similarly to $(D constant NetworkedMultiplayerPeer.TRANSFER_MODE_UNRELIABLE_ORDERED)). This is the only way to use ordering with the RPC system. 339 */ 340 @property bool alwaysOrdered() 341 { 342 return isAlwaysOrdered(); 343 } 344 /// ditto 345 @property void alwaysOrdered(bool v) 346 { 347 setAlwaysOrdered(v); 348 } 349 /** 350 The number of channels to be used by ENet. Channels are used to separate different kinds of data. In reliable or ordered mode, for example, the packet delivery order is ensured on a per-channel basis. This is done to combat latency and reduces ordering restrictions on packets. The delivery status of a packet in one channel won't stall the delivery of other packets in another channel. 351 */ 352 @property long channelCount() 353 { 354 return getChannelCount(); 355 } 356 /// ditto 357 @property void channelCount(long v) 358 { 359 setChannelCount(v); 360 } 361 /** 362 The compression method used for network packets. These have different tradeoffs of compression speed versus bandwidth, you may need to test which one works best for your use case if you use compression at all. 363 */ 364 @property NetworkedMultiplayerENet.CompressionMode compressionMode() 365 { 366 return getCompressionMode(); 367 } 368 /// ditto 369 @property void compressionMode(long v) 370 { 371 setCompressionMode(v); 372 } 373 /** 374 Enable or disable certificate verification when $(D useDtls) `true`. 375 */ 376 @property bool dtlsVerify() 377 { 378 return isDtlsVerifyEnabled(); 379 } 380 /// ditto 381 @property void dtlsVerify(bool v) 382 { 383 setDtlsVerifyEnabled(v); 384 } 385 /** 386 Enable or disable the server feature that notifies clients of other peers' connection/disconnection, and relays messages between them. When this option is `false`, clients won't be automatically notified of other peers and won't be able to send them packets through the server. 387 */ 388 @property bool serverRelay() 389 { 390 return isServerRelayEnabled(); 391 } 392 /// ditto 393 @property void serverRelay(bool v) 394 { 395 setServerRelayEnabled(v); 396 } 397 /** 398 Set the default channel to be used to transfer data. By default, this value is `-1` which means that ENet will only use 2 channels: one for reliable packets, and one for unreliable packets. The channel `0` is reserved and cannot be used. Setting this member to any value between `0` and $(D channelCount) (excluded) will force ENet to use that channel for sending data. See $(D channelCount) for more information about ENet channels. 399 */ 400 @property long transferChannel() 401 { 402 return getTransferChannel(); 403 } 404 /// ditto 405 @property void transferChannel(long v) 406 { 407 setTransferChannel(v); 408 } 409 /** 410 When enabled, the client or server created by this peer, will use $(D PacketPeerDTLS) instead of raw UDP sockets for communicating with the remote peer. This will make the communication encrypted with DTLS at the cost of higher resource usage and potentially larger packet size. 411 Note: When creating a DTLS server, make sure you setup the key/certificate pair via $(D setDtlsKey) and $(D setDtlsCertificate). For DTLS clients, have a look at the $(D dtlsVerify) option, and configure the certificate accordingly via $(D setDtlsCertificate). 412 */ 413 @property bool useDtls() 414 { 415 return isDtlsEnabled(); 416 } 417 /// ditto 418 @property void useDtls(bool v) 419 { 420 setDtlsEnabled(v); 421 } 422 }