1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BitWasp\Bitcoin\Networking\Peer; |
4
|
|
|
|
5
|
|
|
use BitWasp\Bitcoin\Networking\Messages\Factory as MsgFactory; |
6
|
|
|
use BitWasp\Bitcoin\Networking\Structure\NetworkAddressInterface; |
7
|
|
|
use React\Dns\Resolver\Resolver; |
8
|
|
|
use React\EventLoop\LoopInterface; |
9
|
|
|
use React\Stream\Stream; |
10
|
|
|
|
11
|
|
|
class Connector extends \React\SocketClient\Connector |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var ConnectionParams |
15
|
|
|
*/ |
16
|
|
|
private $params; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var MsgFactory |
20
|
|
|
*/ |
21
|
|
|
private $msgs; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var LoopInterface |
25
|
|
|
*/ |
26
|
|
|
private $loop; |
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Connector constructor. |
30
|
|
|
* @param MsgFactory $msgs |
31
|
|
|
* @param ConnectionParams $params |
32
|
|
|
* @param LoopInterface $loop |
33
|
|
|
* @param Resolver $resolver |
34
|
|
|
*/ |
35
|
12 |
|
public function __construct(MsgFactory $msgs, ConnectionParams $params, LoopInterface $loop, Resolver $resolver) |
36
|
|
|
{ |
37
|
12 |
|
$this->params = $params; |
38
|
12 |
|
$this->msgs = $msgs; |
39
|
12 |
|
$this->loop = $loop; |
40
|
|
|
|
41
|
12 |
|
parent::__construct($loop, $resolver); |
42
|
12 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param NetworkAddressInterface $remotePeer |
46
|
|
|
* @return \React\Promise\PromiseInterface|static |
47
|
|
|
*/ |
48
|
12 |
|
public function rawConnect(NetworkAddressInterface $remotePeer) |
49
|
|
|
{ |
50
|
12 |
|
return $this |
51
|
12 |
|
->create($remotePeer->getIp(), $remotePeer->getPort()) |
52
|
|
|
->then(function (Stream $stream) { |
53
|
12 |
|
$peer = new Peer($this->msgs, $this->loop); |
54
|
12 |
|
$peer->setupStream($stream); |
55
|
12 |
|
return $peer; |
56
|
12 |
|
}); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param NetworkAddressInterface $remotePeer |
61
|
|
|
* @return \React\Promise\PromiseInterface|static |
62
|
|
|
*/ |
63
|
12 |
|
public function connect(NetworkAddressInterface $remotePeer) |
64
|
|
|
{ |
65
|
12 |
|
return $this |
|
|
|
|
66
|
12 |
|
->rawConnect($remotePeer) |
67
|
12 |
|
->then(function (Peer $peer) use ($remotePeer) { |
68
|
12 |
|
return $peer->outboundHandshake($remotePeer, $this->params); |
69
|
12 |
|
}); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|