Part XI.NTService

ProudNet provides a simple API module that can register a game server in Windows service.

Windows service is a process that conducts specific functions and is operated for a long time. Windows service is operated when Microsoft Windows operating system is booted up and it can be operated even if a user does not log on. And as long as Windows is operated, it is operated at a background mode. This is similar with the concept of Unix daemon.

  1. Overview

    NTService.exe [-install | -uninstall]

  2. Option

    Options such as -install and -uninstall are for registration & termination of Windows service.

  3. Execution Screen

A Sample Program registered at Windows Service

4. Example code of making registration module

Please refer to <Installation Folder>/Sample/NTService for Windows source files related to this example.

#include "stdafx.h"
#include "../../include/ProudNetServer.h"
#include "../../include/NTService.h"
#include "conio.h"

using namespace Proud;
using namespace std;

int g_ServerPort = 33334;

class CMySvrEvent: public INTServiceEvent
{
public:
    virtual void Log(int type, LPCWSTR text)
    {
        _tprintf(L"%s\n", text);
    }

    virtual void Pause()
    {
        // 윈도우 서비스 일시 중지시 게임서버 프로세스를 일시 중지시킬 수 있는 코드를 작성합니다.
        // When Wndows service is stopped, design a code that can stop a game server process.
        // 编写在Windows服务暂停时暂停游戏引擎Process的代码。
        // ウィンドウサービス一時停止の時、ゲームサーバープロセスを一時停止させるコードを作成します。
        printf("pause");
    }

    virtual void Stop()
    {
        // 윈도우 서비스 중지시 게임서버 프로세스를 종료시킬 수 있는 코드를 작성합니다.
        // When Wndows service is stopped, design a code that can shut down a game server process.
        // 编写在Windows服务终了时终了游戏引擎Process的代码。
        // ウィンドウサービス停止の時、ゲームサーバープロセスを終了させるコードを作成します。
        printf("stop");
    }

    virtual void Continue()
    {
        // 일시 중지된 게임서버를 다시 실행시킬 수 있는 코드를 작성합니다.
        // Design a code that can re-execute a game server that has been temporarily stopped.
        // 编写可以使暂停的游戏引擎重新运行的代码。
        // 一時停止されたゲームサーバーを再び実行させるコードを作成します。
        printf("continue");
    }

    virtual void Run()
    {
        // 윈도우 서비스 시작과 동시에 게임서버를 수행시킬 수 있는 코드를 작성합니다.
        // At the same time when Windows service gets started, design a code that can execute a game server.
        // 编写Windows服务启动的同时运行游戏引擎的代码。
        // ウィンドウサービススタートと同時にゲームサーバーを随行できるコードを作成します
        CNetServer* srv = CNetServer::Create();
        CStartServerParameter p1;
        p1.m_tcpPorts.Add(g_ServerPort);

        ErrorInfoPtr err;

        bool result = srv->Start(p1, err);
        if (result == false)
        {
            printf("Server Start Error: %s\n",
                StringW2A(ErrorInfo::TypeToString_Kor(err->m_errorType)));
            delete srv;
            return;
        }

        puts("Game Server started.\n");

        while (1)
        {
            Sleep(100);
            if (_kbhit())
            {
                int ch = _getch();
                switch (ch)
                {
                case 27:
                    return;
                }
            }

            MSG msg;

            // 최대 일정 짧은 시간동안, 콘솔 입력, 윈도 메시지 수신, 메인 스레드 종료 중 하나를 기다린다.
            // Wait for one of console input, Windows message receiving or main thread shutting down in a short period of time.
            // 等待最短时间, 输入Console, 接收Windows消息, 终了主线程中一个。
            // 最大日程短い時間の間、コンソール入力、ウィンドウメッセージ受信、メインスレッド終了中一つを待ちます。
            MsgWaitForMultipleObjects(0, 0, TRUE, 100, QS_ALLEVENTS);
            if (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
            {
                if (!GetMessage(&msg, NULL, NULL, NULL))
                    break;
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
        }
    }
}

CMySvrEvent g_svrEvent;

int wmain(int argc, WCHAR* argv[], WCHAR* envp[])
{
    int nRetCode = 0;

    CNTServiceStartParameter param;
    param.m_serviceName = L"ProudNet NT Service Sample";
    param.m_serviceEvent = &g_svrEvent;

    CNTService::WinMain(argc, argv, envp, param);

    return nRetCode;
}

5. Utilization of NTService

Windows Service API Reference

The internal link is invalid.