{"id":1971,"date":"2018-10-26T10:27:36","date_gmt":"2018-10-26T02:27:36","guid":{"rendered":"https:\/\/damogame.cn:15443\/wordpress\/?p=1971"},"modified":"2018-10-26T10:27:36","modified_gmt":"2018-10-26T02:27:36","slug":"%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86%e7%9a%84demo","status":"publish","type":"post","link":"https:\/\/i007.cc\/wordpress\/archives\/1971","title":{"rendered":"\u5f02\u5e38\u5904\u7406\u7684demo"},"content":{"rendered":"<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">\/*\r\n            This file is part of: \r\n                NoahFrame\r\n            https:\/\/github.com\/ketoo\/NoahGameFrame\r\n   Copyright 2009 - 2018 NoahFrame(NoahGameFrame)\r\n   File creator: lvsheng.huang\r\n   \r\n   NoahFrame is open-source software and you can redistribute it and\/or modify\r\n   it under the terms of the License; besides, anyone who use this file\/software must include this copyright announcement.\r\n   Licensed under the Apache License, Version 2.0 (the \"License\");\r\n   you may not use this file except in compliance with the License.\r\n   You may obtain a copy of the License at\r\n       http:\/\/www.apache.org\/licenses\/LICENSE-2.0\r\n   Unless required by applicable law or agreed to in writing, software\r\n   distributed under the License is distributed on an \"AS IS\" BASIS,\r\n   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r\n   See the License for the specific language governing permissions and\r\n   limitations under the License.\r\n*\/\r\n\r\n#include \"NFComm\/NFNetPlugin\/NFCNet.h\"\r\n#include &lt;thread&gt;\r\n#include &lt;string&gt;\r\n#include \"NFComm\/NFLogPlugin\/easylogging++.h\"\r\n#if NF_PLATFORM != NF_PLATFORM_WIN\r\n#include &lt;unistd.h&gt;\r\n#include &lt;netdb.h&gt;\r\n#include &lt;arpa\/inet.h&gt;\r\n#include &lt;signal.h&gt;\r\n#include &lt;execinfo.h&gt;\r\n#endif\r\n#pragma comment(lib,\"ws2_32.lib\")\r\n#pragma comment(lib,\"NFNet_d.lib\")\r\n\r\nINITIALIZE_EASYLOGGINGPP\r\n\r\nclass TestServerClass\r\n{\r\npublic:\r\n    TestServerClass()\r\n    {\r\n        pNet = new NFCNet(this, &amp;TestServerClass::ReciveHandler, &amp;TestServerClass::EventHandler);\r\n        pNet-&gt;Initialization(1000, 8088);\r\n    }\r\n    virtual ~TestServerClass()\r\n    {\r\n        \r\n    }\r\n\r\n    void ReciveHandler(const NFSOCK nSockIndex, const int nMsgID, const char* msg, const uint32_t nLen)\r\n    {\r\n        std::string str;\r\n        str.assign(msg, nLen);\r\n\r\n        pNet-&gt;SendMsgWithOutHead(nMsgID, msg, nLen, nSockIndex);\r\n        std::cout &lt;&lt; \" fd: \" &lt;&lt; nSockIndex &lt;&lt; \" msg_id: \" &lt;&lt; nMsgID \/*&lt;&lt;  \" data: \" &lt;&lt; str*\/ &lt;&lt; std::endl;\r\n    }\r\n\r\n    void EventHandler(const NFSOCK nSockIndex, const NF_NET_EVENT e, NFINet* p)\r\n    {\r\n        std::cout &lt;&lt; \" fd: \" &lt;&lt; nSockIndex &lt;&lt; \" event_id: \" &lt;&lt; e &lt;&lt; \" thread_id: \" &lt;&lt; std::this_thread::get_id() &lt;&lt; std::endl;\r\n    }\r\n\r\n    void Execute()\r\n    {\r\n        pNet-&gt;Execute();\r\n    }\r\n\r\nprotected:\r\n    NFINet* pNet;\r\n};\r\n\r\n\r\n#if NF_PLATFORM == NF_PLATFORM_WIN\r\n\r\n#pragma comment( lib, \"DbgHelp\" )\r\nbool ApplicationCtrlHandler(DWORD fdwctrltype)\r\n{\r\n    switch (fdwctrltype)\r\n    {\r\n    case CTRL_C_EVENT:\r\n    case CTRL_CLOSE_EVENT:\r\n    case CTRL_BREAK_EVENT:\r\n    case CTRL_LOGOFF_EVENT:\r\n    case CTRL_SHUTDOWN_EVENT:\r\n    {\r\n        bExitApp = true;\r\n    }\r\n        return true;\r\n    default:\r\n        return false;\r\n    }\r\n}\r\n\r\nvoid CreateDumpFile(const std::string&amp; strDumpFilePathName, EXCEPTION_POINTERS* pException)\r\n{\r\n    \/\/Dump\r\n    HANDLE hDumpFile = CreateFile(strDumpFilePathName.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);\r\n\r\n    MINIDUMP_EXCEPTION_INFORMATION dumpInfo;\r\n    dumpInfo.ExceptionPointers = pException;\r\n    dumpInfo.ThreadId = GetCurrentThreadId();\r\n    dumpInfo.ClientPointers = TRUE;\r\n\r\n    MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hDumpFile, MiniDumpNormal, &amp;dumpInfo, NULL, NULL);\r\n\r\n    CloseHandle(hDumpFile);\r\n}\r\n\r\nlong ApplicationCrashHandler(EXCEPTION_POINTERS* pException)\r\n{\r\n    time_t t = time(0);\r\n    char szDmupName[MAX_PATH];\r\n    tm* ptm = localtime(&amp;t);\r\n\r\n    sprintf_s(szDmupName, \"%04d_%02d_%02d_%02d_%02d_%02d.dmp\",  ptm-&gt;tm_year + 1900, ptm-&gt;tm_mon + 1, ptm-&gt;tm_mday, ptm-&gt;tm_hour, ptm-&gt;tm_min, ptm-&gt;tm_sec);\r\n    CreateDumpFile(szDmupName, pException);\r\n\r\n    FatalAppExit(-1,  szDmupName);\r\n\r\n    return EXCEPTION_EXECUTE_HANDLER;\r\n}\r\n#endif\r\n\r\n#if NF_PLATFORM != NF_PLATFORM_WIN\r\nvoid CrashHandler(int sig) {\r\n    \/\/ FOLLOWING LINE IS ABSOLUTELY NEEDED AT THE END IN ORDER TO ABORT APPLICATION\r\n    \/\/el::base::debug::StackTrace();\r\n    \/\/el::Helpers::logCrashReason(sig, true);\r\n\r\n\r\n    LOG(FATAL) &lt;&lt; \"crash sig:\" &lt;&lt; sig;\r\n\r\n    int size = 16;\r\n    void * array[16];\r\n    int stack_num = backtrace(array, size);\r\n    char ** stacktrace = backtrace_symbols(array, stack_num);\r\n    for (int i = 0; i &lt; stack_num; ++i)\r\n    {\r\n        \/\/printf(\"%s\\n\", stacktrace[i]);\r\n        LOG(FATAL) &lt;&lt; stacktrace[i];\r\n    }\r\n\r\n    free(stacktrace);\r\n}\r\n#endif\r\n\r\nint main(int argc, char** argv)\r\n{\r\n    TestServerClass x;\r\n\r\n    while (1)\r\n    {\r\n        x.Execute();\r\n    }\r\n\r\n    return 0;\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\/* This file is part<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[],"tags":[119],"class_list":["post-1971","post","type-post","status-publish","format-standard","hentry","tag-03-"],"_links":{"self":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/1971","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/comments?post=1971"}],"version-history":[{"count":0,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/1971\/revisions"}],"wp:attachment":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/media?parent=1971"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/categories?post=1971"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/tags?post=1971"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}