|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Nucleus - XMPP Library for PHP |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (C) 2016, Some rights 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
|
|
|
use Kadet\Xmpp\Network\Connector; |
|
19
|
|
|
use Kadet\Xmpp\Network\TcpStream; |
|
20
|
|
|
use Kadet\Xmpp\Utils\BetterEmitter; |
|
21
|
|
|
use Kadet\Xmpp\Utils\DnsResolver; |
|
22
|
|
|
use Kadet\Xmpp\Utils\Logging; |
|
23
|
|
|
use React\EventLoop\LoopInterface; |
|
24
|
|
|
use React\Stream\DuplexStreamInterface; |
|
25
|
|
|
|
|
26
|
|
|
class TcpXmppConnector implements Connector |
|
27
|
|
|
{ |
|
28
|
|
|
use Logging, BetterEmitter; |
|
29
|
|
|
|
|
30
|
|
|
private $_host; |
|
31
|
|
|
/** @var DnsResolver */ |
|
32
|
|
|
private $_resolver; |
|
33
|
|
|
private $_loop; |
|
34
|
|
|
|
|
35
|
|
|
public function connect(array $options = []) : DuplexStreamInterface |
|
36
|
|
|
{ |
|
37
|
|
|
foreach ($this->_resolver as list($ip, $port)) { |
|
38
|
|
|
$this->getLogger()->debug('Trying to connect to {ip}:{port}', [ |
|
39
|
|
|
'ip' => $ip, |
|
40
|
|
|
'port' => $port |
|
41
|
|
|
]); |
|
42
|
|
|
|
|
43
|
|
|
if ($stream = @stream_socket_client("tcp://$ip:$port")) { |
|
44
|
|
|
stream_context_set_option($stream, 'ssl', 'peer_name', $this->_host); |
|
45
|
|
|
|
|
46
|
|
|
$stream = new TcpStream($stream, $this->_loop); |
|
47
|
|
|
$this->emit('connect', [ $stream ]); |
|
48
|
|
|
|
|
49
|
|
|
return $stream; |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
throw new \RuntimeException('Cannot connect to '.$this->_host); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function __construct(string $host, LoopInterface $loop) |
|
57
|
|
|
{ |
|
58
|
|
|
$this->_resolver = new DnsResolver([ |
|
59
|
|
|
"_xmpp-client._tcp.$host" => DNS_SRV, |
|
60
|
|
|
$host => DNS_A |
|
61
|
|
|
], 5222); |
|
62
|
|
|
|
|
63
|
|
|
$this->_host = $host; |
|
64
|
|
|
$this->_loop = $loop; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function getLoop() : LoopInterface |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->_loop; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|