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\Promise\RejectedPromise; |
10
|
|
|
use React\Socket\ConnectionInterface; |
11
|
|
|
use React\Socket\ConnectorInterface; |
12
|
|
|
|
13
|
|
|
class Connector |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var ConnectionParams |
17
|
|
|
*/ |
18
|
|
|
private $params; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var MsgFactory |
22
|
|
|
*/ |
23
|
|
|
private $msgs; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var LoopInterface |
27
|
|
|
*/ |
28
|
|
|
private $eventLoop; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var \React\Socket\Connector|ConnectorInterface |
32
|
|
|
*/ |
33
|
|
|
private $socketConnector; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Connector constructor. |
37
|
|
|
* @param MsgFactory $msgs |
38
|
|
|
* @param ConnectionParams $params |
39
|
|
|
* @param LoopInterface $loop |
40
|
|
|
* @param Resolver $resolver |
41
|
|
|
* @param ConnectorInterface $connector |
42
|
|
|
*/ |
43
|
12 |
|
public function __construct(MsgFactory $msgs, ConnectionParams $params, LoopInterface $loop, Resolver $resolver, ConnectorInterface $connector = null) |
44
|
|
|
{ |
45
|
12 |
|
$this->params = $params; |
46
|
12 |
|
$this->msgs = $msgs; |
47
|
12 |
|
$this->eventLoop = $loop; |
48
|
12 |
|
if (null === $connector) { |
49
|
12 |
|
$connector = new \React\Socket\Connector($loop, [ |
50
|
12 |
|
'dns' => $resolver, |
51
|
12 |
|
'timeout' => 3, |
52
|
4 |
|
]); |
53
|
4 |
|
} |
54
|
|
|
|
55
|
12 |
|
$this->socketConnector = $connector; |
56
|
12 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param NetworkAddressInterface $remotePeer |
60
|
|
|
* @return \React\Promise\PromiseInterface |
61
|
|
|
*/ |
62
|
12 |
|
public function rawConnect(NetworkAddressInterface $remotePeer) |
63
|
|
|
{ |
64
|
12 |
|
return $this->socketConnector |
65
|
12 |
|
->connect("tcp://{$remotePeer->getIp()->getHost()}:{$remotePeer->getPort()}") |
66
|
|
|
->then(function (ConnectionInterface $stream) { |
67
|
11 |
|
$peer = new Peer($this->msgs, $this->eventLoop); |
68
|
11 |
|
$peer->setupStream($stream); |
69
|
11 |
|
return $peer; |
70
|
12 |
|
}); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param NetworkAddressInterface $remotePeer |
75
|
|
|
* @return \React\Promise\PromiseInterface |
76
|
|
|
*/ |
77
|
12 |
|
public function connect(NetworkAddressInterface $remotePeer) |
78
|
|
|
{ |
79
|
4 |
|
return $this |
80
|
12 |
|
->rawConnect($remotePeer) |
81
|
|
|
->then(function (Peer $peer) use ($remotePeer) { |
82
|
11 |
|
return $peer->outboundHandshake($remotePeer, $this->params); |
83
|
12 |
|
})->then(function (Peer $peer) { |
84
|
8 |
|
$reqService = $this->params->getRequiredServices(); |
85
|
8 |
|
if ($reqService != 0) { |
86
|
|
|
if ($reqService != ($peer->getRemoteVersion()->getServices() & $reqService)) { |
87
|
|
|
return new RejectedPromise(new \RuntimeException('peer does not satisfy required services')); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
8 |
|
return $peer; |
92
|
12 |
|
}); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|