Example #1
0
 public ServerSingletonSizedDecoder(int maxViaLength, int maxContentTypeLength)
 {
     viaDecoder         = new ViaStringDecoder(maxViaLength);
     contentTypeDecoder = new ContentTypeStringDecoder(maxContentTypeLength);
     currentState       = State.ReadingViaRecord;
 }
Example #2
0
        public override int Decode(ReadOnlySequence <byte> buffer)
        {
            DecoderHelper.ValidateSize(buffer.Length);
            var data = buffer.First.Span;

            try
            {
                int bytesConsumed;
                FramingRecordType recordType;
                switch (currentState)
                {
                case State.ReadingViaRecord:
                    recordType = (FramingRecordType)data[0];
                    ValidateRecordType(FramingRecordType.Via, recordType);
                    bytesConsumed = 1;
                    viaDecoder.Reset();
                    currentState = State.ReadingViaString;
                    break;

                case State.ReadingViaString:
                    bytesConsumed = viaDecoder.Decode(buffer);
                    if (viaDecoder.IsValueDecoded)
                    {
                        currentState = State.ReadingContentTypeRecord;
                    }
                    break;

                case State.ReadingContentTypeRecord:
                    recordType = (FramingRecordType)data[0];
                    if (recordType == FramingRecordType.KnownEncoding)
                    {
                        bytesConsumed = 1;
                        currentState  = State.ReadingContentTypeByte;
                    }
                    else
                    {
                        ValidateRecordType(FramingRecordType.ExtensibleEncoding, recordType);
                        bytesConsumed = 1;
                        contentTypeDecoder.Reset();
                        currentState = State.ReadingContentTypeString;
                    }
                    break;

                case State.ReadingContentTypeByte:
                    contentType   = ContentTypeStringDecoder.GetString((FramingEncodingType)data[0]);
                    bytesConsumed = 1;
                    currentState  = State.PreUpgradeStart;
                    break;

                case State.ReadingContentTypeString:
                    bytesConsumed = contentTypeDecoder.Decode(buffer);
                    if (contentTypeDecoder.IsValueDecoded)
                    {
                        currentState = State.PreUpgradeStart;
                        contentType  = contentTypeDecoder.Value;
                    }
                    break;

                case State.PreUpgradeStart:
                    bytesConsumed = 0;
                    currentState  = State.ReadingUpgradeRecord;
                    break;

                case State.ReadingUpgradeRecord:
                    recordType = (FramingRecordType)data[0];
                    if (recordType == FramingRecordType.UpgradeRequest)
                    {
                        bytesConsumed = 1;
                        contentTypeDecoder.Reset();
                        currentState = State.ReadingUpgradeString;
                    }
                    else
                    {
                        bytesConsumed = 0;
                        currentState  = State.ReadingPreambleEndRecord;
                    }
                    break;

                case State.ReadingUpgradeString:
                    bytesConsumed = contentTypeDecoder.Decode(buffer);
                    if (contentTypeDecoder.IsValueDecoded)
                    {
                        currentState = State.UpgradeRequest;
                        upgrade      = contentTypeDecoder.Value;
                    }
                    break;

                case State.UpgradeRequest:
                    bytesConsumed = 0;
                    currentState  = State.ReadingUpgradeRecord;
                    break;

                case State.ReadingPreambleEndRecord:
                    recordType = (FramingRecordType)data[0];
                    ValidateRecordType(FramingRecordType.PreambleEnd, recordType);
                    bytesConsumed = 1;
                    currentState  = State.Start;
                    break;

                case State.Start:
                    bytesConsumed = 0;
                    currentState  = State.ReadingEndRecord;
                    break;

                case State.ReadingEndRecord:
                    recordType = (FramingRecordType)data[0];
                    if (recordType == FramingRecordType.End)
                    {
                        bytesConsumed = 1;
                        currentState  = State.End;
                    }
                    else
                    {
                        bytesConsumed = 0;
                        currentState  = State.ReadingEnvelopeRecord;
                    }
                    break;

                case State.ReadingEnvelopeRecord:
                    ValidateRecordType(FramingRecordType.SizedEnvelope, (FramingRecordType)data[0]);
                    bytesConsumed = 1;
                    currentState  = State.ReadingEnvelopeSize;
                    sizeDecoder.Reset();
                    break;

                case State.ReadingEnvelopeSize:
                    bytesConsumed = sizeDecoder.Decode(buffer);
                    if (sizeDecoder.IsValueDecoded)
                    {
                        currentState        = State.EnvelopeStart;
                        envelopeSize        = sizeDecoder.Value;
                        envelopeBytesNeeded = envelopeSize;
                    }
                    break;

                case State.EnvelopeStart:
                    bytesConsumed = 0;
                    currentState  = State.ReadingEnvelopeBytes;
                    break;

                case State.ReadingEnvelopeBytes:
                    bytesConsumed = (int)buffer.Length;
                    if (bytesConsumed > envelopeBytesNeeded)
                    {
                        bytesConsumed = envelopeBytesNeeded;
                    }
                    envelopeBytesNeeded -= bytesConsumed;
                    if (envelopeBytesNeeded == 0)
                    {
                        currentState = State.EnvelopeEnd;
                    }
                    break;

                case State.EnvelopeEnd:
                    bytesConsumed = 0;
                    currentState  = State.ReadingEndRecord;
                    break;

                case State.End:
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                              CreateException(new InvalidDataException(SR.FramingAtEnd)));

                default:
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                              CreateException(new InvalidDataException(SR.InvalidDecoderStateMachine)));
                }

                return(bytesConsumed);
            }
            catch (InvalidDataException e)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateException(e));
            }
        }
Example #3
0
 public ServerSingletonDecoder(int maxViaLength, int maxContentTypeLength)
 {
     viaDecoder         = new ViaStringDecoder(maxViaLength);
     contentTypeDecoder = new ContentTypeStringDecoder(maxContentTypeLength);
     Reset();
 }