Skip to content

Commit 1c8ffc2

Browse files
Merge pull request #21 from stefanak-michal/issue-18-socket_non_static
removed static Socket access and passing instance
2 parents ae7eb19 + 82c2e22 commit 1c8ffc2

File tree

7 files changed

+83
-72
lines changed

7 files changed

+83
-72
lines changed

Bolt.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ final class Bolt
3131
*/
3232
private $protocol;
3333

34+
/**
35+
* @var Socket
36+
*/
37+
private $socket;
38+
3439
/**
3540
* @var array
3641
*/
@@ -72,7 +77,7 @@ final class Bolt
7277
*/
7378
public function __construct(string $ip = '127.0.0.1', int $port = 7687, int $timeout = 15)
7479
{
75-
Socket::initialize($ip, $port, $timeout);
80+
$this->socket = new Socket($ip, $port, $timeout);
7681

7782
$packerClass = "\\Bolt\\PackStream\\v" . $this->packStreamVersion . "\\Packer";
7883
if (!class_exists($packerClass)) {
@@ -124,8 +129,8 @@ private function handshake(): bool
124129
if (self::$debug)
125130
echo 'HANDSHAKE';
126131

127-
Socket::write(chr(0x60) . chr(0x60) . chr(0xb0) . chr(0x17));
128-
Socket::write($this->packProtocolVersions());
132+
$this->socket->write(chr(0x60) . chr(0x60) . chr(0xb0) . chr(0x17));
133+
$this->socket->write($this->packProtocolVersions());
129134

130135
$this->unpackProtocolVersion();
131136
if (empty($this->version)) {
@@ -137,7 +142,7 @@ private function handshake(): bool
137142
if (!class_exists($protocolClass)) {
138143
Bolt::error('Requested Protocol version (' . $this->version . ') not yet implemented');
139144
} else {
140-
$this->protocol = new $protocolClass($this->packer, $this->unpacker);
145+
$this->protocol = new $protocolClass($this->packer, $this->unpacker, $this->socket);
141146
}
142147

143148
return $this->protocol instanceof AProtocol;
@@ -150,7 +155,7 @@ private function unpackProtocolVersion()
150155
{
151156
$result = [];
152157

153-
foreach (str_split(Socket::readBuffer(4)) as $ch)
158+
foreach (str_split($this->socket->readBuffer(4)) as $ch)
154159
$result[] = unpack('C', $ch)[1] ?? 0;
155160

156161
$result = array_filter($result);
@@ -387,7 +392,7 @@ public static function printHex(string $str, bool $write = true)
387392
}
388393

389394
/**
390-
* Close socket
395+
* Say goodbye
391396
*/
392397
public function __destruct()
393398
{
@@ -396,8 +401,6 @@ public function __destruct()
396401
echo 'GOODBYE';
397402
$this->protocol->goodbye();
398403
}
399-
400-
@socket_close(Socket::$socket);
401404
}
402405

403406
}

Socket.php

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,45 +17,41 @@ final class Socket
1717
/**
1818
* @var resource
1919
*/
20-
public static $socket;
20+
private $socket;
2121

2222
/**
2323
* @param string $ip
2424
* @param int $port
2525
* @param int $timeout
2626
* @throws Exception
2727
*/
28-
public static function initialize(string $ip, int $port, int $timeout)
28+
public function __construct(string $ip, int $port, int $timeout)
2929
{
30-
if (is_resource(self::$socket))
31-
return;
32-
3330
if (!extension_loaded('sockets')) {
3431
Bolt::error('PHP Extension sockets not enabled');
3532
return;
3633
}
3734

38-
self::$socket = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
39-
if (!is_resource(self::$socket)) {
35+
$this->socket = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
36+
if (!is_resource($this->socket)) {
4037
Bolt::error('Cannot create socket');
4138
return;
4239
}
4340

44-
if (socket_set_block(self::$socket) === false) {
41+
if (socket_set_block($this->socket) === false) {
4542
Bolt::error('Cannot set socket into blocking mode');
4643
return;
4744
}
4845

49-
socket_set_option(self::$socket, SOL_TCP, TCP_NODELAY, 1);
50-
socket_set_option(self::$socket, SOL_SOCKET, SO_KEEPALIVE, 1);
51-
socket_set_option(self::$socket, SOL_SOCKET, SO_RCVTIMEO, ['sec' => $timeout, 'usec' => 0]);
52-
socket_set_option(self::$socket, SOL_SOCKET, SO_SNDTIMEO, ['sec' => $timeout, 'usec' => 0]);
46+
socket_set_option($this->socket, SOL_TCP, TCP_NODELAY, 1);
47+
socket_set_option($this->socket, SOL_SOCKET, SO_KEEPALIVE, 1);
48+
socket_set_option($this->socket, SOL_SOCKET, SO_RCVTIMEO, ['sec' => $timeout, 'usec' => 0]);
49+
socket_set_option($this->socket, SOL_SOCKET, SO_SNDTIMEO, ['sec' => $timeout, 'usec' => 0]);
5350

54-
$conn = socket_connect(self::$socket, $ip, $port);
51+
$conn = socket_connect($this->socket, $ip, $port);
5552
if (!$conn) {
56-
$code = socket_last_error(self::$socket);
53+
$code = socket_last_error($this->socket);
5754
Bolt::error(socket_strerror($code), $code);
58-
return;
5955
}
6056
}
6157

@@ -64,9 +60,9 @@ public static function initialize(string $ip, int $port, int $timeout)
6460
* @param string $buffer
6561
* @throws Exception
6662
*/
67-
public static function write(string $buffer)
63+
public function write(string $buffer)
6864
{
69-
if (!is_resource(self::$socket)) {
65+
if (!is_resource($this->socket)) {
7066
Bolt::error('Not initialized socket');
7167
return;
7268
}
@@ -78,9 +74,9 @@ public static function write(string $buffer)
7874
Bolt::printHex($buffer);
7975

8076
while ($sent < $size) {
81-
$sent = socket_write(self::$socket, $buffer, $size);
77+
$sent = socket_write($this->socket, $buffer, $size);
8278
if ($sent === false) {
83-
$code = socket_last_error(self::$socket);
79+
$code = socket_last_error($this->socket);
8480
Bolt::error(socket_strerror($code), $code);
8581
return;
8682
}
@@ -96,20 +92,20 @@ public static function write(string $buffer)
9692
* @return mixed
9793
* @throws Exception
9894
*/
99-
public static function read(IUnpacker $unpacker)
95+
public function read(IUnpacker $unpacker)
10096
{
101-
if (!is_resource(self::$socket)) {
97+
if (!is_resource($this->socket)) {
10298
Bolt::error('Not initialized socket');
10399
return;
104100
}
105101

106102
$msg = '';
107103
while (true) {
108-
$header = self::readBuffer(2);
104+
$header = $this->readBuffer(2);
109105
if (ord($header[0]) == 0x00 && ord($header[1]) == 0x00)
110106
break;
111107
$length = unpack('n', $header)[1] ?? 0;
112-
$msg .= self::readBuffer($length);
108+
$msg .= $this->readBuffer($length);
113109
}
114110

115111
$output = null;
@@ -134,18 +130,26 @@ public static function read(IUnpacker $unpacker)
134130
* @return string
135131
* @throws Exception
136132
*/
137-
public static function readBuffer(int $length = 2048): string
133+
public function readBuffer(int $length = 2048): string
138134
{
139135
$output = '';
140136
do {
141-
$readed = socket_read(self::$socket, $length - mb_strlen($output, '8bit'), PHP_BINARY_READ);
137+
$readed = socket_read($this->socket, $length - mb_strlen($output, '8bit'), PHP_BINARY_READ);
142138
if ($readed === false) {
143-
$code = socket_last_error(self::$socket);
139+
$code = socket_last_error($this->socket);
144140
Bolt::error(socket_strerror($code), $code);
145141
} else {
146142
$output .= $readed;
147143
}
148144
} while (mb_strlen($output, '8bit') < $length);
149145
return $output;
150146
}
147+
148+
/**
149+
* Close socket
150+
*/
151+
public function __destruct()
152+
{
153+
@socket_close($this->socket);
154+
}
151155
}

protocol/AProtocol.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Bolt\protocol;
44

55
use Bolt\PackStream\{IPacker, IUnpacker};
6+
use Bolt\Socket;
67

78
/**
89
* Abstract class AProtocol
@@ -29,15 +30,22 @@ abstract class AProtocol implements IProtocol
2930
*/
3031
protected $unpacker;
3132

33+
/**
34+
* @var Socket
35+
*/
36+
protected $socket;
37+
3238
/**
3339
* AProtocol constructor.
3440
* @param IPacker $packer
3541
* @param IUnpacker $unpacker
42+
* @param Socket $socket
3643
*/
37-
public function __construct(IPacker $packer, IUnpacker $unpacker)
44+
public function __construct(IPacker $packer, IUnpacker $unpacker, Socket $socket)
3845
{
3946
$this->packer = $packer;
4047
$this->unpacker = $unpacker;
48+
$this->socket = $socket;
4149
}
4250

4351
public function begin(...$args): bool

protocol/V1.php

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Bolt\protocol;
44

55
use Bolt\Bolt;
6-
use Bolt\Socket;
76
use Exception;
87

98
/**
@@ -34,9 +33,9 @@ public function init(...$args): bool
3433
return false;
3534
}
3635

37-
Socket::write($msg);
36+
$this->socket->write($msg);
3837

39-
list($signature, $output) = Socket::read($this->unpacker);
38+
list($signature, $output) = $this->socket->read($this->unpacker);
4039
if ($signature == self::FAILURE) {
4140
try {
4241
$msg = $this->packer->pack(0x0E);
@@ -46,7 +45,7 @@ public function init(...$args): bool
4645
}
4746

4847
//AckFailure after init do not respond with any message
49-
Socket::write($msg);
48+
$this->socket->write($msg);
5049
Bolt::error($output['message'], $output['code']);
5150
}
5251

@@ -67,9 +66,9 @@ public function run(...$args)
6766
return false;
6867
}
6968

70-
Socket::write($msg);
69+
$this->socket->write($msg);
7170

72-
list($signature, $output) = Socket::read($this->unpacker);
71+
list($signature, $output) = $this->socket->read($this->unpacker);
7372
if ($signature == self::FAILURE) {
7473
$this->ackFailure();
7574
Bolt::error($output['message'], $output['code']);
@@ -86,11 +85,11 @@ public function pullAll(...$args)
8685
return false;
8786
}
8887

89-
Socket::write($msg);
88+
$this->socket->write($msg);
9089

9190
$output = [];
9291
do {
93-
list($signature, $ret) = Socket::read($this->unpacker);
92+
list($signature, $ret) = $this->socket->read($this->unpacker);
9493
$output[] = $ret;
9594
} while ($signature == self::RECORD);
9695

@@ -112,9 +111,9 @@ public function discardAll(...$args): bool
112111
return false;
113112
}
114113

115-
Socket::write($msg);
114+
$this->socket->write($msg);
116115

117-
list($signature,) = Socket::read($this->unpacker);
116+
list($signature,) = $this->socket->read($this->unpacker);
118117
return $signature == self::SUCCESS;
119118
}
120119

@@ -132,9 +131,9 @@ private function ackFailure(): bool
132131
return false;
133132
}
134133

135-
Socket::write($msg);
134+
$this->socket->write($msg);
136135

137-
list($signature,) = Socket::read($this->unpacker);
136+
list($signature,) = $this->socket->read($this->unpacker);
138137
return $signature == self::SUCCESS;
139138
}
140139

@@ -147,9 +146,9 @@ public function reset(...$args)
147146
return false;
148147
}
149148

150-
Socket::write($msg);
149+
$this->socket->write($msg);
151150

152-
list($signature,) = Socket::read($this->unpacker);
151+
list($signature,) = $this->socket->read($this->unpacker);
153152
return $signature == self::SUCCESS;
154153
}
155154

0 commit comments

Comments
 (0)