@@ -25,8 +25,8 @@ class Client
2525 * This value SHOULD NOT be given unless you're sure you want to explicitly use a
2626 * given event loop instance.
2727 *
28- * @param ? LoopInterface $loop
29- * @param ? MulticastFactory $multicast
28+ * @param LoopInterface|null $loop
29+ * @param MulticastFactory|null $multicast
3030 */
3131 public function __construct (LoopInterface $ loop = null , MulticastFactory $ multicast = null )
3232 {
@@ -36,7 +36,7 @@ public function __construct(LoopInterface $loop = null, MulticastFactory $multic
3636
3737 public function search ($ searchTarget = 'ssdp:all ' , $ mx = 2 )
3838 {
39- $ data = "M-SEARCH * HTTP/1.1 \r\n" ;
39+ $ data = "M-SEARCH * HTTP/1.1 \r\n" ;
4040 $ data .= "HOST: " . self ::ADDRESS . "\r\n" ;
4141 $ data .= "MAN: \"ssdp:discover \"\r\n" ;
4242 $ data .= "MX: $ mx \r\n" ;
@@ -46,11 +46,6 @@ public function search($searchTarget = 'ssdp:all', $mx = 2)
4646 $ socket = $ this ->multicast ->createSender ();
4747 // TODO: The TTL for the IP packet SHOULD default to 2 and SHOULD be configurable.
4848
49- $ timer = $ this ->loop ->addTimer ($ mx , function () use ($ socket , &$ deferred ) {
50- $ deferred ->resolve ();
51- $ socket ->close ();
52- });
53-
5449 $ loop = $ this ->loop ;
5550 $ deferred = new Deferred (function () use ($ socket , &$ timer , $ loop ) {
5651 // canceling resulting promise cancels timer and closes socket
@@ -59,16 +54,21 @@ public function search($searchTarget = 'ssdp:all', $mx = 2)
5954 throw new RuntimeException ('Cancelled ' );
6055 });
6156
62- $ that = $ this ;
63- $ socket ->on ('message ' , function ($ data , $ remote ) use ($ deferred , $ that ) {
64- $ message = $ that ->parseMessage ($ data , $ remote );
57+ $ timer = $ this ->loop ->addTimer ($ mx , function () use ($ socket , $ deferred ) {
58+ $ deferred ->resolve ();
59+ $ socket ->close ();
60+ });
6561
66- $ deferred ->progress ($ message );
67- });
62+ $ that = $ this ;
63+ $ socket ->on ('message ' , function ($ data , $ remote ) use ($ deferred , $ that ) {
64+ $ message = $ that ->parseMessage ($ data , $ remote );
65+
66+ $ deferred ->progress ($ message );
67+ });
6868
69- $ socket ->send ($ data , self ::ADDRESS );
69+ $ socket ->send ($ data , self ::ADDRESS );
7070
71- return $ deferred ->promise ();
71+ return $ deferred ->promise ();
7272 }
7373
7474 /** @internal */
@@ -81,4 +81,4 @@ public function parseMessage($message, $remote)
8181 );
8282 return $ message ;
8383 }
84- }
84+ }
0 commit comments