Example #1
0
        protected override bool DecodeBytes(byte[] buffer, ref int offset, ref int size, ref bool isAtEof)
        {
            while (size > 0)
            {
                int bytesRead = decoder.Decode(buffer, offset, size);
                if (bytesRead > 0)
                {
                    offset += bytesRead;
                    size   -= bytesRead;
                }

                switch (decoder.CurrentState)
                {
                case ServerSingletonDecoder.State.EnvelopeStart:
                    // we're at the envelope
                    return(true);

                case ServerSingletonDecoder.State.End:
                    isAtEof = true;
                    return(false);
                }
            }

            return(false);
        }
 protected override int Decode(byte[] buffer, int offset, int size)
 {
     return(decoder.Decode(buffer, offset, size));
 }
Example #3
0
        async Task ReadAndDispatchAsync()
        {
            bool success = false;

            try
            {
                while ((size > 0 || !isReadPending) && !IsClosed)
                {
                    if (size == 0)
                    {
                        isReadPending = true;
                        size          = await Connection.ReadAsync(0, connectionBuffer.Length, GetRemainingTimeout());

                        offset        = 0;
                        isReadPending = false;
                        if (size == 0)
                        {
                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(decoder.CreatePrematureEOFException());
                        }
                    }

                    int bytesRead = decoder.Decode(connectionBuffer, offset, size);
                    if (bytesRead > 0)
                    {
                        offset += bytesRead;
                        size   -= bytesRead;
                    }

                    if (decoder.CurrentState == ServerSingletonDecoder.State.PreUpgradeStart)
                    {
                        via = decoder.Via;
                        var validated = await Connection.ValidateAsync(via);

                        if (!ContinuePostValidationProcessing())
                        {
                            // This goes through the failure path (Abort) even though it doesn't throw.
                            return;
                        }
                        break; //exit loop, set success=true;
                    }
                }
                success = true;
            }
            catch (CommunicationException exception)
            {
                DiagnosticUtility.TraceHandledException(exception, TraceEventType.Information);
            }
            catch (TimeoutException exception)
            {
                DiagnosticUtility.TraceHandledException(exception, TraceEventType.Information);
            }
            catch (Exception e)
            {
                if (Fx.IsFatal(e))
                {
                    throw;
                }
                if (!ExceptionHandlerHelper.HandleTransportExceptionHelper(e))
                {
                    throw;
                }

                // containment -- we abort ourselves for any error, no extra containment needed
            }
            finally
            {
                if (!success)
                {
                    Abort();
                }
            }
        }