void ReadTelemetry() { PerformInjection(); if (InjectionManager.GetState() == InjectionManager.State.Failed) { return; } UdpClient socket = new UdpClient(); socket.ExclusiveAddressUse = false; socket.Client.Bind(new IPEndPoint(IPAddress.Any, readPort)); Stopwatch sw = new Stopwatch(); sw.Start(); StartSending(); //read and process while (!IsStopped) { try { //wait for telemetry if (socket.Available == 0) { if (sw.ElapsedMilliseconds > 500) { Thread.Sleep(1000); } continue; } Byte[] received = socket.Receive(ref senderIP); while (socket.Available != 0) { received = socket.Receive(ref senderIP); continue; } var alloc = GCHandle.Alloc(received, GCHandleType.Pinned); data = (VTOLVRData)Marshal.PtrToStructure(alloc.AddrOfPinnedObject(), typeof(VTOLVRData)); alloc.Free(); if (data.packetId < lastPacketId && Math.Abs((long)data.packetId - (long)lastPacketId) < 1000) { continue; } lastPacketId = data.packetId; if (!data.paused) { dt = (float)sw.Elapsed.TotalSeconds; sw.Restart(); ProcessVTOLVRData(dt);// data.dt); } } catch (Exception e) { Thread.Sleep(1000); } } StopSending(); Thread.CurrentThread.Join(); }
void ReadTelemetry() { PerformInjection(); if (InjectionManager.GetState() == InjectionManager.State.Failed) { return; } UdpClient socket = new UdpClient(); socket.ExclusiveAddressUse = false; socket.Client.Bind(new IPEndPoint(IPAddress.Any, readPort)); StartSending(); //read and process while (!IsStopped) { try { //wait for telemetry if (socket.Available == 0) { using (var sleeper = new ManualResetEvent(false)) { sleeper.WaitOne(1); } continue; } Byte[] received = socket.Receive(ref senderIP); if (socket.Available == 0) { data = JsonConvert.DeserializeObject <OverloadData>(System.Text.Encoding.UTF8.GetString(received)); if (data.packetId < lastPacketId && Math.Abs((long)data.packetId - (long)lastPacketId) < 1000) { continue; } lastPacketId = data.packetId; if (!data.paused) { ProcessOverloadData(data.dt); } } } catch (Exception e) { Thread.Sleep(1000); } } StopSending(); Thread.CurrentThread.Join(); }
public static void Monitor(string processName, byte[] dllContent, AutoResetEvent injectionEvent, string namespaceString) { IntPtr assembly = IntPtr.Zero; string lastPid = null; string pidPath = processName + "enabler.lastppid"; string pidfile = Path.Combine(Path.GetTempPath(), pidPath); InjectionManager.minimizeMemory(); if (File.Exists(pidfile)) { lastPid = File.ReadAllText(pidfile); } for (; ;) { try { InjectionManager.minimizeMemory(); Thread.Sleep(1000); Process process = Process.GetProcessesByName(processName).FirstOrDefault <Process>(); if (process != null) { InjectionManager.SetStatus("Game process found.", State.GameProcessFound); injectionEvent.Set(); if (process.Id.ToString() == lastPid) { InjectionManager.SetStatus("Telemetry plugin already running in the current game.", State.Success); injectionEvent.Set(); } else { InjectionManager.SetStatus("Waiting 5s before plugin injection.", State.Waiting); injectionEvent.Set(); Thread.Sleep(5000); Injector injector; Injector enabler = injector = new Injector(process.Id); try { assembly = IntPtr.Zero; try { assembly = enabler.Inject(dllContent, namespaceString, "Loader", "Init"); InjectionManager.minimizeMemory(); } catch (InjectorException ie) { string str = "Failed to add assembly: "; InjectorException ex = ie; InjectionManager.SetStatus(str + ((ex != null) ? ex.ToString() : null), State.Failed); injectionEvent.Set(); } catch (Exception exc) { string str2 = "Failed to add assembly (unknown error): "; Exception ex2 = exc; InjectionManager.SetStatus(str2 + ((ex2 != null) ? ex2.ToString() : null), State.Failed); injectionEvent.Set(); } if (assembly == IntPtr.Zero) { break; } InjectionManager.SetStatus("Telemetry plugin successfully injected", State.Success); File.WriteAllText(pidfile, process.Id.ToString()); injectionEvent.Set(); while (!process.HasExited) { Thread.Sleep(1); } try { if (!process.HasExited) { enabler.Eject(assembly, "TelemetryExporter", "Loader", "Unload"); } File.Delete(pidfile); } catch { } } finally { if (injector != null) { ((IDisposable)injector).Dispose(); } } } Thread.Sleep(1000); } continue; } catch { continue; } break; } }