|
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
|
|
|
public function __construct(MsgFactory $msgs, ConnectionParams $params, LoopInterface $loop, Resolver $resolver) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->params = $params; |
|
38
|
|
|
$this->msgs = $msgs; |
|
39
|
|
|
$this->loop = $loop; |
|
40
|
|
|
|
|
41
|
|
|
parent::__construct($loop, $resolver); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param NetworkAddressInterface $remotePeer |
|
46
|
|
|
* @return \React\Promise\PromiseInterface|static |
|
47
|
|
|
*/ |
|
48
|
|
|
public function rawConnect(NetworkAddressInterface $remotePeer) |
|
49
|
|
|
{ |
|
50
|
|
|
return $this |
|
51
|
|
|
->create($remotePeer->getIp(), $remotePeer->getPort()) |
|
52
|
|
|
->then(function (Stream $stream) { |
|
53
|
|
|
$peer = new Peer($this->msgs, $this->loop); |
|
54
|
|
|
$peer->setupStream($stream); |
|
55
|
|
|
return $peer; |
|
56
|
|
|
}); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param NetworkAddressInterface $remotePeer |
|
61
|
|
|
* @return \React\Promise\PromiseInterface|static |
|
62
|
|
|
*/ |
|
63
|
|
|
public function connect(NetworkAddressInterface $remotePeer) |
|
64
|
|
|
{ |
|
65
|
|
|
return $this |
|
|
|
|
|
|
66
|
|
|
->rawConnect($remotePeer) |
|
67
|
|
|
->then(function (Peer $peer) use ($remotePeer) { |
|
68
|
|
|
return $peer->outboundHandshake($remotePeer, $this->params); |
|
69
|
|
|
}); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|