123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- #if !BESTHTTP_DISABLE_WEBSOCKET && (!UNITY_WEBGL || UNITY_EDITOR)
- using System;
- using System.Collections.Generic;
- using System.IO;
- using BestHTTP.Extensions;
- using BestHTTP.WebSocket.Extensions;
- namespace BestHTTP.WebSocket.Frames
- {
-
-
-
- public sealed class WebSocketFrameReader
- {
- #region Properties
- public byte Header { get; private set; }
-
-
-
- public bool IsFinal { get; private set; }
-
-
-
- public WebSocketFrameTypes Type { get; private set; }
-
-
-
- public bool HasMask { get; private set; }
-
-
-
- public UInt64 Length { get; private set; }
-
-
-
- public byte[] Mask { get; private set; }
-
-
-
- public byte[] Data { get; private set; }
-
-
-
- public string DataAsText { get; private set; }
- #endregion
- #region Internal & Private Functions
- internal void Read(Stream stream)
- {
-
-
- this.Header = ReadByte(stream);
-
- IsFinal = (this.Header & 0x80) != 0;
- Type = (WebSocketFrameTypes)(this.Header & 0xF);
- byte maskAndLength = ReadByte(stream);
-
- HasMask = (maskAndLength & 0x80) != 0;
-
- Length = (UInt64)(maskAndLength & 127);
-
- if (Length == 126)
- {
- byte[] rawLen = new byte[2];
- stream.ReadBuffer(rawLen);
- if (BitConverter.IsLittleEndian)
- Array.Reverse(rawLen, 0, rawLen.Length);
- Length = (UInt64)BitConverter.ToUInt16(rawLen, 0);
- }
- else if (Length == 127)
- {
-
-
- byte[] rawLen = new byte[8];
- stream.ReadBuffer(rawLen);
- if (BitConverter.IsLittleEndian)
- Array.Reverse(rawLen, 0, rawLen.Length);
- Length = (UInt64)BitConverter.ToUInt64(rawLen, 0);
- }
-
- if (HasMask)
- {
- Mask = new byte[4];
- if (stream.Read(Mask, 0, 4) < Mask.Length)
- throw ExceptionHelper.ServerClosedTCPStream();
- }
- Data = new byte[Length];
- if (Length == 0L)
- return;
- int readLength = 0;
- do
- {
- int read = stream.Read(Data, readLength, Data.Length - readLength);
- if (read <= 0)
- throw ExceptionHelper.ServerClosedTCPStream();
- readLength += read;
- } while (readLength < Data.Length);
-
- if (HasMask)
- for (int i = 0; i < Data.Length; ++i)
- Data[i] = (byte)(Data[i] ^ Mask[i % 4]);
- }
- private byte ReadByte(Stream stream)
- {
- int read = stream.ReadByte();
- if (read < 0)
- throw ExceptionHelper.ServerClosedTCPStream();
- return (byte)read;
- }
- #endregion
- #region Public Functions
-
-
-
-
- public void Assemble(List<WebSocketFrameReader> fragments)
- {
-
- fragments.Add(this);
- UInt64 finalLength = 0;
- for (int i = 0; i < fragments.Count; ++i)
- finalLength += fragments[i].Length;
- byte[] buffer = new byte[finalLength];
- UInt64 pos = 0;
- for (int i = 0; i < fragments.Count; ++i)
- {
- Array.Copy(fragments[i].Data, 0, buffer, (int)pos, (int)fragments[i].Length);
- pos += fragments[i].Length;
- }
-
- this.Type = fragments[0].Type;
-
- this.Header = fragments[0].Header;
- this.Length = finalLength;
- this.Data = buffer;
- }
-
-
-
- public void DecodeWithExtensions(WebSocket webSocket)
- {
- if (webSocket.Extensions != null)
- for (int i = 0; i < webSocket.Extensions.Length; ++i)
- {
- var ext = webSocket.Extensions[i];
- if (ext != null)
- this.Data = ext.Decode(this.Header, this.Data);
- }
- if (this.Type == WebSocketFrameTypes.Text && this.Data != null)
- this.DataAsText = System.Text.Encoding.UTF8.GetString(this.Data, 0, this.Data.Length);
- }
- #endregion
- }
- }
- #endif
|