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); }
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)); }
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)); }
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); }
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; }
public StumpResponseFactory(TcpResponse failureResponse, IEnumerable <TcpResponse> responses) : this(failureResponse) { responses = responses ?? throw new ArgumentNullException(nameof(responses)); _responses.AddRange(responses); }
public StumpResponseFactory(TcpResponse failureResponse) : this(ResponseFactoryBehavior.OrderedThenFailure) { _failureResponse = failureResponse ?? throw new ArgumentNullException(nameof(failureResponse)); }
public HeaderDefinedLengthProtocolFactory(TcpResponse defaultResponse) { this.DefaultResponse = defaultResponse; }
public FixedLengthProtocolFactory(TcpResponse defaultResponse) { this.DefaultResponse = defaultResponse; }