Wiki

Clone wiki

MNF_Example / MNF Basic Guide (TCP) / 2. Create Server

1. Purpose

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

2. Create Basic_1_Server Scene

스크린샷 2017-08-31 오전 9.50.35.png

3. Create Basic_1_ServerSession class

Create a Basic_1_ServerSession class that inherits from the MNF.JsonSession class for communication with the client.

Then, it creates twe functions that receive accept success, and connection termination event.

JsonSession is the default session class defined in MNF.

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

#!c#
using MNF;

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

    /**
     * @brief Called when Basic_1_ServerSession is disconnected from the client.
     * @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 Basic_1_ServerMessageDispatcher Class

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

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

#!c#
using MNF;

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

5. Create Basic_1_Server Class

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

스크린샷 2017-09-01 오전 9.44.52.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 server and listen, bind, accept for client.
 */
public class Basic_1_Server : 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;
        }

        // If Basic_1_ServerSession and Basic_1_ServerMessageDispatcher are passed as template parameters of the TcpHelper.StartAccept function, 
        // MNF will automatically create a Basic_1_ServerSession object when Basic_1_ClientSession contacts the server.
        // The Basic_1_Server server should be connected with IP: "127.0.0.1" and Port: "10000".
        if (TcpHelper.Instance.StartAccept<Basic_1_ServerSession, Basic_1_ServerMessageDispatcher>(
            "127.0.0.1", "10000", 500) == false)
        {
            LogManager.Instance.WriteError("TcpHelper.Instance.StartAccept<Basic_1_ServerSession, Basic_1_ServerMessageDispatcher>() failed");
            return;
        }
    }

    /**
     * @brief Called when the Basic_1_Server 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_ServerSession can handle messages received by calling TcpHelper.Instance.dipatchNetworkInterMessage ().
     */
    void Update()
    {
        // The message received by Basic_1_ServerSession 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