Skip to content

Commit

Permalink
Add signal support
Browse files Browse the repository at this point in the history
  • Loading branch information
bha-evs committed Mar 18, 2024
1 parent d38ebc4 commit 0005b0d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 8 deletions.
12 changes: 12 additions & 0 deletions lib/include/bofstd/bofstd.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ cmake -DCMAKE_TOOLCHAIN_FILE=C:/pro/vcpkg/scripts/buildsystems/vcpkg.cmake -DBUI
#include <string.h>
#include <string>

#if defined(_WIN32)
#define NOMINMAX
#include <WinSock2.h>
#include <ws2tcpip.h>
#include <Windows.h>
typedef bool(WINAPI *BofSignalHandler)(uint32_t);
#else
typedef bool (*BofSignalHandler)(uint32_t);
#endif
using BofAssertCallback = std::function<BOFERR(const std::string &_rFile_S, uint32_t _Line_U32, const std::string &_rMasg_S)>;
#if defined(__EMSCRIPTEN__)
using BofEmscriptenCallback = std::function<BOFERR(void *)>;
Expand All @@ -57,6 +66,7 @@ struct BOFSTDPARAM
// Input Param
bool AssertInRelease_B;
BofAssertCallback AssertCallback;
BofSignalHandler SignalHandler;
#if defined(__EMSCRIPTEN__)
BofEmscriptenCallback EmscriptenCallback;
void *pEmscriptenCallbackArg;
Expand All @@ -78,6 +88,7 @@ struct BOFSTDPARAM
{
AssertInRelease_B = false;
AssertCallback = nullptr;
SignalHandler = nullptr;
#if defined(__EMSCRIPTEN__)
EmscriptenCallback = nullptr;
EmscriptenCallbackFps_U32 = 0;
Expand Down Expand Up @@ -702,6 +713,7 @@ class BOFSTD_EXPORT BofException : public std::exception
BOF::BofException Exception(Header, Context, Where.str(), ErrorCode); \
throw Exception; \
}
//extern BOFSTDPARAM GL_BofStdParam_X;

END_BOF_NAMESPACE()

Expand Down
53 changes: 48 additions & 5 deletions lib/src/bofstd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,8 @@ cmake -DCMAKE_TOOLCHAIN_FILE=/home/bha/pro/github/vcpkg/scripts/buildsystems/vcp
#if defined(__EMSCRIPTEN__)
#include <emscripten.h>
#endif
// Just to check: see std::string Bof_GetVersion()
// #include <libavutil/avutil.h>
// #include <openssl/crypto.h>
// #include <boost/version.hpp>

#if defined(_WIN32)
DWORD S_ModeIn_DW = 0, S_ModeOut_DW = 0;
#else
#include <signal.h>
#include <sys/ioctl.h>
Expand All @@ -103,6 +98,41 @@ DWORD S_ModeIn_DW = 0, S_ModeOut_DW = 0;
BOFSTD_EXPORT BOF::BOFSTDPARAM GL_BofStdParam_X;

BEGIN_BOF_NAMESPACE()

#if defined(_WIN32)
static BOOL WINAPI S_SignalHandler(DWORD _CtrlType_DW)
{
BOOL Rts_B = false;
//For all if (_CtrlType_DW == CTRL_C_EVENT || _CtrlType_DW == CTRL_BREAK_EVENT)
{
if (GL_BofStdParam_X.SignalHandler != nullptr)
{
Rts_B = GL_BofStdParam_X.SignalHandler(_CtrlType_DW);
}
}
return Rts_B;
}
#else
static void S_SignalHandler(int _Signal_i)
{
if (GL_BofStdParam_X.SignalHandler != nullptr)
{
GL_BofStdParam_X.SignalHandler(_Signal_i);
}
}
#endif

static bool S_DefaultBofSignalHandler(uint32_t _Signal_U32)
{
Bof_Shutdown();
exit(_Signal_U32);
return true;
//return (Bof_Shutdown() == BOF_ERR_NO_ERROR);
}
#if defined(_WIN32)
DWORD S_ModeIn_DW = 0, S_ModeOut_DW = 0;
#else
#endif
static char S_pUnknownError_c[128];
static std::map<BOFERR, const char *> S_ErrorCodeCollection{
{BOF_ERR_NO_ERROR, "BOF_ERR_NO_ERROR: No error"},
Expand Down Expand Up @@ -398,12 +428,19 @@ BOFERR Bof_Initialize(BOFSTDPARAM &_rStdParam_X)
GL_BofStdParam_X = _rStdParam_X;
GL_BofDbgPrintfStartTime_U32 = Bof_GetMsTickCount();

if (GL_BofStdParam_X.SignalHandler == nullptr)
{
GL_BofStdParam_X.SignalHandler = S_DefaultBofSignalHandler;
}
Rts_E = BofSocket::S_InitializeStack();
/* Set the locale to the POSIX C environment */
setlocale(LC_ALL, "C");
_rStdParam_X.ComputerName_S = Bof_GetHostName();;

#if defined(_WIN32)
// Register signal handler for Ctrl+C and Ctrl+Break events
SetConsoleCtrlHandler(S_SignalHandler, TRUE);

OSVERSIONINFOEX osVersionInfo;
ZeroMemory(&osVersionInfo, sizeof(OSVERSIONINFOEX));
osVersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
Expand Down Expand Up @@ -445,6 +482,12 @@ BOFERR Bof_Initialize(BOFSTDPARAM &_rStdParam_X)
}
}
#else
signal(SIGINT, S_SignalHandler);
signal(SIGQUIT, S_SignalHandler);
signal(SIGTERM, S_SignalHandler);
signal(SIGKILL, S_SignalHandler);
signal(SIGABRT, S_SignalHandler);

struct utsname Si_X;

_rStdParam_X.OsName_S = "Linux";
Expand Down
6 changes: 3 additions & 3 deletions lib/src/bofsystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ extern "C"

#if defined(_WIN32)
#include <Sddl.h>
#include <Winsock2.h>
//#include <Winsock2.h>
#include <conio.h>
#include <windows.h>
//#include <windows.h>

PSECURITY_DESCRIPTOR CreateWinSecurityDescriptor(SECURITY_ATTRIBUTES *_pSecurityAttribute_X)
{
Expand Down Expand Up @@ -150,7 +150,7 @@ PSECURITY_DESCRIPTOR CreateWinSecurityDescriptor(SECURITY_ATTRIBUTES *_pSecurity

#else
#include <fcntl.h>
#include <signal.h>
//#include <signal.h>
#include <sys/mman.h>
#include <sys/resource.h>
#include <sys/shm.h>
Expand Down

0 comments on commit 0005b0d

Please sign in to comment.