1010use React \Dns \Resolver \Factory as ResolverFactory ;
1111use React \SocketClient \Connector ;
1212use Clue \React \Quassel \Io \Protocol ;
13+ use React \Promise ;
14+ use InvalidArgumentException ;
1315
1416class Factory
1517{
@@ -31,23 +33,34 @@ public function __construct(LoopInterface $loop, ConnectorInterface $connector =
3133 public function createClient ($ address )
3234 {
3335 if (strpos ($ address , ':// ' ) === false ) {
34- $ address = 'dummy :// ' . $ address ;
36+ $ address = 'tcp :// ' . $ address ;
3537 }
3638 $ parts = parse_url ($ address );
3739 if (!$ parts || !isset ($ parts ['host ' ])) {
38- return ;
40+ return Promise \reject ( new InvalidArgumentException ( ' Given argument " ' . $ address . ' " is not a valid URI ' )) ;
3941 }
4042 if (!isset ($ parts ['port ' ])) {
4143 $ parts ['port ' ] = 4242 ;
4244 }
4345
44- $ connector = $ this ->connector ;
45- $ prober = $ this ->prober ;
46+ // default to automatic probing protocol unless scheme is explicitly given
47+ $ probe = 0 ;
48+ if (isset ($ parts ['scheme ' ])) {
49+ if ($ parts ['scheme ' ] === 'legacy ' ) {
50+ $ probe = Protocol::TYPE_LEGACY ;
51+ } elseif ($ parts ['scheme ' ] !== 'tcp ' ) {
52+ return Promise \reject (new InvalidArgumentException ('Given URI scheme " ' . $ parts ['scheme ' ] . '" is invalid ' ));
53+ }
54+ }
55+
56+ $ promise = $ this ->connector ->create ($ parts ['host ' ], $ parts ['port ' ]);
4657
47- return $ connector ->create ($ parts ['host ' ], $ parts ['port ' ])->then (
48- function (Stream $ stream ) use ($ prober , $ connector , $ parts ) {
49- $ probe = 0 ;
58+ // protocol probe not already set
59+ if ($ probe === 0 ) {
60+ $ connector = $ this ->connector ;
61+ $ prober = $ this ->prober ;
5062
63+ $ promise = $ promise ->then (function (Stream $ stream ) use ($ prober , &$ probe , $ connector , $ parts ) {
5164 return $ prober ->probe ($ stream )->then (
5265 function ($ ret ) use (&$ probe , $ stream ) {
5366 // probe returned successfully, create new client for this stream
@@ -56,17 +69,21 @@ function ($ret) use (&$probe, $stream) {
5669 return $ stream ;
5770 },
5871 function ($ e ) use ($ connector , $ parts ) {
72+ // probing failed
5973 if ($ e ->getCode () === Prober::ERROR_CLOSED ) {
6074 // legacy servers will terminate connection while probing
75+ // let's just open a new connection and assume default probe
6176 return $ connector ->create ($ parts ['host ' ], $ parts ['port ' ]);
6277 }
6378 throw $ e ;
6479 }
65- )->then (
66- function (Stream $ stream ) use (&$ probe ) {
67- return new Client ($ stream , Protocol::createFromProbe ($ probe ));
68- }
6980 );
81+ });
82+ }
83+
84+ return $ promise ->then (
85+ function (Stream $ stream ) use (&$ probe ) {
86+ return new Client ($ stream , Protocol::createFromProbe ($ probe ));
7087 }
7188 );
7289 }
0 commit comments