55use React \EventLoop \LoopInterface ;
66use React \Datagram \Socket as DatagramSocket ;
77use BadMethodCallException ;
8+ use RuntimeException ;
89
910class Factory
1011{
@@ -19,7 +20,10 @@ public function __construct(LoopInterface $loop)
1920
2021 public function createSender ()
2122 {
22- $ stream = stream_socket_server ('udp://0.0.0.0:0 ' , $ errno , $ errstr , STREAM_SERVER_BIND );
23+ $ stream = @stream_socket_server ('udp://0.0.0.0:0 ' , $ errno , $ errstr , STREAM_SERVER_BIND );
24+ if ($ stream === false ) {
25+ throw new RuntimeException ('Unable to create sending socket: ' . $ errstr , $ errno );
26+ }
2327
2428 return new DatagramSocket ($ this ->loop , $ stream );
2529 }
@@ -32,20 +36,32 @@ public function createReceiver($address)
3236
3337 $ parts = parse_url ('udp:// ' . $ address );
3438
35- $ stream = stream_socket_server ('udp://0.0.0.0: ' . $ parts ['port ' ], $ errno , $ errstr , STREAM_SERVER_BIND );
39+ $ stream = @stream_socket_server ('udp://0.0.0.0: ' . $ parts ['port ' ], $ errno , $ errstr , STREAM_SERVER_BIND );
40+ if ($ stream === false ) {
41+ throw new RuntimeException ('Unable to create receiving socket: ' . $ errstr , $ errno );
42+ }
3643
3744 $ socket = socket_import_stream ($ stream );
45+ if ($ stream === false ) {
46+ throw new RuntimeException ('Unable to access underlying socket resource ' );
47+ }
3848
3949 // allow multiple processes to bind to the same address
40- socket_set_option ($ socket , SOL_SOCKET , SO_REUSEADDR , 1 );
50+ $ ret = socket_set_option ($ socket , SOL_SOCKET , SO_REUSEADDR , 1 );
51+ if ($ ret === false ) {
52+ throw new RuntimeException ('Unable to enable SO_REUSEADDR ' );
53+ }
4154
4255 // join multicast group and bind to port
43- socket_set_option (
56+ $ ret = socket_set_option (
4457 $ socket ,
4558 IPPROTO_IP ,
4659 MCAST_JOIN_GROUP ,
4760 array ('group ' => $ parts ['host ' ], 'interface ' => 0 )
4861 );
62+ if ($ ret === false ) {
63+ throw new RuntimeException ('Unable to join multicast group ' );
64+ }
4965
5066 return new DatagramSocket ($ this ->loop , $ stream );
5167 }
0 commit comments