1 /**
2 A class representing a specific WebSocket connection.
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.websocketpeer;
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.packetpeer;
25 import godot.reference;
26 /**
27 A class representing a specific WebSocket connection.
28 
29 This class represent a specific WebSocket connection, you can do lower level operations with it.
30 You can choose to write to the socket in binary or text mode, and you can recognize the mode used for writing by the other peer.
31 */
32 @GodotBaseClass struct WebSocketPeer
33 {
34 	package(godot) enum string _GODOT_internal_name = "WebSocketPeer";
35 public:
36 @nogc nothrow:
37 	union { /** */ godot_object _godot_object; /** */ PacketPeer _GODOT_base; }
38 	alias _GODOT_base this;
39 	alias BaseClasses = AliasSeq!(typeof(_GODOT_base), typeof(_GODOT_base).BaseClasses);
40 	package(godot) __gshared bool _classBindingInitialized = false;
41 	package(godot) static struct GDNativeClassBinding
42 	{
43 		__gshared:
44 		@GodotName("close") GodotMethod!(void, long, String) close;
45 		@GodotName("get_connected_host") GodotMethod!(String) getConnectedHost;
46 		@GodotName("get_connected_port") GodotMethod!(long) getConnectedPort;
47 		@GodotName("get_write_mode") GodotMethod!(WebSocketPeer.WriteMode) getWriteMode;
48 		@GodotName("is_connected_to_host") GodotMethod!(bool) isConnectedToHost;
49 		@GodotName("set_no_delay") GodotMethod!(void, bool) setNoDelay;
50 		@GodotName("set_write_mode") GodotMethod!(void, long) setWriteMode;
51 		@GodotName("was_string_packet") GodotMethod!(bool) wasStringPacket;
52 	}
53 	/// 
54 	pragma(inline, true) bool opEquals(in WebSocketPeer other) const
55 	{ return _godot_object.ptr is other._godot_object.ptr; }
56 	/// 
57 	pragma(inline, true) typeof(null) opAssign(typeof(null) n)
58 	{ _godot_object.ptr = n; return null; }
59 	/// 
60 	pragma(inline, true) bool opEquals(typeof(null) n) const
61 	{ return _godot_object.ptr is n; }
62 	/// 
63 	size_t toHash() const @trusted { return cast(size_t)_godot_object.ptr; }
64 	mixin baseCasts;
65 	/// Construct a new instance of WebSocketPeer.
66 	/// Note: use `memnew!WebSocketPeer` instead.
67 	static WebSocketPeer _new()
68 	{
69 		static godot_class_constructor constructor;
70 		if(constructor is null) constructor = _godot_api.godot_get_class_constructor("WebSocketPeer");
71 		if(constructor is null) return typeof(this).init;
72 		return cast(WebSocketPeer)(constructor());
73 	}
74 	@disable new(size_t s);
75 	/// 
76 	enum WriteMode : int
77 	{
78 		/**
79 		Specifies that WebSockets messages should be transferred as text payload (only valid UTF-8 is allowed).
80 		*/
81 		writeModeText = 0,
82 		/**
83 		Specifies that WebSockets messages should be transferred as binary payload (any byte combination is allowed).
84 		*/
85 		writeModeBinary = 1,
86 	}
87 	/// 
88 	enum Constants : int
89 	{
90 		writeModeText = 0,
91 		writeModeBinary = 1,
92 	}
93 	/**
94 	Closes this WebSocket connection. `code` is the status code for the closure (see RFC 6455 section 7.4 for a list of valid status codes). `reason` is the human readable reason for closing the connection (can be any UTF-8 string that's smaller than 123 bytes).
95 	$(B Note:) To achieve a clean close, you will need to keep polling until either $(D WebSocketClient.connectionClosed) or $(D WebSocketServer.clientDisconnected) is received.
96 	$(B Note:) The HTML5 export might not support all status codes. Please refer to browser-specific documentation for more details.
97 	*/
98 	void close(in long code = 1000, in String reason = gs!"")
99 	{
100 		checkClassBinding!(typeof(this))();
101 		ptrcall!(void)(GDNativeClassBinding.close, _godot_object, code, reason);
102 	}
103 	/**
104 	Returns the IP address of the connected peer.
105 	$(B Note:) Not available in the HTML5 export.
106 	*/
107 	String getConnectedHost() const
108 	{
109 		checkClassBinding!(typeof(this))();
110 		return ptrcall!(String)(GDNativeClassBinding.getConnectedHost, _godot_object);
111 	}
112 	/**
113 	Returns the remote port of the connected peer.
114 	$(B Note:) Not available in the HTML5 export.
115 	*/
116 	long getConnectedPort() const
117 	{
118 		checkClassBinding!(typeof(this))();
119 		return ptrcall!(long)(GDNativeClassBinding.getConnectedPort, _godot_object);
120 	}
121 	/**
122 	Gets the current selected write mode. See $(D writemode).
123 	*/
124 	WebSocketPeer.WriteMode getWriteMode() const
125 	{
126 		checkClassBinding!(typeof(this))();
127 		return ptrcall!(WebSocketPeer.WriteMode)(GDNativeClassBinding.getWriteMode, _godot_object);
128 	}
129 	/**
130 	Returns `true` if this peer is currently connected.
131 	*/
132 	bool isConnectedToHost() const
133 	{
134 		checkClassBinding!(typeof(this))();
135 		return ptrcall!(bool)(GDNativeClassBinding.isConnectedToHost, _godot_object);
136 	}
137 	/**
138 	Disable Nagle's algorithm on the underling TCP socket (default). See $(D StreamPeerTCP.setNoDelay) for more information.
139 	$(B Note:) Not available in the HTML5 export.
140 	*/
141 	void setNoDelay(in bool enabled)
142 	{
143 		checkClassBinding!(typeof(this))();
144 		ptrcall!(void)(GDNativeClassBinding.setNoDelay, _godot_object, enabled);
145 	}
146 	/**
147 	Sets the socket to use the given $(D writemode).
148 	*/
149 	void setWriteMode(in long mode)
150 	{
151 		checkClassBinding!(typeof(this))();
152 		ptrcall!(void)(GDNativeClassBinding.setWriteMode, _godot_object, mode);
153 	}
154 	/**
155 	Returns `true` if the last received packet was sent as a text payload. See $(D writemode).
156 	*/
157 	bool wasStringPacket() const
158 	{
159 		checkClassBinding!(typeof(this))();
160 		return ptrcall!(bool)(GDNativeClassBinding.wasStringPacket, _godot_object);
161 	}
162 }