lan-mouse icon indicating copy to clipboard operation
lan-mouse copied to clipboard

Add auto restart client by return error emulation server: not responding.

Open F1st3K opened this issue 3 months ago • 3 comments

Sometime i have a problem, connection is dissconected by error view^

[2025-10-07T17:15:46Z WARN  lan_mouse::emulation] releasing keys: 192.168.0.188:44609 not responding!

And i have work around for it (windows client)

Its powershell script, its rerun client by error in stdout

# =============================
# LAN-Mouse Watchdog (clean)
# =============================

$LanMousePath = "C:\Users\kostinnik\Documents\lan\lan-mouse.exe"
$ErrorPattern = "not responding|network error|connection reset|os error 10040"

function Log { param($msg) ; Write-Host $msg }

$Global:LMProcess = $null
$Global:RestartNeeded = $false

function Start-LanMouse {
    if ($Global:LMProcess) {
        try { $Global:LMProcess.Kill() } catch {}
        $Global:LMProcess.Dispose()
        $Global:LMProcess = $null
    }

    # remove prev event subcription
    Get-EventSubscriber | Where-Object {$_.SourceIdentifier -in @("LMOut","LMErr")} | Unregister-Event

    $startInfo = New-Object System.Diagnostics.ProcessStartInfo
    $startInfo.FileName = $LanMousePath
    $startInfo.Arguments = "daemon"
    $startInfo.RedirectStandardOutput = $true
    $startInfo.RedirectStandardError  = $true
    $startInfo.UseShellExecute = $false
    $startInfo.CreateNoWindow = $true

    $proc = New-Object System.Diagnostics.Process
    $proc.StartInfo = $startInfo
    $proc.Start() | Out-Null
    $proc.BeginOutputReadLine()
    $proc.BeginErrorReadLine()
    $Global:LMProcess = $proc

    # stdout event
    Register-ObjectEvent -InputObject $proc -EventName OutputDataReceived -SourceIdentifier LMOut -Action {
        if ($EventArgs.Data) {
            Log "$($EventArgs.Data)"
            if ($EventArgs.Data -match $ErrorPattern) {
                Log "[!] Error detected -> scheduling restart"
                $Global:RestartNeeded = $true
            }
        }
    }

    # stderr event
    Register-ObjectEvent -InputObject $proc -EventName ErrorDataReceived -SourceIdentifier LMErr -Action {
        if ($EventArgs.Data) {
            Log "$($EventArgs.Data)"
            if ($EventArgs.Data -match $ErrorPattern) {
                Log "[!] Error detected -> scheduling restart"
                $Global:RestartNeeded = $true
            }
        }
    }

    Log "✅ lan-mouse started."
}


try {
    # основной код Watchdog
    Start-LanMouse
    Log "Watchdog started. Press Ctrl+C to stop."
    while ($true) {
        Start-Sleep -Milliseconds 500
        if ($Global:RestartNeeded) {
            Log "[!] Restarting lan-mouse..."
            Start-LanMouse
            $Global:RestartNeeded = $false
        }

        if ($Global:LMProcess -and $Global:LMProcess.HasExited) {
            Log "[!] lan-mouse crashed. Restarting..."
            Start-LanMouse
        }
    }
}
finally {
    # cleanup при выходе скрипта
    Log "🛑 Watchdog stopping. Killing lan-mouse..."
    if ($Global:LMProcess) {
        try { $Global:LMProcess.Kill() } catch {}
    }
}

Maybe add this feature to main project?

F1st3K avatar Oct 07 '25 17:10 F1st3K

https://github.com/feschber/lan-mouse/blob/e46fe60b3e24be83de38701faa99a4fbd9186f08/src/emulation.rs#L179-L187

F1st3K avatar Oct 07 '25 17:10 F1st3K

I would prefer to fix this within lan-mouse. Technically you should be able to reconnect automatically? Does that not work?

feschber avatar Oct 07 '25 21:10 feschber

No, now it's not work. We don't have automatic rerun for it.

F1st3K avatar Oct 07 '25 23:10 F1st3K