@@ -28,8 +28,14 @@ class Client
28
28
* @param ?LoopInterface $loop
29
29
* @param ?MulticastFactory $multicast
30
30
*/
31
- public function __construct (LoopInterface $ loop = null , MulticastFactory $ multicast = null )
31
+ public function __construct ($ loop = null , $ multicast = null )
32
32
{
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
+ }
33
39
$ this ->loop = $ loop ?: Loop::get ();
34
40
$ this ->multicast = $ multicast ?: new MulticastFactory ($ this ->loop );
35
41
}
@@ -46,11 +52,7 @@ public function search($searchTarget = 'ssdp:all', $mx = 2)
46
52
$ socket = $ this ->multicast ->createSender ();
47
53
// TODO: The TTL for the IP packet SHOULD default to 2 and SHOULD be configurable.
48
54
49
- $ timer = $ this ->loop ->addTimer ($ mx , function () use ($ socket , &$ deferred ) {
50
- $ deferred ->resolve ();
51
- $ socket ->close ();
52
- });
53
-
55
+ $ messages = array ();
54
56
$ loop = $ this ->loop ;
55
57
$ deferred = new Deferred (function () use ($ socket , &$ timer , $ loop ) {
56
58
// canceling resulting promise cancels timer and closes socket
@@ -59,11 +61,15 @@ public function search($searchTarget = 'ssdp:all', $mx = 2)
59
61
throw new RuntimeException ('Cancelled ' );
60
62
});
61
63
64
+ $ timer = $ this ->loop ->addTimer ($ mx , function () use ($ socket , &$ deferred , &$ messages ) {
65
+ $ deferred ->resolve ($ messages );
66
+ $ socket ->close ();
67
+ });
68
+
62
69
$ that = $ this ;
63
- $ socket ->on ('message ' , function ($ data , $ remote ) use ($ deferred , $ that ) {
70
+ $ socket ->on ('message ' , function ($ data , $ remote ) use (& $ messages , $ that ) {
64
71
$ message = $ that ->parseMessage ($ data , $ remote );
65
-
66
- $ deferred ->progress ($ message );
72
+ $ messages [] = $ message ;
67
73
});
68
74
69
75
$ socket ->send ($ data , self ::ADDRESS );
0 commit comments