@@ -37,41 +37,54 @@ var SSDP = function(config) {
37
37
/**
38
38
* Initialise the SSDP connection by opening a UDP socket and joining the multicast group.
39
39
*/
40
- SSDP . prototype . init = function ( ) {
40
+ SSDP . prototype . init = function ( callback ) {
41
41
this . log ( "Initialising..." ) ;
42
-
42
+
43
43
var that = this ;
44
- chrome . socket . create ( 'udp' , function ( socket ) {
45
- var socketId = socket . socketId ;
44
+ var cb = callback || function ( ) { }
46
45
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
+ } )
47
52
// House keeping on TTL & loopback
48
- chrome . socket . setMulticastTimeToLive ( socketId , 12 , function ( result ) {
53
+ chrome . sockets . udp . setMulticastTimeToLive ( socketId , 12 , function ( result ) {
49
54
if ( result !== 0 ) {
50
55
that . log ( 'Error setting multicast TTL' + result ) ;
51
56
} } ) ;
52
57
53
- chrome . socket . setMulticastLoopbackMode ( socketId , true , function ( result ) {
58
+ chrome . sockets . udp . setMulticastLoopbackMode ( socketId , true , function ( result ) {
54
59
if ( result !== 0 ) {
55
60
that . log ( 'Error setting multicast loop-back mode: ' + result ) ;
56
61
}
57
62
} ) ;
58
63
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 )
73
75
}
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
+
75
88
} ) ;
76
89
} ;
77
90
@@ -84,57 +97,40 @@ SSDP.prototype.sendDiscover = function(config) {
84
97
var that = this ;
85
98
var c = config || { } ;
86
99
var respondDelay = c . delay || 3 ;
100
+ var target = c . target || 'ssdp:all'
87
101
88
102
var search = 'M-SEARCH * HTTP/1.1\r\n' +
89
103
'HOST: 239.255.255.250:1900\r\n' +
90
- 'MAN: ssdp:discover\r\n' +
104
+ 'MAN: " ssdp:discover" \r\n' +
91
105
'MX: ' + respondDelay + '\r\n' +
92
- 'ST: ssdp:all \r\n\r\n' ;
106
+ 'ST: ' + target + ' \r\n\r\n'
93
107
94
108
var buffer = this . stringToBuffer ( search ) ;
95
- chrome . socket . sendTo ( this . socketId , buffer , that . multicast ,
109
+ chrome . sockets . udp . send ( this . socketId , buffer , that . multicast ,
96
110
that . port , function ( info ) {
97
111
that . log ( "Sent M-SEARCH discovery message..." ) ;
98
112
} ) ;
99
113
} ;
100
114
101
115
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
-
121
116
/**
122
117
* Processes the NOTIFY broadcast and stores the service details in the services array
123
118
* @param {string } str Contains the string of the NOTIFY resposne sent by SSDP services.
124
119
* @private
125
120
*/
126
121
SSDP . prototype . processNotify = function ( str ) {
127
122
var notify = { } ;
128
- if ( str . indexOf ( 'NOTIFY' ) < 0 ) {
129
- // only interested in notify broadcasts
130
- return ;
131
- }
132
- str . replace ( / ( [ A - Z \- ] * ) { 1 } : ( [ a - z A - Z \- _ 0 - 9 \. : = \/ ? ] * ) { 1 } / gi,
123
+
124
+ str . replace ( / ( [ A - Z \- ] * ) { 1 } : ( [ ^ \n ] * ) { 1 } / gi,
133
125
function ( match , m1 , m2 ) {
134
126
var name = m1 . toLowerCase ( ) . trim ( ) ;
135
127
name = name . replace ( '-' , '' ) ; // remove any hypens, e.g. cache-control
136
128
notify [ name ] = m2 . trim ( ) ;
137
129
} ) ;
130
+ if ( ! notify . usn )
131
+ return ;
132
+
133
+ console . log ( 'notify packet' , notify )
138
134
139
135
// Check for expiration/max-age
140
136
if ( notify . cachecontrol ) {
@@ -249,18 +245,11 @@ SSDP.prototype.updateCache = function() {
249
245
250
246
/**
251
247
* Converts a string to an array buffer
252
- * @param {string } str String to e converted
248
+ * @param {string } str String to be converted
253
249
* @private
254
250
*/
255
251
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 ;
264
253
} ;
265
254
266
255
/**
@@ -269,9 +258,7 @@ SSDP.prototype.stringToBuffer = function(str) {
269
258
* @private
270
259
*/
271
260
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 ) )
275
262
} ;
276
263
277
264
/**
0 commit comments