public AsyncTcpSocketClient(IPEndPoint remoteEP, IPEndPoint localEP, IAsyncTcpSocketClientMessageDispatcher dispatcher, AsyncTcpSocketClientConfiguration configuration = null)
        {
            if (remoteEP == null)
            {
                throw new ArgumentNullException("remoteEP");
            }
            if (dispatcher == null)
            {
                throw new ArgumentNullException("dispatcher");
            }

            _remoteEndPoint = remoteEP;
            _localEndPoint  = localEP;
            _dispatcher     = dispatcher;
            _configuration  = configuration ?? new AsyncTcpSocketClientConfiguration();

            if (_configuration.BufferManager == null)
            {
                throw new InvalidProgramException("The buffer manager in configuration cannot be null.");
            }
            if (_configuration.FrameBuilder == null)
            {
                throw new InvalidProgramException("The frame handler in configuration cannot be null.");
            }
        }
Beispiel #2
0
 public AsyncTcpSocketClient(IPAddress remoteAddress, int remotePort, IPEndPoint localEP,
                             Func <AsyncTcpSocketClient, byte[], int, int, Task> onServerDataReceived = null,
                             Func <AsyncTcpSocketClient, Task> onServerConnected    = null,
                             Func <AsyncTcpSocketClient, Task> onServerDisconnected = null,
                             AsyncTcpSocketClientConfiguration configuration        = null)
     : this(new IPEndPoint(remoteAddress, remotePort), localEP,
            onServerDataReceived, onServerConnected, onServerDisconnected, configuration)
 {
 }
Beispiel #3
0
 public AsyncTcpSocketClient(IPEndPoint remoteEP,
                             Func <AsyncTcpSocketClient, byte[], int, int, Task> onServerDataReceived = null,
                             Func <AsyncTcpSocketClient, Task> onServerConnected    = null,
                             Func <AsyncTcpSocketClient, Task> onServerDisconnected = null,
                             AsyncTcpSocketClientConfiguration configuration        = null)
     : this(remoteEP, null,
            onServerDataReceived, onServerConnected, onServerDisconnected, configuration)
 {
 }
 public AsyncTcpSocketClient(IPAddress remoteAddress, int remotePort, IPAddress localAddress, int localPort,
     Func<AsyncTcpSocketClient, byte[], int, int, Task> onServerDataReceived = null,
     Func<AsyncTcpSocketClient, Task> onServerConnected = null,
     Func<AsyncTcpSocketClient, Task> onServerDisconnected = null,
     AsyncTcpSocketClientConfiguration configuration = null)
     : this(new IPEndPoint(remoteAddress, remotePort), new IPEndPoint(localAddress, localPort),
           onServerDataReceived, onServerConnected, onServerDisconnected, configuration)
 {
 }
Beispiel #5
0
 public AsyncTcpSocketClient(IPEndPoint remoteEP, IPEndPoint localEP,
                             Func <AsyncTcpSocketClient, byte[], int, int, Task> onServerDataReceived = null,
                             Func <AsyncTcpSocketClient, Task> onServerConnected    = null,
                             Func <AsyncTcpSocketClient, Task> onServerDisconnected = null,
                             AsyncTcpSocketClientConfiguration configuration        = null)
     : this(remoteEP, localEP,
            new InternalAsyncTcpSocketClientMessageDispatcherImplementation(onServerDataReceived, onServerConnected, onServerDisconnected),
            configuration)
 {
 }
Beispiel #6
0
 public AsyncTcpSocketClient(IPEndPoint remoteEP, IPEndPoint localEP,
                             Func <AsyncTcpSocketClient, byte[], int, int, Task> onServerDataReceived = null,
                             Func <AsyncTcpSocketClient, Task> onServerConnected    = null,
                             Func <AsyncTcpSocketClient, Task> onServerDisconnected = null,
                             AsyncTcpSocketClientConfiguration configuration        = null)
     : this(remoteEP, localEP,
            new DefaultAsyncTcpSocketClientEventDispatcher(onServerDataReceived, onServerConnected, onServerDisconnected),
            configuration)
 {
 }
        public AsyncTcpSocketClient(IPEndPoint remoteEP, IPEndPoint localEP, IAsyncTcpSocketClientMessageDispatcher dispatcher, AsyncTcpSocketClientConfiguration configuration = null)
        {
            if (remoteEP == null)
                throw new ArgumentNullException("remoteEP");
            if (dispatcher == null)
                throw new ArgumentNullException("dispatcher");

            _remoteEndPoint = remoteEP;
            _localEndPoint = localEP;
            _dispatcher = dispatcher;
            _configuration = configuration ?? new AsyncTcpSocketClientConfiguration();

            if (_configuration.FrameBuilder == null)
                throw new InvalidProgramException("The frame handler in configuration cannot be null.");

            Initialize();
        }
Beispiel #8
0
 public AsyncTcpSocketClient(IPEndPoint remoteEP, IAsyncTcpSocketClientMessageDispatcher dispatcher, AsyncTcpSocketClientConfiguration configuration = null)
     : this(remoteEP, null, dispatcher, configuration)
 {
 }
