示例#1
0
        private async Task AcceptTcpClientsAsync(ITcpListener tcpListener)
        {
            ExceptionDispatchInfo exceptionDispatchInfo = default;
            bool errorOcurred = false;

            while (!Disconnecting && !errorOcurred)
            {
                ITcpClient tcpClient = await DoAcceptTcpClientAsync(tcpListener).ConfigureAwait(false);

                AddClient(_tcpClients, tcpClient);

                async Task ClientReceiveAsync()
                {
                    try
                    {
                        await TcpReceiver.ReceiveAsync(tcpClient).ConfigureAwait(false);
                    }
                    catch (Exception e)
                    {
                        errorOcurred          = true;
                        exceptionDispatchInfo = ExceptionDispatchInfo.Capture(e);
                        tcpListener.Stop();
                    }
                }

                Task task = Task.Run(ClientReceiveAsync);
            }
            if (exceptionDispatchInfo != null)
            {
                throw new TcpIpAcceptClientsFailedException(exceptionDispatchInfo.SourceException);
            }
        }
示例#2
0
        private async Task ConnectAsyncImpl(IPAddress ipAddress, int port)
        {
            try
            {
                Disconnecting = false;
                _tcpClient    = _getTcpClient();
                OnConnecting(_tcpClient);
                await _tcpClient.ConnectAsync(ipAddress, port).ConfigureAwait(false);

                OnConnected(_tcpClient);
                await TcpReceiver.ReceiveAsync(_tcpClient).ConfigureAwait(false);
            }
            catch (Exception exception)
            {
                throw new TcpIpClientConnectFailedException(exception);
            }
        }