Skip to content

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

@F1st3K

Description

@F1st3K

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions