diff --git a/CellFrame/python-cellframe.c b/CellFrame/python-cellframe.c index 82f30495318eabf145a310ae2b2b3f665e423ca9..78217fe04fa6af54b2115f55b424e1d531a30df2 100644 --- a/CellFrame/python-cellframe.c +++ b/CellFrame/python-cellframe.c @@ -21,9 +21,32 @@ static bool s_init_cs_dag_pos = false; static bool s_init_chain_net_srv = false; static bool s_init_ks = false; +static bool submodules_deint; + PyObject* CellFrame_error = NULL; +#ifdef _WIN32 + +BOOL WINAPI consoleHandler(DWORD dwType){ + if (dwType == CTRL_C_EVENT){ + log_it(L_NOTICE, "Handler Ctrl+C"); + dap_server_loop_stop(); + deinit_modules(); + } + return TRUE; +} +#else + +void sigfunc(int sig){ + if (sig == SIGINT){ + log_it(L_NOTICE, "Handler Ctrl+C"); + dap_server_loop_stop(); + deinit_modules(); + } +} +#endif + PyObject *python_cellframe_init(PyObject *self, PyObject *args){ const char *app_name; const char *file_name_log; @@ -32,6 +55,13 @@ PyObject *python_cellframe_init(PyObject *self, PyObject *args){ const char *JSON_str; s_init_ks = true; + submodules_deint = false; + + #ifdef _WIN32 + setConsoleCtrlHandler((PHANDLER_ROUTINE)consoleHandler, TRUE); + #else + signal(SIGINT, sigfunc); + #endif if (!PyArg_ParseTuple(args, "s", &JSON_str)){ PyErr_SetString(CellFrame_error, "ERROR in function call signature: can't get one String argument"); @@ -463,32 +493,39 @@ PyMODINIT_FUNC PyInit_libCellFrame(void){ return module; } -PyObject *python_cellframe_deinit(PyObject *self, PyObject *args){ - if (s_init_crypto){ - dap_crypto_deinit(); - dap_cert_deinit(); - } - if (s_init_chain){ - deinit_chain_py(); - dap_chain_cs_deinit_py(); - } - if (s_init_stream){ - dap_stream_deinit(); - } - if (s_init_stream_ctl){ - dap_stream_ctl_deinit(); - } - if (s_init_http_folder){ - dap_http_folder_deinit(); - } - if (s_init_http){ - dap_http_deinit(); - } - if (s_init_server_core){ - dap_server_core_deinit(); - } - if (s_init_ks){ - dap_enc_ks_deinit(); +void deinit_modules(void){ + if (!submodules_deint){ + log_it(L_NOTICE, "Start deint submodules"); + if (s_init_crypto){ + dap_crypto_deinit(); + dap_cert_deinit(); + } + if (s_init_chain){ + deinit_chain_py(); + dap_chain_cs_deinit_py(); + } + if (s_init_stream){ + dap_stream_deinit(); + } + if (s_init_stream_ctl){ + dap_stream_ctl_deinit(); + } + if (s_init_http_folder){ + dap_http_folder_deinit(); + } + if (s_init_http){ + dap_http_deinit(); + } + if (s_init_server_core){ + dap_server_core_deinit(); + } + if (s_init_ks){ + dap_enc_ks_deinit(); + } + dap_config_close(g_config); + dap_config_deinit(); + dap_common_deinit(); + submodules_deint = true; } if (s_init_wallet){ dap_chain_wallet_deinit_py(); @@ -496,6 +533,10 @@ PyObject *python_cellframe_deinit(PyObject *self, PyObject *args){ dap_config_close(g_config); dap_config_deinit(); dap_common_deinit(); +} + +PyObject *python_cellframe_deinit(PyObject *self, PyObject *args){ + deinit_modules(); return PyLong_FromLong(0); } diff --git a/cellframe-sdk b/cellframe-sdk index f94212973623702bff498780fc2d0426e595d5ec..c833958ae5e623292aa1d54617548cb4a42f9525 160000 --- a/cellframe-sdk +++ b/cellframe-sdk @@ -1 +1 @@ -Subproject commit f94212973623702bff498780fc2d0426e595d5ec +Subproject commit c833958ae5e623292aa1d54617548cb4a42f9525 diff --git a/include/python-cellframe.h b/include/python-cellframe.h index f864b877015214e08f3aa82ac4cb250caf8b80d2..c78e2998039a93479cd912e8ab2f099fbacb05f9 100644 --- a/include/python-cellframe.h +++ b/include/python-cellframe.h @@ -60,9 +60,21 @@ #include "wrapping_dap_chain_gdb.h" #include "dap_common.h" +#include "dap_server.h" +#ifdef _WIN32 +#include "Windows.h" + +BOOL WINAPI consoleHandler(DWORD); +#else +#include "signal.h" + +void sigfunc(int sig); +#endif PyObject *python_cellframe_init(PyObject *self, PyObject *args); +void deinit_modules(void); + PyObject *python_cellframe_deinit(PyObject *self, PyObject *args); static PyMethodDef CellFramePythonMethods[] = {