private async Task <RpcPayload> ReceiveAsync(ClientWebSocket client, TaskCompletionSource <bool> readySignal, CancellationToken cancelToken) { // Reset stream _decompressed.Position = 0; _decompressed.SetLength(0); // Receive data WebSocketReceiveResult result; do { var buffer = _decompressed.Buffer.RequestSegment(10 * 1024); // 10 KB result = await client.ReceiveAsync(buffer, cancelToken).ConfigureAwait(false); _decompressed.Buffer.Advance(result.Count); if (result.CloseStatus != null) { throw new WebSocketClosedException(result.CloseStatus.Value, result.CloseStatusDescription); } }while (!result.EndOfMessage); // Deserialize var payload = JsonSerializer.Read <RpcPayload>(_decompressed.Buffer.AsReadOnlySpan()); // Handle result HandleEvent(payload, readySignal); // Must be before event so slow user handling can't trigger our timeouts ReceivedPayload?.Invoke(payload, new PayloadInfo(_decompressed.Buffer.Length, _decompressed.Buffer.Length)); return(payload); }
// Emit a protocol event private void Emit(int channel, IProtocolEvent protevent) { protevent.Channel = channel; log.Debug("Assembler: protocol event:", protevent); ReceivedPayload <IProtocolEvent> payload = new ReceivedPayload <IProtocolEvent>(); payload.Payload = protevent; if (protevent is ConnectionCloseOk) { if (Closed != null) { Closed(this, EventArgs.Empty); } } else { if (ReceivedEvent != null) { ReceivedEvent(this, payload); } else { log.Debug("No listener for event: {0}", protevent); } } }
private async Task <PubsubPayload> ReceiveAsync(ClientWebSocket client, CancellationToken cancelToken) { // Reset memory stream _memoryStream.Position = 0; _memoryStream.SetLength(0); WebSocketReceiveResult result; do { var buffer = _memoryStream.Buffer.RequestSegment(10 * 1024); result = await client.ReceiveAsync(buffer, cancelToken).ConfigureAwait(false); _memoryStream.Buffer.Advance(result.Count); if (result.CloseStatus != null) { throw new WebSocketClosedException(result.CloseStatus.Value, result.CloseStatusDescription); } }while (!result.EndOfMessage); var payload = Serializer.Read <PubsubPayload>(_memoryStream.Buffer.AsReadOnlySpan()); HandleEvent(payload); ReceivedPayload?.Invoke(payload, _memoryStream.Buffer.Length); return(payload); }
private async Task <GatewayPayload> ReceiveAsync(ClientWebSocket client, TaskCompletionSource <bool> readySignal, CancellationToken cancelToken) { // Reset compressed stream _compressed.Position = 0; _compressed.SetLength(0); // Receive compressed data WebSocketReceiveResult result; do { var buffer = _compressed.Buffer.RequestSegment(10 * 1024); // 10 KB result = await client.ReceiveAsync(buffer, cancelToken).ConfigureAwait(false); _compressed.Buffer.Advance(result.Count); _receivedData = true; if (result.CloseStatus != null) { throw new WebSocketClosedException(result.CloseStatus.Value, result.CloseStatusDescription); } }while (!result.EndOfMessage); // Skip zlib header if (!_readZlibHeader) { if (_compressed.Buffer.Array[0] != 0x78) { throw new SerializationException("First payload is missing zlib header"); } _compressed.Position = 2; _readZlibHeader = true; } // Reset decompressed stream _decompressed.Position = 0; _decompressed.SetLength(0); // Decompress _zlibStream.CopyTo(_decompressed); // Deserialize var payload = EtfSerializer.Read <GatewayPayload>(_decompressed.Buffer.AsReadOnlySpan()); if (payload.Sequence.HasValue) { _lastSeq = payload.Sequence.Value; } // Handle result HandleEvent(payload, readySignal); // Must be before event so slow user handling can't trigger our timeouts ReceivedPayload?.Invoke(payload, new PayloadInfo(_decompressed.Buffer.Length, _compressed.Buffer.Length)); return(payload); }
private void OnWillDisappear(ReceivedPayload payload) { if (this._actions.ContainsKey(payload.Context)) { var action = this._actions[payload.Context]; action.OnWillDisappear(new SettingsEventArgs { Settings = payload.Payload.Settings, SetSettings = (string key, object value) => this.SetSettings(payload, key, value), SetTitle = (string title) => this.SetTitle(payload, title) }); } this.UnregisterAction(payload.Context); }
private void SetTitle(ReceivedPayload payload, string title) { var json = JsonConvert.SerializeObject(new { @event = "setTitle", context = payload.Context, payload = new { title, target = 0 } }); _socket.SendAsync(new ArraySegment <byte>(Encoding.UTF8.GetBytes(json)), WebSocketMessageType.Text, true, CancellationToken.None); }
private void Fire_NetworkEvent(INetworkEvent netevent) { log.Debug("InputHandler: network event:", netevent); ReceivedPayload <INetworkEvent> payload = new ReceivedPayload <INetworkEvent>(); payload.Payload = netevent; if (ReceivedEvent != null) { ReceivedEvent(this, payload); } else { log.Debug("Nobody listening for event: {0}"); } }
void Go() { // create a BufferedStream on top of the NetworkStream. int threshold = m_bufferSize / 2; byte[] buffer = new byte[m_bufferSize]; try { int read; int offset = 0; ReceivedPayload <MemoryStream> payload = new ReceivedPayload <MemoryStream>(); while ((read = m_bufStream.Read(buffer, offset, m_bufferSize - offset)) > 0) { MemoryStream memStream = new MemoryStream(buffer, offset, read); if (ReceivedBuffer != null) { // call the event payload.Payload = memStream; ReceivedBuffer(this, payload); } offset += read; if (offset > threshold) { offset = 0; buffer = new byte[m_bufferSize]; } } log.Debug("Receiver thread terminating"); } catch (Exception t) { if (ExceptionReading != null) { ExceptionReading(this, new ExceptionArgs(t)); } } finally { if (ReceiverClosed != null) { ReceiverClosed(this, new EventArgs()); } } }
private void OnKeyUp(ReceivedPayload payload) { if (this._actions.ContainsKey(payload.Context)) { var action = this._actions[payload.Context]; action.OnKeyUp(new KeyEventArgs { Column = payload.Payload.Coordinates.Column, Device = payload.Device, IsInMultiAction = payload.Payload.IsInMultiAction, Row = payload.Payload.Coordinates.Row, Settings = payload.Payload.Settings, State = payload.Payload.State, UserDesiredState = payload.Payload.UserDesiredState, SetSettings = (string key, object value) => this.SetSettings(payload, key, value), SetTitle = (string title) => this.SetTitle(payload, title) }); } }
private void SetSettings(ReceivedPayload payload, string key, object value) { var valueJSON = JsonConvert.SerializeObject(value); if (payload.Payload.Settings.ContainsKey(key)) { payload.Payload.Settings[key] = valueJSON; } else { payload.Payload.Settings.Add(key, valueJSON); } var json = JsonConvert.SerializeObject(new { @event = "setSettings", context = payload.Context, payload = payload.Payload.Settings }); _socket.SendAsync(new ArraySegment <byte>(Encoding.UTF8.GetBytes(json)), WebSocketMessageType.Text, true, CancellationToken.None); }
public void On_ReceivedEvent(object sender, ReceivedPayload<IProtocolEvent> payload) { log.Debug("RECV: [{0}] {1}", this, payload.Payload); if (_channels.ContainsKey(payload.Payload.Channel)) return; Channel channel = GetChannel(payload.Payload.Channel); channel.On_ReceivedEvent(sender, payload); }
// Invoked when a network event is received public void On_ReceivedEvent(object sender, ReceivedPayload <INetworkEvent> payload) { payload.Payload.ProcessNetworkEvent(this); }
// The command listening for a buffer read. public void On_ReceivedBuffer(object sender, ReceivedPayload <MemoryStream> payload) { MemoryStream buf = payload.Payload; int remaining = (int)buf.Length; if (input != null) { remaining += (int)input.Length; } try { while (remaining > 0) { if (remaining >= needed) { if (input != null) { byte[] tmp = new byte[buf.Length]; buf.Read(tmp, 0, tmp.Length); input.Write(tmp, 0, tmp.Length); input.Seek(0, SeekOrigin.Begin); buf = input; } int startPos = (int)buf.Position; int consumed = needed; state = Next(buf); if ((buf.Position - startPos) < consumed) { buf.Seek(consumed - (buf.Position - startPos), SeekOrigin.Current); } remaining -= consumed; input = null; } else { byte[] tmp; if (input == null) { input = new MemoryStream(); tmp = new byte[remaining]; } else { // this is a full buffer tmp = new byte[buf.Length]; } buf.Read(tmp, 0, tmp.Length); input.Write(tmp, 0, tmp.Length); remaining = 0; } } } catch (Exception t) { Console.Write(t); if (ExceptionProcessing != null) { ExceptionProcessing(this, new ExceptionArgs(t)); } } }