使用node-ffi时如何防止Node崩溃

时间:2018-07-18 18:26:15

标签: c++ node.js error-handling crash node-ffi

我有一个使用Node-ffi调用C ++代码并将其发送到客户端的node.js服务器。我面临的问题是客户端可以发送使C ++程序崩溃的用户输入,如何防止这种情况发生?

当C ++程序崩溃时,服务器的终端中将显示以下输出:

npm ERR! code ELIFECYCLE
npm ERR! errno 3221225477
npm ERR! ws_send_json_server@1.0.0 start: `node app.js`
npm ERR! Exit status 3221225477
npm ERR!
npm ERR! Failed at the ws_send_json_server@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:

我想知道是否有可能在node-ffi函数调用周围编写try-catch块或其他内容,例如:

try {
    greenbuild.GB_SetArchOffset(messageJSON.Item.arch_offset);
    greenbuild.GB_SetArchRadius(messageJSON.Item.arch_radius);
    greenbuild.GB_SetBayLength(messageJSON.Item.bay_length);
    greenbuild.GB_SetBayWidth(messageJSON.Item.bay_width);
    greenbuild.GB_SetPeakHeight(messageJSON.Item.peak_height);
    greenbuild.GB_SetWallHeight(messageJSON.Item.wall_height);
    greenbuild.GB_SetColumnSpacing(messageJSON.Item.column_spacing);
    greenbuild.GB_SetNumBayLength(messageJSON.Item.number_bays_length);
    greenbuild.GB_SetNumBayWidth(messageJSON.Item.number_bays_width);
    greenbuild.GB_SetNumPanelHorzLength(messageJSON.Item.horizontal_panels_length);
    greenbuild.GB_SetNumPanelHorzWidth(messageJSON.Item.horizontal_panels_width)
    greenbuild.GB_SetNumPanelVert(messageJSON.Item.vertical_panels);

} catch (ERROR) {
    console.error("error occurred, but the whole server is still running.")
}

其中“ greenbuild”是ffi。C++代码库。

谢谢。

1 个答案:

答案 0 :(得分:1)

答案是在C ++端进行输入验证,并进行调试以将文本文件写为崩溃输出。