LogoLogo
English
English
  • Diarkis Help Center
  • Overview of Diarkis
  • Getting Started
    • Diarkis Server Template
    • Diarkis Client SDK
    • Tutorials
      • 1. Launch Diarkis Server in Local Environment
      • 2. Perform Connectivity Check with Test Client
      • 3. Implement Custom Command
      • Connect to Server from Diarkis Client
    • Samples
  • Diarkis Modules
    • Room Module
      • Set Up Room Module on Server
      • Room Sample
        • room_broadcast
      • Utilizing Room Module from Client
      • Additional Features of Room
    • MatchMaker Module
      • Set Up MatchMaker Module on Server
    • Field Module
      • Set Up Field Module on Server
    • P2P Module
      • Set Up P2P Module on Server
      • P2P Sample
    • DM (Direct Message) Module
      • Set Up DM Module on Server
    • Notifier Module
      • Set Up Notifier Module on Server
    • Session Module
      • Set Up Session Module on Server
    • Group Module
      • Set Up Group Module on Server
  • Diarkis Server
    • Launch Diarkis Server in Cloud Environment
      • AWS
    • Launch Diarkis Server on Windows Environment
    • MARS Server
    • UDP Server
    • TCP Server
    • HTTP Server
    • Metrics API
    • Inter-server Communication - Mesh
  • Diarkis Client
    • Runtime Library
      • Diarkis RUDP
    • Diarkis Module
      • Initialization and Termination of Diarkis Module
      • Customization of Diarkis Module
      • Logging System of Diarkis Module
      • Migration
      • Threads of Diarkis
    • Samples
      • C++
        • room_broadcast
        • directmessage_simple
        • group_sample
        • matching_and_turn
        • matchmaker_ticket
        • p2p_rudp_sample
        • session_simple
      • Unreal Engine Plugin
        • FieldWalker
      • Unity Plugin
        • FieldWalker
          • HowToReplicatePosition.md
  • Diarkis Tools
    • Diarkis CLI
      • Procedures to Switch to Diarkis CLI v3
  • References
    • API Reference
    • Release Notes
      • v1.0
      • v1.0.1
      • v1.0.2
      • v1.0.3
      • v1.0.4
      • v1.0.5
      • v1.0.6
  • Support
    • License and Billing
Powered by GitBook
On this page
  • Customizing Each Feature of the Diarkis Module
  • Customizing DiarkisInterfaceBase
  • Important Notes

Was this helpful?

  1. Diarkis Client
  2. Diarkis Module

Customization of Diarkis Module

This page introduces parts of the code from the directmessage_simple sample and explains how to customize and use the Diarkis Module. The actual sample source code can be found in samples\directmessage_simple\directmessage_simple.cpp.

Customizing Each Feature of the Diarkis Module

You can integrate application-specific processing into the module you wish to use. For example, the application can recognize when someone joins a Room or leaves it, and update the UI accordingly. In directmessage_simple, the DM feature of the Diarkis Module is customized to determine whether a message has been received from another user at least once. To customize the features of the Diarkis Module, you inherit from the base class of each feature (DiarkisModuleNameBase). The DM feature is implemented in DiarkisDirectMessageBase, and in the sample, this class is inherited to implement the necessary processing. The following code is a partial implementation.

/*
 * Implementation for customizing the DirectMessage-related processing on the application side.
 * On... related callbacks will be triggered upon receiving DirectMessage-related data, allowing the application-specific processing to be implemented.
 * In this sample, the received messages are displayed on the console.
 */
class DirectMessageSimple : public DiarkisDirectMessageBase
{
public:
    DirectMessageSimple() : DiarkisDirectMessageBase() {}
    virtual ~DirectMessageSimple() {}

    bool IsAnyMessageReceived() const
    {
        return anyMessageReceived_;
    }
private:
    /**
     * @~english
     * @brief Get the callback event that is called when disconnecting DirectMessage notifications.
     * @details Triggered when a DirectMessage Disconnect notification (Push from the server) is sent.
     * @~
     */
    virtual void OnDisconnect(const DiarkisDirectMessageEventArgs& e) override
    {
        ...
    }

