Skip to content
This repository was archived by the owner on Jul 3, 2020. It is now read-only.

Commit e1ee34e

Browse files
author
matt
committed
Merge branch 'mrk-its-master'
2 parents 826f7ed + ab4edd8 commit e1ee34e

File tree

2 files changed

+55
-61
lines changed

2 files changed

+55
-61
lines changed

manifest.json

+8-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88
"scripts": ["scripts/background/background.js"]
99
}
1010
},
11+
"sockets": {
12+
"udp": {
13+
"send": "*",
14+
"bind": "*",
15+
"multicastMembership": ""
16+
}
17+
},
1118
"permissions": [
1219
"storage",
1320
{
@@ -16,4 +23,4 @@
1623
"http://*/" // Pretty wild but the UPnP discovery URL could be more or less anything
1724
],
1825
"icons": {"128": "images/logo-128.png" }
19-
}
26+
}

scripts/ssdp/ssdp.js

+47-60
Original file line numberDiff line numberDiff line change
@@ -37,41 +37,54 @@ var SSDP = function(config) {
3737
/**
3838
* Initialise the SSDP connection by opening a UDP socket and joining the multicast group.
3939
*/
40-
SSDP.prototype.init = function() {
40+
SSDP.prototype.init = function(callback) {
4141
this.log("Initialising...");
42-
42+
4343
var that = this;
44-
chrome.socket.create('udp', function (socket) {
45-
var socketId = socket.socketId;
44+
var cb = callback || function(){}
4645

46+
chrome.sockets.udp.create({}, function (socket) {
47+
var socketId = socket.socketId;
48+
chrome.sockets.udp.onReceive.addListener(function(result) {
49+
var data = that.bufferToString(result.data);
50+
that.processNotify(data);
51+
})
4752
// House keeping on TTL & loopback
48-
chrome.socket.setMulticastTimeToLive(socketId, 12, function (result) {
53+
chrome.sockets.udp.setMulticastTimeToLive(socketId, 12, function (result) {
4954
if (result !== 0) {
5055
that.log('Error setting multicast TTL' + result);
5156
}});
5257

53-
chrome.socket.setMulticastLoopbackMode(socketId, true, function (result) {
58+
chrome.sockets.udp.setMulticastLoopbackMode(socketId, true, function (result) {
5459
if (result !== 0) {
5560
that.log('Error setting multicast loop-back mode: ' + result);
5661
}
5762
});
5863

59-
chrome.socket.bind(socketId, that.address, that.port, function (result) {
60-
if (result !== 0) {
61-
that.log('Unable to bind to new socket: ' + result);
62-
} else {
63-
chrome.socket.joinGroup(socketId, that.multicast, function (result) {
64-
if (result !== 0) {
65-
that.log('Unable to join multicast group ' + that.multicast + ': ' + result);
66-
} else {
67-
that.socketId = socketId;
68-
that.pollData();
69-
that.sendDiscover();
70-
that.log("Waiting for SSDP broadcasts.");
71-
}
72-
});
64+
function bind(port, cb) {
65+
chrome.sockets.udp.bind(socketId, that.address, port, function (result) {
66+
if(result === 0) return cb(result)
67+
that.log('Unable to bind to new socket: ' + result + ", trying to bind to random port");
68+
chrome.sockets.udp.bind(socketId, that.address, 0, cb)
69+
})
70+
}
71+
bind(that.port, function(result) {
72+
if(result !== 0) {
73+
that.log('Unable to bind to new socket: ' + result)
74+
return cb(result)
7375
}
74-
});
76+
chrome.sockets.udp.joinGroup(socketId, that.multicast, function (result) {
77+
if (result !== 0) {
78+
that.log('Unable to join multicast group ' + that.multicast + ': ' + result);
79+
} else {
80+
that.socketId = socketId;
81+
that.sendDiscover();
82+
that.log("Waiting for SSDP broadcasts.");
83+
}
84+
cb(result);
85+
});
86+
})
87+
7588
});
7689
};
7790

@@ -84,57 +97,40 @@ SSDP.prototype.sendDiscover = function(config) {
8497
var that = this;
8598
var c = config || {};
8699
var respondDelay = c.delay || 3;
100+
var target = c.target || 'ssdp:all'
87101

88102
var search = 'M-SEARCH * HTTP/1.1\r\n' +
89103
'HOST: 239.255.255.250:1900\r\n' +
90-
'MAN: ssdp:discover\r\n' +
104+
'MAN: "ssdp:discover"\r\n' +
91105
'MX: ' + respondDelay + '\r\n' +
92-
'ST: ssdp:all\r\n\r\n';
106+
'ST: ' + target + '\r\n\r\n'
93107

94108
var buffer = this.stringToBuffer(search);
95-
chrome.socket.sendTo(this.socketId, buffer, that.multicast,
109+
chrome.sockets.udp.send(this.socketId, buffer, that.multicast,
96110
that.port, function(info) {
97111
that.log("Sent M-SEARCH discovery message...");
98112
});
99113
};
100114

101115

102-
/**
103-
* Handles incomming UDP data from the multicast group
104-
* @private
105-
*/
106-
SSDP.prototype.pollData = function() {
107-
var that = this;
108-
if (that.socketId > -1) {
109-
chrome.socket.recvFrom(that.socketId, that.bufferLength, function (result) {
110-
if (result.resultCode >= 0) {
111-
var data = that.bufferToString(result.data);
112-
that.processNotify(data);
113-
that.pollData();
114-
} else {
115-
that.log("Error handling data");
116-
}
117-
});
118-
}
119-
};
120-
121116
/**
122117
* Processes the NOTIFY broadcast and stores the service details in the services array
123118
* @param {string} str Contains the string of the NOTIFY resposne sent by SSDP services.
124119
* @private
125120
*/
126121
SSDP.prototype.processNotify = function(str) {
127122
var notify = {};
128-
if (str.indexOf('NOTIFY') < 0) {
129-
// only interested in notify broadcasts
130-
return;
131-
}
132-
str.replace(/([A-Z\-]*){1}:([a-zA-Z\-_0-9\.:=\/ ?]*){1}/gi,
123+
124+
str.replace(/([A-Z\-]*){1}:([^\n]*){1}/gi,
133125
function (match, m1, m2) {
134126
var name = m1.toLowerCase().trim();
135127
name = name.replace('-',''); // remove any hypens, e.g. cache-control
136128
notify[name] = m2.trim();
137129
});
130+
if(!notify.usn)
131+
return;
132+
133+
console.log('notify packet', notify)
138134

139135
// Check for expiration/max-age
140136
if (notify.cachecontrol) {
@@ -249,18 +245,11 @@ SSDP.prototype.updateCache = function() {
249245

250246
/**
251247
* Converts a string to an array buffer
252-
* @param {string} str String to e converted
248+
* @param {string} str String to be converted
253249
* @private
254250
*/
255251
SSDP.prototype.stringToBuffer = function(str) {
256-
// courtesy of Renato Mangini / HTML5Rocks
257-
// http://updates.html5rocks.com/2012/06/How-to-convert-ArrayBuffer-to-and-from-String
258-
var buf = new ArrayBuffer(str.length*2); // 2 bytes for each char
259-
var bufView = new Uint8Array(buf);
260-
for (var i=0, strLen=str.length; i<strLen; i++) {
261-
bufView[i] = str.charCodeAt(i);
262-
}
263-
return buf;
252+
return new TextEncoder().encode(str).buffer;
264253
};
265254

266255
/**
@@ -269,9 +258,7 @@ SSDP.prototype.stringToBuffer = function(str) {
269258
* @private
270259
*/
271260
SSDP.prototype.bufferToString = function(buffer) {
272-
// courtesy of Renato Mangini / HTML5Rocks
273-
// http://updates.html5rocks.com/2012/06/How-to-convert-ArrayBuffer-to-and-from-String
274-
return String.fromCharCode.apply(null, new Uint8Array(buffer));
261+
return new TextDecoder().decode(new DataView(buffer))
275262
};
276263

277264
/**

0 commit comments

Comments
 (0)