Part XI.NTService

ProudNet 은 게임서버를 윈도우 서비스에 등록할 수 있는 간편한 API 모듈을 제공합니다.

윈도우 서비스는 오랜 시간 동작하며 특정한 기능을 수행하는 프로세스입니다. 보통 마이크로소프트 윈도우 운영체제가 시동될 때 실행되며 컴퓨터 사용자가 로그온 하지 않은 상태에서도 실행되는 특징이 있습니다. 그리고 윈도우가 실행되고 있는 한 백그라운드 모드에서 실행됩니다. 이것은 유닉스의 데몬과도 개념이 비슷합니다.

  1. 개요

    NTService.exe [-install | -uninstall]

  2. 옵션

    -install과 -uninstall 옵션은 기본적으로 윈도우 서비스 등록과 해제를 위한 것입니다.

  3. 실행화면

Windows Service에 등록된 샘플 프로그램

4. 등록모듈 작성 예제코드

본 예제와 관련된 윈도우용 소스 파일은 <설치 폴더>/Sample/NTService 를 참고하십시오.

#include "stdafx.h"
#include "../../include/ProudNetServer.h"
#include "../../include/NTService.h"
#include "conio.h"
#include <tchar.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;

        try{
            srv->Start(p1, err);
        }
        catch (Proud::Exception &e)
        {
            std::cout << "An Exception occured : " << e.what() << std::endl;
        }

        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. NTService 활용하기

Windows Service API 레퍼런스

2.4 NTService에서 오류덤프시스템(MiniDump) 구축하기