using System.Net.Sockets;
using Quadarax.Foundation.Core.Object;
using Quadarax.Foundation.Core.Thread;

namespace Quadarax.Foundation.Core.QMonitor
{
    public class QMonWorkerContext : DisposableObject, IWorkerContext
    {
        #region *** Fields ***
        private UdpClient? _udpClient;
        #endregion

        #region *** Properties ***
        public CancellationToken Cancel { get; set; }
        public UdpClient Client => _udpClient ?? throw new InvalidOperationException("UDP client channel is not open (is null).");
        #endregion

        #region *** Constructors ***
        public QMonWorkerContext(UdpClient? udpClient)
        {
            SetClient(udpClient);
        }
        #endregion

        #region *** Public Operations ***
        public void SetClient(UdpClient? udpClient)
        {
            _udpClient = udpClient;
        }
        #endregion
        #region *** Protected Operations ***
        protected override void OnDisposing()
        {
            Client?.Close();
            Client?.Dispose();
            SetClient(null);
        }
        #endregion

    }
}
