forked from MerlinCooper/iRacingSDK.Net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iRacingListener.cs
165 lines (140 loc) · 5.01 KB
/
iRacingListener.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
// 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 iRacingSDK.Support;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
namespace iRacingSDK
{
public class iRacingEvents : IDisposable
{
readonly iRacingConnection instance = new iRacingConnection();
readonly CrossThreadEvents<DataSample> newData = new CrossThreadEvents<DataSample>();
readonly CrossThreadEvents<DataSample> newSessionData = new CrossThreadEvents<DataSample>();
readonly CrossThreadEvents connected = new CrossThreadEvents();
readonly CrossThreadEvents disconnected = new CrossThreadEvents();
readonly TimeSpan period;
Task backListener;
bool requestCancel;
/// <summary>
///
/// </summary>
/// <param name="period">The time interval for raising the NewData event</param>
public iRacingEvents(TimeSpan period)
{
this.period = period;
}
/// <summary>
///
/// </summary>
/// <param name="periodInMilliseconds">The time interval in milliseconds to raise the NewData Event. 0 means as often as data arrives from iRacing.</param>
public iRacingEvents(int periodInMilliseconds = 0)
{
period = new TimeSpan(0, 0, 0, 0, periodInMilliseconds);
}
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> NewData
{
add { newData.Event += value; }
remove { newData.Event -= value; }
}
public event Action<DataSample> NewSessionData
{
add { newSessionData.Event += value; }
remove { newSessionData.Event -= value; }
}
public void StartListening()
{
if( backListener != null )
throw new Exception("Already listening to iRacing data");
requestCancel = false;
backListener = new Task(Listen);
backListener.Start();
}
public void StopListening()
{
var bl = backListener;
if (backListener == null)
throw new Exception("Not currently listening to iRacing data");
requestCancel = true;
bl.Wait(500);
}
void Listen()
{
var isConnected = false;
var isDisconnected = true;
var lastSessionInfoUpdate = -1;
var periodCount = this.period;
var lastTimeStamp = DateTime.Now;
try
{
foreach (var d in instance.GetDataFeed(logging: false))
{
if (requestCancel)
return;
if (!isConnected && d.IsConnected)
{
isConnected = true;
isDisconnected = false;
connected.Invoke();
}
if (!isDisconnected && !d.IsConnected)
{
isConnected = false;
isDisconnected = true;
disconnected.Invoke();
}
if (period >= (DateTime.Now - lastTimeStamp))
continue;
lastTimeStamp = DateTime.Now;
if ( d.IsConnected)
newData.Invoke(d);
if (d.IsConnected && d.SessionData.InfoUpdate != lastSessionInfoUpdate)
{
lastSessionInfoUpdate = d.SessionData.InfoUpdate;
newSessionData.Invoke(d);
}
}
}
catch(Exception e)
{
TraceError.WriteLine(e.Message);
TraceError.WriteLine(e.StackTrace);
}
finally
{
backListener = null;
}
}
public void Dispose()
{
StopListening();
}
}
}