简体   繁体   English

从 c++ 向铬扩展 [Native Messaging] 发送消息时的奇怪行为 - 没有发送数据

[英]Strange behaviour when sending messages from c++ to chromium extension [Native Messaging] - No data being sent

Hi and hope everyone is doing good!嗨,希望每个人都做得很好!

I started working on a chrome extension which talks to a cpp exe.我开始研究与 cpp exe 对话的 chrome 扩展。 At the moment I am just trying to read some data from the extension which works as expected using the following code目前我只是想从扩展中读取一些数据,这些数据使用以下代码按预期工作

unsigned int length = 0;

{
    unsigned int read_char = getchar();
    length = length | (read_char << index*8);
}

std::string message = "";
for (int index = 0; index < length; index++)
{
    message += getchar();
}

When trying to send the same information from the cpp -> extension using the following code当尝试使用以下代码从 cpp -> 扩展发送相同的信息时

unsigned int len = message.length();


printf("%c%c%c%c",  (char) (len & 0xff),
                    (char) (len << 8 & 0xff),
                    (char) (len << 16 & 0xff),
                    (char) (len << 24 & 0xff));


printf("%s", message.c_str());

it works as expected Screenshot of the working scenario它按预期工作 工作场景的屏幕截图

However, when i try to send back another string rather than the same string that we send it won't work.但是,当我尝试发回另一个字符串而不是我们发送的相同字符串时,它将不起作用。 Example:例子:

    unsigned int length = 0;


for (int index = 0; index < 4; index++)
{
    unsigned int read_char = getchar();
    cerr << read_char << endl;
    length = length | (read_char << index*8);
}

std::string message = "";
for (int index = 0; index < length; index++)
{
    message += getchar();
}

std::string outputMessage = "output";

unsigned int len = outputMessage.length();


printf("%c%c%c%c",  (char) (len & 0xff),
                    (char) (len << 8 & 0xff),
                    (char) (len << 16 & 0xff),
                    (char) (len << 24 & 0xff));


printf("%s", outputMessage.c_str());



return 0;

Here is also the screenshot of the scenario that is failing.这也是失败的场景的屏幕截图。 Failed scenario失败的场景


I am not sure what I missed here, consider that I am just creating a new string and also using it's length for information that the extension needs.我不确定我在这里错过了什么,考虑到我只是在创建一个新字符串并将它的长度用于扩展所需的信息。

Anyway, it would be great if anyone could look at this one.无论如何,如果有人能看到这个,那就太好了。 Thanks in advance:)提前致谢:)

UPDATE: After trying different ways of returning messages I found this on the web and it works as expected更新:在尝试了不同的返回消息方式后,我在 web 上发现了这个,它按预期工作

message_t encode_message(json content) {
    string encoded_content = content.dump();
    message_t m;
    m.content = encoded_content;
    m.length = (uint32_t)encoded_content.length();
    return m;
}

// Send an encoded message to stdout.
void send_message(message_t encoded_message) {
    char* raw_length = reinterpret_cast<char*>(&encoded_message.length);
    fwrite(raw_length, 4, sizeof(char), stdout);
    fwrite(encoded_message.content.c_str(), encoded_message.length, sizeof(char), stdout);
    fflush(stdout);
}

You have to explicitly flush the buffer, if those data are to be sent to the native messaging host ASAP as shown below:如果要尽快将这些数据发送到本机消息传递主机,则必须显式刷新缓冲区,如下所示:

std::string outputMessage = "output";

unsigned int len = outputMessage.length();

printf("%c%c%c%c", (char) (len & 0xff),
                   (char) ((len >> 8) & 0xff),
                   (char) ((len >> 16) & 0xff),
                   (char) ((len >> 24) & 0xff));


printf("%s", outputMessage.c_str());
fflush(stdout); // This will flush the buffer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM