示例#1
0
        /// <summary>
        /// 解析客戶端信息
        /// </summary>
        /// <param name="data"></param>
        /// <param name="readState"></param>
        /// <param name="processFrame"></param>
        /// <returns>[0]:FrameType [1]:byte[]</returns>
        private static object[] ParseData(List<byte> data, ReadState readState)
        {
            while (data.Count >= 2)
            {
                var isFinal = (data[0] & 128) != 0; //0x80
                var reservedBits = (data[0] & 112);
                var frameType = (FrameType)(data[0] & 15);
                var isMasked = (data[1] & 128) != 0;//0x80
                var length = (data[1] & 127); //0x7F

                if (!isMasked)
                {
                    //暫時不處理無掩碼的數據
                    return new object[] { "", "" };
                }

                var index = 2;
                int payloadLength;

                if (length == 127)
                {
                    //if (data.Count < index + 8)
                    //    return; //Not complete
                    payloadLength = data.Skip(index).Take(8).ToArray().ToLittleEndianInt();
                    index += 8;
                }
                else if (length == 126)
                {
                    //if (data.Count < index + 2)
                    //    return; //Not complete
                    payloadLength = data.Skip(index).Take(2).ToArray().ToLittleEndianInt();
                    index += 2;
                }
                else
                {
                    payloadLength = length;
                }

                //if (data.Count < index + 4)
                //    return; //Not complete

                var maskBytes = data.Skip(index).Take(4).ToArray();

                index += 4;


                //if (data.Count < index + payloadLength)
                //    return; //Not complete

                var payload = data
                                .Skip(index)
                                .Take(payloadLength)
                                .Select((x, i) => (byte)(x ^ maskBytes[i % 4]));


                readState.Data.AddRange(payload);
                data.RemoveRange(0, index + payloadLength);

                if (frameType != FrameType.Continuation)
                    readState.FrameType = frameType;

                if (isFinal && readState.FrameType.HasValue)
                {
                    byte[] stateData = readState.Data.ToArray();
                    FrameType stateFrameType = (FrameType)readState.FrameType;
                    readState.Clear();

                    //Done
                    return ProcessFrame(stateFrameType, stateData);
                }
            }
            return new object[] { "", "" };
        }
示例#2
0
        /// <summary>
        /// 接收并處理客戶端信息
        /// </summary>
        /// <param name="clientSkt"></param>
        /// <returns></returns>
        public static object[] GetClientData(Socket clientSkt)
        {
            ReadState readState = new ReadState();
            int blockLength = MSGINFOHEAD.BodyLength + MSGINFOHEAD.TypeLength + MSGINFOHEAD.TerminalLength + MSGINFOHEAD.TerminalNameLength + MSGINFOHEAD.TicksLength + MSGINFOHEAD.ReciveObjLength + MSGINFOHEAD.GuidLength + MSGINFOHEAD.BlankLength;
            List<byte> data = new List<byte>();
            var buffer = new byte[blockLength];

            Read(data, buffer, clientSkt);

            if (data.Count > 0)
            {
                return ParseData(data, readState);
            }
            else
            {
                return new object[] { "", "" };
            }
        }