void ProtocolViolation(IChannelHandlerContext ctx, IByteBuffer input, CorruptedWebSocketFrameException ex) { this.state = State.Corrupt; int readableBytes = input.ReadableBytes; if (readableBytes > 0) { // Fix for memory leak, caused by ByteToMessageDecoder#channelRead: // buffer 'cumulation' is released ONLY when no more readable bytes available. _ = input.SkipBytes(readableBytes); } if (ctx.Channel.IsActive && _config.CloseOnProtocolViolation) { object closeMessage; if (this.receivedClosingHandshake) { closeMessage = Unpooled.Empty; } else { WebSocketCloseStatus closeStatus = ex.CloseStatus; var errMsg = ex.Message; ICharSequence reasonText = !string.IsNullOrWhiteSpace(errMsg) ? new StringCharSequence(errMsg) : closeStatus.ReasonText; closeMessage = new CloseWebSocketFrame(closeStatus, reasonText); } _ = ctx.WriteAndFlushAsync(closeMessage).CloseOnComplete(ctx.Channel); } ExceptionDispatchInfo.Capture(ex).Throw(); }
public virtual Task CloseAsync(IChannel channel, CloseWebSocketFrame frame) { Contract.Requires(channel != null); return(channel.WriteAndFlushAsync(frame).ContinueWith((t, s) => ((IChannel)s).CloseAsync(), channel, TaskContinuationOptions.ExecuteSynchronously)); }
/// <summary> /// Performs the closing handshake /// </summary> /// <param name="channel">Channel</param> /// <param name="frame">Closing Frame that was received</param> public virtual Task CloseAsync(IChannel channel, CloseWebSocketFrame frame) { if (channel is null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.channel); } return(channel.WriteAndFlushAsync(frame).CloseOnComplete(channel)); }
void ProtocolViolation(IChannelHandlerContext ctx, CorruptedFrameException ex) { this.state = State.Corrupt; if (ctx.Channel.Active) { object closeMessage; if (this.receivedClosingHandshake) { closeMessage = Unpooled.Empty; } else { closeMessage = new CloseWebSocketFrame(1002, null); } ctx.WriteAndFlushAsync(closeMessage) .ContinueWith(t => ctx.Channel.CloseAsync()); } throw ex; }
public override Task CloseAsync(IChannel channel, CloseWebSocketFrame frame) => channel.WriteAndFlushAsync(frame);