Skip to content
This repository has been archived by the owner on Sep 9, 2022. It is now read-only.

Fix TCP packet reordering on android, "unknown socket id" on iOS #27

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 11 additions & 28 deletions sockets.tcp.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ exports.connect = function(socketId, peerAddress, peerPort, callback) {
callback(0);
};
var fail = callback && function(error) {
if(!('message' in error && 'resultCode' in error))
callbackWithError(error, callback, -1);
else
callbackWithError(error.message, callback, error.resultCode);
};
exec(win, fail, 'ChromeSocketsTcp', 'connect', [socketId, peerAddress, peerPort]);
Expand Down Expand Up @@ -147,51 +150,31 @@ exports.onReceiveError = new Event('onReceiveError');

function registerReceiveEvents() {

// iOS onRecieve callback
var win = function(info, data) {
if (data) { // Binary data has to be a top level argument.
info.data = data;
}
exports.onReceive.fire(info);

if (data) { // Only exec readyToRead when not redirect to file

// readyToRead signals the plugin to read the next tcp packet. exec
// it after fire() will allow all API calls in the onReceive
// listener exec before next read, such as, pause the socket.
exec(null, null, 'ChromeSocketsTcp', 'readyToRead', []);
var args = [];
if('socketId' in data) args = [data.socketId];
exec(null, null, 'ChromeSocketsTcp', 'readyToRead', args);
}
};

// TODO: speical callback for android, DELETE when multipart result for
// android is avaliable
if (platform.id == 'android') {
win = (function() {
var recvInfo;
var call = 0;
return function(info) {
if (call === 0) {
recvInfo = info;
if (!recvInfo.uri) {
call++;

// uri implies only one callback becasue redirect to
// file is enabled, and binary data is not included in
// the receiveInfo.
return;
}
} else {
recvInfo.data = info;
call = 0;
}
exports.onReceive.fire(recvInfo);
if (recvInfo.data) { // Only exec readyToRead when not redirect to file

// readyToRead signals the plugin to read the next tcp
// packet. exec it after fire() will allow all API calls in
// the onReceive listener exec before next read, such as,
// pause the socket.
exec(null, null, 'ChromeSocketsTcp', 'readyToRead', [recvInfo.socketId]);
}
if ('socketId' in info) recvInfo = info;
else recvInfo.data = info;
if('data' in recvInfo)
exports.onReceive.fire(recvInfo);
};
})();
}
Expand Down