QMonWorkerContext.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using System.Net.Sockets;
  2. using Quadarax.Foundation.Core.Object;
  3. using Quadarax.Foundation.Core.Thread;
  4. namespace Quadarax.Foundation.Core.QMonitor
  5. {
  6. public class QMonWorkerContext : DisposableObject, IWorkerContext
  7. {
  8. #region *** Fields ***
  9. private UdpClient? _udpClient;
  10. #endregion
  11. #region *** Properties ***
  12. public CancellationToken Cancel { get; set; }
  13. public UdpClient Client => _udpClient ?? throw new InvalidOperationException("UDP client channel is not open (is null).");
  14. #endregion
  15. #region *** Constructors ***
  16. public QMonWorkerContext(UdpClient? udpClient)
  17. {
  18. SetClient(udpClient);
  19. }
  20. #endregion
  21. #region *** Public Operations ***
  22. public void SetClient(UdpClient? udpClient)
  23. {
  24. _udpClient = udpClient;
  25. }
  26. #endregion
  27. #region *** Protected Operations ***
  28. protected override void OnDisposing()
  29. {
  30. Client?.Close();
  31. Client?.Dispose();
  32. SetClient(null);
  33. }
  34. #endregion
  35. }
  36. }