/// <summary>
        /// Internal use in sdk only.
        /// Raise the connection and disconnection events as iRacing is started, stopped.
        /// </summary>
        internal static IEnumerable <DataSample> WithEvents(this IEnumerable <DataSample> samples, CrossThreadEvents connectionEvent, CrossThreadEvents disconnectionEvent, CrossThreadEvents <DataSample> newSessionData)
        {
            var isConnected           = false;
            var isDisconnected        = true;
            var lastSessionInfoUpdate = -1;

            foreach (var data in samples)
            {
                if (!isConnected && data.IsConnected)
                {
                    isConnected    = true;
                    isDisconnected = false;
                    connectionEvent.Invoke();
                }

                if (!isDisconnected && !data.IsConnected)
                {
                    isConnected    = false;
                    isDisconnected = true;
                    disconnectionEvent.Invoke();
                }

                if (data.IsConnected && data.SessionData.InfoUpdate != lastSessionInfoUpdate)
                {
                    lastSessionInfoUpdate = data.SessionData.InfoUpdate;
                    newSessionData.Invoke(data);
                }

                yield return(data);
            }
        }
Example #2
0
        void Listen()
        {
            var isConnected           = false;
            var isDisconnected        = true;
            var lastSessionInfoUpdate = -1;

            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 (d.IsConnected)
                    {
                        newData.Invoke(d);
                    }

                    if (d.IsConnected && d.SessionData.InfoUpdate != lastSessionInfoUpdate)
                    {
                        lastSessionInfoUpdate = d.SessionData.InfoUpdate;
                        newSessionData.Invoke(d);
                    }
                }
            }
            catch (Exception e)
            {
                Trace.WriteLine(e.Message, "DEBUG");
                Trace.WriteLine(e.StackTrace, "DEBUG");
            }
            finally
            {
                backListener = null;
            }
        }
        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;
            }
        }