private void SetClose() { if (_wsi.IsValid()) { _is_closing = true; SetReadyState(ReadyState.CLOSING); WSApi.lws_callback_on_writable(_wsi); _wsi = lws.Null; } else { SetReadyState(ReadyState.CLOSED); } }
private void OnWrite() { if (_pending.Count > 0) { var packet = _pending.Dequeue(); var protocol = packet.is_binary ? lws_write_protocol.LWS_WRITE_BINARY : lws_write_protocol.LWS_WRITE_TEXT; var len = packet.buffer.writerIndex - WSApi.LWS_PRE; unsafe { fixed(byte *buf = packet.buffer.data) { WSApi.lws_write(_wsi, &buf[WSApi.LWS_PRE], len, protocol); } } _bufferedAmount -= len; packet.Release(); if (_pending.Count > 0) { WSApi.lws_callback_on_writable(_wsi); } } }
private static JSValue _js_send(JSContext ctx, JSValue this_obj, int argc, JSValue[] argv) { try { WebSocket self; if (!js_get_classvalue(ctx, this_obj, out self)) { throw new ThisBoundException(); } if (argc == 0) { throw new ParameterException("data", typeof(string), 0); } if (!self._wsi.IsValid() || !self._context.IsValid()) { return(JSApi.JS_ThrowInternalError(ctx, "websocket closed")); } if (argv[0].IsString()) { // send text data size_t psize; var pointer = JSApi.JS_ToCStringLen(ctx, out psize, argv[0]); if (pointer != IntPtr.Zero && psize > 0) { var buffer = ScriptEngine.AllocByteBuffer(ctx, psize + WSApi.LWS_PRE); if (buffer != null) { buffer.WriteBytes(WSApi.LWS_PRE); buffer.WriteBytes(pointer, psize); self._pending.Enqueue(new Packet(false, buffer)); self._bufferedAmount += psize; WSApi.lws_callback_on_writable(self._wsi); } else { JSApi.JS_FreeCString(ctx, pointer); return(JSApi.JS_ThrowInternalError(ctx, "buf alloc failed")); } } JSApi.JS_FreeCString(ctx, pointer); } else { size_t psize; var pointer = JSApi.JS_GetArrayBuffer(ctx, out psize, argv[0]); if (pointer != IntPtr.Zero && psize > 0) { var buffer = ScriptEngine.AllocByteBuffer(ctx, psize + WSApi.LWS_PRE); if (buffer != null) { buffer.WriteBytes(WSApi.LWS_PRE); buffer.WriteBytes(pointer, psize); self._pending.Enqueue(new Packet(false, buffer)); self._bufferedAmount += psize; WSApi.lws_callback_on_writable(self._wsi); } else { return(JSApi.JS_ThrowInternalError(ctx, "buf alloc failed")); } } else { return(JSApi.JS_ThrowInternalError(ctx, "unknown buf type")); } } return(JSApi.JS_UNDEFINED); } catch (Exception exception) { return(JSApi.ThrowException(ctx, exception)); } }