1

我在C++中使用本机主机时,当我从本机应用程序发送base64到基本大小为64的Chrome扩展(本地消息传递)时,程序仍在运行。但是当我与大小的base64> 1M发送从本机应用程序的base64 Chrome浏览器扩展(本地消息),该计划是错误的“错误与本地消息主机通信时” 我的代码如下本地消息传递主机无法发送1 MB数据

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; 
} 
+1

那么,问题在哪里?你想知道什么? – 2014-12-02 13:54:57

+0

是的,这是通过设计,他们根本不接受大于1M的消息。您需要将其分解为一系列较小的消息,并重新组合扩展中的数据。 – donaddon 2014-12-02 15:28:05

+0

@donaddon您是否有索赔来源? – Xan 2014-12-02 16:04:39

回答

2

本地消息主机不能发送超过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,则在收到一条回复后导致本地消息传递主机终止后关闭该端口)。

+0

非常感谢您提供适当的来源链接。也就是说,这种情况令人非常遗憾。 – Xan 2014-12-02 23:19:12

+0

@Xan什么是遗憾? 1MB限制,解决方法或本地消息文档? – 2014-12-02 23:19:59

+0

限制。一个已经很笨拙的消息协议更加尴尬。当然,这是没有记录的事实。 – Xan 2014-12-02 23:21:18