public WebSocketFrame( WebSocketFin fin, WebSocketRsv rsv1, WebSocketRsv rsv2, WebSocketRsv rsv3, WebSocketOpcode opcode, WebSocketMask mask, byte[] maskingKey, byte payloadLength, byte[] extPayloadLength, Stream payload ) { if (mask == WebSocketMask.Mask && (maskingKey == null || maskingKey.Length != 4)) { throw new ArgumentOutOfRangeException("maskingKey", "must by 4 bytes long"); } _fin = fin; _rsv1 = rsv1; _rsv2 = rsv2; _rsv3 = rsv3; _opcode = opcode; _mask = mask; _maskingKey = maskingKey; _payloadLength = payloadLength; _extPayloadLength = extPayloadLength; _payload = payload; }
public WebSocketFrame( WebSocketFin fin, WebSocketRsv rsv1, WebSocketRsv rsv2, WebSocketRsv rsv3, WebSocketOpcode opcode, WebSocketMask mask, byte[] maskingKey, Stream payload ) : this(fin, rsv1, rsv2, rsv3, opcode, mask, maskingKey, 0, new byte[0], payload) { if (payload != null) { var len = payload.Length; if (len < 126) { _payloadLength = (byte)len; _extPayloadLength = new byte[0]; } else if (len < 0x010000) { _payloadLength = (byte)126; _extPayloadLength = WebSocketUtils.GetBigEndianBytes((ushort)len); } else { _payloadLength = (byte)127; _extPayloadLength = WebSocketUtils.GetBigEndianBytes((ulong)len); } } }
public WebSocketFrame( WebSocketFin fin, WebSocketRsv rsv1, WebSocketRsv rsv2, WebSocketRsv rsv3, WebSocketOpcode opcode, WebSocketMask mask, byte[] maskingKey, byte payloadLength, byte[] extPayloadLength, Stream payload ) { if (mask == WebSocketMask.Mask && (maskingKey == null || maskingKey.Length != 4)) throw new ArgumentOutOfRangeException("maskingKey", "must by 4 bytes long"); _fin = fin; _rsv1 = rsv1; _rsv2 = rsv2; _rsv3 = rsv3; _opcode = opcode; _mask = mask; _maskingKey = maskingKey; _payloadLength = payloadLength; _extPayloadLength = extPayloadLength; _payload = payload; }
public WebSocketFrame( WebSocketFin fin, WebSocketOpcode opcode, WebSocketMask mask, Stream payload ) : this(fin, WebSocketRsv.Off, WebSocketRsv.Off, WebSocketRsv.Off, opcode, mask, mask == WebSocketMask.Mask ? WebSocketUtils.CreateMaskingKey() : new byte[0], payload) { }
/// <summary> /// Applies the current masking key on the payload /// </summary> public void Unmask() { if (Mask == WebSocketMask.Unmask) { return; } _mask = WebSocketMask.Unmask; if (Payload != null) { using (Payload) { var unmasked = new MemoryStream((int)Payload.Length); Payload.Position = 0; for (var i = 0; i < Payload.Length; i++) { unmasked.WriteByte((byte)(Payload.ReadByte() ^ MaskingKey[i % 4])); } unmasked.Position = 0; _payload = unmasked; } } _maskingKey = new byte[0]; }
/// <summary> /// Applies the current masking key on the payload /// </summary> public void Unmask() { if (Mask == WebSocketMask.Unmask) return; _mask = WebSocketMask.Unmask; if (Payload != null) { using (Payload) { var unmasked = new MemoryStream((int)Payload.Length); Payload.Position = 0; for (var i = 0; i < Payload.Length; i++) { unmasked.WriteByte((byte)(Payload.ReadByte() ^ MaskingKey[i % 4])); } unmasked.Position = 0; _payload = unmasked; } } _maskingKey = new byte[0]; }