Exemple #1
0
        private void lutBox_Click(object sender, EventArgs e)
        {
            if (lutLoadDialog.ShowDialog() == DialogResult.OK)
            {
                LockControls();
                try {
                    UIConsole.Log($"Selected {lutLoadDialog.FileName} for new LUT");
                    var ifile = Bitmap.FromFile(lutLoadDialog.FileName);
                    if (ifile.Width != 256 || ifile.Height != 256)
                    {
                        throw new ArgumentException($"The image size should be 256x256 pixels. Got {ifile.Width}x{ifile.Height}");
                    }

                    lutBmp = new Bitmap(ifile);
                    lutBmp = lutBmp.ToFormat(System.Drawing.Imaging.PixelFormat.Format32bppPArgb, true);
                    UIConsole.Log("New LUT loaded");
                    Monitor.Enter(lutLocker);
                    LUTChanged = true;
                    LUTLoaded  = false;
                    Monitor.Exit(lutLocker);
                    ParametersChanged = true;
                } catch (Exception ex) {
                    MessageBox.Show($"There was an error loading the file:{Environment.NewLine}{ex.Message}");
                }
                UnlockControls();
            }
        }
 private void ThreadLoop()
 {
     UIConsole.Log("MultiImage Thread running.");
     while (running)
     {
         imageManagers.ForEach(ManageImageManager);
         Thread.Sleep(2);
     }
     UIConsole.Log("MultiImage Thread stopped.");
 }
Exemple #3
0
 public void Stop()
 {
     if (running)
     {
         UIConsole.Log("Stopping Thread");
         running = false;
         channelThread.Join();
     }
     else
     {
         UIConsole.Error("MSDU Manager already stopped!");
     }
 }
Exemple #4
0
        public static void Main(string[] args)
        {
            long startTime;

            Image16 im  = new Image16(21000, 21000);
            Image16 im2 = new Image16(4096, 4096, 0x0);

            UIConsole.Log("Benchmark Draw");
            startTime = LLTools.TimestampMS();
            im.DrawImage(im2, 1024, 1024, true);
            UIConsole.Log($"Delta: {LLTools.TimestampMS() - startTime} ms");

            UIConsole.Log("Benchmark Save PGM");
            startTime = LLTools.TimestampMS();
            im.SavePGM("test.pgm");
            UIConsole.Log($"Delta: {LLTools.TimestampMS() - startTime} ms");
        }
 public void Start()
 {
     if (!running)
     {
         UIConsole.Log("Starting channel thread");
         running       = true;
         channelThread = new Thread(new ThreadStart(ThreadLoop))
         {
             IsBackground = true,
             Priority     = ThreadPriority.AboveNormal,
         };
         channelThread.Start();
     }
     else
     {
         UIConsole.Error("File Handler already running!");
     }
 }
 public void Stop()
 {
     if (!running)
     {
         return;
     }
     UIConsole.Log("Stopping Multi-Image Manager.");
     if (multiThread)
     {
         imageManagers.ForEach(im => im.Stop());
     }
     else
     {
         running = false;
         imageThread?.Join();
         imageThread = null;
     }
 }
Exemple #7
0
 public void Start()
 {
     if (!running)
     {
         UIConsole.Log("Starting MSDU thread");
         running       = true;
         channelThread = new Thread(new ThreadStart(ThreadLoop))
         {
             IsBackground = true,
             Priority     = ThreadPriority.Highest,
         };
         channelThread.Start();
     }
     else
     {
         UIConsole.Error("MSDU Manager already running!");
     }
 }
