ExtractLengthOfDataBlockInBytes_Int() public static method

public static ExtractLengthOfDataBlockInBytes_Int ( byte Data ) : int
Data byte
return int
        public static string DecodeAsterixData(string FileName)
        {
            long Position = 0;
            // get total byte length of the file
            long TotalBytes = new System.IO.FileInfo(FileName).Length;

            try
            {
                // Open file for reading
                System.IO.FileStream FileStream = new System.IO.FileStream(FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);

                // attach filestream to binary reader
                System.IO.BinaryReader BinaryReader = new System.IO.BinaryReader(FileStream);

                while (Position < TotalBytes)
                {
                    // First determine the size of the message
                    // octet    data
                    // 0        ASTERIX CATEGORY
                    // 1 .. 2   LENGTH OF MESSAGE BLOCK
                    byte[] Data_Block_Buffer    = BinaryReader.ReadBytes((Int32)3);
                    int    LengthOfMessageBlock = ASTERIX.ExtractLengthOfDataBlockInBytes_Int(Data_Block_Buffer);

                    BinaryReader.BaseStream.Position = BinaryReader.BaseStream.Position - 3;
                    // Now read the message and pass it to the parser
                    // for decoding
                    Data_Block_Buffer = BinaryReader.ReadBytes((Int32)LengthOfMessageBlock);
                    ExtractAndDecodeASTERIX_CAT_DataBlock(Data_Block_Buffer, false);
                    Position = BinaryReader.BaseStream.Position;
                }

                // close file reader
                FileStream.Close();
                FileStream.Dispose();
                BinaryReader.Close();
            }
            catch (Exception Exception)
            {
                // Error
                MessageBox.Show("Exception caught in process: {0}", Exception.ToString());
            }

            return("Read " + Position.ToString() + " of total " + TotalBytes.ToString() + " bytes");
        }
        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
        // The method that gets invoked as a thread when a Connect button is
        // pressed. It will listen on a given multicast address and store messages in the
        // list box above.
        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
        public static void ListenForData()
        {
            bool ThereWasAnException = false;

            // File stream
            // Stream RecordingStream_debug = new FileStream(@"C:\ASTERIX\Italy", FileMode.Create);
            //BinaryWriter RecordingBinaryWriter_debug = new BinaryWriter(RecordingStream_debug);

            // Loop forever
            while (!_shouldStop)
            {
                // Do something only if user has requested so
                if (SharedData.bool_Listen_for_Data)
                {
                    ThereWasAnException = false;
                    try
                    {
                        // Lets receive data in an array of bytes
                        // (an octet, of course composed of 8bits)
                        UDPBuffer = sock.Receive(ref iep);
                        LastDataBlockDateTimeForStalleData = DateTime.Now;
                    }
                    catch
                    {
                        ThereWasAnException = true;
                    }

                    if (ThereWasAnException == false)
                    {
                        int DataBufferIndexForThisExtraction = 0;

                        // Check if this is non-standard 6 byte RMCDE header ASTERIX.
                        if (Properties.Settings.Default.RMCDE_ASTERIX)
                        {
                            UDPBuffer_Non_Standard = new byte[(UDPBuffer.Length - 6)];
                            Array.Copy(UDPBuffer, 6, UDPBuffer_Non_Standard, 0, (UDPBuffer.Length - 6));
                            UDPBuffer = UDPBuffer_Non_Standard;
                        }

                        // Now write the data block
                        //RecordingBinaryWriter_debug.Write(UDPBuffer);

                        // Extract lenghts
                        int LengthOfASTERIX_CAT = ASTERIX.ExtractLengthOfDataBlockInBytes_Int(UDPBuffer);
                        int LenghtOfDataBuffer  = UDPBuffer.Length;

                        // Loop through the buffer and pass on each ASTERIX category block to
                        // the category extractor. The extractor itslef will then extract individual
                        // data records.
                        try
                        {
                            while (DataBufferIndexForThisExtraction < LenghtOfDataBuffer)
                            {
                                byte[] LocalSingle_ASTERIX_CAT_Buffer = new byte[LengthOfASTERIX_CAT];
                                Array.Copy(UDPBuffer, DataBufferIndexForThisExtraction, LocalSingle_ASTERIX_CAT_Buffer, 0, LengthOfASTERIX_CAT);
                                ExtractAndDecodeASTERIX_CAT_DataBlock(LocalSingle_ASTERIX_CAT_Buffer, true);

                                DataBufferIndexForThisExtraction = DataBufferIndexForThisExtraction + LengthOfASTERIX_CAT;

                                if (DataBufferIndexForThisExtraction < LenghtOfDataBuffer)
                                {
                                    Array.Copy(UDPBuffer, DataBufferIndexForThisExtraction, LocalSingle_ASTERIX_CAT_Buffer, 0, 3);
                                    LengthOfASTERIX_CAT = ASTERIX.ExtractLengthOfDataBlockInBytes_Int(LocalSingle_ASTERIX_CAT_Buffer);
                                }
                            }
                        }
                        catch
                        {
                            MessageBox.Show("There was an error in data acquire, please check if standard ASTERIX or RMCDE header is properly selected.");
                        }

                        // Check if recording of the currently live connection is requested
                        if (SharedData.DataRecordingClass.DataRecordingRequested == true)
                        {
                            if (RecordingJustStarted == true)
                            {
                                RecordingJustStarted              = false;
                                RecordingStream                   = new FileStream(SharedData.DataRecordingClass.FilePathandName, FileMode.Create);
                                RecordingBinaryWriter             = new BinaryWriter(RecordingStream);
                                LastDataBlockDateTimeForRecording = DateTime.Now;
                            }

                            // Determine the type of the recording
                            if (Properties.Settings.Default.RecordActiveInRaw == true)
                            {
                                // Just record in the raw format with not headers added
                                RecordingBinaryWriter.Write(UDPBuffer);
                            }
                            else // Here add headers
                            {
                                TimeSpan TimeDiff = DateTime.Now - LastDataBlockDateTimeForRecording;
                                LastDataBlockDateTimeForRecording = DateTime.Now;
                                // Header 1: Size of the original data block
                                // Header 2: The time between two data blocks (current and the last one)
                                //-----------------------------------------------------------------------
                                // Header 1 // Header 2
                                // ----------------------------------------------------------------------
                                // 4 bytes  // 4 bytes
                                // First add the size of the data block, not including the two headers

                                // Block size
                                RecordingBinaryWriter.Write(UDPBuffer.Length);
                                // Time between last and this block
                                RecordingBinaryWriter.Write(TimeDiff.Milliseconds);
                                // Now write the data block
                                RecordingBinaryWriter.Write(UDPBuffer);
                            }
                        }
                        else if (RecordingJustStarted == false)
                        {
                            RecordingJustStarted = true;
                            // Close the data stream
                            if (RecordingBinaryWriter != null)
                            {
                                RecordingBinaryWriter.Close();
                            }
                            if (RecordingStream != null)
                            {
                                RecordingStream.Close();
                            }
                        }
                    }
                }
            }

            _shouldStop = false;
        }