1 /** 2 A WebSocket server implementation. 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.websocketserver; 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.websocketmultiplayerpeer; 25 import godot.networkedmultiplayerpeer; 26 import godot.packetpeer; 27 import godot.reference; 28 import godot.x509certificate; 29 import godot.cryptokey; 30 /** 31 A WebSocket server implementation. 32 33 This class implements a WebSocket server that can also support the high-level multiplayer API. 34 After starting the server ($(D listen)), you will need to $(D NetworkedMultiplayerPeer.poll) it at regular intervals (e.g. inside $(D Node._process)). When clients connect, disconnect, or send data, you will receive the appropriate signal. 35 $(B Note:) Not available in HTML5 exports. 36 */ 37 @GodotBaseClass struct WebSocketServer 38 { 39 package(godot) enum string _GODOT_internal_name = "WebSocketServer"; 40 public: 41 @nogc nothrow: 42 union { /** */ godot_object _godot_object; /** */ WebSocketMultiplayerPeer _GODOT_base; } 43 alias _GODOT_base this; 44 alias BaseClasses = AliasSeq!(typeof(_GODOT_base), typeof(_GODOT_base).BaseClasses); 45 package(godot) __gshared bool _classBindingInitialized = false; 46 package(godot) static struct GDNativeClassBinding 47 { 48 __gshared: 49 @GodotName("disconnect_peer") GodotMethod!(void, long, long, String) disconnectPeer; 50 @GodotName("get_bind_ip") GodotMethod!(String) getBindIp; 51 @GodotName("get_ca_chain") GodotMethod!(X509Certificate) getCaChain; 52 @GodotName("get_peer_address") GodotMethod!(String, long) getPeerAddress; 53 @GodotName("get_peer_port") GodotMethod!(long, long) getPeerPort; 54 @GodotName("get_private_key") GodotMethod!(CryptoKey) getPrivateKey; 55 @GodotName("get_ssl_certificate") GodotMethod!(X509Certificate) getSslCertificate; 56 @GodotName("has_peer") GodotMethod!(bool, long) hasPeer; 57 @GodotName("is_listening") GodotMethod!(bool) isListening; 58 @GodotName("listen") GodotMethod!(GodotError, long, PoolStringArray, bool) listen; 59 @GodotName("set_bind_ip") GodotMethod!(void, String) setBindIp; 60 @GodotName("set_ca_chain") GodotMethod!(void, X509Certificate) setCaChain; 61 @GodotName("set_private_key") GodotMethod!(void, CryptoKey) setPrivateKey; 62 @GodotName("set_ssl_certificate") GodotMethod!(void, X509Certificate) setSslCertificate; 63 @GodotName("stop") GodotMethod!(void) stop; 64 } 65 /// 66 pragma(inline, true) bool opEquals(in WebSocketServer other) const 67 { return _godot_object.ptr is other._godot_object.ptr; } 68 /// 69 pragma(inline, true) typeof(null) opAssign(typeof(null) n) 70 { _godot_object.ptr = n; return null; } 71 /// 72 pragma(inline, true) bool opEquals(typeof(null) n) const 73 { return _godot_object.ptr is n; } 74 /// 75 size_t toHash() const @trusted { return cast(size_t)_godot_object.ptr; } 76 mixin baseCasts; 77 /// Construct a new instance of WebSocketServer. 78 /// Note: use `memnew!WebSocketServer` instead. 79 static WebSocketServer _new() 80 { 81 static godot_class_constructor constructor; 82 if(constructor is null) constructor = _godot_api.godot_get_class_constructor("WebSocketServer"); 83 if(constructor is null) return typeof(this).init; 84 return cast(WebSocketServer)(constructor()); 85 } 86 @disable new(size_t s); 87 /** 88 Disconnects the peer identified by `id` from the server. See $(D WebSocketPeer.close) for more information. 89 */ 90 void disconnectPeer(in long id, in long code = 1000, in String reason = gs!"") 91 { 92 checkClassBinding!(typeof(this))(); 93 ptrcall!(void)(GDNativeClassBinding.disconnectPeer, _godot_object, id, code, reason); 94 } 95 /** 96 97 */ 98 String getBindIp() const 99 { 100 checkClassBinding!(typeof(this))(); 101 return ptrcall!(String)(GDNativeClassBinding.getBindIp, _godot_object); 102 } 103 /** 104 105 */ 106 Ref!X509Certificate getCaChain() const 107 { 108 checkClassBinding!(typeof(this))(); 109 return ptrcall!(X509Certificate)(GDNativeClassBinding.getCaChain, _godot_object); 110 } 111 /** 112 Returns the IP address of the given peer. 113 */ 114 String getPeerAddress(in long id) const 115 { 116 checkClassBinding!(typeof(this))(); 117 return ptrcall!(String)(GDNativeClassBinding.getPeerAddress, _godot_object, id); 118 } 119 /** 120 Returns the remote port of the given peer. 121 */ 122 long getPeerPort(in long id) const 123 { 124 checkClassBinding!(typeof(this))(); 125 return ptrcall!(long)(GDNativeClassBinding.getPeerPort, _godot_object, id); 126 } 127 /** 128 129 */ 130 Ref!CryptoKey getPrivateKey() const 131 { 132 checkClassBinding!(typeof(this))(); 133 return ptrcall!(CryptoKey)(GDNativeClassBinding.getPrivateKey, _godot_object); 134 } 135 /** 136 137 */ 138 Ref!X509Certificate getSslCertificate() const 139 { 140 checkClassBinding!(typeof(this))(); 141 return ptrcall!(X509Certificate)(GDNativeClassBinding.getSslCertificate, _godot_object); 142 } 143 /** 144 Returns `true` if a peer with the given ID is connected. 145 */ 146 bool hasPeer(in long id) const 147 { 148 checkClassBinding!(typeof(this))(); 149 return ptrcall!(bool)(GDNativeClassBinding.hasPeer, _godot_object, id); 150 } 151 /** 152 Returns `true` if the server is actively listening on a port. 153 */ 154 bool isListening() const 155 { 156 checkClassBinding!(typeof(this))(); 157 return ptrcall!(bool)(GDNativeClassBinding.isListening, _godot_object); 158 } 159 /** 160 Starts listening on the given port. 161 You can specify the desired subprotocols via the "protocols" array. If the list empty (default), no sub-protocol will be requested. 162 If `true` is passed as `gd_mp_api`, the server will behave like a network peer for the $(D MultiplayerAPI), connections from non-Godot clients will not work, and $(D dataReceived) will not be emitted. 163 If `false` is passed instead (default), you must call $(D PacketPeer) functions (`put_packet`, `get_packet`, etc.), on the $(D WebSocketPeer) returned via `get_peer(id)` to communicate with the peer with given `id` (e.g. `get_peer(id).get_available_packet_count`). 164 */ 165 GodotError listen(in long port, in PoolStringArray protocols = PoolStringArray.init, in bool gd_mp_api = false) 166 { 167 checkClassBinding!(typeof(this))(); 168 return ptrcall!(GodotError)(GDNativeClassBinding.listen, _godot_object, port, protocols, gd_mp_api); 169 } 170 /** 171 172 */ 173 void setBindIp(in String arg0) 174 { 175 checkClassBinding!(typeof(this))(); 176 ptrcall!(void)(GDNativeClassBinding.setBindIp, _godot_object, arg0); 177 } 178 /** 179 180 */ 181 void setCaChain(X509Certificate arg0) 182 { 183 checkClassBinding!(typeof(this))(); 184 ptrcall!(void)(GDNativeClassBinding.setCaChain, _godot_object, arg0); 185 } 186 /** 187 188 */ 189 void setPrivateKey(CryptoKey arg0) 190 { 191 checkClassBinding!(typeof(this))(); 192 ptrcall!(void)(GDNativeClassBinding.setPrivateKey, _godot_object, arg0); 193 } 194 /** 195 196 */ 197 void setSslCertificate(X509Certificate arg0) 198 { 199 checkClassBinding!(typeof(this))(); 200 ptrcall!(void)(GDNativeClassBinding.setSslCertificate, _godot_object, arg0); 201 } 202 /** 203 Stops the server and clear its state. 204 */ 205 void stop() 206 { 207 checkClassBinding!(typeof(this))(); 208 ptrcall!(void)(GDNativeClassBinding.stop, _godot_object); 209 } 210 /** 211 When not set to `*` will restrict incoming connections to the specified IP address. Setting `bind_ip` to `127.0.0.1` will cause the server to listen only to the local host. 212 */ 213 @property String bindIp() 214 { 215 return getBindIp(); 216 } 217 /// ditto 218 @property void bindIp(String v) 219 { 220 setBindIp(v); 221 } 222 /** 223 When using SSL (see $(D privateKey) and $(D sslCertificate)), you can set this to a valid $(D X509Certificate) to be provided as additional CA chain information during the SSL handshake. 224 */ 225 @property X509Certificate caChain() 226 { 227 return getCaChain(); 228 } 229 /// ditto 230 @property void caChain(X509Certificate v) 231 { 232 setCaChain(v); 233 } 234 /** 235 When set to a valid $(D CryptoKey) (along with $(D sslCertificate)) will cause the server to require SSL instead of regular TCP (i.e. the `wss://` protocol). 236 */ 237 @property CryptoKey privateKey() 238 { 239 return getPrivateKey(); 240 } 241 /// ditto 242 @property void privateKey(CryptoKey v) 243 { 244 setPrivateKey(v); 245 } 246 /** 247 When set to a valid $(D X509Certificate) (along with $(D privateKey)) will cause the server to require SSL instead of regular TCP (i.e. the `wss://` protocol). 248 */ 249 @property X509Certificate sslCertificate() 250 { 251 return getSslCertificate(); 252 } 253 /// ditto 254 @property void sslCertificate(X509Certificate v) 255 { 256 setSslCertificate(v); 257 } 258 }