Added packet class and
This commit is contained in:
parent
e65a5f3803
commit
99f5cae65f
34
Program.cs
34
Program.cs
@ -1,6 +1,7 @@
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using EFBTracker.Tracking;
|
||||
|
||||
bool done = false;
|
||||
int listenPort = 49003;
|
||||
@ -15,44 +16,13 @@ using(UdpClient listener = new UdpClient(listenPort))
|
||||
Console.WriteLine("Received broadcast message from client {0}", listenEndPoint.ToString());
|
||||
|
||||
Console.WriteLine("Decoded data is:");
|
||||
var packets = Packet.ReadPackets(receivedData);
|
||||
//Console.WriteLine(Encoding.ASCII.GetString(receivedData));
|
||||
Console.WriteLine(BitConverter.ToString(receivedData));
|
||||
|
||||
|
||||
|
||||
|
||||
var pos = 0;
|
||||
var header = Encoding.UTF8.GetString(receivedData, pos, 4);
|
||||
pos += 5; // Including tailing 0
|
||||
|
||||
if (header == "DATA") // Ignore other messages
|
||||
{
|
||||
while (pos < receivedData.Length)
|
||||
{
|
||||
var id = BitConverter.ToInt32(receivedData, pos);
|
||||
pos += 4;
|
||||
|
||||
try
|
||||
{
|
||||
var value = BitConverter.ToSingle(receivedData, pos);
|
||||
pos += 4;
|
||||
// var localDataRefs = DataRefs.ToArray();
|
||||
// foreach (var dr in localDataRefs)
|
||||
// if (dr.Update(id, value))
|
||||
// OnDataRefReceived?.Invoke(dr);
|
||||
Console.WriteLine($"Header: {header} Value: {value}");
|
||||
}
|
||||
catch (ArgumentException ex)
|
||||
{
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var error = ex.Message;
|
||||
Console.WriteLine(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
56
Tracking/Packet.cs
Normal file
56
Tracking/Packet.cs
Normal file
@ -0,0 +1,56 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Text;
|
||||
|
||||
namespace EFBTracker.Tracking
|
||||
{
|
||||
public class Packet
|
||||
{
|
||||
private string Type { get; set; }
|
||||
private float[]? Data { get; set; }
|
||||
|
||||
public Packet(string type, float[] data){
|
||||
Type = type;
|
||||
Data = data;
|
||||
}
|
||||
|
||||
public static Packet[]? ReadPackets(byte[] data){
|
||||
|
||||
|
||||
if((data.Length - 5) % 36 == 0){//If there are correctly sized packets
|
||||
int packetPos = 0;
|
||||
List<Packet> packets = new List<Packet>();
|
||||
|
||||
//Next 4 bytes are packet header
|
||||
string header = Encoding.UTF8.GetString(data, packetPos, 4);
|
||||
packetPos += 5;
|
||||
|
||||
while(packetPos < data.Length)
|
||||
{
|
||||
|
||||
int id = BitConverter.ToInt32(data, packetPos);
|
||||
packetPos += 4;
|
||||
|
||||
//Array to take 4 values
|
||||
float[] values = new float[4];
|
||||
|
||||
for (var i = 0; i < values.Length; i++)
|
||||
{//Read 4 values from packet and save into array
|
||||
var value = BitConverter.ToSingle(data, packetPos);
|
||||
values[i] = value;
|
||||
packetPos += 8;
|
||||
}
|
||||
|
||||
//Generate new packet object and add to list
|
||||
packets.Add(new Packet(header, values));
|
||||
}
|
||||
return packets.ToArray();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user