I am using chrome native messaging api to communicate between my chrome-extension and native-windows-app which is written in c++.

The connection establishes finely and data also gets exchanged. But the connection breaks after random number of calls are made from extension to the native-app.

I tried running native-app independently and it works fine when run in an infinite loop(no exceptions occur).

My native-app generates almost 300KB of data at first call(encode_frame()) and then consecutive calls(per 300 ms) are made that generate 0 to 300KB of data(encode_frame_difference()). Data is base64 encoded.

FYI: communication occur via stdin and stdout between native-app and extension.

The problem is I am not able to figure out why connection breaks after some time.

Here is the native-app code: windows-native-app-cpp

Here is the extension code: chrome-extension-js

Any help would be appreciated!

Thank you.

EDIT: Till now I have found out that there is problem with certain length of data I'm sending.

eg. If the JSON length is between 2560 and 2815, it stops working. while for JSON lengths like 2816 or 6656 it works.

有帮助吗?

解决方案

I got this from chromium-extension group and it WORKS for me.

Pasting the exact same:

The problem is likely in the header that contains the four-byte length of the message. If it's weird, Chrome will break the connection. Since stdout is in text mode by default, certain ASCII characters may be getting converted to different ones on Windows, such as \n becomes \r\n. You end up with more bytes in the header than you need, so the Chrome extension will think that you're sending millions of bytes of data, gets confused, and breaks the connection, and some of the header bytes bleed into the message, causing the JSON parser to encounter unexpected characters.

Try setting stdout into binary mode:

_setmode(_fileno(stdout), _O_BINARY);

If that doesn't help, you can additionally try this alternate method of writing to stdout:

unsigned int len = final_msg.length();
fwrite(&len, 4, 1, stdout);
printf("%s", final_msg.c_str());
fflush(stdout);

You may need to add some includes: fcntl.h io.h

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top