ClientProvider.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. return Task.FromResult(true);
  29. }
  30. public Task<bool> RequestFile(Guid guid, FileType fileType)
  31. {
  32. Debug.WriteLine($"RequestFile {guid} {fileType}");
  33. string? filePath = fileType switch
  34. {
  35. FileType.Config => baseProvider.ConfigPath,
  36. FileType.Recipe => baseProvider.RecipePath,
  37. _ => default
  38. };
  39. if (string.IsNullOrEmpty(filePath))
  40. return Task.FromResult(false);
  41. if (!Directory.Exists(filePath))
  42. return Task.FromResult(false);
  43. using MemoryStream stream = new();
  44. Compressor.CompressZipFileDirectory(new(filePath), stream);
  45. return SendFile(guid, stream);
  46. }
  47. private async Task<bool> SendFile(Guid guid, MemoryStream stream)
  48. {
  49. if (!stream.Split(out List<byte[]>? buffer, 8192) || buffer is null)
  50. return false;
  51. for (int i = 0; i < buffer.Count; i++)
  52. {
  53. try
  54. {
  55. if (!await clientCaller.FilePack(guid, buffer[i], i + 1, buffer.Count))
  56. return false;
  57. }
  58. catch
  59. {
  60. return false;
  61. }
  62. }
  63. return true;
  64. }
  65. private void FileCleanerTimerCallback(object? state)
  66. {
  67. if (state is not FileType fileType)
  68. return;
  69. if (_streamBuffer.TryRemove(fileType, out MemoryStream? memoryStream) && memoryStream is not null)
  70. memoryStream.Dispose();
  71. }
  72. }
  73. public interface IClientBaseProvider
  74. {
  75. public string? RecipePath { get; set; }
  76. public string? ConfigPath { get; set; }
  77. }