I'm developing a Chrome extension working with native messaging host. It works in most cases, but I have found a strange behavior when I send messages of certain sizes. It seems that message is dropped, when the size is between 2560 and 2815 bytes (A00 and AFF in hex). All subsequent messages are also not arriving, which suggests that the stream is corrupted for some reason.
Here is a stripped down Python native messaging app, which can be used to test it:
import sys
import struct
def output(message):
encoded_message = message.encode('utf-8')
# Write message size.
sys.stdout.write(struct.pack('I', len(encoded_message)))
# Write the message itself.
sys.stdout.write(encoded_message)
sys.stdout.flush()
if __name__ == "__main__":
output('{"type": "%s"}' % ('x'*2820))
output('{"type": "%s"}' % ('x'*2560))
I'm getting the first message and not the second one.
I have took a look at the code in Chrome repository. Function, which seems to be responsible for that functionality, doesn't have anything special:
void NativeMessageProcessHost::ProcessIncomingData(
const char* data, int data_size) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
incoming_data_.append(data, data_size);
while (true) {
if (incoming_data_.size() < kMessageHeaderSize)
return;
size_t message_size =
*reinterpret_cast<const uint32*>(incoming_data_.data());
if (message_size > kMaximumMessageSize) {
LOG(ERROR) << "Native Messaging host tried sending a message that is "
<< message_size << " bytes long.";
Close(kHostInputOuputError);
return;
}
if (incoming_data_.size() < message_size + kMessageHeaderSize)
return;
content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
base::Bind(&Client::PostMessageFromNativeProcess, weak_client_ui_,
destination_port_,
incoming_data_.substr(kMessageHeaderSize, message_size)));
incoming_data_.erase(0, kMessageHeaderSize + message_size);
}
}
Does anybody have any idea what may be happening here?
Update
I have experienced this problem on 64 bit versions of Windows 7 and Windows 8.1.
I tried Chrome 64-bit on Stable, Beta and Dev channels - versions 37, 38 and 39. I have also tried stable Chrome 32-bit
I use 32 bit version of Python 2.7.7 and PyInstaller 2.1 to create an executable for native messaging host.
{"type": ... }
). – Ezekiel