Example #1
0
        public override void Append(byte[] someBytes, bool asFrame = false)
        {
            byte[] data = someBytes;
            if (asFrame)
            {
                UInt64 dataLength;
                if (InternalState == DataState.Empty)
                {
                    byte[] headerBytes = _header.FromBytes(someBytes);
                    Payload.Add(new ArraySegment <byte>(headerBytes));
                    int dataStart = headerBytes.Length;
                    data =
                        new byte[
                            Math.Min(Convert.ToInt32(Math.Min(_header.PayloadSizeRemaining, int.MaxValue)),
                                     someBytes.Length - dataStart)];
                    dataLength = Math.Min(_header.PayloadSizeRemaining, Convert.ToUInt64(someBytes.Length - dataStart));
                    Array.Copy(someBytes, dataStart, data, 0, Convert.ToInt32(dataLength));
                    Format = DataFormat.Frame;
                }
                else
                {
                    dataLength = Math.Min(Convert.ToUInt64(data.Length), _header.PayloadSizeRemaining);
                    data       = new byte[dataLength];
                    Array.Copy(someBytes, 0, data, 0, Convert.ToInt32(dataLength));
                }

                _header.PayloadSizeRemaining -= dataLength;
                switch (_header.OpCode)
                {
                case OpCode.Close:
                    InternalState = DataState.Closed;
                    break;

                case OpCode.Ping:
                    InternalState = DataState.Ping;
                    break;

                case OpCode.Pong:
                    InternalState = DataState.Pong;
                    break;

                default:
                    InternalState = _header.PayloadSizeRemaining == 0 ? DataState.Complete : DataState.Receiving;
                    break;
                }
            }
            else
            {
                Format = DataFormat.Raw;
            }
            Payload.Add(new ArraySegment <byte>(data));
        }
        public override int Append(byte[] someBytes, int receivedByteCount = -1, bool asFrame = false, long maxLength = 0)
        {
            if (receivedByteCount < 0)
            {
                receivedByteCount = someBytes.Length;
            }

            int readCount;

            if (asFrame)
            {
                // append data while receiving from network
                int dataLength;
                if (InternalState == DataState.Empty)
                {
                    int    dataStart;
                    byte[] headerBytes = _header.FromBytes(someBytes, receivedByteCount, out dataStart);
                    if (headerBytes == null)
                    {
                        return(0); // not all header bytes received
                    }

                    // header received
                    if (_header.PayloadSize < 0 || _header.PayloadSize > maxLength)
                    {
                        return(-1); // invalid data - disconnect
                    }
                    Payload.Add(new ArraySegment <byte>(headerBytes));
                    dataLength = Convert.ToInt32(Math.Min(_header.PayloadSizeRemaining, (long)(receivedByteCount - dataStart)));
                    var data = new byte[dataLength];
                    Array.Copy(someBytes, dataStart, data, 0, dataLength);
                    Format = DataFormat.Frame;
                    Payload.Add(new ArraySegment <byte>(data));
                    readCount = dataStart + dataLength;
                }
                else
                {
                    dataLength = Convert.ToInt32(Math.Min(_header.PayloadSizeRemaining, (long)receivedByteCount));
                    var data = new byte[dataLength];
                    Array.Copy(someBytes, 0, data, 0, dataLength);
                    Payload.Add(new ArraySegment <byte>(data));
                    readCount = dataLength;
                }

                _header.PayloadSizeRemaining -= (long)dataLength;
                switch (_header.OpCode)
                {
                case OpCode.Close:
                    InternalState = DataState.Closed;
                    break;

                case OpCode.Ping:
                    InternalState = DataState.Ping;
                    break;

                case OpCode.Pong:
                    InternalState = DataState.Pong;
                    break;

                default:
                    InternalState = _header.PayloadSizeRemaining == 0 ? DataState.Complete : DataState.Receiving;
                    break;
                }
            }
            else
            {
                // append user data to send later on - or append authentication header
                Format = DataFormat.Raw;
                Payload.Add(new ArraySegment <byte>(someBytes, 0, receivedByteCount));
                readCount = receivedByteCount;
            }

            return(readCount);
        }
        public override int Append(byte[] someBytes, int receivedByteCount = -1, bool asFrame = false)
        {
            byte[] data = someBytes;
            if (receivedByteCount < 0)
            {
                receivedByteCount = someBytes.Length;
            }

            int readCount;

            if (asFrame)
            {
                int dataLength;
                if (InternalState == DataState.Empty)
                {
                    int    dataStart;
                    byte[] headerBytes = _header.FromBytes(someBytes, receivedByteCount, out dataStart);
                    if (headerBytes == null)
                    {
                        return(0); // not all header bytes received
                    }

                    Payload.Add(new ArraySegment <byte>(headerBytes));
                    dataLength = Math.Min(Convert.ToInt32(_header.PayloadSizeRemaining), (receivedByteCount - dataStart));
                    data       = new byte[dataLength];
                    Array.Copy(someBytes, dataStart, data, 0, dataLength);
                    Format    = DataFormat.Frame;
                    readCount = dataStart + dataLength;
                }
                else
                {
                    dataLength = Math.Min(Convert.ToInt32(_header.PayloadSizeRemaining), receivedByteCount);
                    data       = new byte[dataLength];
                    Array.Copy(someBytes, 0, data, 0, dataLength);
                    readCount = dataLength;
                }

                _header.PayloadSizeRemaining -= Convert.ToUInt64(dataLength);
                switch (_header.OpCode)
                {
                case OpCode.Close:
                    InternalState = DataState.Closed;
                    break;

                case OpCode.Ping:
                    InternalState = DataState.Ping;
                    break;

                case OpCode.Pong:
                    InternalState = DataState.Pong;
                    break;

                default:
                    InternalState = _header.PayloadSizeRemaining == 0 ? DataState.Complete : DataState.Receiving;
                    break;
                }
            }
            else
            {
                Format    = DataFormat.Raw;
                readCount = data.Length;
            }

            Payload.Add(new ArraySegment <byte>(data));
            return(readCount);
        }