|
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\EventLoop\LoopInterface; |
|
8
|
|
|
use React\Promise\RejectedPromise; |
|
9
|
|
|
use React\Socket\ConnectionInterface; |
|
10
|
|
|
|
|
11
|
|
|
class 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 $eventLoop; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var \React\Socket\Connector |
|
30
|
|
|
*/ |
|
31
|
|
|
private $socketConnector; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Connector constructor. |
|
35
|
|
|
* @param MsgFactory $msgs |
|
36
|
|
|
* @param ConnectionParams $params |
|
37
|
|
|
* @param LoopInterface $loop |
|
38
|
|
|
* @param array $settings |
|
39
|
|
|
*/ |
|
40
|
12 |
|
public function __construct(MsgFactory $msgs, ConnectionParams $params, LoopInterface $loop, array $settings) |
|
41
|
|
|
{ |
|
42
|
12 |
|
$this->params = $params; |
|
43
|
12 |
|
$this->msgs = $msgs; |
|
44
|
12 |
|
$this->eventLoop = $loop; |
|
45
|
12 |
|
$this->socketConnector = new \React\Socket\Connector($loop, $settings); |
|
46
|
12 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param NetworkAddressInterface $remotePeer |
|
50
|
|
|
* @return \React\Promise\PromiseInterface |
|
51
|
|
|
*/ |
|
52
|
12 |
|
public function rawConnect(NetworkAddressInterface $remotePeer) |
|
53
|
|
|
{ |
|
54
|
12 |
|
return $this->socketConnector |
|
55
|
12 |
|
->connect("tcp://{$remotePeer->getIp()->getHost()}:{$remotePeer->getPort()}") |
|
56
|
|
|
->then(function (ConnectionInterface $stream) { |
|
57
|
12 |
|
$peer = new Peer($this->msgs, $this->eventLoop); |
|
58
|
12 |
|
$peer->setupStream($stream); |
|
59
|
12 |
|
return $peer; |
|
60
|
12 |
|
}); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param NetworkAddressInterface $remotePeer |
|
65
|
|
|
* @return \React\Promise\PromiseInterface |
|
66
|
|
|
*/ |
|
67
|
12 |
|
public function connect(NetworkAddressInterface $remotePeer) |
|
68
|
|
|
{ |
|
69
|
4 |
|
return $this |
|
70
|
12 |
|
->rawConnect($remotePeer) |
|
71
|
|
|
->then(function (Peer $peer) use ($remotePeer) { |
|
72
|
12 |
|
return $peer->outboundHandshake($remotePeer, $this->params); |
|
73
|
12 |
|
})->then(function (Peer $peer) { |
|
74
|
9 |
|
$reqService = $this->params->getRequiredServices(); |
|
75
|
9 |
|
if ($reqService != 0) { |
|
76
|
|
|
if ($reqService != ($peer->getRemoteVersion()->getServices() & $reqService)) { |
|
77
|
|
|
return new RejectedPromise(new \RuntimeException('peer does not satisfy required services')); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
9 |
|
return $peer; |
|
82
|
12 |
|
}); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|