33namespace Clue \React \Multicast ;
44
55use React \EventLoop \LoopInterface ;
6- use Socket \React \Datagram \Factory as DatagramFactory ;
7- use Socket \Raw \Factory as RawFactory ;
6+ use React \Datagram \Socket as DatagramSocket ;
87use BadMethodCallException ;
98
109class Factory
@@ -13,24 +12,16 @@ class Factory
1312 private $ rawFactory ;
1413 private $ datagramFactory ;
1514
16- public function __construct (LoopInterface $ loop, RawFactory $ rawFactory = null , DatagramFactory $ datagramFactory = null )
15+ public function __construct (LoopInterface $ loop )
1716 {
18- if ($ rawFactory === null ) {
19- $ rawFactory = new RawFactory ();
20- }
21-
22- if ($ datagramFactory === null ) {
23- $ datagramFactory = new DatagramFactory ($ loop );
24- }
25-
26- $ this ->rawFactory = $ rawFactory ;
27- $ this ->datagramFactory = $ datagramFactory ;
17+ $ this ->loop = $ loop ;
2818 }
2919
3020 public function createSender ()
3121 {
32- $ socket = $ this ->rawFactory ->createUdp4 ();
33- return $ this ->datagramFactory ->createFromRaw ($ socket );
22+ $ stream = stream_socket_server ('udp://0.0.0.0:0 ' , $ errno , $ errstr , STREAM_SERVER_BIND );
23+
24+ return new DatagramSocket ($ this ->loop , $ stream );
3425 }
3526
3627 public function createReceiver ($ address )
@@ -41,19 +32,21 @@ public function createReceiver($address)
4132
4233 $ parts = parse_url ('udp:// ' . $ address );
4334
44- $ socket = $ this ->rawFactory ->createUdp4 ();
35+ $ stream = stream_socket_server ('udp://0.0.0.0: ' . $ parts ['port ' ], $ errno , $ errstr , STREAM_SERVER_BIND );
36+
37+ $ socket = socket_import_stream ($ stream );
4538
4639 // allow multiple processes to bind to the same address
47- $ socket-> setOption ( SOL_SOCKET , SO_REUSEADDR , 1 );
40+ socket_set_option ( $ socket, SOL_SOCKET , SO_REUSEADDR , 1 );
4841
4942 // join multicast group and bind to port
50- $ socket ->setOption (
43+ socket_set_option (
44+ $ socket ,
5145 IPPROTO_IP ,
5246 MCAST_JOIN_GROUP ,
5347 array ('group ' => $ parts ['host ' ], 'interface ' => 0 )
5448 );
55- $ socket ->bind ('0.0.0.0: ' . $ parts ['port ' ]);
5649
57- return $ this ->datagramFactory -> createFromRaw ( $ socket );
50+ return new DatagramSocket ( $ this ->loop , $ stream );
5851 }
5952}
0 commit comments