@@ -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,29 +52,28 @@ 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 ();
51- $ socket ->close ();
52- });
53-
54- $ loop = $ this ->loop ;
55- $ deferred = new Deferred (function () use ($ socket , &$ timer , $ loop ) {
55+ $ messages = [];
56+ $ deferred = new Deferred (function () use ($ socket , &$ timer ) {
5657 // canceling resulting promise cancels timer and closes socket
57- $ loop ->cancelTimer ($ timer );
58+ $ this -> loop ->cancelTimer ($ timer );
5859 $ socket ->close ();
5960 throw new RuntimeException ('Cancelled ' );
6061 });
6162
62- $ that = $ this ;
63- $ socket ->on ('message ' , function ($ data , $ remote ) use ($ deferred , $ that ) {
64- $ message = $ that ->parseMessage ($ data , $ remote );
63+ $ timer = $ this ->loop ->addTimer ($ mx , function () use ($ socket , &$ deferred , &$ messages ) {
64+ $ deferred ->resolve ($ messages );
65+ $ socket ->close ();
66+ });
6567
66- $ deferred ->progress ($ message );
67- });
68+ $ that = $ this ;
69+ $ socket ->on ('message ' , function ($ data , $ remote ) use (&$ messages , $ that ) {
70+ $ message = $ that ->parseMessage ($ data , $ remote );
71+ $ messages [] = $ message ;
72+ });
6873
69- $ socket ->send ($ data , self ::ADDRESS );
74+ $ socket ->send ($ data , self ::ADDRESS );
7075
71- return $ deferred ->promise ();
76+ return $ deferred ->promise ();
7277 }
7378
7479 /** @internal */
0 commit comments