public byte[] ToBytes() { var buffer = new List <byte>(); var header = (int)Fin; header = (header << 1) + (int)Rsv1; header = (header << 1) + (int)Rsv2; header = (header << 1) + (int)Rsv3; header = (header << 4) + (int)Opcode; header = (header << 1) + (int)Masked; header = (header << 7) + (int)PayloadLen; buffer.AddRange(((ushort)header).ToBytes(ByteOrder.BIG)); if (PayloadLen >= 126) { buffer.AddRange(ExtPayloadLen); } if (Masked == Mask.MASK) { buffer.AddRange(MaskingKey); } if (PayloadLen > 0) { buffer.AddRange(PayloadData.ToBytes()); } return(buffer.ToArray()); }
public CloseEventArgs(PayloadData data) : base(Opcode.CLOSE, data) { _code = (ushort)CloseStatusCode.NO_STATUS_CODE; _reason = string.Empty; _wasClean = false; if (data.Length >= 2) { _code = Ext.To <ushort>(Ext.SubArray(data.ToBytes(), 0, 2), ByteOrder.BIG); } if (data.Length > 2) { var buffer = Ext.SubArray(data.ToBytes(), 2, (int)(data.Length - 2)); _reason = Encoding.UTF8.GetString(buffer); } }
private string getReasonFrom(PayloadData data) { if (data.Length <= 2) { return(String.Empty); } var buffer = data.ToBytes().SubArray(2, (int)(data.Length - 2)); return(Encoding.UTF8.GetString(buffer)); }
private bool canSendAsCloseFrame(PayloadData data) { if (data.Length >= 2) { var code = data.ToBytes().SubArray(0, 2).To <ushort>(ByteOrder.BIG); if (code == (ushort)CloseStatusCode.NO_STATUS_CODE || code == (ushort)CloseStatusCode.ABNORMAL || code == (ushort)CloseStatusCode.TLS_HANDSHAKE_FAILURE) { return(false); } } return(true); }
public void Print() { byte[] buffer; long count, i, j; int countDigit, remainder; string countFmt, extPayloadLen, headerFmt, topLineFmt, bottomLineFmt, payloadData, spFmt; switch (ExtPayloadLen.Length) { case 2: extPayloadLen = ExtPayloadLen.To <ushort>(ByteOrder.BIG).ToString(); break; case 8: extPayloadLen = ExtPayloadLen.To <ulong>(ByteOrder.BIG).ToString(); break; default: extPayloadLen = String.Empty; break; } if (((Opcode.TEXT | Opcode.PING | Opcode.PONG) & Opcode) == Opcode && Masked == Mask.UNMASK && PayloadLength > 0) { payloadData = Encoding.UTF8.GetString(PayloadData.ToBytes()); } else { payloadData = BitConverter.ToString(PayloadData.ToBytes()); } headerFmt = @" WsFrame: FIN={0}, RSV1={1}, RSV2={2}, RSV3={3}, Opcode={4}, MASK={5}, Payload Len={6}, Extended Payload Len={7}, Masking Key ={8}, Payload Data={9}"; buffer = ToBytes(); count = (long)(Length / 4); remainder = (int)(Length % 4); if (count < 10000) { countDigit = 4; countFmt = "{0,4}"; } else if (count < 0x010000) { countDigit = 4; countFmt = "{0,4:X}"; } else if (count < 0x0100000000) { countDigit = 8; countFmt = "{0,8:X}"; } else { countDigit = 16; countFmt = "{0,16:X}"; } spFmt = String.Format("{{0,{0}}}", countDigit); topLineFmt = String.Format(@" {0} 01234567 89ABCDEF 01234567 89ABCDEF {0}+--------+--------+--------+--------+", spFmt); Func <string, Action <string, string, string, string> > func = s => { long lineCount = 0; string lineFmt = String.Format(" {0}|{{1,8}} {{2,8}} {{3,8}} {{4,8}}|", s); return((arg1, arg2, arg3, arg4) => { Console.WriteLine(lineFmt, ++lineCount, arg1, arg2, arg3, arg4); }); }; var printLine = func(countFmt); bottomLineFmt = String.Format(" {0}+--------+--------+--------+--------+", spFmt); Console.WriteLine(headerFmt, Fin, Rsv1, Rsv2, Rsv3, Opcode, Masked, PayloadLen, extPayloadLen, BitConverter.ToString(MaskingKey), payloadData); Console.WriteLine(topLineFmt, String.Empty); for (i = 0; i <= count; i++) { j = i * 4; if (i < count) { printLine( Convert.ToString(buffer[j], 2).PadLeft(8, '0'), Convert.ToString(buffer[j + 1], 2).PadLeft(8, '0'), Convert.ToString(buffer[j + 2], 2).PadLeft(8, '0'), Convert.ToString(buffer[j + 3], 2).PadLeft(8, '0')); } else if (i == count && remainder > 0) { printLine( Convert.ToString(buffer[j], 2).PadLeft(8, '0'), remainder >= 2 ? Convert.ToString(buffer[j + 1], 2).PadLeft(8, '0') : String.Empty, remainder == 3 ? Convert.ToString(buffer[j + 2], 2).PadLeft(8, '0') : String.Empty, String.Empty); } } Console.WriteLine(bottomLineFmt, String.Empty); }
private ushort getCodeFrom(PayloadData data) { return(data.Length >= 2 ? data.ToBytes().SubArray(0, 2).To <ushort>(ByteOrder.BIG) : (ushort)CloseStatusCode.NO_STATUS_CODE); }