> For the complete documentation index, see [llms.txt](https://easyauth.papelship.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://easyauth.papelship.com/csharp-document/main-functions/heartbeat.md).

# Heartbeat

The Heartbeat subsystem maintains an active, cryptographically signed telemetry link between the client application and the authentication server while the software runs.

## Technical Mechanism

Once authentication completes, a dedicated native worker thread initiates continuous heartbeat pulses:

```mermaid
sequenceDiagram
    participant Client as Client Application
    participant Server as EasyAuth Server
    
    loop Every N Seconds (Heartbeat Interval)
        Client->>Server: Encrypted Heartbeat (Session Token, Status)
        Server->>Server: Validate Session Integrity & Check Expiration
        Server-->>Client: Heartbeat Response (Active Status, Time Sync)
    end
```

## Key Responsibilities

1. **Active Session Tracking**: Notifies the server that the client process is alive and active.
2. **Dynamic Ban Enforcement**: If an administrator terminates or bans a license from the web panel, the next heartbeat pulse receives a termination command and closes the client instantly.
3. **Concurrent Session Control**: Prevents multiple users from sharing a single key simultaneously across different machines.
4. **Memory Hash Verification**: Each pulse includes a dynamic rolling CRC32 hash of process memory sections.

## C# Implementation

Heartbeat pulses are managed automatically by the native core. You can configure the interval and handle connection events:

```csharp
using System;
using qPapelEasyAuth;

public class SessionMonitor
{
    public static void ConfigureWatchdog()
    {
        var config = new EasyAuthConfig
        {
            // Heartbeat & watchdog scan interval (default: 500 ms)
            WatchdogIntervalMs = 500,
            Flags = ProtectionFlags.All
        };

        qPapelEasyAuth.qPapelEasyAuth.SetConfig(config);
    }

    public static void VerifyActiveSession()
    {
        // Check if connection to server remains healthy
        bool isConnected = qPapelEasyAuth.qPapelEasyAuth.IsConnected();
        
        if (!isConnected)
        {
            Console.WriteLine("[-] Lost connection to authentication server.");
        }
    }
}
```

## Server Heartbeat Actions

| Server Action   | Cause                              | Client Response                          |
| --------------- | ---------------------------------- | ---------------------------------------- |
| `HEARTBEAT_ACK` | Healthy active session             | Session continues normally.              |
| `FORCE_CLOSE`   | Session expired or closed by admin | Process terminates gracefully.           |
| `BAN_HWID`      | Tamper or memory hook detected     | HWID banned, process killed immediately. |
| `SYNC_TIME`     | Clock drift detected               | Authoritative server timestamp updated.  |

{% hint style="info" %}
If network connectivity drops temporarily, the client will retry up to a configured threshold before safely terminating protected routines.
{% endhint %}