Exemple #8
0
        public static void LoadLRIT()
        {
            UIConsole.Log($"Loading Headers from Visible file at {visFilename}");
            XRITHeader header   = FileParser.GetHeaderFromFile(visFilename);
            Regex      x        = new Regex(@".*\((.*)\)", RegexOptions.IgnoreCase);
            var        regMatch = x.Match(header.ImageNavigationHeader.ProjectionName);

            satelliteLongitude = float.Parse(regMatch.Groups[1].Captures[0].Value, CultureInfo.InvariantCulture);
            var inh = header.ImageNavigationHeader;

            gc = new GeoConverter(satelliteLongitude, inh.ColumnOffset, inh.LineOffset, inh.ColumnScalingFactor, inh.LineScalingFactor);
            var od = new OrganizerData();

            od.Segments.Add(0, visFilename);
            od.FirstSegment = 0;
            od.Columns      = header.ImageStructureHeader.Columns;
            od.Lines        = header.ImageStructureHeader.Lines;
            od.ColumnOffset = inh.ColumnOffset;
            od.PixelAspect  = 1;
            UIConsole.Log($"Generating Visible Bitmap");
            bmp = ImageTools.GenerateFullImage(od);
            if (bmp.PixelFormat != PixelFormat.Format32bppArgb)
            {
                bmp = bmp.ToFormat(PixelFormat.Format32bppArgb, true);
            }

            //
            od.Segments[0] = irfilename;
            irBmp          = ImageTools.GenerateFullImage(od);
            if (bmp.PixelFormat != PixelFormat.Format32bppArgb)
            {
                bmp = bmp.ToFormat(PixelFormat.Format32bppArgb, true);
            }
            // Geo Converter
            latRange = new[] { gc.MinLatitude, gc.MaxLatitude };
            lonRange = new[] { gc.MinLongitude, gc.MaxLongitude };
            coverage = new[] { gc.LatitudeCoverage, gc.LongitudeCoverage };
            trim     = new[] { gc.TrimLatitude, gc.TrimLongitude };
            size     = new[] { (uint)bmp.Width, (uint)bmp.Height };
            coff     = inh.ColumnOffset;
            loff     = inh.LineOffset;
            cfac     = inh.ColumnScalingFactor;
            lfac     = inh.LineScalingFactor;
        }
Exemple #9
0
        public static void CLBuilder(ComputeContext context)
        {
            UIConsole.Log("Loading kernel.cl");
            var clProgramSource = File.ReadAllText("kernel.cl");

            UIConsole.Log("Compiling kernel");
            program = new ComputeProgram(context, clProgramSource);
            try {
                program.Build(null, null, null, IntPtr.Zero);
            } catch (Exception e) {
                UIConsole.Error("Build Log: \n" + program.GetBuildLog(context.Devices[0]));
                throw e;
            }

            reprojectKernel  = program.CreateKernel("reproject");
            apply2DLUTKernel = program.CreateKernel("apply2DLUT");
            applyCurveKernel = program.CreateKernel("applyCurve");

            UIConsole.Log("Building Curve LUT");
            curveLut = new byte[256];
            for (int i = 0; i < 256; i++)
            {
                float v = 255 * OpenSatelliteProject.Presets.NEW_VIS_FALSE_CURVE[i];
                curveLut[i] = (byte)(((int)Math.Floor(v)) & 0xFF);
            }
            curveLutBuffer = new ComputeBuffer <byte>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, curveLut);

            UIConsole.Log("Loading LUT2D");
            byte[] buffer = ReadFileFromOSPAssembly("falsecolor.png");
            Bitmap lutBmp;

            using (MemoryStream stream = new MemoryStream(buffer)) {
                lutBmp = new Bitmap(stream);
            }
            lut2D = new uint[256 * 256];
            for (int i = 0; i < 256; i++)
            {
                for (int j = 0; j < 256; j++)
                {
                    lut2D[(i * 256) + j] = (uint)(lutBmp.GetPixel(j, i).ToArgb() & 0xFFFFFFFF);
                }
            }
            lut2DBuffer = new ComputeBuffer <uint>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, lut2D);
        }
Exemple #10
0
        public static void Main(string[] args)
        {
            LoadLRIT();
            ComputePlatform[] availablePlatforms = new ComputePlatform[ComputePlatform.Platforms.Count];
            for (int i = 0; i < availablePlatforms.Length; i++)
            {
                availablePlatforms [i] = ComputePlatform.Platforms [i];
                Console.WriteLine(ComputePlatform.Platforms [i].Name);
            }

            var platform = availablePlatforms [0];

            ComputeContextPropertyList properties = new ComputeContextPropertyList(platform);
            ComputeContext             context    = new ComputeContext(platform.Devices, properties, null, IntPtr.Zero);

            CLBuilder(context);
            CLApply2DLUT(context);
            UIConsole.Log("Finish");
        }
Exemple #11
0
        public MainForm()
        {
            InitializeComponent();

            UIConsole.MessageAvailable += UIConsole_MessageAvailable;
            UIConsole.Log("Starting Load LRIT");
            UIConsole.Log("Loading Defaults");

            radianceOffset.Value     = OpenSatelliteProject.Presets.RadianceOffset;
            thermalOffset.Value      = OpenSatelliteProject.Presets.ThermalOffset;
            radianceOffsetLabel.Text = $"Radiance Offset: {radianceOffset.Value}";
            thermalOffsetLabel.Text  = $"Thermal Offset: {thermalOffset.Value}";

            LoadLRIT();
            LoadLUT();

            loadingLabel.Parent = falseColorBox;
            loadingLabel.Anchor = AnchorStyles.None;
            RefreshUI();
        }
Exemple #12
0
        void ChannelDataLoop()
        {
            try {
                UIConsole.Log($"UDP Channel Data Loop started at port {ChannelDataServerPort}");
                UdpClient  udpClient        = new UdpClient(ChannelDataServerPort);
                IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
                udpClient.Client.ReceiveTimeout = 200;
                // udpClient.Client.ReceiveBufferSize = 7278 * BufferSizeInFrames; // Ray found out that this is making rx bad.
                while (channelDataThreadRunning)
                {
                    try {
                        byte[] buffer = udpClient.Receive(ref RemoteIpEndPoint);
                        switch (buffer.Length)
                        {
                        case 5380:
                        case 5384:
                        case 7274:
                        case 7278:
                            HandleBBFrame(buffer);
                            break;

                        case 2052:
                        case 2048:
                            HandleCADU(buffer);
                            break;

                        default:
                            UIConsole.Error($"Received invalid frame size: {buffer.Length}");
                            break;
                        }
                    } catch (Exception e) {
                        // TODO: Handle
                        UIConsole.Error(e.ToString());
                    }
                }

                UIConsole.Log("UDP Channel Data Thread closed.");
            } catch (Exception e) {
                CrashReport.Report(e);
            }
        }
Exemple #13
0
        public async void LoadLRIT()
        {
            await Task.Run(async() => {
                LockControls();
                UIConsole.Log($"Loading Headers from Visible file at {visFilename}");
                XRITHeader header        = FileParser.GetHeaderFromFile(visFilename);
                Regex x                  = new Regex(@".*\((.*)\)", RegexOptions.IgnoreCase);
                var regMatch             = x.Match(header.ImageNavigationHeader.ProjectionName);
                float satelliteLongitude = float.Parse(regMatch.Groups[1].Captures[0].Value, CultureInfo.InvariantCulture);
                var inh                  = header.ImageNavigationHeader;
                gc     = new GeoConverter(satelliteLongitude, inh.ColumnOffset, inh.LineOffset, inh.ColumnScalingFactor, inh.LineScalingFactor);
                var od = new OrganizerData();
                od.Segments.Add(0, visFilename);
                od.FirstSegment = 0;
                od.Columns      = header.ImageStructureHeader.Columns;
                od.Lines        = header.ImageStructureHeader.Lines;
                od.ColumnOffset = inh.ColumnOffset;
                od.PixelAspect  = 1;
                UIConsole.Log($"Generating Visible Bitmap");
                visBmp = ImageTools.GenerateFullImage(od);
                visBmp = ImageTools.ResizeImage(visBmp, visBmp.Width / 4, visBmp.Height / 4, true);

                od = new OrganizerData();
                od.Segments.Add(0, irfilename);
                od.FirstSegment = 0;
                od.Columns      = header.ImageStructureHeader.Columns;
                od.Lines        = header.ImageStructureHeader.Lines;
                od.ColumnOffset = inh.ColumnOffset;
                od.PixelAspect  = 1;
                UIConsole.Log($"Generating Infrared Bitmap");
                irBmp = ImageTools.GenerateFullImage(od);
                irBmp = ImageTools.ResizeImage(irBmp, irBmp.Width / 4, irBmp.Height / 4, true);

                UIConsole.Log("Generating False Color");
                await GenerateFalseColor();
                UIConsole.Log("Done");
                UnlockControls();
            });
        }
