【问题标题】:Native Messaging host not able to send 1 MB data本机消息传递主机无法发送 1 MB 数据
【发布时间】:2014-12-02 06:54:36
【问题描述】:

我在 C++ 中使用本机主机,当我将 base64 从本机应用程序发送到大小为 base64 1M的chrome扩展(本机消息传递)时,程序出现错误“与本机消息传递主机通信时出错” 我的代码如下

int _tmain(int argc, _TCHAR* argv[])
{
    std::cout.setf( std::ios_base::unitbuf );
    unsigned int c, t=0;
    inp="";
    t=0;
    // Sum the first 4 chars from stdin (the length of the message passed).
    for (int i = 0; i <= 3; i++) {
        //t += getchar();
        t += std::pow(256.0f, i) * getchar();
    }
    // Loop getchar to pull in the message until we reach the total
    //  length provided.
    for (int i=0; i < t; i++) {
        c = getchar();
        inp += c;
    }

    unsigned int len =  inp.length();
    // We need to send the 4 btyes of length information
    std::cout << char(((len>>0) & 0xFF))
              << char(((len>>8) & 0xFF))
              << char(((len>>16) & 0xFF))
              << char(((len>>24) & 0xFF));
    // Now we can output our message
    std::cout << inp;
    return 0;
}

【问题讨论】:

  • 那么,问题在哪里...?你想知道什么?
  • 是的,这是设计使然,他们根本不接受大于 1M 的消息。您需要将其分解为一系列较小的消息并重新组合扩展程序中的数据。
  • @donaddon 你有声明的来源吗?

标签: c++ google-chrome visual-c++ google-chrome-extension chrome-native-messaging


【解决方案1】:

本机消息传递主机无法发送超过 1024*1024 字节的消息。来自

https://cs.chromium.org/file%3Anative_message_process_host.cc%20kMaximumMessageSize:

// Maximum message size in bytes for messages received from Native Messaging
// hosts. Message size is limited mainly to prevent Chrome from crashing when
// native application misbehaves (e.g. starts writing garbage to the pipe).
const size_t kMaximumMessageSize = 1024 * 1024;

要解决此问题,您必须将从本地消息传递主机发送到您的扩展程序/应用程序的消息拆分为小于 1MB 的块。
在您的本地消息传递主机中,您可以创建一个循环,重复输出 32 位消息长度(最大 1MB),然后输出一段消息。
在您的应用程序/扩展程序中,使用chrome.runtime.connectNative 而不是chrome.runtime.sendNativeMessage 打开一个持续时间超过一条消息的端口(如果您使用sendNativeMessage,该端口将在收到一条回复后关闭,这会导致本机消息传递主机终止)。

【讨论】:

  • 非常感谢您提供适当的源链接。话虽如此,这种情况还是很令人遗憾的。
  • @Xan 有什么遗憾? 1MB 的限制、变通方法还是原生消息传递文档?
  • 限制。一个已经很尴尬的消息传递协议更加尴尬。当然,它没有记录在案。
  • @Xan 无限的消息大小(本机应用程序 -> Chrome 浏览器)是危险的,因此必须有一些最大消息大小。此限制将记录在案,请参阅chrome-apps-doc.appspot.com/_patch/768373004/extensions/… 的预览。我上周提交了一个补丁,但审稿人显然在休假。这是补丁,如果您有任何改进建议,请告诉我:codereview.chromium.org/768373004
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-27
  • 1970-01-01
  • 2017-07-22
  • 1970-01-01
相关资源
最近更新 更多