Wiki

Clone wiki

MNF_Example / MNF Basic Guide (TCP) / 1. Create Client

1. Purpose

This document will teach you how to create a unity client through MNF.

Next, Create a server.

2. Create Basic_1_Client Scene

스크린샷 2017-08-26 오전 11.18.00.png

3. Create Basic_1_ClientSession class

Create a Basic_1_ClientSession class that inherits from the MNF.JsonSession class for communication with the server.

Then, it creates three functions that receive connection success, connection failure, and connection termination event.

JsonSession is the default session class defined in MNF.

This class allows you to communicate with the server in a Json-style message.

#!c#
/**
 * @brief Client to communicate with server.
 * @details
 * It can handle events such as client connection, connection failure, connection termination, etc., and can store the client's state.
 * @ref JsonSession
 */
public class Basic_1_ClientSession : JsonSession
{
    /**
     * @brief Called when Basic_1_ClientSession is successfully connected to the server.
     * @return Returns 0 if it is normal, otherwise it returns an arbitrary value.
     */
    public override int OnConnectSuccess()
    {
         LogManager.Instance.Write("onConnectSuccess : {0}:{1}", this.ToString(), this.GetType());
        return 0;
    }

    /**
     * @brief Called when Basic_1_ClientSession fails to connect to the server.
     * @return Returns 0 if it is normal, otherwise it returns an arbitrary value.
     */
    public override int OnConnectFail()
    {
        LogManager.Instance.Write("onConnectFail : {0}:{1}", this.ToString(), this.GetType());
        return 0;
    }

    /**
     * @brief Called when Basic_1_ClientSession is disconnected from the server.
     * @return Returns 0 if it is normal, otherwise it returns an arbitrary value.
     */
    public override int OnDisconnect()
    {
        LogManager.Instance.Write("onDisconnect : {0}:{1}", this.ToString(), this.GetType());
        return 0;
    }
}

4. Create BasicMessageDefine Class

BasicMessageDefine is a class that defines a message to be exchanged between a server and a client.

#!c#
/**
 * @brief The class that defines the message
 * @details The class that defines the messages to be exchanged between the server and the client.
 */
public class BasicMessageDefine
{
    // CLIENT_TO_SERVER
    public enum CS
    {
    }

    // SERVER_TO_CLIENT
    public enum SC
    {
    }
}

5. Create Basic_1_ClientMessageDispatcher Class

Basic_1_ClientSession is now a class that can exchange raw level messages (binary []) with the server.

However, since the raw level message is not immediately available, create a Basic_1_ClientMessageDispatcher class that can parse the message.

#!c#
/**
 * @brief Process the received message.
 * @details
 * Basic_1_ClientMessageDispatcher is a class that handles messages received by Basic_1_ClientSession.
 * Basic_1_ClientMessageDispatcher inherits the DefaultDispatchHelper class. 
 * The DefaultDispatchHelper class takes three template arguments.
 * @param Basic_1_ClientSession The first is the object to receive the message.
 * @param BasicMessageDefine The second is a class that contains a message class.
 * @param BasicMessageDefine.SC The third is the enum in which the message is defined.
 */
public class Basic_1_ClientMessageDispatcher
    : DefaultDispatchHelper<Basic_1_ClientSession, BasicMessageDefine, BasicMessageDefine.SC>
{
}

6. Create Basic_1_Client Class

Now We are almost ready to use the MNF Client. Next, we will create the MNF Client through Unity. Create an Empty GameObject in Unity and name it Basic_1_Client. Then create a Basic_1_Client.cs file.

스크린샷 2017-09-01 오전 9.45.15.png

In the Basic_1_Client.cs script, we will connect Basic_1_ClientMessageDispatcher and Basic_1_ClientSession to the MNF Client and connect to the server.

#!c#
using UnityEngine;
using MNF;

/**
 * @brief The class to be connected to the Unity GameObject.
 * @details Create an MNF client and connect to the server.
 */
public class Basic_1_Client : MonoBehaviour
{
    /**
     * @brief The function that is called first when the Unity app is executed.
     * @details Set the LogManager and TcpHelper, the core of MNF.
     */
    void Awake()
    {
        // LogManager is the log interface used by MNF.
        // The LogManager will log the logs through the UnityLogWriter class.
        LogManager.Instance.SetLogWriter(new UnityLogWriter());
        if (LogManager.Instance.Init() == false)
            Debug.Log("LogWriter init failed");

        // Really important !! When starting TcpHelper in Unity, isRunThread should be set to false.
        // Otherwise, your app can crash with multi-thread issues.
        if (TcpHelper.Instance.Start(isRunThread: false) == false)
        {
            LogManager.Instance.WriteError("TcpHelper.Instance.run() failed");
            return;
        }

        // When we pass Basic_1_ClientSession and Basic_1_ClientMessageDispatcher
        // as template parameter of TcpHelper.AsyncConnect function, MN_1 automatically
        // creates Basic_1_ClientSession object and connects to the server activated with IP: "127.0.0.1", Port: "10000".
        if (TcpHelper.Instance.AsyncConnect<Basic_1_ClientSession, Basic_1_ClientMessageDispatcher>("127.0.0.1", "10000") == null)
        {
            LogManager.Instance.WriteError("TcpHelper.Instance.AsyncConnect() failed");
            return;
        }
    }

    /**
     * @brief Called when the Basic_1_Client object is released from Unity.
     * @details MNF's LogManager and TcpHelper are also released here.
     */
    void Release()
    {
        Debug.Log("Application ending after " + Time.time + " seconds");
        LookAround.Instance.Stop();
        TcpHelper.Instance.Stop();
        LogManager.Instance.Release();
    }

    /**
     * @brief A function that is called every frame in Unity.
     * @details Basic_1_ClientSession can handle messages received by calling TcpHelper.Instance.dipatchNetworkInterMessage ().
     */
    void Update()
    {
        // The message received by Basic_1_ClientSession is managed as a queue inside the MNF,
        // and it is necessary to call the dipatchNetworkInterMessage () function to process the message loaded in the queue.
        TcpHelper.Instance.dipatchNetworkInterMessage();
    }

    void OnDestroy()
    {
        Release();
    }

    void OnApplicationQuit()
    {
        Release();
    }
}

Updated