ketoo / NoahGameFrame

A fast, scalable, distributed game server engine/framework for C++, include the actor library, network library, can be used as a real time multiplayer game engine ( MMO RPG/MOBA ), which support C#/Lua script/ Unity3d, Cocos2dx and plan to support Unreal.
https://github.com/ketoo/NoahGameFrame/wiki
Apache License 2.0
3.97k stars 1.09k forks source link

How to stop it friendly #260

Open yangcancai opened 4 years ago

yangcancai commented 4 years ago

I try to stop NFServer friendly but it not work, The callback function "ApplictionCtrlHandler" will be HOOK some exit event for window but it do nothing work. so it is any plant to make a command plugin to control the NFServer. Thank you very much!

ketoo commented 4 years ago

Good question.

A friend way I knew is to catch the system signal to exit the process.

The code listed below:

void releaseYourMemory()
{
    //TODO release your memory
}

void func1()
{
    releaseYourMemory();

    exit(0);
}

void func2()
{
    releaseYourMemory();

    exit(0);
}

void func3()
{
    releaseYourMemory();

    exit(0);
}

void main()
{

    signal (SIGINT, func1);

    signal(SIGKILL, func0);

    signal(SIGSEGV, func3);

    signal(SIGTERM, func2);

    while (1)
    {
        //your loop
    }

}

Welcome to post your PR after you have tested it.

Thanks.

sniper00 commented 4 years ago

This is not the right way, in *unix system only some c api can called in signal handler

发自我的iPhone

在 2020年7月11日,上午5:18,kytooooo notifications@github.com 写道:



Good question.

A friend way I knew is to catch the system signal to exit the process.

The code below:

` void releaseYourMemory() { //TODO release your memory }

void func1() { releaseYourMemory();

exit(0);

}

void func2() { releaseYourMemory();

exit(0);

}

void func3() { releaseYourMemory();

exit(0);

}

void main() {

signal (SIGINT, func1);

signal(SIGKILL, func0);

signal(SIGSEGV, func3);

signal(SIGTERM, func2);

while (1) { //your loop }

} `

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHubhttps://github.com/ketoo/NoahGameFrame/issues/260#issuecomment-656895249, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ADDQZK2GYLE3BI4DD7SHYLTR26AQXANCNFSM4OVN7NHA.

sniper00 commented 4 years ago

The routine handler must be very careful, since processing elsewhere was interrupted at some arbitrary point. POSIX has the concept of "safe function". If a signal interrupts an unsafe function, and handler calls an unsafe function, then the behavior is undefined. Safe functions are listed explicitly in the various standards. The POSIX.1-2003 list is

_Exit() _exit() abort() accept() access() aio_error() aio_return() aio_suspend() alarm() bind() cfgetispeed() cfgetospeed() cfsetispeed() cfsetospeed() chdir() chmod() chown() clock_gettime() close() connect() creat() dup() dup2() execle() execve() fchmod() fchown() fcntl() fdatasync() fork() fpathconf() fstat() fsync() ftruncate() getegid() geteuid() getgid() getgroups() getpeername() getpgrp() getpid() getppid() getsockname() getsockopt() getuid() kill() link() listen() lseek() lstat() mkdir() mkfifo() open() pathconf() pause() pipe() poll() posix_trace_event() pselect() raise() read() readlink() recv() recvfrom() recvmsg() rename() rmdir() select() sem_post() send() sendmsg() sendto() setgid() setpgid() setsid() setsockopt() setuid() shutdown() sigaction() sigaddset() sigdelset() sigemptyset() sigfillset() sigismember() signal() sigpause() sigpending() sigprocmask() sigqueue() sigset() sigsuspend() sleep() socket() socketpair() stat() symlink() sysconf() tcdrain() tcflow() tcflush() tcgetattr() tcgetpgrp() tcsendbreak() tcsetattr() tcsetpgrp() time() timer_getoverrun() timer_gettime() timer_settime() times() umask() uname() unlink() utime() wait() waitpid() write().

https://www.tutorialspoint.com/unix_system_calls/signal.htm