Connection breaks after some time between native-app and chrome-extension

I use the chrome native messaging api to communicate between my chrome extension and native-windows-app written in C ++.

The connection is established precisely and the data is also exchanged. But the connection is terminated after a random number of calls are made from the extension to the native application.

I tried to launch the native application myself, and it works fine when launched in an infinite loop (no exceptions occur).

My native application generates almost 300 Kbytes of data on the first call ( encode_frame () ), and then make consecutive calls (over 300 ms) that generate 0 to 300 Kbytes of data ( encode_frame_difference () ). The data is base64 encoded.

FYI: communication occurs through stdin and stdout between the native application and the extension.

The problem is that I cannot understand why the connection is interrupted after a while.

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

Here is the extension code : chrome-extension-js

Any help would be appreciated!

Thanks.

EDIT : So far, I have discovered that there is a problem with the specific length of the data that I send.

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

+4
source share
1 answer

I got this from the chrome-added group and it WORKS for me.

The insert is the same:

The problem is probably related to the header, which contains the four-byte length of the message. If this is strange, Chrome will break the connection. Since stdout is in text mode by default, some ASCII characters can be converted to different characters in Windows, such as \ n becomes \ r \ n. You have more bytes in the header than you need, so the Chrome extension will assume that you send millions of bytes of data, become confused and disconnect, and some of the bytes of the header will expire in the message, causing the JSON parser to encounter unexpected characters.

Try setting stdout to binary mode:

_setmode(_fileno(stdout), _O_BINARY); 

If this does not help, you can additionally try this alternative method of writing to standard output:

 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 of them: fcntl.h io.h

+4
source

All Articles