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

OnOfflineは、接続されたサーバーがオフラインになる準備をしているときに呼び出されます。このイベントが発生した場合、クライアントは現在の接続を終了し、新しい接続を再確立する必要があります。

DiarkisUdpBase.h

class DIARKIS_API DiarkisUdpBase
{
protected:
    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);
    void OnOffline(void);

    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); });
   REG_EVENT(this->udp->GetOfflineEvent(), [this](void*) { this->OnOffline(void); });
}
void DiarkisUdpBase::OnOffline(void)
{
    // We set a flag, so we can perform re-connection in OnDisconnect event listener
    reconnectFlag = true;

    // This is raise OnDisconnect event
    this->udp->Disconnect();
}

// bool reconnecting is set to true when you use udp.Reconnect()
void DiarkisUdpBase::OnDisconnect(bool isReconnect)
{
    if (this->reconnectFlag)
    {
        // Call the application server to indirectly obtain a new endpoint here
        // Assuming we have the new endpoint, we proceed with reconnect
        this->udp->SetEncryptionKeys(newSid, newKey, newIv, newMacKey);
        this->udp->Connect(newAddr, newPort);
    }
}

最終更新