Ejemplo n.º 1
0
        /* Receives packets and raises events continuously on a thread */
        private void ThreadRoutine()
        {
            /* Sets up UDP connection objects */
            UdpClient  client = new UdpClient(port);
            IPEndPoint ep     = new IPEndPoint(IPAddress.Parse(ip), port);

            client.Client.ReceiveTimeout = timeout;

            /* Loops until broken by the isRunning flag */
            while (true)
            {
                /* Break if isRunning is false */
                flagMtx.WaitOne();
                if (!isRunning)
                {
                    flagMtx.ReleaseMutex(); break;
                }
                else
                {
                    flagMtx.ReleaseMutex();
                }

                byte[] data;

                /* Try to get data from the socket and restart the loop on timeout */
                try { data = client.Receive(ref ep); }
                catch (SocketException e) { continue; }

                /* Restart the loop if the packet is invalid */
                if (!IsValidPacketData(ref data))
                {
                    continue;
                }

                /* Create arguments object for triggering event */
                F117ReceivedEventArgs args = new F117ReceivedEventArgs(ParsePacket(ref data));

                /* Raise event */
                RaisePacketReceived(args);
            }

            client.Close();
        }
Ejemplo n.º 2
0
        /* Runs when triggered by an F117Reader object */
        private void OnPacketReceived(object sender, F117ReceivedEventArgs args)
        {
            UDPPacket packet = args.packet;

            dataMtx.WaitOne();
            maxRpm = (int)packet.m_max_rpm;
            dataMtx.ReleaseMutex();

            PositionEventArgs newPos = new PositionEventArgs();

            newPos.x = packet.m_x;
            newPos.y = packet.m_y;
            newPos.z = packet.m_z;

            GForceEventArgs newGs = new GForceEventArgs();

            newGs.lat = packet.m_gforce_lat;
            newGs.lon = packet.m_gforce_lon;

            PowertrainEventArgs newPow = new PowertrainEventArgs();

            newPow.rpm = (int)packet.m_engineRate;
            newPow.kmh = packet.m_speed * 3.6f;
            newPow.mph = packet.m_speed * 2.23694f;

            AxesEventArgs newAxes = new AxesEventArgs();

            newAxes.throttle = packet.m_throttle;
            newAxes.brake    = packet.m_brake;
            newAxes.clutch   = packet.m_clutch;
            newAxes.steering = packet.m_steer * -1.0f;

            RaisePositionReceived(newPos);
            RaiseGForceReceived(newGs);
            RaisePowertrainReceived(newPow);
            RaiseAxesReceived(newAxes);
        }
Ejemplo n.º 3
0
 /* Raises a PacketReceivedEvent if someone has subscribed to it */
 private void RaisePacketReceived(F117ReceivedEventArgs args)
 {
     PacketReceivedEvent?.Invoke(this, args);
 }