|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BitWasp\Bitcoin\Networking\Peer; |
|
4
|
|
|
|
|
5
|
|
|
use BitWasp\Bitcoin\Networking\Dns\Resolver; |
|
6
|
|
|
use BitWasp\Bitcoin\Networking\Structure\NetworkAddress; |
|
7
|
|
|
use BitWasp\Bitcoin\Networking\Structure\NetworkAddressInterface; |
|
8
|
|
|
use BitWasp\Buffertools\Buffer; |
|
9
|
|
|
use Doctrine\Common\Cache\Cache; |
|
10
|
|
|
use React\EventLoop\LoopInterface; |
|
11
|
|
|
use React\Socket\Server; |
|
12
|
|
|
use React\SocketClient\Connector; |
|
13
|
|
|
|
|
14
|
|
|
class Factory |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var Resolver |
|
18
|
|
|
*/ |
|
19
|
|
|
private $dns; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var NetworkAddressInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
private $local; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var LoopInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
private $loop; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var \BitWasp\Bitcoin\Networking\Messages\Factory |
|
33
|
|
|
*/ |
|
34
|
|
|
private $msgFactory; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param Resolver $dns |
|
38
|
|
|
* @param \BitWasp\Bitcoin\Networking\Messages\Factory $factory |
|
39
|
|
|
* @param LoopInterface $loop |
|
40
|
|
|
* @param NetworkAddressInterface $localAddress |
|
41
|
|
|
*/ |
|
42
|
33 |
|
public function __construct( |
|
43
|
|
|
Resolver $dns, |
|
44
|
|
|
\BitWasp\Bitcoin\Networking\Messages\Factory $factory, |
|
45
|
|
|
LoopInterface $loop, |
|
46
|
|
|
NetworkAddressInterface $localAddress = null |
|
47
|
|
|
) { |
|
48
|
33 |
|
$this->dns = $dns; |
|
49
|
33 |
|
$this->msgFactory = $factory; |
|
50
|
33 |
|
$this->loop = $loop; |
|
51
|
33 |
|
$this->setLocalAddr($localAddress ?: $this->getAddress('0.0.0.0')); |
|
52
|
33 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return Connector |
|
56
|
|
|
*/ |
|
57
|
14 |
|
public function getConnector() |
|
58
|
|
|
{ |
|
59
|
14 |
|
return new Connector($this->loop, $this->dns); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return Server |
|
64
|
|
|
*/ |
|
65
|
9 |
|
public function getServer() |
|
66
|
|
|
{ |
|
67
|
9 |
|
return new Server($this->loop); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param NetworkAddressInterface $localAddress |
|
72
|
|
|
*/ |
|
73
|
33 |
|
public function setLocalAddr(NetworkAddressInterface $localAddress) |
|
74
|
|
|
{ |
|
75
|
33 |
|
$this->local = $localAddress; |
|
76
|
33 |
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param string $ipAddress |
|
80
|
|
|
* @param int $port |
|
81
|
|
|
* @param Buffer|null $services |
|
82
|
|
|
* @return NetworkAddress |
|
83
|
|
|
*/ |
|
84
|
33 |
|
public function getAddress($ipAddress, $port = 8333, Buffer $services = null) |
|
85
|
|
|
{ |
|
86
|
33 |
|
return new NetworkAddress( |
|
87
|
33 |
|
$services ?: Buffer::hex('0000000000000001'), |
|
88
|
33 |
|
$ipAddress, |
|
89
|
|
|
$port |
|
90
|
33 |
|
); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @return Peer |
|
95
|
|
|
*/ |
|
96
|
14 |
|
public function getPeer() |
|
97
|
|
|
{ |
|
98
|
14 |
|
return new Peer( |
|
99
|
14 |
|
$this->local, |
|
100
|
14 |
|
$this->msgFactory, |
|
101
|
14 |
|
$this->loop |
|
102
|
14 |
|
); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @return Locator |
|
107
|
|
|
*/ |
|
108
|
15 |
|
public function getLocator() |
|
109
|
|
|
{ |
|
110
|
15 |
|
return new Locator($this->dns); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @return PacketHandler |
|
115
|
|
|
*/ |
|
116
|
3 |
|
public function getPacketHandler() |
|
117
|
|
|
{ |
|
118
|
3 |
|
return new PacketHandler(); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param bool|false $shouldRelay |
|
123
|
|
|
* @return Manager |
|
124
|
|
|
*/ |
|
125
|
12 |
|
public function getManager($shouldRelay = false) |
|
126
|
|
|
{ |
|
127
|
12 |
|
return new Manager($this, $shouldRelay); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @param Server $server |
|
132
|
|
|
* @return Listener |
|
133
|
|
|
*/ |
|
134
|
9 |
|
public function getListener(Server $server) |
|
135
|
|
|
{ |
|
136
|
9 |
|
return new Listener($this->local, $this->msgFactory, $server, $this->loop); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|