    /**
     * @~english
     * @brief Get the callback event that is called when a DirectMessage is sent from another remote user.
     * @details Triggered when a DirectMessage Message notification (Push from the server) is sent.
     * @~
     */
    virtual void OnMessage(const DiarkisDirectMessageEventArgs& e) override
    {
        ...
        anyMessageReceived_ = true;
    }
private:
    bool anyMessageReceived_ = false;
};

DiarkisDirectMessageBase primarily defines interfaces for using the DM feature and interfaces for receiving events. The interface for using the DM feature is used for purposes such as executing specific processing before invoking DM functions. On the other hand, the interface for receiving events allows implementing processing for notifications when events like receiving a message occur. In the sample implementation, DiarkisDirectMessageBase::OnMessage is overridden to save whether a message has been received via DM.

Customizing DiarkisInterfaceBase

When using the Diarkis Module, all instances of classes implementing each feature are managed by the DiarkisInterfaceBase class, and the creation of instances also takes place within DiarkisInterfaceBase. If you customize the base class, it is necessary to change the type of the instances generated within DiarkisInterfaceBase, so you implement a customized DiarkisInterfaceBase class by inheriting it. Below is a partial implementation of the sample.

/*
 * To replace modules for each feature managed internally by DirectInterface with application-specific ones, implement a class inheriting from DiarkisInterfaceBase.
 */
class DiarkisInterfaceDirectMessageSimple : public DiarkisInterfaceBase
{
public:
    ...

    void SetupDirectMessage(void)
    {
        if (tcpBase_ != nullptr && tcpBase_->Get() != nullptr)
        {
            // Create the customized class on the application side.
            if (dmBase_ == nullptr)
                dmBase_ = Diarkis::DiarkisAllocShared<DirectMessageSimple>();

            // Initialize the DirectMessage class and register callback events
            dmBase_->SetupTcp(tcpBase_->Get(), this->GetLoggerFactory());
        }

        if (udpBase_ != nullptr && udpBase_->Get() != nullptr)
        {
            // Create the customized class on the application side.
            if (dmBase_ == nullptr)
                dmBase_ = Diarkis::DiarkisAllocShared<DirectMessageSimple>();
            // Initialize the DirectMessage class and register callback events
            dmBase_->SetupUdp(udpBase_->Get(), this->GetLoggerFactory());
        }
    }

    std::shared_ptr<DirectMessageSimple> GetDirectMessage()
    {
        return std::static_pointer_cast<DirectMessageSimple>(GetDirectMessageBase());
    }
};

Instance generation for each feature is performed in DiarkisInterfaceBase::Setup.... Since instance generation for the DM feature is done in DarkisInterfaceBase::SetupDirectMessage(), this method is overridden to generate the user-customized DM feature class. Additionally, a method for obtaining instances of the DM feature with the customized type is added.

By using these customized classes, you can incorporate application-specific processing into the Diarkis Module and customize it.

std::shared_ptr<DiarkisInterfaceDirectMessageSimple> diarkis;

...

// Create a class inheriting from DiarkisInterface to access all features of Diarkis.
diarkis = Diarkis::DiarkisAllocShared<DiarkisInterfaceDirectMessageSimple>(uid);

...

std::shared_ptr<DirectMessageSimple> dm = diarkis->GetDirectMessage();

...

// A simple implementation for synchronizing timing.
// Start sending data from "1111" when the first message arrives from "2222."
while (!dm->IsAnyMessageReceived())
{
    std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}

Important Notes

Given the nature of Diarkis Module being provided as source code, it's also possible to make the same customization by directly modifying it without inheriting the base class. However, there's a possibility that the source code of Diarkis Module might undergo significant changes during version upgrades, making merges difficult. To avoid such situations, it is recommended to customize as described on this page.

PreviousInitialization and Termination of Diarkis ModuleNextLogging System of Diarkis Module

Last updated 2 months ago

Was this helpful?