@@ -18,18 +18,15 @@ class Client
1818 /** @var MulticastFactory */
1919 private $ multicast ;
2020
21- /**
22- * This class takes an optional `LoopInterface|null $loop` parameter that can be used to
23- * pass the event loop instance to use for this object. You can use a `null` value
24- * here in order to use the [default loop](https://github.com/reactphp/event-loop#loop).
25- * This value SHOULD NOT be given unless you're sure you want to explicitly use a
26- * given event loop instance.
27- *
28- * @param ?LoopInterface $loop
29- * @param ?MulticastFactory $multicast
30- */
31- public function __construct (LoopInterface $ loop = null , MulticastFactory $ multicast = null )
21+ public function __construct ($ loop = null , $ multicast = null )
3222 {
23+ if ($ loop !== null && !$ loop instanceof LoopInterface) {
24+ throw new \InvalidArgumentException ('Argument #1 ($loop) expected null|React\EventLoop\LoopInterface ' );
25+ }
26+ if ($ multicast !== null && !$ multicast instanceof MulticastFactory) {
27+ throw new \InvalidArgumentException ('Argument #2 ($multicast) expected null|Clue\React\Multicast\Factory ' );
28+ }
29+
3330 $ this ->loop = $ loop ?: Loop::get ();
3431 $ this ->multicast = $ multicast ?: new MulticastFactory ($ this ->loop );
3532 }
@@ -46,29 +43,32 @@ public function search($searchTarget = 'ssdp:all', $mx = 2)
4643 $ socket = $ this ->multicast ->createSender ();
4744 // TODO: The TTL for the IP packet SHOULD default to 2 and SHOULD be configurable.
4845
49- $ timer = $ this ->loop ->addTimer ($ mx , function () use ($ socket , &$ deferred ) {
50- $ deferred ->resolve ();
51- $ socket ->close ();
52- });
46+ $ messages = array ();
5347
54- $ loop = $ this ->loop ;
55- $ deferred = new Deferred (function () use ($ socket , &$ timer , $ loop ) {
56- // canceling resulting promise cancels timer and closes socket
57- $ loop ->cancelTimer ($ timer );
48+ $ timer = $ this ->loop ->addTimer ($ mx , function () use ($ socket , &$ deferred , &$ messages ) {
49+ // resolve promise with all collected messages
50+ $ deferred ->resolve ($ messages );
5851 $ socket ->close ();
59- throw new RuntimeException ('Cancelled ' );
6052 });
6153
62- $ that = $ this ;
63- $ socket ->on ('message ' , function ($ data , $ remote ) use ($ deferred , $ that ) {
64- $ message = $ that ->parseMessage ($ data , $ remote );
54+ $ loop = $ this ->loop ;
55+ $ deferred = new Deferred (function () use ($ socket , &$ timer , $ loop ) {
56+ // canceling resulting promise cancels timer and closes socket
57+ $ loop ->cancelTimer ($ timer );
58+ $ socket ->close ();
59+ throw new RuntimeException ('Cancelled ' );
60+ });
6561
66- $ deferred ->progress ($ message );
67- });
62+ $ that = $ this ;
63+ $ socket ->on ('message ' , function ($ data , $ remote ) use ($ that , &$ messages ) {
64+ $ message = $ that ->parseMessage ($ data , $ remote );
65+
66+ $ messages [] = $ 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