ClientProvider.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. baseProvider?.StartFileReceiveNotify(fileType);
  17. stream = new();
  18. _streamBuffer[fileType] = stream;
  19. Timer timer = new(FileCleanerTimerCallback, fileType, 600000, Timeout.Infinite);
  20. }
  21. stream.Write(buffer);
  22. if (current != total)
  23. return Task.FromResult(true);
  24. string? tempPath = fileType switch
  25. {
  26. FileType.Recipe => baseProvider.RecipePath,
  27. FileType.Config => baseProvider.ConfigPath,
  28. _ => "Undefined"
  29. };
  30. tempPath ??= "Undefined";
  31. string path = Path.Combine(tempPath, "Receive");
  32. if (!Compressor.DecompressZipFile(new(path), stream))
  33. return Task.FromResult(false);
  34. if (_streamBuffer.TryRemove(fileType, out MemoryStream? memoryStream) && memoryStream is not null)
  35. memoryStream.Dispose();
  36. baseProvider?.FileReceivedNotify(fileType);
  37. return Task.FromResult(true);
  38. }
  39. public Task<bool> RequestFile(Guid guid, FileType fileType)
  40. {
  41. Debug.WriteLine($"RequestFile {guid} {fileType}");
  42. string? filePath = fileType switch
  43. {
  44. FileType.Config => baseProvider.ConfigPath,
  45. FileType.Recipe => baseProvider.RecipePath,
  46. _ => default
  47. };
  48. if (string.IsNullOrEmpty(filePath))
  49. return Task.FromResult(false);
  50. if (!Directory.Exists(filePath))
  51. return Task.FromResult(false);
  52. using MemoryStream stream = new();
  53. Compressor.CompressZipFileDirectory(new(filePath), stream);
  54. return SendFile(guid, stream);
  55. }
  56. private async Task<bool> SendFile(Guid guid, MemoryStream stream)
  57. {
  58. if (!stream.Split(out List<byte[]>? buffer, 8192) || buffer is null)
  59. return false;
  60. for (int i = 0; i < buffer.Count; i++)
  61. {
  62. try
  63. {
  64. if (!await clientCaller.FilePack(guid, buffer[i], i + 1, buffer.Count))
  65. return false;
  66. }
  67. catch
  68. {
  69. return false;
  70. }
  71. }
  72. return true;
  73. }
  74. private void FileCleanerTimerCallback(object? state)
  75. {
  76. if (state is not FileType fileType)
  77. return;
  78. if (_streamBuffer.TryRemove(fileType, out MemoryStream? memoryStream) && memoryStream is not null)
  79. memoryStream.Dispose();
  80. }
  81. }
  82. public interface IClientBaseProvider
  83. {
  84. string? RecipePath { get; set; }
  85. string? ConfigPath { get; set; }
  86. void FileReceivedNotify(FileType fileType);
  87. void StartFileReceiveNotify(FileType fileType);
  88. }