UDP クライアントイベント - OnEcho

OnEchoは、Diarkis サーバクラスターに対するEcho要求への応答を受信したときに呼び出されます。UDPクライアントライブラリは、サーバとの接続を維持するために、一定の間隔でEchoパケットを自動的に送信を行います。Diarkis サーバクラスターはクライアントから一定期間Echoが送信されないと、自動的にクライアントとの接続を切断します。またEcho応答はRUDPを使用します。

DiarkisUdpBase.h

class DIARKIS_API DiarkisUdpBase
{
protected:
    void OnConnect(bool isReconnect);
    void OnConnect(bool isReconnect);
    void OnDisconnect(bool isReconnect);
    void OnResponse(const DiarkisResponseEventArgs& args, DiarkisTransportType tt);
    void OnPush(const DiarkisPushEventArgs& args, DiarkisTransportType tt);
    void OnEcho(const DiarkisUdpEchoEventArgs& args);
    void OnPing(const DiarkisUdpPingEventArgs& args);

    std::vector<std::function<void()>> eventUnsubscriptions;
}

DiarkisUdpBase.cpp

#define REG_EVENT(__EVENT__, __LAMBDA__) this->eventUnsubscriptions.push_back(__EVENT__->Register(__LAMBDA__))

void DiarkisUdpBase::Setup(std::shared_ptr<LoggerFactory> loggerFactory, std::shared_ptr<ILoggerBackend> loggerBackend)
{
   if (this->udp == nullptr)
   {
       this->udp.reset(DiarkisCreateUdp(200, 5000));
   }

   this->udp->SetLoggerFactory(loggerFactory);
   this->fileAndDebugLoggerBackend = loggerBackend;

   REG_EVENT(this->udp->GetConnectedEvent(), [this](void*, bool reconnect) { this->OnConnect(reconnect); });
   REG_EVENT(this->udp->GetDisconnectedEvent(), [this](void*, bool reconnect) { this->OnDisconnect(reconnect); });
   REG_EVENT(this->udp->GetResponseEvent(), [this](void*, const DiarkisResponseEventArgs& args) { this->OnResponse(args, DiarkisTransportType::UDP); });
   REG_EVENT(this->udp->GetPushEvent(), [this](void*, const DiarkisPushEventArgs& args) { this->OnPush(args, DiarkisTransportType::UDP); });
   REG_EVENT(this->udp->GetEchoEvent(), [this](void*, const DiarkisUdpEchoEventArgs& args) { this->OnEcho(args); });
   REG_EVENT(this->udp->GetPingEvent(), [this](void*, const DiarkisUdpPingEventArgs& args) { this->OnPing(args); });

}
void DiarkisUdpBase::OnEcho(const DiarkisUdpEchoEventArgs& args)
{
    // echoTime is the time it took to send and return echo. It is in milliseconds.
    // clientPublicAddr is available if the UDP server enables P2P.
}

最終更新