TCP カスタムメッセージの送信方法

Diarkis サーバにカスタムコマンドを実装し、それらのコマンドを呼び出すことができます。

// version : command version
// command : command ID
// payload : payload data
// payloadSize : payload data size

tcp->Send(version, command, payload, payloadSize);

DiarkisTcpBase.h

class DIARKIS_API DiarkisTcpBase
{
protected:
    void OnConnect(bool isReconnect);
    void OnResponse(const DiarkisResponseEventArgs& args, DiarkisTransportType tt);
    void OnPush(const DiarkisPushEventArgs& args, DiarkisTransportType tt);
    void OnHeartbeat();
    void OnOffline();

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

DiarkisTcpBase.cpp

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

void DiarkisTcpBase::Setup(std::shared_ptr<LoggerFactory> loggerFactory, std::shared_ptr<ILoggerBackend> loggerBackend)
{
   if (this->tcp == nullptr)
   {
       this->tcp.reset(DiarkisCreateTcp( 5000));
   }

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

   REG_EVENT(this->tcp->GetConnectedEvent(), [this](void*, bool isReconnect) { this->OnConnect(isReconnect); });
   REG_EVENT(this->tcp->GetResponseEvent(), [this](void*, const DiarkisResponseEventArgs& args) { this->OnResponse(args, DiarkisTransportType::TCP); });
   REG_EVENT(this->tcp->GetPushEvent(), [this](void*, const DiarkisPushEventArgs& args) { this->OnPush(args, DiarkisTransportType::TCP); });
   REG_EVENT(this->tcp->GetHeartbeatEvent(), [this](void*, void*) { this->OnHeartbeat(); });
   REG_EVENT(this->tcp->GetOfflineEvent(), [this](void*, void*) { this->OnOffline(); });

}

TCP クライアント - OnConnect

接続が正常に確立されると、OnConnect が呼び出されます。

void DiarkisTcpBase::OnConnect(bool isReconnect)
{
    // A connection is successfully established. Now you may interactive wit the server cluster
}

TCP クライアント - OnHeartbeat

TCPクライアントは、接続を維持するために、特定の間隔(デフォルトでは5秒ごと)でハートビートパケットを送受信します。OnHearbeatは、クライアントがサーバクラスターから応答を受信したときに呼び出されます。

void DiarkisTcpBase::OnHeartbeat()
{
   // Do something if you would like to
}

TCP クライアント - OnOffline

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

void DiarkisTcpBase::OnOffline(void)
{
    // We set a flag, so we can perform re-connection in OnDisconnect event listener
    reconnectFlag = true;

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

// bool reconnecting is set to true when you use tcp->Reconnect()
void DiarkisTcpBase::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->tcp->SetEncryptionKeys(newSid, newKey, newIv, newMacKey);
        this->tcp->Connect(newAddr, newPort);
    }
}

TCP クライアント - OnDisconnect

OnDisconnectは、クライアントがDiarkisサーバクラスターから正常に切断されたときに呼び出されます。

void DiarkisTcpBase::OnDisconnect(bool isReconnect)
{
    // bool reconnecting is set to true only when tcp->Reconnect() is used.
}

TCP クライアント - OnPush

OnPushは、クライアントがDiarkisサーバークラスターからプッシュとしてパケット(クライアントから要求されていないパケット)を受信したときに呼び出されます。

void DiarkisTcpBase::OnPush(const DiarkisPushEventArgs& args, DiarkisTransportType tt)
{
    uint8_t version = args.GetVersion();
    uint16_t command = args.GetCommand();
    const vector<uint8_t>& payload = args.GetPayload();

    if (version == 2 && command == Commands::CustomCommand)
    {
        // CustomCommand Process
    }
}

最終更新