Пример #1
0
        protected void EnqueueMessage(WSFrameType opCode, bool isMasked, byte[] payLoad, bool highPriority = false)
        {
            WSFrame wsFrame = WSFrame.CreateFrame(opCode, isMasked, payLoad);

            if (highPriority)
            {
                this.sendQueue.Poke(wsFrame);
            }
            else
            {
                this.sendQueue.Enqueue(wsFrame);
            }
        }
Пример #2
0
        public static WSFrame HarriOlli(WSFrameType opCode, bool isMasked, byte[] payLoad)
        {
            // TODO - add code to support continuation frames
            // - add long maxFrameLength parameter
            // - return array of WSFrame objects

            byte controlByte0 = (byte)(0x80 | (byte)opCode);
            byte controlByte1 = (isMasked && payLoad != null && payLoad.Length > 0 ? (byte)0x80 : (byte)0x00);
            byte[] mask = (isMasked ? CryptoUtils.GetRandomBytes(4) : new byte[0]);
            int maskIndex = 0;
            Int32 payloadIndex = 0;
            UInt16 length16 = 0;
            UInt32 length32 = 0;
            UInt64 length64 = 0;
            byte[] frameData = null;

            if (payLoad == null || payLoad.Length == 0)
            {
                length32 = 0;
                payloadIndex = 0;
                frameData = ArrayUtil.Concat(controlByte0, controlByte1);
            }
            else if (payLoad.Length <= 125)
            {
                controlByte1 = (byte)(controlByte1 | (byte)payLoad.Length);
                length32 = (UInt32)payLoad.Length;
                maskIndex = (isMasked ? 2 : 0);
                payloadIndex = (isMasked ? 6 : 2);
                frameData = ArrayUtil.Concat(controlByte0, controlByte1, mask, payLoad);
            }
            else if (payLoad.Length <= UInt16.MaxValue)
            {
                controlByte1 = (byte)(controlByte1 | (byte)126);
                length16 = (UInt16)payLoad.Length;
                length32 = length16;
                maskIndex = (isMasked ? 4 : 0);
                payloadIndex = (isMasked ? 8 : 4);
                frameData = ArrayUtil.Concat(controlByte0, controlByte1, length16, mask, payLoad);
            }
            else
            {
                controlByte1 = (byte)(controlByte1 | (byte)127);
                length32 = (UInt32)payLoad.Length;
                length64 = (UInt64)payLoad.Length;
                maskIndex = (isMasked ? 10 : 0);
                payloadIndex = (isMasked ? 14 : 10);
                frameData = ArrayUtil.Concat(controlByte0, controlByte1, length64, mask, payLoad);
            }

            WSFrame dataFrame = new WSFrame()
            {
                maskIndex = maskIndex,
                payloadIndex = payloadIndex,
                payloadLength = (Int32)length32,
                frameData = frameData
            };

            if (isMasked)
            {
                //	Logger.WriteDebug("wsframe", string.Concat("raw: ", frameData.ToHex()));
                ApplyMask(dataFrame);
            }

            return dataFrame;
        }
Пример #3
0
        /// <summary>
        /// Creates an RFC 6455 BFP frame with the opcode and payload bytes specified.
        /// </summary>
        /// <param name="opCode">BFP opcode.</param>
        /// <param name="isMasked">Boolean indicating whether to mask the payload.</param>
        /// <param name="payLoad">Payload bytes.</param>
        /// <returns></returns>
        public static WSFrame CreateFrame(WSFrameType opCode, bool isMasked, byte[] payLoad)
        {
            /*
            DebugEx.WriteLine(" ########################## ---------------------------- ");
            DebugEx.WriteLine("Val:");
            DebugEx.WriteLine(opCode.ToString());
            string m = GadgeteerOrchestratorJSClient.ByteExtensions.ToHexString(payLoad);
            DebugEx.WriteLine(m);
            DebugEx.WriteLine(" ########################## ---------------------------- ");
            */

            // HalliOlli -function's CryptoUtils.GetRandomBytes randomly causes an exception, so giving false for isMasked parameter always
            int i = 0;
            while (i < 10)
            {
                ++i;
                WSFrame retVal;
                try
                {
                    return HarriOlli(opCode, false, payLoad);
                    break;
                }
                catch (Exception eeeXXX)
                {
                    DebugEx.WriteLine(" ########################## ---------------------------- EEEEEE");
                    DebugEx.WriteLine(eeeXXX.ToString());
                    DebugEx.WriteLine(" ########################## ---------------------------- EEEEEE");
                    if(i > 2)
                    {
                        //throw(eeeXXX);
                    }
                }
            }
            return null;
        }
Пример #4
0
 /// <summary>
 /// Creates a BFP frame with the opcode and payload string specified.
 /// </summary>
 /// <param name="opCode">BFP opcode.</param>
 /// <param name="isMasked">Boolean indicating whether to mask the payload.</param>
 /// <param name="payLoad">Payload string.</param>
 /// <returns></returns>
 public static WSFrame CreateFrame(WSFrameType opCode, bool isMasked, string payLoad)
 {
     return CreateFrame(opCode, isMasked, Encoding.UTF8.GetBytes(payLoad));
 }
