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;
}