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