-
Notifications
You must be signed in to change notification settings - Fork 19
/
iRacingInstance.cs
169 lines (141 loc) · 5.74 KB
/
iRacingInstance.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// This file is part of iRacingSDK.
//
// Copyright 2014 Dean Netherton
// https://github.com/vipoo/iRacingSDK.Net
//
// iRacingSDK is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// iRacingSDK is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with iRacingSDK. If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Collections.Generic;
using System.Threading;
using Win32.Synchronization;
using System.Runtime.InteropServices;
using System.IO.MemoryMappedFiles;
using System.Diagnostics;
using iRacingSDK;
using iRacingSDK.Support;
namespace iRacingSDK
{
public class iRacingConnection
{
readonly CrossThreadEvents connected = new CrossThreadEvents();
readonly CrossThreadEvents disconnected = new CrossThreadEvents();
readonly CrossThreadEvents<DataSample> newSessionData = new CrossThreadEvents<DataSample>();
DataFeed dataFeed = null;
bool isRunning = false;
iRacingMemory iRacingMemory;
internal bool IsRunning { get { return isRunning; } }
long processingTime;
public long ProcessingTime { get { return processingTime * 1000000L / Stopwatch.Frequency; } }
long waitingTime;
public long WaitingTime { get { return waitingTime * 1000000L / Stopwatch.Frequency; } }
long yieldTime;
public long YieldTime { get { return (yieldTime * 1000000L / Stopwatch.Frequency) ; } }
public readonly Replay Replay;
public readonly PitCommand PitCommand;
public bool IsConnected { get; private set; }
public event Action Connected
{
add { connected.Event += value; }
remove { connected.Event -= value; }
}
public event Action Disconnected
{
add { disconnected.Event += value; }
remove { disconnected.Event -= value; }
}
public event Action<DataSample> NewSessionData
{
add { newSessionData.Event += value; }
remove { newSessionData.Event -= value; }
}
public iRacingConnection()
{
this.Replay = new Replay(this);
this.PitCommand = new PitCommand();
this.iRacingMemory = new iRacingMemory();
}
public IEnumerable<DataSample> GetDataFeed(bool logging = true)
{
return GetRawDataFeed(logging).WithLastSample().WithEvents(connected, disconnected, newSessionData);
}
internal IEnumerable<DataSample> GetRawDataFeed(bool logging = true)
{
if (isRunning)
throw new Exception("Can not call GetDataFeed concurrently.");
isRunning = true;
try
{
foreach (var notConnectedSample in WaitForInitialConnection())
{
IsConnected = false;
yield return notConnectedSample;
}
foreach (var sample in AllSamples(logging))
{
IsConnected = sample.IsConnected;
yield return sample;
}
}
finally
{
isRunning = false;
}
}
IEnumerable<DataSample> WaitForInitialConnection()
{
bool wasConnected = iRacingMemory.Accessor != null;
TraceInfo.WriteLineIf(!wasConnected, "Waiting to connect to iRacing application");
while (!iRacingMemory.IsConnected())
{
yield return DataSample.YetToConnected;
Thread.Sleep(10);
}
TraceInfo.WriteLineIf(!wasConnected, "Connected to iRacing application");
}
IEnumerable<DataSample> AllSamples(bool logging)
{
if (dataFeed == null)
dataFeed = new DataFeed(iRacingMemory.Accessor);
var nextTickCount = 0;
var lastTickTime = DateTime.Now;
var watchProcessingTime = new Stopwatch();
var watchWaitingTime = new Stopwatch();
while (true)
{
watchWaitingTime.Restart();
iRacingMemory.WaitForData();
waitingTime = watchWaitingTime.ElapsedTicks;
watchProcessingTime.Restart();
var data = dataFeed.GetNextDataSample(nextTickCount, logging);
if (data != null)
{
if (data.IsConnected)
{
if (data.Telemetry.TickCount == nextTickCount - 1)
continue; //Got the same sample - try again.
if (logging && data.Telemetry.TickCount != nextTickCount && nextTickCount != 0)
Debug.WriteLine("Dropped DataSample from {0} to {1}. Over time of {2}",
nextTickCount, data.Telemetry.TickCount - 1, (DateTime.Now - lastTickTime).ToString(@"s\.fff"), "WARN");
nextTickCount = data.Telemetry.TickCount + 1;
lastTickTime = DateTime.Now;
}
processingTime = watchProcessingTime.ElapsedTicks;
watchProcessingTime.Restart();
yield return data;
yieldTime = watchProcessingTime.ElapsedTicks;
}
}
}
}
}