1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* XMPP Library |
4
|
|
|
* |
5
|
|
|
* Copyright (C) 2016, Some right reserved. |
6
|
|
|
* |
7
|
|
|
* @author Kacper "Kadet" Donat <[email protected]> |
8
|
|
|
* |
9
|
|
|
* Contact with author: |
10
|
|
|
* Xmpp: [email protected] |
11
|
|
|
* E-mail: [email protected] |
12
|
|
|
* |
13
|
|
|
* From Kadet with love. |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace Kadet\Xmpp\Network\Connector; |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
use Kadet\Xmpp\Network\Connector; |
20
|
|
|
use Kadet\Xmpp\Network\TcpStream; |
21
|
|
|
use Kadet\Xmpp\Utils\BetterEmitter; |
22
|
|
|
use Kadet\Xmpp\Utils\DnsResolver; |
23
|
|
|
use Kadet\Xmpp\Utils\Logging; |
24
|
|
|
use React\EventLoop\LoopInterface; |
25
|
|
|
use React\Stream\DuplexStreamInterface; |
26
|
|
|
|
27
|
|
|
class TcpXmppConnector implements Connector |
28
|
|
|
{ |
29
|
|
|
use Logging, BetterEmitter; |
30
|
|
|
|
31
|
|
|
private $_host; |
32
|
|
|
/** @var DnsResolver */ |
33
|
|
|
private $_resolver; |
34
|
|
|
private $_loop; |
35
|
|
|
|
36
|
|
|
public function connect(array $options = []) : DuplexStreamInterface |
37
|
|
|
{ |
38
|
|
|
foreach ($this->_resolver as list($ip, $port)) { |
39
|
|
|
$this->getLogger()->debug('Trying to connect to {ip}:{port}', [ |
40
|
|
|
'ip' => $ip, |
41
|
|
|
'port' => $port |
42
|
|
|
]); |
43
|
|
|
|
44
|
|
|
if($stream = @stream_socket_client("tcp://$ip:$port")) { |
45
|
|
|
$stream = new TcpStream($stream, $this->_loop); |
46
|
|
|
$this->emit('connect', [ $stream ]); |
47
|
|
|
return $stream; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
throw new \RuntimeException('Cannot connect to '.$this->_host); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function __construct(string $host, LoopInterface $loop) |
55
|
|
|
{ |
56
|
|
|
$this->_resolver = new DnsResolver([ |
57
|
|
|
"_xmpp-client._tcp.$host" => DNS_SRV, |
58
|
|
|
$host => DNS_AAAA |
59
|
|
|
]); |
60
|
|
|
|
61
|
|
|
$this->_host = $host; |
62
|
|
|
$this->_loop = $loop; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function getLoop() |
66
|
|
|
{ |
67
|
|
|
return $this->_loop; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|