Exemple #14
0
        static void ProcessBigImage(string bPath, ImageAssembler segment, GRBImageHeader header)
        {
            int    apid         = header.apid;
            ulong  currentEpoch = header.epoch;
            string imKey        = $"{apid:X3}-{currentEpoch}";
            var    product      = Products.GetProductByAPID(apid);
            var    imsz         = (ImageSize)product.Meta[1];

            if (APIDStamp.ContainsKey(apid) && APIDStamp[apid] != currentEpoch)
            {
                ulong  oldEpoch  = APIDStamp[apid];
                string oldImKey  = $"{apid:X3}-{oldEpoch}";
                string outfolder = Path.Combine(FileHandler.FinalFileFolder, bPath, oldEpoch.ToString());
                Task.Run(async() => {
                    var imas = BigImageCache[oldImKey];
                    // UIConsole.Log($"Saving{outfolder}.pgm");
                    await imas.AsyncSavePGM($"{outfolder}.pgm");
                    // UIConsole.Log($"Saving{outfolder}.png");
                    await imas.AsyncSavePNG($"{outfolder}.png");
                    BigImageCache[oldImKey] = null;
                    UIConsole.Log($"New {product.Name} at {outfolder}.pgm");
                });

                BigImageCache[imKey] = new ImageAssembler(imsz.Width, imsz.Height, currentEpoch);
                APIDStamp[apid]      = currentEpoch;
                // UIConsole.Debug($"Starting for {imKey} with expected size ({imsz.Width}, {imsz.Height})");
            }
            else if (!BigImageCache.ContainsKey(imKey))
            {
                BigImageCache[imKey] = new ImageAssembler(imsz.Width, imsz.Height, currentEpoch);
                APIDStamp[apid]      = currentEpoch;
                // UIConsole.Debug($"Starting for {imKey} with expected size ({imsz.Width}, {imsz.Height})");
            }
            // UIConsole.Debug ($"{header.apid:X3} - Drawing {header.sequence} at {header.ulX}, {header.ulY}");
            BigImageCache[imKey].DrawAt(segment.Image, (int)header.ulX, (int)header.ulY + (int)header.rowOffset, true); // rowOffset to int might be bad
        }
        public void Start()
        {
            if (running)
            {
                return;
            }
            if (multiThread)
            {
                UIConsole.Log("Starting Multi-Image Manager in Multi Thread mode.");
                imageManagers.ForEach(im => im.Start());
            }
            else
            {
                UIConsole.Log("Starting Multi-Image Manager in Single Thread mode.");
                imageThread = new Thread(ThreadLoop)
                {
                    IsBackground = true,
                    Priority     = ThreadPriority.BelowNormal
                };
                imageThread.Start();
            }

            running = true;
        }
