/// <summary> /// Convert to string. /// </summary> /// <param name="opcode">The opcode.</param> /// <param name="rawData">The frame data.</param> /// <returns>The string value.</returns> private static string ConvertToString(OpCodeFrame opcode, byte[] rawData) { return(rawData.LongLength == 0 ? String.Empty : opcode == OpCodeFrame.Text ? Encoding.UTF8.GetString(rawData) : opcode.ToString()); }
/// <summary> /// Message event. /// </summary> /// <param name="opcode">The opcode.</param> /// <param name="rawData">The frame data.</param> internal MessageEventArgs(OpCodeFrame opcode, byte[] rawData) { if ((ulong)rawData.LongLength > Payload.MaxLength) { throw new WebSocketException(CloseCodeStatus.MessageTooBig); } _opcode = opcode; _rawData = rawData; _data = ConvertToString(opcode, rawData); }
/// <summary> /// Get the message type from the opcode frame type. /// </summary> /// <param name="opCode">The opcode frame.</param> /// <returns>The message type.</returns> public static MessageType GetMessageType(OpCodeFrame opCode) { switch (opCode) { case OpCodeFrame.Binary: return(MessageType.Binary); case OpCodeFrame.Close: return(MessageType.Close); case OpCodeFrame.Ping: return(MessageType.Ping); case OpCodeFrame.Text: return(MessageType.Text); default: return(MessageType.Close); } }
/// <summary> /// Web socket frame structure. /// </summary> /// <param name="fin">The state of the data.</param> /// <param name="opcode">Represents the frame type of the WebSocket frame as defined in section 11.8 of the WebSocket protocol spec.</param> /// <param name="payloadData">The payload.</param> /// <param name="compressed">True to compress the data.</param> /// <param name="mask">True if masked.</param> public Frame(Fin fin, OpCodeFrame opcode, Payload payloadData, bool compressed, bool mask) { _fin = fin; _rsv1 = IsDataEx(opcode) && compressed ? Rsv.On : Rsv.Off; _rsv2 = Rsv.Off; _rsv3 = Rsv.Off; _opcode = opcode; var len = payloadData.Length; if (len < 126) { _payloadLength = (byte)len; _extPayloadLength = new byte[0]; } else if (len < 0x010000) { _payloadLength = (byte)126; _extPayloadLength = ((ushort)len).InternalToByteArray(Nequeo.Custom.ByteOrder.Big); } else { _payloadLength = (byte)127; _extPayloadLength = len.InternalToByteArray(Nequeo.Custom.ByteOrder.Big); } if (mask) { _mask = Mask.Mask; _maskingKey = CreateMaskingKey(); payloadData.Mask(_maskingKey); } else { _mask = Mask.Unmask; _maskingKey = new byte[0]; } _payloadData = payloadData; }
/// <summary> /// Send a data frame. /// </summary> /// <param name="stream">The stream containing the data.</param> /// <param name="opcode">The opcode frame type.</param> /// <param name="compressed">Is the data compressed.</param> /// <returns>The data frame to send.</returns> private Frame DataOpCodeFrame(MemoryStream stream, OpCodeFrame opcode, bool compressed) { // Determin the if more will be sent. long dataLength = stream.Length; long quo = dataLength / FragmentLength; int rem = (int)(dataLength % FragmentLength); // Frame. Frame frame = null; // Not fragmented. byte[] buff = null; int bytesRead = 0; if (quo == 0) { // Read the data from the stream. buff = new byte[rem]; bytesRead = stream.Read(buff, 0, rem); // Create the fragment frame. Return the frame. frame = new Frame(Fin.Final, opcode, buff, compressed, _client); return(frame); } buff = new byte[FragmentLength]; if (quo == 1 && rem == 0) { // Read the data from the stream. bytesRead = stream.Read(buff, 0, FragmentLength); // Create the fragment frame. Return the frame. frame = new Frame(Fin.Final, opcode, buff, compressed, _client); return(frame); } // Send fragmented. // Begin // Read the data from the stream. bytesRead = stream.Read(buff, 0, FragmentLength); // If the bytes read is equal to the fragment length. if (bytesRead > 0) { // Create the fragment frame. Return the frame. frame = new Frame(Fin.More, opcode, buff, compressed, _client); if (frame != null) { // Send the frame data. SendBytes(frame.ToByteArray()); } } long n = rem == 0 ? quo - 2 : quo - 1; for (long i = 0; i < n; i++) { // Read the data from the stream. bytesRead = stream.Read(buff, 0, FragmentLength); // If the bytes read is equal to the fragment length. if (bytesRead > 0) { // Create the fragment frame. Return the frame. frame = new Frame(Fin.More, OpCodeFrame.Continuation, buff, compressed, _client); if (frame != null) { // Send the frame data. SendBytes(frame.ToByteArray()); } } } // End if (rem == 0) { rem = FragmentLength; } else { buff = new byte[rem]; } // Read the data from the stream. bytesRead = stream.Read(buff, 0, rem); // Create the fragment frame. Return the frame. frame = new Frame(Fin.Final, OpCodeFrame.Continuation, buff, compressed, _client); return(frame); }
/// <summary> /// Message event. /// </summary> /// <param name="frame">The frame.</param> internal MessageEventArgs(Frame frame) { _opcode = frame.Opcode; _rawData = frame.PayloadData.ApplicationData; _data = ConvertToString(_opcode, _rawData); }
/// <summary> /// Web socket frame structure. /// </summary> /// <param name="fin">The state of the data.</param> /// <param name="opcode">Represents the frame type of the WebSocket frame as defined in section 11.8 of the WebSocket protocol spec.</param> /// <param name="data">The payload.</param> /// <param name="compressed">True to compress the data.</param> /// <param name="mask">True if masked.</param> public Frame(Fin fin, OpCodeFrame opcode, byte[] data, bool compressed, bool mask) : this(fin, opcode, new Payload(data), compressed, mask) { }
/// <summary> /// Web socket frame structure. /// </summary> /// <param name="opcode">Represents the frame type of the WebSocket frame as defined in section 11.8 of the WebSocket protocol spec.</param> /// <param name="payloadData">The payload.</param> /// <param name="mask">True if masked.</param> public Frame(OpCodeFrame opcode, Payload payloadData, bool mask) : this(Fin.Final, opcode, payloadData, false, mask) { }
/// <summary> /// Is data. /// </summary> /// <param name="opcode">The opcode.</param> /// <returns>True if is data.</returns> private static bool IsDataEx(OpCodeFrame opcode) { return(opcode == OpCodeFrame.Text || opcode == OpCodeFrame.Binary); }
/// <summary> /// Is control. /// </summary> /// <param name="opcode">The opcode.</param> /// <returns>True if is control.</returns> private static bool IsControlEx(OpCodeFrame opcode) { return(opcode == OpCodeFrame.Close || opcode == OpCodeFrame.Ping || opcode == OpCodeFrame.Pong); }