예제 #1
0
        /* Runs when triggered by an ACReader object */
        private void OnPacketReceived(object sender, ACReceivedEventArgs args)
        {
            PacketID id = args.id;

            if (id == PacketID.RESPONSE)
            {
                responseMtx.WaitOne();
                responsePacket = (HandshakeResponse)args.packet;
                responseMtx.ReleaseMutex();

                dataMtx.WaitOne();
                maxRpm = 0;
                dataMtx.ReleaseMutex();
            }
            else if (id == PacketID.UPDATE)
            {
                RTCarInfo packet = (RTCarInfo)args.packet;

                dataMtx.WaitOne();
                if (packet.engineRPM > maxRpm)
                {
                    maxRpm = (int)packet.engineRPM;
                }
                dataMtx.ReleaseMutex();

                PositionEventArgs newPos = new PositionEventArgs();
                newPos.x = packet.carCoordinates[0];
                newPos.y = packet.carCoordinates[1];
                newPos.z = packet.carCoordinates[2];

                GForceEventArgs newGs = new GForceEventArgs();
                newGs.lat = packet.accG_horizontal;
                newGs.lon = packet.accG_frontal;

                PowertrainEventArgs newPow = new PowertrainEventArgs();
                newPow.rpm = (int)packet.engineRPM;
                newPow.kmh = packet.speed_Kmh;
                newPow.mph = packet.speed_Mph;

                AxesEventArgs newAxes = new AxesEventArgs();
                newAxes.throttle = packet.gas;
                newAxes.brake    = packet.brake;
                newAxes.clutch   = 1.0f - packet.clutch;
                newAxes.steering = packet.steer;

                RaisePositionReceived(newPos);
                RaiseGForceReceived(newGs);
                RaisePowertrainReceived(newPow);
                RaiseAxesReceived(newAxes);
            }
        }
예제 #2
0
        /* Receives packets and raises events continuously on a thread */
        private void ThreadRoutine()
        {
            Console.WriteLine("Test");

            /* Sets up UDP connection objects */
            UdpClient  client = new UdpClient(clientPort);
            IPEndPoint ep     = new IPEndPoint(IPAddress.Parse(serverIP), serverPort);

            client.Client.ReceiveTimeout = timeout;

            /* Sends required connection handshake and gets back static data */
            byte[] responseData = new byte[1];
            while (true)
            {
                flagMtx.WaitOne();
                if (!isRunning)
                {
                    flagMtx.ReleaseMutex(); expectedID = (PacketID)(-1); break;
                }
                else
                {
                    flagMtx.ReleaseMutex();
                }

                client.Send(CONNECT_HANDSHAKE, HANDSHAKE_PACKET_SIZE, ep);
                try {
                    responseData = client.Receive(ref ep);
                    if (IsValidPacketData(ref responseData, PacketID.RESPONSE))
                    {
                        break;
                    }
                    else
                    {
                        Console.WriteLine("Received response data of unexpected size!"); continue;
                    }
                }
                catch (SocketException e) { continue; }
            }

            /* Parse the data into a packet, wrap it inside an EventArgs object and raise the received event */
            ACReceivedEventArgs responseArgs = new ACReceivedEventArgs(ParsePacket <HandshakeResponse>(ref responseData), PacketID.RESPONSE);

            RaisePacketReceived(responseArgs);

            /* Loop to receive data depending on set subscription type */
            if (expectedID == PacketID.UPDATE)
            {
                /* Send a request to start updating */
                client.Send(UPDATE_HANDSHAKE, HANDSHAKE_PACKET_SIZE, ep);

                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 on timeout */
                    try { data = client.Receive(ref ep); }
                    catch (SocketException e) { continue; }

                    // Console.WriteLine(data.Length);

                    /* Restart the loop if the received data is not of the correct length */
                    if (!IsValidPacketData(ref data, PacketID.UPDATE))
                    {
                        continue;
                    }

                    /* Parse the data into a packet, wrap it inside an EventArgs object and raise the received event */
                    ACReceivedEventArgs updateArgs = new ACReceivedEventArgs(ParsePacket <RTCarInfo>(ref data), PacketID.UPDATE);
                    RaisePacketReceived(updateArgs);
                    // Console.WriteLine(Marshal.SizeOf(ParsePacket<RTCarInfo>(ref data)));
                }
            }
            else if (expectedID == PacketID.SPOT)
            {
                /* Send a request to receive spot updates */
                client.Send(SPOT_HANDSHAKE, HANDSHAKE_PACKET_SIZE, ep);

                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 on timeout */
                    try { data = client.Receive(ref ep); }
                    catch (SocketException e) { continue; }

                    /* Restart the loop of the received data is not of the correct length */
                    if (!IsValidPacketData(ref data, PacketID.SPOT))
                    {
                        continue;
                    }

                    /* Parse the data into a pakcet, wrap it inside an EventArgs object and raise the received event */
                    ACReceivedEventArgs updateArgs = new ACReceivedEventArgs(ParsePacket <RTCarInfo>(ref data), PacketID.SPOT);
                    RaisePacketReceived(updateArgs);
                }
            }

            /* Send a request to stop updates and close the client */
            client.Send(DISMISS_HANDSHAKE, HANDSHAKE_PACKET_SIZE, ep);
            client.Close();
        }
예제 #3
0
 /* Raises a PacketReceivedEvent if someone has subscribed to it */
 private void RaisePacketReceived(ACReceivedEventArgs args)
 {
     PacketReceivedEvent?.Invoke(this, args);
 }