> For the complete documentation index, see [llms.txt](https://docs.proudnet.com/proudnet.eng/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.proudnet.com/proudnet.eng/proudnet/using_pn/eventhandling.md).

# Event handling

## Working with event handlers

ProudNet's event handling is in the delegate pattern.

The client implements it by inheriting from class <mark style="color:orange;">Proud.INetClientEvent</mark>, and the server implements it by inheriting from class <mark style="color:orange;">Proud.INetServerEvent</mark>.\
After creating instances of the classes you implemented, you need to associate them with <mark style="color:orange;">Proud.CNetServer.SetEventSink</mark> on the server and <mark style="color:orange;">Proud.CNetClient.SetEventSink</mark> on the client.

{% tabs %}
{% tab title="C++" %}

```cpp
class MyEvent:public INetClientEvent
{
    virtual void OnJoinServerComplete(...) override 
    { 
        // my event handler
        ...
    }
}
 
MyEvent m_myEvent;
myNetServer->SetEventSink(&m_myEvent); 
```

{% endtab %}
{% endtabs %}

{% hint style="info" %}
If you are using a compiler that supports **C++11,** \
you can do more concise programming by using lambda expressions instead of inheriting the class above.
{% endhint %}

{% tabs %}
{% tab title="C++" %}

```cpp
myNetServer->OnJoinServerComplete = [...](...) 
    {
        // my event handler
        ...
    };
```

{% endtab %}

{% tab title="C#" %}

```csharp
myNetServer.OnJoinServerComplete = () =>
    {
        // my event handler
        ...
    };
```

{% endtab %}
{% endtabs %}

See [**About main loop**](/proudnet.eng/proudnet-note/notes/main_loop.md) or more information on when you can receive event callbacks from the above process.

<figure><img src="/files/xQwlZ3ZvOKQq430y1yK5" alt=""><figcaption><p>Linking event handlers on the client</p></figcaption></figure>

Typical events that can occur on the server include "receiving a connection from a client" and typical events that occur on the client include "establishing a connection with the server" and "joining a P2P member".

{% hint style="danger" %}
**Make sure to Log the Event.**

During technical support, the most common problem is that it is difficult to find the cause of the problem because it is not logged. In particular, be sure to log the following events and functions that take <mark style="color:orange;">errorInfo</mark> as a parameter.

* <mark style="color:orange;">OnError</mark>: Callbacks information about errors or usage issues that occur inside ProudNet.
* <mark style="color:orange;">OnWarning</mark>: Callback information that is not critical but potentially problematic.
* <mark style="color:orange;">OnInformation</mark>: Callback information about the internal situation, tracking, etc.
* <mark style="color:orange;">OnException</mark>: Callback internal Exception error information.

You can easily get information about the problem by using <mark style="color:orange;">errorInfo -> ToString();</mark> in the parameter <mark style="color:orange;">errorInfo</mark>.
{% endhint %}
