示例#1
0
 /// <summary>
 /// Since the basic protocol will expect 0x00 and 0xFF (startbyte/endbyte)
 /// we implement (override) the receivedata method and just send the "frame" to be processed
 /// </summary>
 /// <param name="segment"></param>
 /// <param name="readState"></param>
 /// <param name="processFrame"></param>
 public override void ReceiveData(ArraySegment <byte> segment, IReadState readState, Action <FrameType, IList <byte> > processFrame)
 {
     readState.Data.AddRange(segment.Array.Take(segment.Count));
     processFrame(FrameType.Text, readState.Data);
     readState.Data.Clear();
     readState.Clear();
 }
示例#2
0
 /// <summary>
 /// Since the basic protocol will expect 0x00 and 0xFF (startbyte/endbyte)
 /// we implement (override) the receivedata method and just send the "frame" to be processed 
 /// </summary>
 /// <param name="segment"></param>
 /// <param name="readState"></param>
 /// <param name="processFrame"></param>
 public override void ReceiveData(ArraySegment<byte> segment, IReadState readState, Action<FrameType, IList<byte>> processFrame)
 {      
     readState.Data.AddRange(segment.Array.Take(segment.Count));
     processFrame(FrameType.Text, readState.Data);
     readState.Data.Clear();
     readState.Clear();    
 }
示例#3
0
        private void ReceiveData(List <byte> data, IReadState readState, Action <FrameType, byte[]> processFrame)
        {
            while (data.Count >= 2)
            {
                bool isFinal   = (data[0] & 128) != 0;
                var  frameType = (FrameType)(data[0] & 15);
                int  length    = (data[1] & 127);
                int  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
                }
                if (data.Count < index + payloadLength)
                {
                    return; //Not complete
                }
                IEnumerable <byte> payload = data
                                             .Skip(index)
                                             .Take(payloadLength)
                                             .Select(b => b);
                readState.Data.AddRange(payload);
                data.RemoveRange(0, index + payloadLength);
                if (frameType != FrameType.Continuation)
                {
                    readState.FrameType = frameType;
                }
                if (!isFinal || !readState.FrameType.HasValue)
                {
                    continue;
                }
                byte[]    stateData      = readState.Data.ToArray();
                FrameType?stateFrameType = readState.FrameType;
                readState.Clear();
                processFrame(stateFrameType.Value, stateData);
            }
        }
        private void ReceiveData(List<byte> data, IReadState readState, Action<FrameType, byte[]> processFrame)
        {
            while (data.Count >= 2)
            {
                bool isFinal = (data[0] & 128) != 0;
                var frameType = (FrameType)(data[0] & 15);
                int length = (data[1] & 127);
                int 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

                if (data.Count < index + payloadLength)
                    return; //Not complete
                IEnumerable<byte> payload = data
                    .Skip(index)
                    .Take(payloadLength)
                    .Select(b => b);
                readState.Data.AddRange(payload);
                data.RemoveRange(0, index + payloadLength);
                if (frameType != FrameType.Continuation)
                    readState.FrameType = frameType;
                if (!isFinal || !readState.FrameType.HasValue) continue;
                byte[] stateData = readState.Data.ToArray();
                FrameType? stateFrameType = readState.FrameType;
                readState.Clear();
                processFrame(stateFrameType.Value, stateData);
            }
        }