Skip to content
View in the app

A better way to browse. Learn more.

LCPDFR.com

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Game freezing in infinite loop

Featured Replies

Hey everyone, I'm a Python programmer and recently decided to start a project with AI and the Rage Plugin Hook V / LSPDFR API but I'm having a bit of troubles getting it to work, especially because of named pipes and server-client connections between Python and C#, anyone has any idea why the game immediately freezes upon connection? How can I prevent it? Thanks!

 

using System;
using System.IO.Pipes;
using System.Text;
using Newtonsoft.Json;
using Rage;
using LSPD_First_Response.Mod.API;
using LSPD_First_Response;

namespace LASO
{
    class DispatcherClient
    {
        private string pipeName;
        public DispatcherClient(string pipeName)
        {
            this.pipeName = pipeName;
        }

        public void ConnectAndListen()
        {
            GameFiber.StartNew(() =>
            {

                using (NamedPipeClientStream pipeClient = new NamedPipeClientStream(".", this.pipeName, PipeDirection.InOut))
                {
                    try
                    {
                        Game.Console.Print("LASO: Connecting to dispatcher...");
                        pipeClient.Connect(5000); // Wait up to 5 seconds to connect
                        Game.Console.Print("LASO: Connected to dispatcher, starting main loop...");

                        // Main loop
                        while (pipeClient.IsConnected)
                        {
                            GameFiber.Yield();

                            try
                            {
                                if (pipeClient.CanRead)
                                {
                                    // Receive the method to execute from Python
                                    byte[] buffer = new byte[1024];
                                    int bytesRead = pipeClient.Read(buffer, 0, buffer.Length);

                                    if (bytesRead == 0)
                                    {
                                        throw new Exception("LASO: Received 0 bytes from the server");
                                    }

                                    string commandJson = Encoding.UTF8.GetString(buffer, 0, bytesRead);
                                    dynamic command = JsonConvert.DeserializeObject(commandJson);
                                    HandleCommand(command);
                                }
                            }
                            catch (TimeoutException)
                            {
                                // Prevent game freeze
                                GameFiber.Sleep(100);
                            }
                            catch (Exception e)
                            {
                                Game.Console.Print("LASO: Exception in ConnectAndListen: " + e.Message);
                                break; // Break the loop
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Game.Console.Print("LASO: Exception in ConnectAndListen: " + e.Message);
                    }
                }
            });
        }

        private void HandleCommand(dynamic command)
        {
            // Handle the command
            switch ((string)command.function)
            {
                case "call_backup_to_player_position":
                    RequestBackup(command);
                    break;

                default:
                    UknonwnCommand(command);
                    break;
            }
        }

        private void RequestBackup(dynamic command)
        {
            Game.DisplayNotification("LASO: Requesting backup to your position");

            Vector3 playerPosition = Game.LocalPlayer.Character.Position;
            EBackupResponseType code = (EBackupResponseType)Enum.Parse(typeof(EBackupResponseType), command.code);
            EBackupUnitType unitType = (EBackupUnitType)Enum.Parse(typeof(EBackupUnitType), command.unitType);

            Functions.RequestBackup(playerPosition, code, unitType);
        }

        private void UknonwnCommand(dynamic command)
        {
            Game.DisplayNotification("LASO: Unknown command received from dispatcher" + command.function);
        }
    }
}

 

  • Author

like this?
 

using System;
using System.IO.Pipes;
using System.Text;
using System.Threading;
using System.Collections.Concurrent;
using Newtonsoft.Json;
using Rage;
using LSPD_First_Response.Mod.API;
using LSPD_First_Response;

namespace LASO
{
    class DispatcherClient
    {
        private string pipeName;
        private Thread ioThread;
        private ConcurrentQueue<dynamic> commandQueue;

        public DispatcherClient(string pipeName)
        {
            this.pipeName = pipeName;
            this.commandQueue = new ConcurrentQueue<dynamic>();
        }

        public void ConnectAndListen()
        {
            ioThread = new Thread(() => ConnectAndListenThread());
            ioThread.IsBackground = true;
            ioThread.Start();

            GameFiber.StartNew(() =>
            {
                while (true)
                {
                    try
                    {
                        if (commandQueue.TryDequeue(out dynamic command))
                        {
                            HandleCommand(command);
                        }
                    }
                    catch (Exception e)
                    {
                        Game.Console.Print("LASO: Exception in GameFiber loop: " + e.Message);
                    }

                    GameFiber.Yield();
                }
            });
        }

        private void ConnectAndListenThread()
        {
            using (NamedPipeClientStream pipeClient = new NamedPipeClientStream(".", this.pipeName, PipeDirection.InOut))
            {
                try
                {
                    Game.Console.Print("LASO: Connecting to dispatcher...");
                    pipeClient.Connect(5000); // Wait up to 5 seconds to connect
                    Game.Console.Print("LASO: Connected to dispatcher, starting main loop...");

                    // Main loop
                    while (pipeClient.IsConnected)
                    {
                        try
                        {
                            if (pipeClient.CanRead)
                            {
                                byte[] buffer = new byte[1024];
                                int bytesRead = pipeClient.Read(buffer, 0, buffer.Length);

                                if (bytesRead > 0)
                                {
                                    string commandJson = Encoding.UTF8.GetString(buffer, 0, bytesRead);
                                    dynamic command = JsonConvert.DeserializeObject(commandJson);
                                    commandQueue.Enqueue(command);
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            Game.Console.Print("LASO: Exception in ConnectAndListenThread: " + e.Message);
                        }
                    }
                }
                catch (Exception e)
                {
                    Game.Console.Print("LASO: Exception in ConnectAndListenThread: " + e.Message);
                }
            }
        }

        private void HandleCommand(dynamic command)
        {
            // Handle the command
            switch ((string)command.function)
            {
                case "call_backup_to_player_position":
                    RequestBackup(command);
                    break;

                default:
                    UknonwnCommand(command);
                    break;
            }
        }

        private void RequestBackup(dynamic command)
        {
            Game.DisplayNotification("LASO: Requesting backup to your position");

            Vector3 playerPosition = Game.LocalPlayer.Character.Position;
            EBackupResponseType code = (EBackupResponseType)Enum.Parse(typeof(EBackupResponseType), command.code);
            EBackupUnitType unitType = (EBackupUnitType)Enum.Parse(typeof(EBackupUnitType), command.unitType);

            Functions.RequestBackup(playerPosition, code, unitType);
        }

        private void UknonwnCommand(dynamic command)
        {
            Game.DisplayNotification("LASO: Unknown command received from dispatcher: " + command.function);
        }
    }
}

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.