ClientProvider.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System.Collections.Concurrent;
  2. using System.Diagnostics;
  3. using Universal;
  4. namespace EEMSClientCore;
  5. public class ClientProvider(IClientCaller clientCaller, IClientBaseProvider baseProvider) : IClientProvider
  6. {
  7. private static readonly ConcurrentDictionary<FileType, MemoryStream> _streamBuffer = [];
  8. public static readonly ConcurrentDictionary<FileType, string> _savePath = [];
  9. //Receive Files from Server
  10. public Task<bool> PushFile(Guid guid, FileType fileType, byte[] buffer, int current, int total)
  11. {
  12. if (!_streamBuffer.TryGetValue(fileType, out MemoryStream? stream) || stream is null)
  13. {
  14. if (current != 1)
  15. return Task.FromResult(false);
  16. stream = new();
  17. _streamBuffer[fileType] = stream;
  18. Timer timer = new(FileCleanerTimerCallback, fileType, 600000, Timeout.Infinite);
  19. }
  20. stream.Write(buffer);
  21. if (current != total)
  22. return Task.FromResult(true);
  23. string path = Path.Combine(Environment.CurrentDirectory, "Receive");
  24. if (!Compressor.DecompressZipFile(new(path), stream))
  25. return Task.FromResult(false);
  26. if (_streamBuffer.TryRemove(fileType, out MemoryStream? memoryStream) && memoryStream is not null)
  27. memoryStream.Dispose();
  28. baseProvider?.FileReceivedNotify(fileType);
  29. return Task.FromResult(true);
  30. }
  31. public Task<bool> RequestFile(Guid guid, FileType fileType)
  32. {
  33. Debug.WriteLine($"RequestFile {guid} {fileType}");
  34. string? filePath = fileType switch
  35. {
  36. FileType.Config => baseProvider.ConfigPath,
  37. FileType.Recipe => baseProvider.RecipePath,
  38. _ => default
  39. };
  40. if (string.IsNullOrEmpty(filePath))
  41. return Task.FromResult(false);
  42. if (!Directory.Exists(filePath))
  43. return Task.FromResult(false);
  44. using MemoryStream stream = new();
  45. Compressor.CompressZipFileDirectory(new(filePath), stream);
  46. return SendFile(guid, stream);
  47. }
  48. private async Task<bool> SendFile(Guid guid, MemoryStream stream)
  49. {
  50. if (!stream.Split(out List<byte[]>? buffer, 8192) || buffer is null)
  51. return false;
  52. for (int i = 0; i < buffer.Count; i++)
  53. {
  54. try
  55. {
  56. if (!await clientCaller.FilePack(guid, buffer[i], i + 1, buffer.Count))
  57. return false;
  58. }
  59. catch
  60. {
  61. return false;
  62. }
  63. }
  64. return true;
  65. }
  66. private void FileCleanerTimerCallback(object? state)
  67. {
  68. if (state is not FileType fileType)
  69. return;
  70. if (_streamBuffer.TryRemove(fileType, out MemoryStream? memoryStream) && memoryStream is not null)
  71. memoryStream.Dispose();
  72. }
  73. }
  74. public interface IClientBaseProvider
  75. {
  76. string? RecipePath { get; set; }
  77. string? ConfigPath { get; set; }
  78. void FileReceivedNotify(FileType fileType);
  79. }