Beispiel #9
0
 public AsyncTcpSocketClient(IPAddress remoteAddress, int remotePort, IAsyncTcpSocketClientMessageDispatcher dispatcher, AsyncTcpSocketClientConfiguration configuration = null)
     : this(new IPEndPoint(remoteAddress, remotePort), dispatcher, configuration)
 {
 }
Beispiel #10
0
        private async Task PerformTcpSocketLoad(int connections, IPEndPoint remoteEP)
        {
            var channels = new List<AsyncTcpSocketClient>();
            var configuration = new AsyncTcpSocketClientConfiguration();
            configuration.SslEnabled = _options.IsSetSsl;
            configuration.SslPolicyErrorsBypassed = _options.IsSetSslPolicyErrorsBypassed;
            configuration.SslTargetHost = _options.SslTargetHost;
            configuration.SslClientCertificates = _options.SslClientCertificates;

            for (int c = 0; c < connections; c++)
            {
                var client = new AsyncTcpSocketClient(remoteEP,
                    onServerDataReceived: async (s, b, o, l) => { await Task.CompletedTask; },
                    onServerConnected: async (s) => { await Task.CompletedTask; },
                    onServerDisconnected: async (s) => { await Task.CompletedTask; },
                    configuration: configuration);

                try
                {
                    _logger(string.Format("Connecting to [{0}].", remoteEP));
                    await client.Connect();
                    channels.Add(client);
                    _logger(string.Format("Connected to [{0}] from [{1}].", remoteEP, client.LocalEndPoint));
                }
                catch (Exception ex) when (!ShouldThrow(ex))
                {
                    if (ex is AggregateException)
                    {
                        var a = ex as AggregateException;
                        if (a.InnerExceptions != null && a.InnerExceptions.Any())
                        {
                            _logger(string.Format("Connect to [{0}] error occurred [{1}].", remoteEP, a.InnerExceptions.First().Message));
                        }
                    }
                    else
                        _logger(string.Format("Connect to [{0}] error occurred [{1}].", remoteEP, ex.Message));

                    if (client != null)
                    {
                        await client.Close();
                    }
                }
            }

            if (_options.IsSetChannelLifetime && channels.Any())
            {
                await Task.Delay(_options.ChannelLifetime);
            }

            foreach (var client in channels)
            {
                try
                {
                    _logger(string.Format("Closed to [{0}] from [{1}].", remoteEP, client.LocalEndPoint));
                    await client.Close();
                }
                catch (Exception ex) when (!ShouldThrow(ex))
                {
                    if (ex is AggregateException)
                    {
                        var a = ex as AggregateException;
                        if (a.InnerExceptions != null && a.InnerExceptions.Any())
                        {
                            _logger(string.Format("Closed to [{0}] error occurred [{1}].", remoteEP, a.InnerExceptions.First().Message));
                        }
                    }
                    else
                        _logger(string.Format("Closed to [{0}] error occurred [{1}].", remoteEP, ex.Message));
                }
            }
        }
Beispiel #11
0
 public AsyncTcpSocketClient(IPAddress remoteAddress, int remotePort, IPAddress localAddress, int localPort, IAsyncTcpSocketClientEventDispatcher dispatcher, AsyncTcpSocketClientConfiguration configuration = null)
     : this(new IPEndPoint(remoteAddress, remotePort), new IPEndPoint(localAddress, localPort), dispatcher, configuration)
 {
 }
Beispiel #12
0
 public AsyncTcpSocketClient(IPEndPoint remoteEP, IAsyncTcpSocketClientMessageDispatcher dispatcher, AsyncTcpSocketClientConfiguration configuration = null)
     : this(remoteEP, null, dispatcher, configuration)
 {
 }
Beispiel #13
0
 public AsyncTcpSocketClient(IPAddress remoteAddress, int remotePort, IAsyncTcpSocketClientMessageDispatcher dispatcher, AsyncTcpSocketClientConfiguration configuration = null)
     : this(new IPEndPoint(remoteAddress, remotePort), dispatcher, configuration)
 {
 }
Beispiel #14
0
 public AsyncTcpSocketClient(IPEndPoint remoteEP, IPEndPoint localEP,
     Func<AsyncTcpSocketClient, byte[], int, int, Task> onServerDataReceived = null,
     Func<AsyncTcpSocketClient, Task> onServerConnected = null,
     Func<AsyncTcpSocketClient, Task> onServerDisconnected = null,
     AsyncTcpSocketClientConfiguration configuration = null)
     : this(remoteEP, localEP,
          new InternalAsyncTcpSocketClientMessageDispatcherImplementation(onServerDataReceived, onServerConnected, onServerDisconnected),
          configuration)
 {
 }
Beispiel #15
0
 public AsyncTcpSocketClient(IPEndPoint remoteEP,
     Func<AsyncTcpSocketClient, byte[], int, int, Task> onServerDataReceived = null,
     Func<AsyncTcpSocketClient, Task> onServerConnected = null,
     Func<AsyncTcpSocketClient, Task> onServerDisconnected = null,
     AsyncTcpSocketClientConfiguration configuration = null)
     : this(remoteEP, null,
           onServerDataReceived, onServerConnected, onServerDisconnected, configuration)
 {
 }