public byte[] ToByteArray() { IL2API packet = this; int num = Marshal.SizeOf <IL2API>(packet); byte[] array = new byte[num]; IntPtr intPtr = Marshal.AllocHGlobal(num); Marshal.StructureToPtr <IL2API>(packet, intPtr, false); Marshal.Copy(intPtr, array, 0, num); Marshal.FreeHGlobal(intPtr); return(array); }
public void CopyFields(IL2API other) { packetID = other.packetID; tick = other.tick; yaw = other.yaw; pitch = other.pitch; roll = other.roll; spinX = other.spinX; spinY = other.spinY; spinZ = other.spinZ; accX = other.accX; accY = other.accY; accZ = other.accZ; }
/// <summary> /// The thread funktion to poll the telemetry data and send TelemetryUpdated events. /// </summary> private void Run() { IL2API lastTelemetryData = new IL2API(); lastTelemetryData.Reset(); Session session = new Session(); Stopwatch sw = new Stopwatch(); sw.Start(); UdpClient socket = new UdpClient(); socket.ExclusiveAddressUse = false; socket.Client.Bind(new IPEndPoint(IPAddress.Any, PORTNUM)); Log("Listener started (port: " + PORTNUM.ToString() + ") IL2TelemetryProvider.Thread"); while (!isStopped) { try { // get data from game, if (socket.Available == 0) { if (sw.ElapsedMilliseconds > 500) { IsRunning = false; IsConnected = false; Thread.Sleep(1000); } continue; } else { IsConnected = true; } Byte[] received = socket.Receive(ref _senderIP); var alloc = GCHandle.Alloc(received, GCHandleType.Pinned); IL2API telemetryData = (IL2API)Marshal.PtrToStructure(alloc.AddrOfPinnedObject(), typeof(IL2API)); // otherwise we are connected IsConnected = true; if (telemetryData.packetID == 0x494C0100) { IsRunning = true; sw.Restart(); IL2API telemetryToSend = new IL2API(); telemetryToSend.Reset(); telemetryToSend.CopyFields(telemetryData); telemetryToSend.roll = LoopAngle(telemetryData.roll * (180.0f / (float)Math.PI), 90.0f); telemetryToSend.pitch = telemetryData.pitch * (180.0f / (float)Math.PI); telemetryToSend.yaw = telemetryData.yaw * (180.0f / (float)Math.PI); TelemetryEventArgs args = new TelemetryEventArgs( new IL2TelemetryInfo(telemetryToSend, lastTelemetryData)); RaiseEvent(OnTelemetryUpdate, args); lastTelemetryData = telemetryToSend; } else if (sw.ElapsedMilliseconds > 500) { IsRunning = false; } } catch (Exception e) { LogError("IL2Telemetry Exception while processing data", e); IsConnected = false; IsRunning = false; Thread.Sleep(1000); } } socket.Close(); IsConnected = false; IsRunning = false; }
public IL2TelemetryInfo(IL2API telemetryData, IL2API lastTelemetryData) { _telemetryData = telemetryData; _lastTelemetryData = lastTelemetryData; }