コード例 #1
0
ファイル: TextTcpIpServer.cs プロジェクト: akalnay/TextTcpIp
        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);
            }
        }