@@ -28,8 +28,14 @@ class Client
2828 * @param ?LoopInterface $loop
2929 * @param ?MulticastFactory $multicast
3030 */
31- public function __construct (LoopInterface $ loop = null , MulticastFactory $ multicast = null )
31+ public function __construct ($ loop = null , $ multicast = null )
3232 {
33+ if ($ loop !== null && !$ loop instanceof LoopInterface) { // manual type check to support legacy PHP < 7.1
34+ throw new \InvalidArgumentException ('Argument #1 ($loop) expected null|React\EventLoop\LoopInterface ' );
35+ }
36+ if ($ multicast !== null && !$ multicast instanceof MulticastFactory) { // manual type check to support legacy PHP < 7.1
37+ throw new \InvalidArgumentException ('Argument #2 ($multicast) expected null|Clue\React\Multicast\Factory ' );
38+ }
3339 $ this ->loop = $ loop ?: Loop::get ();
3440 $ this ->multicast = $ multicast ?: new MulticastFactory ($ this ->loop );
3541 }
@@ -46,24 +52,23 @@ public function search($searchTarget = 'ssdp:all', $mx = 2)
4652 $ socket = $ this ->multicast ->createSender ();
4753 // TODO: The TTL for the IP packet SHOULD default to 2 and SHOULD be configurable.
4854
49- $ timer = $ this ->loop ->addTimer ($ mx , function () use ($ socket , &$ deferred ) {
50- $ deferred ->resolve ();
55+ $ messages = array ();
56+ $ deferred = new Deferred (function () use ($ socket , &$ timer ) {
57+ // canceling resulting promise cancels timer and closes socket
58+ $ this ->loop ->cancelTimer ($ timer );
5159 $ socket ->close ();
60+ throw new RuntimeException ('Cancelled ' );
5261 });
5362
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 );
63+ $ timer = $ this ->loop ->addTimer ($ mx , function () use ($ socket , &$ deferred , &$ messages ) {
64+ $ deferred ->resolve ($ messages );
5865 $ socket ->close ();
59- throw new RuntimeException ('Cancelled ' );
6066 });
6167
6268 $ that = $ this ;
63- $ socket ->on ('message ' , function ($ data , $ remote ) use ($ deferred , $ that ) {
69+ $ socket ->on ('message ' , function ($ data , $ remote ) use (& $ messages , $ that ) {
6470 $ message = $ that ->parseMessage ($ data , $ remote );
65-
66- $ deferred ->progress ($ message );
71+ $ messages [] = $ message ;
6772 });
6873
6974 $ socket ->send ($ data , self ::ADDRESS );
0 commit comments