Exemple #16
0
        public static void CLApply2DLUT(ComputeContext context)
        {
            ComputeImageFormat format = new ComputeImageFormat(ComputeImageChannelOrder.Bgra, ComputeImageChannelType.UnsignedInt8);
            var startTime             = LLTools.TimestampMS();

            #region Visible / Temporary Source
            BitmapData     bitmapData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, bmp.PixelFormat);
            ComputeImage2D source0    = new ComputeImage2D(context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.CopyHostPointer, format, bmp.Width, bmp.Height, bitmapData.Stride, bitmapData.Scan0);
            bmp.UnlockBits(bitmapData);
            #endregion
            #region Infrared Source
            bitmapData = irBmp.LockBits(new Rectangle(0, 0, irBmp.Width, irBmp.Height), ImageLockMode.ReadOnly, irBmp.PixelFormat);
            ComputeImage2D source1 = new ComputeImage2D(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, format, irBmp.Width, irBmp.Height, bitmapData.Stride, bitmapData.Scan0);
            irBmp.UnlockBits(bitmapData);
            #endregion
            #region Output
            ComputeImage2D output = new ComputeImage2D(context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.AllocateHostPointer, format, bmp.Width, bmp.Height, 0, IntPtr.Zero);
            #endregion
            #region Variable Initialization
            ComputeEventList    eventList = new ComputeEventList();
            ComputeCommandQueue commands  = new ComputeCommandQueue(context, context.Devices[0], ComputeCommandQueueFlags.None);
            #region Apply Curve
            applyCurveKernel.SetMemoryArgument(0, source0);
            applyCurveKernel.SetMemoryArgument(1, output);
            applyCurveKernel.SetMemoryArgument(2, curveLutBuffer);
            #endregion
            #region Apply LUT 2D
            apply2DLUTKernel.SetMemoryArgument(0, source1);
            apply2DLUTKernel.SetMemoryArgument(1, output);
            apply2DLUTKernel.SetMemoryArgument(2, source0);
            apply2DLUTKernel.SetMemoryArgument(3, lut2DBuffer);
            #endregion
            #region Reprojection
            var latRangeBuff = new ComputeBuffer <float>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, latRange);
            var lonRangeBuff = new ComputeBuffer <float>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, lonRange);
            var coverageBuff = new ComputeBuffer <float>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, coverage);
            var trimBuff     = new ComputeBuffer <float>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, trim);
            var sizeBuff     = new ComputeBuffer <uint>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, size);
            reprojectKernel.SetMemoryArgument(0, source0);
            reprojectKernel.SetMemoryArgument(1, output);
            reprojectKernel.SetValueArgument(2, satelliteLongitude);
            reprojectKernel.SetValueArgument(3, coff);
            reprojectKernel.SetValueArgument(4, cfac);
            reprojectKernel.SetValueArgument(5, loff);
            reprojectKernel.SetValueArgument(6, lfac);
            reprojectKernel.SetValueArgument(7, (uint)(fixAspect ? 1 : 0));
            reprojectKernel.SetValueArgument(8, aspectRatio);
            reprojectKernel.SetMemoryArgument(9, latRangeBuff);
            reprojectKernel.SetMemoryArgument(10, lonRangeBuff);
            reprojectKernel.SetMemoryArgument(11, coverageBuff);
            reprojectKernel.SetMemoryArgument(12, trimBuff);
            reprojectKernel.SetMemoryArgument(13, sizeBuff);
            #endregion
            #endregion
            #region Run Pipeline
            UIConsole.Log("Executing curve kernel");
            commands.Execute(applyCurveKernel, null, new long[] { bmp.Width, bmp.Height }, null, eventList);
            UIConsole.Log("Executing LUT2D kernel");
            commands.Execute(apply2DLUTKernel, null, new long[] { bmp.Width, bmp.Height }, null, eventList);
            UIConsole.Log("Executing kernel");
            commands.Execute(reprojectKernel, null, new long[] { bmp.Width, bmp.Height }, null, eventList);
            #endregion
            #region Dump Bitmap
            UIConsole.Log("Dumping bitmap");
            Bitmap     obmp    = new Bitmap(bmp.Width, bmp.Height, bmp.PixelFormat);
            BitmapData bmpData = obmp.LockBits(new Rectangle(0, 0, obmp.Width, obmp.Height), ImageLockMode.ReadWrite, obmp.PixelFormat);
            commands.ReadFromImage(output, bmpData.Scan0, true, null);
            obmp.UnlockBits(bmpData);

            var delta = LLTools.TimestampMS() - startTime;
            UIConsole.Log($"Took {delta} ms to Apply Curve -> Apply Lut2D (FalseColor) -> Reproject");
            UIConsole.Log("Saving bitmap");
            obmp.Save("teste.png");
            UIConsole.Log("Done");
            bmp.Save("original.png");
            #endregion
        }
Exemple #17
0
        /// <summary>
        /// Saves this instance to disk
        /// </summary>
        /// <returns>Async Task of saving</returns>
        public async Task Save()
        {
            string folder = Path.Combine(FileHandler.FinalFileFolder, ProductFolder);

            if (Filename == null)
            {
                Filename = $"{Epoch}.nc";
            }

            try {
                Directory.CreateDirectory(folder);
            } catch (IOException e) {
                UIConsole.Error($"Cannot create directory {folder}: {e}");
            }

            string dqfFilename  = Filename.Replace(".nc", ".dqf.pgm");
            string imgFilename  = Filename.Replace(".nc", ".pgm");
            string metaFilename = Filename.Replace(".nc", ".xml");
            string dataFilename = Filename.Replace(".nc", ".bin");

            if (Title == null)
            {
                Title = Products.GetNameByAPID(APID);
            }

            UIConsole.Log($"New Product: {Title}");

            if (Metadata != null)
            {
                metaFilename = Path.Combine(folder, metaFilename);
                UIConsole.Debug($"Saving file {metaFilename}");
                File.WriteAllText(metaFilename, Metadata);
            }

            if (Data != null)
            {
                dataFilename = Path.Combine(folder, dataFilename);
                UIConsole.Debug($"Saving file {dataFilename}");
                File.WriteAllBytes(dataFilename, Data);
            }

            if (FullDQFCache != null && SaveDQF)
            {
                dqfFilename = Path.Combine(folder, dqfFilename);
                UIConsole.Debug($"Saving file {dqfFilename}");
                await FullDQFCache.AsyncSavePGM(dqfFilename);
            }

            if (FullImageCache != null)
            {
                if (SavePGM)
                {
                    imgFilename = Path.Combine(folder, imgFilename);
                    UIConsole.Debug($"Saving file {imgFilename}");
                    await FullImageCache.AsyncSavePGM(imgFilename);
                }
                if (SaveJPG)
                {
                    string jpgFilename = imgFilename.Replace(".pgm", ".jpg");
                    jpgFilename = Path.Combine(folder, jpgFilename);
                    UIConsole.Debug($"Saving file {jpgFilename}");
                    await FullImageCache.AsyncSaveJPG(jpgFilename);
                }
                if (SavePNG)
                {
                    string pngFilename = imgFilename.Replace(".pgm", ".png");
                    pngFilename = Path.Combine(folder, pngFilename);
                    UIConsole.Debug($"Saving file {pngFilename}");
                    await FullImageCache.AsyncSavePNG(pngFilename);
                }
            }
        }
