diff --git a/DearFTP/Connection/DataConnection.cs b/DearFTP/Connection/DataConnection.cs index b7bd6a8..36b6f01 100644 --- a/DearFTP/Connection/DataConnection.cs +++ b/DearFTP/Connection/DataConnection.cs @@ -3,6 +3,8 @@ using System.Collections.Generic; using System.Net; using System.Net.Sockets; using System.Text; +using System.Threading; +using System.Threading.Tasks; namespace DearFTP.Connection { @@ -14,11 +16,32 @@ namespace DearFTP.Connection public TcpClient Client { get; private set; } public NetworkStream Stream { get; private set; } public int Port => ((IPEndPoint)Listener.LocalEndpoint).Port; - public bool IsAvailable { get; private set; } + public bool IsAvailable + { + get + { + if (_acceptTask == null) + { + return false; + } + else if (_acceptTask.Wait(Timeout)) + { + return true; + } + else + { + _acceptTask.Dispose(); + _acceptTask = null; + return false; + } + } + } + + private Task _acceptTask; public DataConnection() { - IsAvailable = false; + } public void Create() @@ -35,28 +58,15 @@ namespace DearFTP.Connection public void AcceptClient() { - var task = Listener.AcceptTcpClientAsync(); - - if (task.Wait(Timeout)) + _acceptTask = Listener.AcceptTcpClientAsync().ContinueWith(t => { - Client = task.Result; + Client = t.Result; Stream = Client.GetStream(); - - IsAvailable = true; - } - } - public async void AcceptClientAsync() - { - Client = await Listener.AcceptTcpClientAsync(); - Stream = Client.GetStream(); - - IsAvailable = true; + }); } public void Close() { - IsAvailable = false; - Stream.Close(); Client.Close(); Listener.Stop();