public async Task PutDirectoryAsync(Profile profile, string directory, SharingHandler <ISharingDirectorySender> handler) { if (profile == null || handler == null || !(profile is NotifyClientProfile receiver)) { throw new ArgumentNullException(); } var directoryInfo = new DirectoryInfo(directory); if (!directoryInfo.Exists) { throw new DirectoryNotFoundException("Directory not found!"); } var packet = new { name = directoryInfo.Name }; _ = await this.network.ConnectAsync("link.sharing.directory", packet, receiver.GetTcpEndPoint(), async stream => { var result = await stream.ReadBlockWithHeaderAsync(this.settings.TcpBufferLimits, this.cancellationToken); var data = new Token(this.generator, result); if (data["status"].As <string>() != "wait") { throw new NetworkException(NetworkError.InvalidData); } using (var sender = new DirectorySender(this.context, receiver, stream, directoryInfo.FullName)) { await this.dispatcher.InvokeAsync(() => handler.Invoke(sender)); await sender.LoopAsync(); } return(new Unit()); }, this.cancellationToken); }
public async Task SendDirectoryAsync(LinkProfile profile, string directoryPath, DirectorySenderHandler handler) { if (profile == null || handler == null) { throw new ArgumentNullException(); } var directoryInfo = new DirectoryInfo(directoryPath); if (!directoryInfo.Exists) { throw new DirectoryNotFoundException("Directory not found!"); } var packet = new { name = directoryInfo.Name }; using (var tcp = await Network.ConnectAsync("link.share.directory", packet, profile.GetTcpEndPoint(), CancellationToken)) { var stream = tcp.GetStream(); var result = await stream.ReadBlockWithHeaderAsync(Environment.TcpBufferLimits, CancellationToken); var data = Generator.AsToken(result); if (data["status"].As <string>() != "wait") { throw new LinkException(LinkError.InvalidData); } using (var sender = new DirectorySender(this, profile, stream, directoryInfo.FullName)) { await UpdateUIAsync(() => handler.Invoke(sender)); await sender.LoopAsync(); } } }