Exemple #1
0
        private async Task <State> DoExpectConfigAsync()
        {
            Stream stream = networkStream.Stream;
            Frame  frame  = await Frame.ReadAsync(stream, shutdownTokenSource.Token, logger);

            if (frame == null)
            {
                logger.Site().Information("{0} EOS encountered while waiting for config, so disconnecting.", this);
                return(State.Disconnecting);
            }

            var result = EpoxyProtocol.Classify(frame, logger);

            switch (result.Disposition)
            {
            case EpoxyProtocol.FrameDisposition.ProcessConfig:
                // we don't actually use the config yet
                return(connectionType == ConnectionType.Server ? State.ServerSendConfig : State.Connected);

            case EpoxyProtocol.FrameDisposition.HandleProtocolError:
                // we got a protocol error while we expected config
                handshakeError = result.Error;
                return(State.Disconnecting);

            case EpoxyProtocol.FrameDisposition.HangUp:
                return(State.Disconnecting);

            default:
                protocolError = result.ErrorCode ?? ProtocolErrorCode.PROTOCOL_VIOLATED;
                logger.Site().Error("{0} Unsupported FrameDisposition {1} when waiting for config. ErrorCode: {2})",
                                    this, result.Disposition, protocolError);
                return(State.SendProtocolError);
            }
        }
Exemple #2
0
        private async Task <State> DoConnectedAsync()
        {
            while (!shutdownTokenSource.IsCancellationRequested)
            {
                Frame frame;

                try
                {
                    Stream stream = networkStream.Stream;
                    frame = await Frame.ReadAsync(stream, shutdownTokenSource.Token, logger);

                    if (frame == null)
                    {
                        logger.Site().Information("{0} EOS encountered, so disconnecting.", this);
                        return(State.Disconnecting);
                    }
                }
                catch (EpoxyProtocolErrorException pex)
                {
                    logger.Site().Error(pex, "{0} Protocol error encountered.", this);
                    protocolError = ProtocolErrorCode.PROTOCOL_VIOLATED;
                    return(State.SendProtocolError);
                }
                catch (Exception ex) when(ex is IOException || ex is ObjectDisposedException || ex is SocketException)
                {
                    logger.Site().Error(ex, "{0} IO error encountered.", this);
                    return(State.Disconnecting);
                }

                var result = EpoxyProtocol.Classify(frame, logger);
                switch (result.Disposition)
                {
                case EpoxyProtocol.FrameDisposition.DeliverRequestToService:
                {
                    State?nextState = DispatchRequest(result.Headers, result.MessageData, result.LayerData);
                    if (nextState.HasValue)
                    {
                        return(nextState.Value);
                    }
                    else
                    {
                        // continue the read loop
                        break;
                    }
                }

                case EpoxyProtocol.FrameDisposition.DeliverResponseToProxy:
                    DispatchResponse(result.Headers, result.MessageData, result.LayerData);
                    break;

                case EpoxyProtocol.FrameDisposition.DeliverEventToService:
                    DispatchEvent(result.Headers, result.MessageData, result.LayerData);
                    break;

                case EpoxyProtocol.FrameDisposition.SendProtocolError:
                    protocolError = result.ErrorCode ?? ProtocolErrorCode.INTERNAL_ERROR;
                    return(State.SendProtocolError);

                case EpoxyProtocol.FrameDisposition.HandleProtocolError:
                case EpoxyProtocol.FrameDisposition.HangUp:
                    return(State.Disconnecting);

                default:
                    logger.Site().Error("{0} Unsupported FrameDisposition {1}", this, result.Disposition);
                    protocolError = ProtocolErrorCode.INTERNAL_ERROR;
                    return(State.SendProtocolError);
                }
            }

            // shutdown requested between reading frames
            return(State.Disconnecting);
        }