How to handle an event handler

The server and the client all do event handling. The event handling of Proud is made in a delegate pattern.

If you want to do an event handling, you need to implement by inheriting Proud.INetClientEvent in the client. In the server, you need to implement by inheriting Proud.INetServerEvent class. The two classes are inheriting Proud.INetCoreEvent class. This also needs to be implemented.

And then after making an instance of the implemented class, it must be connected through Proud.CNetServer.SetEventSink in the server and Proud.CNetClient.SetEventSink in the client.

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

If you use a complier that supports C++11, it is better to use the Lambda expression, instead of inheriting a class as shown above. You can do more concise programming.

myNetServer->OnJoinServerComplete = [&](ErrorInfo *info, const ByteArray &replyFromServer)
{
// my event handler
};

For the time you are able to receive an event callback through the above steps, please refer to Understanding server main loop and Understanding client main loop.

event_sink.png
connecting an event handler from a client

Typical events that are generated in the server are ‘getting connection from the client’, etc. and the typical events that are generated in the client are ‘connection successful to the server’, ‘participation of a P2P member’, etc.

Tip: Log processing for an event is essential.

The most common problem in technical support is not being able to find a cause due to not processing the log. Especially for the functions where errorInfo is added as a parameter and the events below must be log processed.

  • OnError: it calls back the errors occurring inside ProudNet or the information on the problems during use.
  • OnWarning: it calls back the information that has potential problems, although not severe.
  • OnInformation: it calls back the information on the situation inside, tracing, etc.
  • OnException: it calls back internal exception errors.

If you use errorInfo->ToString() of errorInfo, which is a parameter, you can easily get information on any problem.