/// <summary> /// Initializes a new instance of the <see cref="TcpClientIo{TRequest,TResponse}"/> class and connects to the specified port on the specified host. /// </summary> /// <param name="address"></param> /// <param name="port"></param> /// <param name="tcpClientIoOptions"></param> /// <param name="logger"></param> public TcpClientIo(IPAddress address, int port, TcpClientIoOptions tcpClientIoOptions = null, ILogger <TcpClientIo <TId, TRequest, TResponse> > logger = null) : this(tcpClientIoOptions, logger) { _tcpClient = new TcpClient(); _tcpClient.Connect(address, port); SetupTcpClient(); SetupTasks(); }
public BitConverterHelper(TcpClientIoOptions options) { _options = options; _customConverters = options.Converters.Select(converter => { var converterType = converter.GetType(); var type = converterType.BaseType; if (type == null) { throw TcpClientIoException.ConverterError(converterType.Name); } var genericType = type.GenericTypeArguments.Single(); return(new KeyValuePair <Type, TcpConverter>(genericType, converter)); }).ToDictionary(pair => pair.Key, pair => pair.Value); _builtInConvertersToBytes = new Dictionary <Type, MethodInfo> { { typeof(bool), typeof(BitConverter).GetMethod(nameof(BitConverter.GetBytes), new[] { typeof(bool) }) }, { typeof(char), typeof(BitConverter).GetMethod(nameof(BitConverter.GetBytes), new[] { typeof(char) }) }, { typeof(double), typeof(BitConverter).GetMethod(nameof(BitConverter.GetBytes), new[] { typeof(double) }) }, { typeof(short), typeof(BitConverter).GetMethod(nameof(BitConverter.GetBytes), new[] { typeof(short) }) }, { typeof(int), typeof(BitConverter).GetMethod(nameof(BitConverter.GetBytes), new[] { typeof(int) }) }, { typeof(long), typeof(BitConverter).GetMethod(nameof(BitConverter.GetBytes), new[] { typeof(long) }) }, { typeof(float), typeof(BitConverter).GetMethod(nameof(BitConverter.GetBytes), new[] { typeof(float) }) }, { typeof(ushort), typeof(BitConverter).GetMethod(nameof(BitConverter.GetBytes), new[] { typeof(ushort) }) }, { typeof(uint), typeof(BitConverter).GetMethod(nameof(BitConverter.GetBytes), new[] { typeof(uint) }) }, { typeof(ulong), typeof(BitConverter).GetMethod(nameof(BitConverter.GetBytes), new[] { typeof(ulong) }) } }; }
private TcpClientIo(TcpClientIoOptions tcpClientIoOptions, ILogger <TcpClientIo <TId, TRequest, TResponse> > logger) { var pipe = new Pipe(); Id = Guid.NewGuid(); _logger = logger; _options = tcpClientIoOptions ?? TcpClientIoOptions.Default; _batchRules = TcpBatchRules <TResponse> .Default; _baseCancellationTokenSource = new CancellationTokenSource(); _baseCancellationToken = _baseCancellationTokenSource.Token; _bufferBlockRequests = new BufferBlock <SerializedRequest>(); var middleware = new MiddlewareBuilder <ITcpBatch <TResponse> >() .RegisterCancellationActionInWait((tcs, hasOwnToken) => { if (_disposing || hasOwnToken) { tcs.TrySetCanceled(); } else if (!_disposing && _pipelineReadEnded) { tcs.TrySetException(TcpClientIoException.ConnectionBroken()); } }) .RegisterDuplicateActionInSet((batch, newBatch) => _batchRules.Update(batch, newBatch.Single())) .RegisterCompletionActionInSet(() => _consumingResetEvent.Set()); _completeResponses = new WaitingDictionary <TId, ITcpBatch <TResponse> >(middleware); _arrayPool = ArrayPool <byte> .Create(); var bitConverterHelper = new BitConverterHelper(_options); _serializer = new TcpSerializer <TRequest>(bitConverterHelper, length => _arrayPool.Rent(length)); _deserializer = new TcpDeserializer <TId, TResponse>(bitConverterHelper); _writeResetEvent = new AsyncManualResetEvent(); _readResetEvent = new AsyncManualResetEvent(); _consumingResetEvent = new AsyncManualResetEvent(); _deserializePipeReader = pipe.Reader; _deserializePipeWriter = pipe.Writer; }
/// <summary> /// Initializes a new instance of the <see cref="TcpClientIo{TId, TRequestResponse}"/> class. /// </summary> /// <param name="tcpClient"></param> /// <param name="tcpClientIoOptions"></param> /// <param name="logger"></param> public TcpClientIo(TcpClient tcpClient, TcpClientIoOptions tcpClientIoOptions = null, ILogger <TcpClientIo <TRequest, TResponse> > logger = null) : base(tcpClient, tcpClientIoOptions, logger) { }
/// <summary> /// Initializes a new instance of the <see cref="TcpClientIo{TId, TRequestResponse}"/> class and connects to the specified port on the specified host. /// </summary> /// <param name="address"></param> /// <param name="port"></param> /// <param name="tcpClientIoOptions"></param> /// <param name="logger"></param> public TcpClientIo(IPAddress address, int port, TcpClientIoOptions tcpClientIoOptions = null, ILogger <TcpClientIo <TRequest, TResponse> > logger = null) : base(address, port, tcpClientIoOptions, logger) { }
/// <summary> /// Initializes a new instance of the <see cref="TcpClientIo{TRequest,TResponse}"/> class and uses connection taken from <see cref="TcpClient"/> /// </summary> /// <param name="tcpClient"></param> /// <param name="tcpClientIoOptions"></param> /// <param name="logger"></param> public TcpClientIo(TcpClient tcpClient, TcpClientIoOptions tcpClientIoOptions = null, ILogger <TcpClientIo <TId, TRequest, TResponse> > logger = null) : this(tcpClientIoOptions, logger) { _tcpClient = tcpClient; SetupTcpClient(); SetupTasks(); }