public async Task OnServerDisconnected(AsyncTcpSocketClient client)
 {
     if (_onServerDisconnected != null)
     {
         await _onServerDisconnected(client);
     }
 }
 public async Task OnServerDataReceived(AsyncTcpSocketClient client, byte[] data, int offset, int count)
 {
     if (_onServerDataReceived != null)
     {
         await _onServerDataReceived(client, data, offset, count);
     }
 }
Пример #3
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));
                }
            }
        }
 public async Task OnServerDisconnected(AsyncTcpSocketClient client)
 {
     if (_onServerDisconnected != null)
         await _onServerDisconnected(client);
 }
 public async Task OnServerDataReceived(AsyncTcpSocketClient client, byte[] data, int offset, int count)
 {
     if (_onServerDataReceived != null)
         await _onServerDataReceived(client, data, offset, count);
 }