@@ -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 {
@@ -46,39 +46,16 @@ 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- });
49+ // Use a variable to track the timer outside closures
50+ $ timer = null ;
5351
52+ // Create the deferred with the canceller function
5453 $ loop = $ this ->loop ;
55- $ deferred = new Deferred ( function () use ($ socket , &$ timer , $ loop ) {
54+ $ canceller = function () use ($ socket , &$ timer , $ loop ) {
5655 // canceling resulting promise cancels timer and closes socket
57- $ loop ->cancelTimer ($ timer );
56+ if ($ timer !== null ) {
57+ $ loop ->cancelTimer ($ timer );
58+ }
5859 $ socket ->close ();
5960 throw new RuntimeException ('Cancelled ' );
60- });
61-
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- });
68-
69- $ socket ->send ($ data , self ::ADDRESS );
70-
71- return $ deferred ->promise ();
72- }
73-
74- /** @internal */
75- public function parseMessage ($ message , $ remote )
76- {
77- // TODO: parse message into message model
78- $ message = array (
79- 'data ' => $ message ,
80- '_sender ' => $ remote
81- );
82- return $ message ;
83- }
84- }
61+ };
0 commit comments