Пример #5
0
 /// <summary>
 /// Creates a BFP frame with the opcode specified, but no payload.
 /// </summary>
 /// <param name="opCode">BFP opcode.</param>
 /// <returns></returns>
 public static WSFrame CreateFrame(WSFrameType opCode)
 {
     return CreateFrame(opCode, false, (byte[])null);
 }
Пример #6
0
 protected void EnqueueMessage(WSFrameType opCode, bool isMasked, byte[] payLoad, bool highPriority = false)
 {
     WSFrame wsFrame = WSFrame.CreateFrame(opCode, isMasked, payLoad);
     if (highPriority)
         this.sendQueue.Poke(wsFrame);
     else
         this.sendQueue.Enqueue(wsFrame);
 }
Пример #7
0
        /// <summary>
        /// Creates an RFC 6455 BFP frame with the opcode and payload bytes specified.
        /// </summary>
        /// <param name="opCode">BFP opcode.</param>
        /// <param name="isMasked">Boolean indicating whether to mask the payload.</param>
        /// <param name="payLoad">Payload bytes.</param>
        /// <returns></returns>
        public static WSFrame CreateFrame(WSFrameType opCode, bool isMasked, byte[] payLoad)
        {
            // TODO - add code to support continuation frames
            // - add long maxFrameLength parameter
            // - return array of WSFrame objects

            byte controlByte0 = (byte)(0x80 | (byte)opCode);
            byte controlByte1 = (isMasked && payLoad != null && payLoad.Length > 0 ? (byte)0x80 : (byte)0x00);

            byte[] mask         = (isMasked ? CryptoUtils.GetRandomBytes(4) : new byte[0]);
            int    maskIndex    = 0;
            Int32  payloadIndex = 0;
            UInt16 length16     = 0;
            UInt32 length32     = 0;
            UInt64 length64     = 0;

            byte[] frameData = null;

            if (payLoad == null || payLoad.Length == 0)
            {
                length32     = 0;
                payloadIndex = 0;
                frameData    = ArrayUtil.Concat(controlByte0, controlByte1);
            }
            else if (payLoad.Length <= 125)
            {
                controlByte1 = (byte)(controlByte1 | (byte)payLoad.Length);
                length32     = (UInt32)payLoad.Length;
                maskIndex    = (isMasked ? 2 : 0);
                payloadIndex = (isMasked ? 6 : 2);
                frameData    = ArrayUtil.Concat(controlByte0, controlByte1, mask, payLoad);
            }
            else if (payLoad.Length <= UInt16.MaxValue)
            {
                controlByte1 = (byte)(controlByte1 | (byte)126);
                length16     = (UInt16)payLoad.Length;
                length32     = length16;
                maskIndex    = (isMasked ? 4 : 0);
                payloadIndex = (isMasked ? 8 : 4);
                frameData    = ArrayUtil.Concat(controlByte0, controlByte1, length16, mask, payLoad);
            }
            else
            {
                controlByte1 = (byte)(controlByte1 | (byte)127);
                length32     = (UInt32)payLoad.Length;
                length64     = (UInt64)payLoad.Length;
                maskIndex    = (isMasked ? 10 : 0);
                payloadIndex = (isMasked ? 14 : 10);
                frameData    = ArrayUtil.Concat(controlByte0, controlByte1, length64, mask, payLoad);
            }

            WSFrame dataFrame = new WSFrame()
            {
                maskIndex     = maskIndex,
                payloadIndex  = payloadIndex,
                payloadLength = (Int32)length32,
                frameData     = frameData
            };

            if (isMasked)
            {
                //	Logger.WriteDebug("wsframe", string.Concat("raw: ", frameData.ToHex()));
                ApplyMask(dataFrame);
            }

            return(dataFrame);
        }
Пример #8
0
 /// <summary>
 /// Creates a BFP frame with the opcode and payload string specified.
 /// </summary>
 /// <param name="opCode">BFP opcode.</param>
 /// <param name="isMasked">Boolean indicating whether to mask the payload.</param>
 /// <param name="payLoad">Payload string.</param>
 /// <returns></returns>
 public static WSFrame CreateFrame(WSFrameType opCode, bool isMasked, string payLoad)
 {
     return(CreateFrame(opCode, isMasked, Encoding.UTF8.GetBytes(payLoad)));
 }
Пример #9
0
 /// <summary>
 /// Creates a BFP frame with the opcode specified, but no payload.
 /// </summary>
 /// <param name="opCode">BFP opcode.</param>
 /// <returns></returns>
 public static WSFrame CreateFrame(WSFrameType opCode)
 {
     return(CreateFrame(opCode, false, (byte[])null));
 }