8000 Free allocated memory (#1), add code and reason to Close method (#2). · ctrl-labs/unity-websocket-webgl@c9a73ed · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Nov 22, 2022. It is now read-only.

Commit c9a73ed

Browse files
committed
Free allocated memory (jirihybek#1), add code and reason to Close method (jirihybek#2).
1 parent b497cc4 commit c9a73ed

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

Plugins/WebSocket.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public enum WebSocketState
3737
*/
3838
public enum WebSocketCloseCode
3939
{
40+
/* Do NOT use NotSet - it's only purpose is to indicate that the close code cannot be parsed. */
4041
NotSet = 0,
4142
Normal = 1000,
4243
Away = 1001,
@@ -62,7 +63,7 @@ public interface IWebSocket
6263
void Connect();
6364

6465
/* Close connection */
65-
void Close();
66+
void Close(WebSocketCloseCode code = WebSocketCloseCode.Normal, string reason = null);
6667

6768
/* Send binary data over the socket */
6869
void Send(byte[] data);
@@ -117,6 +118,7 @@ public static string GetErrorMessageFromCode(int errorCode)
117118
case -4: return "WebSocket is already closing.";
118119
case -5: return "WebSocket is already closed.";
119120
case -6: return "WebSocket is not in open state.";
121+
case -7: return "Cannot close WebSocket. An invalid code was specified or reason is too long.";
120122
default: return "Unknown error.";
121123

122124
}
@@ -159,7 +161,7 @@ public class WebSocket: IWebSocket
159161
public static extern int WebSocketConnect(int instanceId);
160162

161163
[DllImport("__Internal")]
162-
public static extern int WebSocketClose(int instanceId);
164+
public static extern int WebSocketClose(int instanceId, int code, string reason);
163165

164166
[DllImport("__Internal")]
165167
public static extern int WebSocketSend(int instanceId, byte[] dataPtr, int dataLength);
@@ -222,10 +224,10 @@ public void Connect()
222224
/*
223225
* Close connection
224226
*/
225-
public void Close()
227+
public void Close(WebSocketCloseCode code = WebSocketCloseCode.Normal, string reason = null)
226228
{
227229

228-
int ret = WebSocketClose(this.instanceId);
230+
int ret = WebSocketClose(this.instanceId, (int)code, reason);
229231

230232
if (ret < 0)
231233
throw new WebSocketException(
@@ -412,7 +414,7 @@ public void Connect()
412414
/*
413415
* Close connection
414416
*/
415-
public void Close()
417+
public void Close(WebSocketCloseCode code = WebSocketCloseCode.Normal, string reason = null)
416418
{
417419

418420
// Check state
@@ -424,7 +426,7 @@ public void Close()
424426

425427
try
426428
{
427-
this.ws.CloseAsync();
429+
this.ws.CloseAsync((ushort)code, reason);
428430
}
429431
catch (Exception e)
430432
{

Plugins/WebSocket.jslib

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,15 +143,21 @@ var LibraryWebSocket = {
143143

144144
instance.ws.onmessage = function(ev) {
145145

146+
if (webSocketState.onMessage === null)
147+
return;
148+
146149
if (ev.data instanceof ArrayBuffer) {
147150

148151
var dataBuffer = new Uint8Array(ev.data);
149152

150153
var buffer = _malloc(dataBuffer.length);
151154
HEAPU8.set(dataBuffer, buffer);
152155

153-
if (webSocketState.onMessage)
156+
try {
154157
Runtime.dynCall('viii', webSocketState.onMessage, [ instanceId, buffer, dataBuffer.length ]);
158+
} finally {
159+
_free(buffer);
160+
}
155161

156162
}
157163

@@ -166,7 +172,11 @@ var LibraryWebSocket = {
166172
var msgBuffer = _malloc(msgBytes + 1);
167173
stringToUTF8(msg, msgBuffer, msgBytes);
168174

169-
Runtime.dynCall('vii', webSocketState.onError, [ instanceId, msgBuffer ]);
175+
try {
176+
Runtime.dynCall('vii', webSocketState.onError, [ instanceId, msgBuffer ]);
177+
} finally {
178+
_free(msgBuffer);
179+
}
170180

171181
}
172182

@@ -189,8 +199,10 @@ var LibraryWebSocket = {
189199
* Close WebSocket connection
190200
*
191201
* @param instanceId Instance ID
202+
* @param code Close status code
203+
* @param reasonPtr Pointer to reason string
192204
*/
193-
WebSocketClose: function(instanceId) {
205+
WebSocketClose: function(instanceId, code, reasonPtr) {
194206

195207
var instance = webSocketState.instances[instanceId];
196208
if (!instance) return -1;
@@ -204,7 +216,13 @@ var LibraryWebSocket = {
204216
if (instance.ws.readyState === 3)
205217
return -5;
206218

207-
instance.ws.close();
219+
var reason = ( reasonPtr ? Pointer_stringify(reasonPtr) : undefined );
220+
221+
try {
222+
instance.ws.close(code, reason);
223+
} catch(err) {
224+
return -7;
225+
}
208226

209227
return 0;
210228

0 commit comments

Comments
 (0)
0