Exemple #18
0
        void ChannelDataLoop()
        {
            try {
                UIConsole.Log("Channel Data Loop started");
                byte[] buffer = new byte[2042];

                IPHostEntry ipHostInfo = Dns.GetHostEntry(ChannelDataServerName);
                IPAddress   ipAddress  = new IPAddress(new byte[] { 127, 0, 0, 1 });
                foreach (IPAddress ip in ipHostInfo.AddressList)
                {
                    if (ip.AddressFamily != AddressFamily.InterNetworkV6)
                    {
                        ipAddress = ip;
                        break;
                    }
                }
                IPEndPoint remoteEP = new IPEndPoint(ipAddress, ChannelDataServerPort);
                Socket     sender   = null;

                while (channelDataThreadRunning)
                {
                    bool isConnected = true;
                    UIConsole.Log("Channel Data Thread connect");
                    try {
                        sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
                        {
                            ReceiveTimeout = 5000
                        };
                        sender.Connect(remoteEP);
                        isConnected = true;
                        UIConsole.Log($"Socket connected to {sender.RemoteEndPoint}");
                        int nullReceive = 0;
                        while (isConnected)
                        {
                            try {
                                var receivedBytes = sender.Receive(buffer);
                                if (receivedBytes < buffer.Length && receivedBytes != 0)
                                {
                                    UIConsole.Error("Received less bytes than channel data!");
                                    Thread.Sleep(200);
                                    nullReceive = 0;
                                }
                                else if (receivedBytes == 0)
                                {
                                    nullReceive++;
                                    if (nullReceive == 5)
                                    {
                                        UIConsole.Error("Cannot reach server. Dropping connection!");
                                        isConnected = false;
                                        sender.Shutdown(SocketShutdown.Both);
                                        sender.Disconnect(false);
                                        sender.Close();
                                    }
                                }
                                else
                                {
                                    nullReceive = 0;
                                    this.PostChannelData(buffer);
                                }
                            } catch (ArgumentNullException ane) {
                                UIConsole.Error($"ArgumentNullException : {ane}");
                                isConnected = false;
                            } catch (SocketException) {
                                isConnected = false;
                            } catch (Exception e) {
                                UIConsole.Error($"Unexpected exception : {e}");
                                isConnected = false;
                            }

                            DataConnected = isConnected;

                            if (!channelDataThreadRunning)
                            {
                                break;
                            }
                        }

                        sender.Shutdown(SocketShutdown.Both);
                        sender.Disconnect(false);
                        sender.Close();
                    } catch (ArgumentNullException ane) {
                        UIConsole.Error($"ArgumentNullException : {ane}");
                    } catch (SocketException se) {
                        UIConsole.Error($"SocketException : {se}");
                    } catch (Exception e) {
                        UIConsole.Error($"Unexpected exception : {e}");
                    }
                    if (channelDataThreadRunning)
                    {
                        UIConsole.Warn("Socket closed. Waiting 1s before trying again.");
                        Thread.Sleep(1000);
                    }
                }

                UIConsole.Debug("Requested to close Channel Data Thread!");
                try {
                    if (sender != null)
                    {
                        sender.Shutdown(SocketShutdown.Both);
                        sender.Disconnect(false);
                        sender.Close();
                    }
                } catch (Exception e) {
                    UIConsole.Debug($"Exception thrown when closing socket: {e} Ignoring.");
                }

                UIConsole.Log("Channel Data Thread closed.");
            } catch (Exception e) {
                CrashReport.Report(e);
            }
        }