# 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 %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.proudnet.com/proudnet.eng/proudnet/using_pn/eventhandling.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
