Ejemplo n.º 1
0
        private PipelineResult SendResponse(ClientConnection connection, TcpResponse response)
        {
            var result = PipelineResult.Continue;

            if (connection == null || response == null)
            {
                return(result);
            }

            foreach (var message in response)
            {
                var delay = message.ResponseDelay;
                delay = delay < MessagePipeline.MaximumResponseDelay ? delay : MessagePipeline.MaximumResponseDelay;
                delay = delay > MessagePipeline.MinimumResponseDelay ? delay : MessagePipeline.MinimumResponseDelay;

                if (delay > 0)
                {
                    Task.WaitAll(Task.Delay(delay));
                }

                if (message.Message?.Length > 0)
                {
                    Task.WaitAll(connection.SendAsync(message.Message));
                }

                if (message.TerminateConnection)
                {
                    result = PipelineResult.StopAndDisconnect;
                    break;
                }
            }

            return(result);
        }
        public static TcpResponse ReturnsByDefault(this IProtocolFactory protocolFactory)
        {
            var response = new TcpResponse();

            protocolFactory.DefaultResponse = response;
            return(response);
        }
Ejemplo n.º 3
0
        public FixedLengthProtocol(TcpResponse defaultResponse, TcpResponse responseOnConnection, int messageLength)
        {
            this.DefaultResponse      = defaultResponse ?? throw new ArgumentNullException(nameof(defaultResponse));
            this.ResponseOnConnection = responseOnConnection;

            _messageLength = messageLength > 0 ? messageLength : throw new ArgumentOutOfRangeException(nameof(messageLength));
        }
        public SentinelDelimitedProtocol(TcpResponse defaultResponse, TcpResponse responseOnConnection, int readPastLength, params byte[] sentinels)
        {
            this.DefaultResponse      = defaultResponse ?? throw new ArgumentNullException(nameof(defaultResponse));
            this.ResponseOnConnection = responseOnConnection;

            _sentinels      = sentinels ?? throw new ArgumentNullException(nameof(sentinels));
            _readPastLength = readPastLength;
        }
        public HeaderDefinedLengthProtocol(TcpResponse defaultResponse, TcpResponse responseOnConnection, int headerLength, MessageLengthCalculator lengthCalculator)
        {
            this.DefaultResponse      = defaultResponse ?? throw new ArgumentNullException(nameof(defaultResponse));
            this.ResponseOnConnection = responseOnConnection;

            _headerLength     = headerLength > 0 ? headerLength : throw new ArgumentOutOfRangeException(nameof(headerLength));
            _lengthCalculator = lengthCalculator ?? throw new ArgumentNullException(nameof(lengthCalculator));
        }
Ejemplo n.º 6
0
        public TcpResponse AddResponseMessage(byte[] message)
        {
            message = message ?? throw new ArgumentNullException(nameof(message));
            message = message.Length > 0 ? message : throw new ArgumentOutOfRangeException(nameof(message));

            var response = new TcpResponse
            {
                new TcpMessage
                {
                    Message = message
                }
            };

            return(this.Add(response));
        }
Ejemplo n.º 7
0
        public TcpResponse Add(TcpResponse response)
        {
            response = response ?? throw new ArgumentNullException(nameof(response));

            _listLock.EnterWriteLock();
            _positionLock.EnterWriteLock();

            _responses.Add(response);
            Interlocked.Exchange(ref _position, -1);

            _listLock.ExitWriteLock();
            _positionLock.ExitWriteLock();

            return(response);
        }
Ejemplo n.º 8
0
        public PipelineResult ProcessMessage(IConnection connection, byte[] message, TcpResponse defaultResponse)
        {
            var context = new TcpContext(connection, message);

            var result = ExecuteHandlers(context);

            if (result == PipelineResult.StopAndDisconnect)
            {
                return(result);
            }

            if (context.Response.Count == 0)
            {
                defaultResponse.ForEach((r) => context.Response.Add(r));
            }

            result = SendResponseMessages(context);

            return(result);
        }
 public SentinelDelimitedProtocolFactory(TcpResponse defaultResponse)
 {
     this.DefaultResponse = defaultResponse;
 }
Ejemplo n.º 10
0
 public StumpResponseFactory(TcpResponse failureResponse, IEnumerable <TcpResponse> responses) : this(failureResponse)
 {
     responses = responses ?? throw new ArgumentNullException(nameof(responses));
     _responses.AddRange(responses);
 }
Ejemplo n.º 11
0
 public StumpResponseFactory(TcpResponse failureResponse) : this(ResponseFactoryBehavior.OrderedThenFailure)
 {
     _failureResponse = failureResponse ?? throw new ArgumentNullException(nameof(failureResponse));
 }
Ejemplo n.º 12
0
 public HeaderDefinedLengthProtocolFactory(TcpResponse defaultResponse)
 {
     this.DefaultResponse = defaultResponse;
 }
 public FixedLengthProtocolFactory(TcpResponse defaultResponse)
 {
     this.DefaultResponse = defaultResponse;
 }