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.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.classdb;
23 import godot.websocketmultiplayerpeer;
24 import godot.networkedmultiplayerpeer;
25 import godot.packetpeer;
26 import godot.reference;
27 /**
28 A WebSocket server implementation
29 
30 This class implements a WebSocket server that can also support the high level multiplayer API.
31 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.
32 Note: This class will not work in HTML5 exports due to browser restrictions.
33 */
34 @GodotBaseClass struct WebSocketServer
35 {
36 	enum string _GODOT_internal_name = "WebSocketServer";
37 public:
38 @nogc nothrow:
39 	union { godot_object _godot_object; WebSocketMultiplayerPeer _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 _classBinding
44 	{
45 		__gshared:
46 		@GodotName("is_listening") GodotMethod!(bool) isListening;
47 		@GodotName("listen") GodotMethod!(GodotError, long, PoolStringArray, bool) listen;
48 		@GodotName("stop") GodotMethod!(void) stop;
49 		@GodotName("has_peer") GodotMethod!(bool, long) hasPeer;
50 		@GodotName("get_peer_address") GodotMethod!(String, long) getPeerAddress;
51 		@GodotName("get_peer_port") GodotMethod!(long, long) getPeerPort;
52 		@GodotName("disconnect_peer") GodotMethod!(void, long, long, String) disconnectPeer;
53 	}
54 	bool opEquals(in WebSocketServer other) const { return _godot_object.ptr is other._godot_object.ptr; }
55 	WebSocketServer opAssign(T : typeof(null))(T n) { _godot_object.ptr = null; }
56 	bool opEquals(typeof(null) n) const { return _godot_object.ptr is null; }
57 	mixin baseCasts;
58 	static WebSocketServer _new()
59 	{
60 		static godot_class_constructor constructor;
61 		if(constructor is null) constructor = _godot_api.godot_get_class_constructor("WebSocketServer");
62 		if(constructor is null) return typeof(this).init;
63 		return cast(WebSocketServer)(constructor());
64 	}
65 	@disable new(size_t s);
66 	/**
67 	Returns `true` if the server is actively listening on a port.
68 	*/
69 	bool isListening() const
70 	{
71 		checkClassBinding!(typeof(this))();
72 		return ptrcall!(bool)(_classBinding.isListening, _godot_object);
73 	}
74 	/**
75 	Start listening on the given port.
76 	You can specify the desired subprotocols via the "protocols" array. If the list empty (default), "binary" will be used.
77 	You can use this server as a network peer for $(D MultiplayerAPI) by passing `true` as `gd_mp_api`. Note: $(D dataReceived) will not be fired and clients other than Godot will not work in this case.
78 	*/
79 	GodotError listen(in long port, in PoolStringArray protocols = PoolStringArray.init, in bool gd_mp_api = false)
80 	{
81 		checkClassBinding!(typeof(this))();
82 		return ptrcall!(GodotError)(_classBinding.listen, _godot_object, port, protocols, gd_mp_api);
83 	}
84 	/**
85 	Stop the server and clear its state.
86 	*/
87 	void stop()
88 	{
89 		checkClassBinding!(typeof(this))();
90 		ptrcall!(void)(_classBinding.stop, _godot_object);
91 	}
92 	/**
93 	Returns `true` if a peer with the given ID is connected.
94 	*/
95 	bool hasPeer(in long id) const
96 	{
97 		checkClassBinding!(typeof(this))();
98 		return ptrcall!(bool)(_classBinding.hasPeer, _godot_object, id);
99 	}
100 	/**
101 	Returns the IP address of the given peer.
102 	*/
103 	String getPeerAddress(in long id) const
104 	{
105 		checkClassBinding!(typeof(this))();
106 		return ptrcall!(String)(_classBinding.getPeerAddress, _godot_object, id);
107 	}
108 	/**
109 	Returns the remote port of the given peer.
110 	*/
111 	long getPeerPort(in long id) const
112 	{
113 		checkClassBinding!(typeof(this))();
114 		return ptrcall!(long)(_classBinding.getPeerPort, _godot_object, id);
115 	}
116 	/**
117 	Disconnects the peer identified by `id` from the server. See $(D WebSocketPeer.close) for more info.
118 	*/
119 	void disconnectPeer(StringArg2)(in long id, in long code = 1000, in StringArg2 reason = "")
120 	{
121 		checkClassBinding!(typeof(this))();
122 		ptrcall!(void)(_classBinding.disconnectPeer, _godot_object, id, code, reason);
123 	}
124 }