Refactored WebSocketFrame.cs
This commit is contained in:
		@@ -539,7 +539,7 @@ namespace WebSocketSharp.Server
 | 
			
		||||
        return null;
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      return broadping (WebSocketFrame.EmptyUnmaskPingData, _waitTime);
 | 
			
		||||
      return broadping (WebSocketFrame.EmptyUnmaskPingBytes, _waitTime);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// <summary>
 | 
			
		||||
 
 | 
			
		||||
@@ -114,7 +114,7 @@ namespace WebSocketSharp.Server
 | 
			
		||||
    /// </value>
 | 
			
		||||
    public IEnumerable<string> ActiveIDs {
 | 
			
		||||
      get {
 | 
			
		||||
        foreach (var res in Broadping (WebSocketFrame.EmptyUnmaskPingData, _waitTime))
 | 
			
		||||
        foreach (var res in Broadping (WebSocketFrame.EmptyUnmaskPingBytes, _waitTime))
 | 
			
		||||
          if (res.Value)
 | 
			
		||||
            yield return res.Key;
 | 
			
		||||
      }
 | 
			
		||||
@@ -159,7 +159,7 @@ namespace WebSocketSharp.Server
 | 
			
		||||
    /// </value>
 | 
			
		||||
    public IEnumerable<string> InactiveIDs {
 | 
			
		||||
      get {
 | 
			
		||||
        foreach (var res in Broadping (WebSocketFrame.EmptyUnmaskPingData, _waitTime))
 | 
			
		||||
        foreach (var res in Broadping (WebSocketFrame.EmptyUnmaskPingBytes, _waitTime))
 | 
			
		||||
          if (!res.Value)
 | 
			
		||||
            yield return res.Key;
 | 
			
		||||
      }
 | 
			
		||||
@@ -545,7 +545,7 @@ namespace WebSocketSharp.Server
 | 
			
		||||
        return null;
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      return Broadping (WebSocketFrame.EmptyUnmaskPingData, _waitTime);
 | 
			
		||||
      return Broadping (WebSocketFrame.EmptyUnmaskPingBytes, _waitTime);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// <summary>
 | 
			
		||||
 
 | 
			
		||||
@@ -967,8 +967,7 @@ namespace WebSocketSharp
 | 
			
		||||
 | 
			
		||||
    private bool processPingFrame (WebSocketFrame frame)
 | 
			
		||||
    {
 | 
			
		||||
      var mask = _client ? Mask.Mask : Mask.Unmask;
 | 
			
		||||
      if (send (WebSocketFrame.CreatePongFrame (mask, frame.PayloadData).ToByteArray ()))
 | 
			
		||||
      if (send (new WebSocketFrame (Opcode.Pong, frame.PayloadData, _client).ToByteArray ()))
 | 
			
		||||
        _logger.Trace ("Returned a Pong.");
 | 
			
		||||
 | 
			
		||||
      return true;
 | 
			
		||||
@@ -1087,7 +1086,7 @@ namespace WebSocketSharp
 | 
			
		||||
            compressed = true;
 | 
			
		||||
          }
 | 
			
		||||
 | 
			
		||||
          sent = send (opcode, _client ? Mask.Mask : Mask.Unmask, stream, compressed);
 | 
			
		||||
          sent = send (opcode, stream, compressed);
 | 
			
		||||
          if (!sent)
 | 
			
		||||
            error ("Sending the data has been interrupted.", null);
 | 
			
		||||
        }
 | 
			
		||||
@@ -1106,14 +1105,14 @@ namespace WebSocketSharp
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private bool send (Opcode opcode, Mask mask, Stream stream, bool compressed)
 | 
			
		||||
    private bool send (Opcode opcode, Stream stream, bool compressed)
 | 
			
		||||
    {
 | 
			
		||||
      var len = stream.Length;
 | 
			
		||||
 | 
			
		||||
      /* Not fragmented */
 | 
			
		||||
 | 
			
		||||
      if (len == 0)
 | 
			
		||||
        return send (Fin.Final, opcode, mask, new byte[0], compressed);
 | 
			
		||||
        return send (Fin.Final, opcode, new byte[0], compressed);
 | 
			
		||||
 | 
			
		||||
      var quo = len / FragmentLength;
 | 
			
		||||
      var rem = (int) (len % FragmentLength);
 | 
			
		||||
@@ -1122,25 +1121,25 @@ namespace WebSocketSharp
 | 
			
		||||
      if (quo == 0) {
 | 
			
		||||
        buff = new byte[rem];
 | 
			
		||||
        return stream.Read (buff, 0, rem) == rem &&
 | 
			
		||||
               send (Fin.Final, opcode, mask, buff, compressed);
 | 
			
		||||
               send (Fin.Final, opcode, buff, compressed);
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      buff = new byte[FragmentLength];
 | 
			
		||||
      if (quo == 1 && rem == 0)
 | 
			
		||||
        return stream.Read (buff, 0, FragmentLength) == FragmentLength &&
 | 
			
		||||
               send (Fin.Final, opcode, mask, buff, compressed);
 | 
			
		||||
               send (Fin.Final, opcode, buff, compressed);
 | 
			
		||||
 | 
			
		||||
      /* Send fragmented */
 | 
			
		||||
 | 
			
		||||
      // Begin
 | 
			
		||||
      if (stream.Read (buff, 0, FragmentLength) != FragmentLength ||
 | 
			
		||||
          !send (Fin.More, opcode, mask, buff, compressed))
 | 
			
		||||
          !send (Fin.More, opcode, buff, compressed))
 | 
			
		||||
        return false;
 | 
			
		||||
 | 
			
		||||
      var n = rem == 0 ? quo - 2 : quo - 1;
 | 
			
		||||
      for (long i = 0; i < n; i++)
 | 
			
		||||
        if (stream.Read (buff, 0, FragmentLength) != FragmentLength ||
 | 
			
		||||
            !send (Fin.More, Opcode.Cont, mask, buff, compressed))
 | 
			
		||||
            !send (Fin.More, Opcode.Cont, buff, compressed))
 | 
			
		||||
          return false;
 | 
			
		||||
 | 
			
		||||
      // End
 | 
			
		||||
@@ -1150,10 +1149,10 @@ namespace WebSocketSharp
 | 
			
		||||
        buff = new byte[rem];
 | 
			
		||||
 | 
			
		||||
      return stream.Read (buff, 0, rem) == rem &&
 | 
			
		||||
             send (Fin.Final, Opcode.Cont, mask, buff, compressed);
 | 
			
		||||
             send (Fin.Final, Opcode.Cont, buff, compressed);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private bool send (Fin fin, Opcode opcode, Mask mask, byte[] data, bool compressed)
 | 
			
		||||
    private bool send (Fin fin, Opcode opcode, byte[] data, bool compressed)
 | 
			
		||||
    {
 | 
			
		||||
      lock (_forConn) {
 | 
			
		||||
        if (_readyState != WebSocketState.Open) {
 | 
			
		||||
@@ -1162,7 +1161,7 @@ namespace WebSocketSharp
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return sendBytes (
 | 
			
		||||
          WebSocketFrame.CreateWebSocketFrame (fin, opcode, mask, data, compressed).ToByteArray ());
 | 
			
		||||
          new WebSocketFrame (fin, opcode, data, compressed, _client).ToByteArray ());
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -1500,12 +1499,12 @@ namespace WebSocketSharp
 | 
			
		||||
          try {
 | 
			
		||||
            byte[] cached;
 | 
			
		||||
            if (!cache.TryGetValue (_compression, out cached)) {
 | 
			
		||||
              cached = WebSocketFrame.CreateWebSocketFrame (
 | 
			
		||||
              cached = new WebSocketFrame (
 | 
			
		||||
                Fin.Final,
 | 
			
		||||
                opcode,
 | 
			
		||||
                Mask.Unmask,
 | 
			
		||||
                data.Compress (_compression),
 | 
			
		||||
                _compression != CompressionMethod.None)
 | 
			
		||||
                _compression != CompressionMethod.None,
 | 
			
		||||
                false)
 | 
			
		||||
                .ToByteArray ();
 | 
			
		||||
 | 
			
		||||
              cache.Add (_compression, cached);
 | 
			
		||||
@@ -1534,7 +1533,7 @@ namespace WebSocketSharp
 | 
			
		||||
            cached.Position = 0;
 | 
			
		||||
          }
 | 
			
		||||
 | 
			
		||||
          send (opcode, Mask.Unmask, cached, _compression != CompressionMethod.None);
 | 
			
		||||
          send (opcode, cached, _compression != CompressionMethod.None);
 | 
			
		||||
        }
 | 
			
		||||
        catch (Exception ex) {
 | 
			
		||||
          _logger.Fatal (ex.ToString ());
 | 
			
		||||
@@ -1885,7 +1884,7 @@ namespace WebSocketSharp
 | 
			
		||||
    {
 | 
			
		||||
      var bytes = _client
 | 
			
		||||
                  ? WebSocketFrame.CreatePingFrame (true).ToByteArray ()
 | 
			
		||||
                  : WebSocketFrame.EmptyUnmaskPingData;
 | 
			
		||||
                  : WebSocketFrame.EmptyUnmaskPingBytes;
 | 
			
		||||
 | 
			
		||||
      return Ping (bytes, _waitTime);
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -53,7 +53,7 @@ namespace WebSocketSharp
 | 
			
		||||
 | 
			
		||||
    #region Internal Fields
 | 
			
		||||
 | 
			
		||||
    internal static readonly byte[] EmptyUnmaskPingData;
 | 
			
		||||
    internal static readonly byte[] EmptyUnmaskPingBytes;
 | 
			
		||||
 | 
			
		||||
    #endregion
 | 
			
		||||
 | 
			
		||||
@@ -61,7 +61,7 @@ namespace WebSocketSharp
 | 
			
		||||
 | 
			
		||||
    static WebSocketFrame ()
 | 
			
		||||
    {
 | 
			
		||||
      EmptyUnmaskPingData = CreatePingFrame (false).ToByteArray ();
 | 
			
		||||
      EmptyUnmaskPingBytes = CreatePingFrame (false).ToByteArray ();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    #endregion
 | 
			
		||||
@@ -76,32 +76,26 @@ namespace WebSocketSharp
 | 
			
		||||
 | 
			
		||||
    #region Internal Constructors
 | 
			
		||||
 | 
			
		||||
    internal WebSocketFrame (Opcode opcode, PayloadData payload)
 | 
			
		||||
      : this (Fin.Final, opcode, Mask.Mask, payload, false)
 | 
			
		||||
    internal WebSocketFrame (Opcode opcode, PayloadData payloadData, bool mask)
 | 
			
		||||
      : this (Fin.Final, opcode, payloadData, false, mask)
 | 
			
		||||
    {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    internal WebSocketFrame (Opcode opcode, Mask mask, PayloadData payload)
 | 
			
		||||
      : this (Fin.Final, opcode, mask, payload, false)
 | 
			
		||||
    {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    internal WebSocketFrame (Fin fin, Opcode opcode, Mask mask, PayloadData payload)
 | 
			
		||||
      : this (fin, opcode, mask, payload, false)
 | 
			
		||||
    internal WebSocketFrame (Fin fin, Opcode opcode, byte[] data, bool compressed, bool mask)
 | 
			
		||||
      : this (fin, opcode, new PayloadData (data), compressed, mask)
 | 
			
		||||
    {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    internal WebSocketFrame (
 | 
			
		||||
      Fin fin, Opcode opcode, Mask mask, PayloadData payload, bool compressed)
 | 
			
		||||
      Fin fin, Opcode opcode, PayloadData payloadData, bool compressed, bool mask)
 | 
			
		||||
    {
 | 
			
		||||
      _fin = fin;
 | 
			
		||||
      _rsv1 = isData (opcode) && compressed ? Rsv.On : Rsv.Off;
 | 
			
		||||
      _rsv2 = Rsv.Off;
 | 
			
		||||
      _rsv3 = Rsv.Off;
 | 
			
		||||
      _opcode = opcode;
 | 
			
		||||
      _mask = mask;
 | 
			
		||||
 | 
			
		||||
      var len = payload.Length;
 | 
			
		||||
      var len = payloadData.Length;
 | 
			
		||||
      if (len < 126) {
 | 
			
		||||
        _payloadLength = (byte) len;
 | 
			
		||||
        _extPayloadLength = new byte[0];
 | 
			
		||||
@@ -115,15 +109,17 @@ namespace WebSocketSharp
 | 
			
		||||
        _extPayloadLength = len.InternalToByteArray (ByteOrder.Big);
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      if (mask == Mask.Mask) {
 | 
			
		||||
      if (mask) {
 | 
			
		||||
        _mask = Mask.Mask;
 | 
			
		||||
        _maskingKey = createMaskingKey ();
 | 
			
		||||
        payload.Mask (_maskingKey);
 | 
			
		||||
        payloadData.Mask (_maskingKey);
 | 
			
		||||
      }
 | 
			
		||||
      else {
 | 
			
		||||
        _mask = Mask.Unmask;
 | 
			
		||||
        _maskingKey = new byte[0];
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      _payloadData = payload;
 | 
			
		||||
      _payloadData = payloadData;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    #endregion
 | 
			
		||||
@@ -523,36 +519,19 @@ Extended Payload Length: {7}
 | 
			
		||||
 | 
			
		||||
    #region Internal Methods
 | 
			
		||||
 | 
			
		||||
    internal static WebSocketFrame CreateCloseFrame (byte[] data, bool mask)
 | 
			
		||||
    {
 | 
			
		||||
      return CreateCloseFrame (new PayloadData (data), mask);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    internal static WebSocketFrame CreateCloseFrame (PayloadData payloadData, bool mask)
 | 
			
		||||
    {
 | 
			
		||||
      return new WebSocketFrame (Opcode.Close, mask ? Mask.Mask : Mask.Unmask, payloadData);
 | 
			
		||||
      return new WebSocketFrame (Fin.Final, Opcode.Close, payloadData, false, mask);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    internal static WebSocketFrame CreatePingFrame (bool mask)
 | 
			
		||||
    {
 | 
			
		||||
      return new WebSocketFrame (Opcode.Ping, mask ? Mask.Mask : Mask.Unmask, new PayloadData ());
 | 
			
		||||
      return new WebSocketFrame (Fin.Final, Opcode.Ping, new PayloadData (), false, mask);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    internal static WebSocketFrame CreatePingFrame (byte[] data, bool mask)
 | 
			
		||||
    {
 | 
			
		||||
      return new WebSocketFrame (
 | 
			
		||||
        Opcode.Ping, mask ? Mask.Mask : Mask.Unmask, new PayloadData (data));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    internal static WebSocketFrame CreatePongFrame (Mask mask, PayloadData payload)
 | 
			
		||||
    {
 | 
			
		||||
      return new WebSocketFrame (Opcode.Pong, mask, payload);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    internal static WebSocketFrame CreateWebSocketFrame (
 | 
			
		||||
      Fin fin, Opcode opcode, Mask mask, byte[] data, bool compressed)
 | 
			
		||||
    {
 | 
			
		||||
      return new WebSocketFrame (fin, opcode, mask, new PayloadData (data), compressed);
 | 
			
		||||
      return new WebSocketFrame (Fin.Final, Opcode.Ping, new PayloadData (data), false, mask);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    internal static WebSocketFrame Read (Stream stream)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user