Skip to content

Commit 034be7c

Browse files
Merge pull request #27 from stefanak-michal/socket_read_refactor
refactor of socket read method
2 parents 6d46492 + 036c057 commit 034be7c

File tree

9 files changed

+133
-108
lines changed

9 files changed

+133
-108
lines changed

Bolt.php

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ final class Bolt
5454
/**
5555
* @var string
5656
*/
57-
public static $scheme = 'basic';
57+
private $scheme = 'basic';
5858

5959
/**
6060
* Custom error handler instead of throwing Exceptions
@@ -106,10 +106,23 @@ public function setProtocolVersions(...$v): Bolt
106106

107107
/**
108108
* @param int $version
109+
* @return Bolt
109110
*/
110111
public function setPackStreamVersion(int $version = 1)
111112
{
112113
$this->packStreamVersion = $version;
114+
return $this;
115+
}
116+
117+
/**
118+
* @param string $scheme
119+
* @return Bolt
120+
*/
121+
public function setScheme(string $scheme = 'basic')
122+
{
123+
if (in_array($scheme, ['none', 'basic', 'kerberos']))
124+
$this->scheme = $scheme;
125+
return $this;
113126
}
114127

115128
/**
@@ -155,7 +168,7 @@ private function unpackProtocolVersion()
155168
{
156169
$result = [];
157170

158-
foreach (str_split($this->socket->readBuffer(4)) as $ch)
171+
foreach (str_split($this->socket->read(4)) as $ch)
159172
$result[] = unpack('C', $ch)[1] ?? 0;
160173

161174
$result = array_filter($result);
@@ -193,7 +206,7 @@ private function packProtocolVersions(): string
193206
/**
194207
* Send INIT message
195208
* @version <3
196-
* @param string $name
209+
* @param string $name should conform to "Name/Version" https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent
197210
* @param string $user
198211
* @param string $password
199212
* @param array $routing routing::Dictionary(address::String)
@@ -211,7 +224,7 @@ public function init(string $name, string $user, string $password, array $routin
211224
if (self::$debug)
212225
echo 'INIT';
213226

214-
return $this->protocol->init($name, Bolt::$scheme, $user, $password, $routing);
227+
return $this->protocol->init($name, $this->scheme, $user, $password, $routing);
215228
}
216229

217230
/**
@@ -373,23 +386,6 @@ public static function error(string $msg, string $code = '')
373386
throw new Exception($msg);
374387
}
375388
}
376-
377-
/**
378-
* Print buffer as HEX
379-
* @param string $str
380-
* @param bool $write
381-
*/
382-
public static function printHex(string $str, bool $write = true)
383-
{
384-
$str = implode(unpack('H*', $str));
385-
echo '<pre>';
386-
echo $write ? '> ' : '< ';
387-
foreach (str_split($str, 8) as $chunk) {
388-
echo implode(' ', str_split($chunk, 2));
389-
echo ' ';
390-
}
391-
echo '</pre>';
392-
}
393389

394390
/**
395391
* Say goodbye

PackStream/IUnpacker.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ interface IUnpacker
1616
* @param int $signature
1717
* @return mixed
1818
*/
19-
public function unpack(string $msg, int &$signature = 0);
19+
public function unpack(string $msg, int &$signature);
2020
}

PackStream/v1/Unpacker.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class Unpacker implements IUnpacker
5757
* @return mixed
5858
* @throws Exception
5959
*/
60-
public function unpack(string $msg, int &$signature = 0)
60+
public function unpack(string $msg, int &$signature)
6161
{
6262
if (empty($msg)) {
6363
return null;

Socket.php

Lines changed: 30 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
namespace Bolt;
44

5-
use Bolt\PackStream\IUnpacker;
6-
use Exception;
7-
85
/**
96
* Static class Socket
107
*
@@ -23,7 +20,7 @@ final class Socket
2320
* @param string $ip
2421
* @param int $port
2522
* @param int $timeout
26-
* @throws Exception
23+
* @throws \Exception
2724
*/
2825
public function __construct(string $ip, int $port, int $timeout)
2926
{
@@ -56,9 +53,9 @@ public function __construct(string $ip, int $port, int $timeout)
5653
}
5754

5855
/**
59-
* Write to socket
56+
* Write buffer to socket
6057
* @param string $buffer
61-
* @throws Exception
58+
* @throws \Exception
6259
*/
6360
public function write(string $buffer)
6461
{
@@ -71,7 +68,7 @@ public function write(string $buffer)
7168
$sent = 0;
7269

7370
if (Bolt::$debug)
74-
Bolt::printHex($buffer);
71+
$this->printHex($buffer);
7572

7673
while ($sent < $size) {
7774
$sent = socket_write($this->socket, $buffer, $size);
@@ -86,58 +83,21 @@ public function write(string $buffer)
8683
}
8784
}
8885

89-
/**
90-
* Read unpacked from socket
91-
* @param IUnpacker $unpacker
92-
* @return mixed
93-
* @throws Exception
94-
*/
95-
public function read(IUnpacker $unpacker)
96-
{
97-
if (!is_resource($this->socket)) {
98-
Bolt::error('Not initialized socket');
99-
return;
100-
}
101-
102-
$msg = '';
103-
while (true) {
104-
$header = $this->readBuffer(2);
105-
if (ord($header[0]) == 0x00 && ord($header[1]) == 0x00)
106-
break;
107-
$length = unpack('n', $header)[1] ?? 0;
108-
$msg .= $this->readBuffer($length);
109-
}
110-
111-
$output = null;
112-
$signature = 0;
113-
if (!empty($msg)) {
114-
if (Bolt::$debug)
115-
Bolt::printHex($msg, false);
116-
117-
try {
118-
$output = $unpacker->unpack($msg, $signature);
119-
} catch (Exception $ex) {
120-
Bolt::error($ex->getMessage());
121-
}
122-
}
123-
124-
return [$signature, $output];
125-
}
126-
12786
/**
12887
* Read buffer from socket
12988
* @param int $length
13089
* @return string
131-
* @throws Exception
90+
* @throws \Exception
13291
*/
133-
public function readBuffer(int $length = 2048): string
92+
public function read(int $length = 2048): string
13493
{
94+
$output = '';
95+
13596
if (!is_resource($this->socket)) {
13697
Bolt::error('Not initialized socket');
137-
return;
98+
return $output;
13899
}
139100

140-
$output = '';
141101
do {
142102
$readed = socket_read($this->socket, $length - mb_strlen($output, '8bit'), PHP_BINARY_READ);
143103
if ($readed === false) {
@@ -147,9 +107,30 @@ public function readBuffer(int $length = 2048): string
147107
$output .= $readed;
148108
}
149109
} while (mb_strlen($output, '8bit') < $length);
110+
111+
if (Bolt::$debug)
112+
$this->printHex($output, false);
113+
150114
return $output;
151115
}
152116

117+
/**
118+
* Print buffer as HEX
119+
* @param string $str
120+
* @param bool $write
121+
*/
122+
private function printHex(string $str, bool $write = true)
123+
{
124+
$str = implode(unpack('H*', $str));
125+
echo '<pre>';
126+
echo $write ? '> ' : '< ';
127+
foreach (str_split($str, 8) as $chunk) {
128+
echo implode(' ', str_split($chunk, 2));
129+
echo ' ';
130+
}
131+
echo '</pre>';
132+
}
133+
153134
/**
154135
* Close socket
155136
*/

protocol/AProtocol.php

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
namespace Bolt\protocol;
44

5+
use Bolt\Bolt;
56
use Bolt\PackStream\{IPacker, IUnpacker};
67
use Bolt\Socket;
8+
use Exception;
79

810
/**
911
* Abstract class AProtocol
@@ -33,7 +35,7 @@ abstract class AProtocol implements IProtocol
3335
/**
3436
* @var Socket
3537
*/
36-
protected $socket;
38+
private $socket;
3739

3840
/**
3941
* AProtocol constructor.
@@ -68,4 +70,44 @@ public function goodbye(...$args)
6870

6971
}
7072

73+
/**
74+
* Write to socket
75+
* @param string $buffer
76+
* @throws Exception
77+
*/
78+
protected function write(string $buffer)
79+
{
80+
$this->socket->write($buffer);
81+
}
82+
83+
/**
84+
* Read from socket
85+
* @param int|null $signature
86+
* @return mixed|null
87+
* @throws Exception
88+
*/
89+
protected function read(?int &$signature)
90+
{
91+
$msg = '';
92+
while (true) {
93+
$header = $this->socket->read(2);
94+
if (ord($header[0]) == 0x00 && ord($header[1]) == 0x00)
95+
break;
96+
$length = unpack('n', $header)[1] ?? 0;
97+
$msg .= $this->socket->read($length);
98+
}
99+
100+
$output = null;
101+
$signature = 0;
102+
if (!empty($msg)) {
103+
try {
104+
$output = $this->unpacker->unpack($msg, $signature);
105+
} catch (Exception $ex) {
106+
Bolt::error($ex->getMessage());
107+
}
108+
}
109+
110+
return $output;
111+
}
112+
71113
}

protocol/V1.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ public function init(...$args): bool
3333
return false;
3434
}
3535

36-
$this->socket->write($msg);
36+
$this->write($msg);
37+
$output = $this->read($signature);
3738

38-
list($signature, $output) = $this->socket->read($this->unpacker);
3939
if ($signature == self::FAILURE) {
4040
try {
4141
$msg = $this->packer->pack(0x0E);
@@ -45,7 +45,7 @@ public function init(...$args): bool
4545
}
4646

4747
//AckFailure after init do not respond with any message
48-
$this->socket->write($msg);
48+
$this->write($msg);
4949
Bolt::error($output['message'], $output['code']);
5050
}
5151

@@ -66,13 +66,14 @@ public function run(...$args)
6666
return false;
6767
}
6868

69-
$this->socket->write($msg);
69+
$this->write($msg);
70+
$output = $this->read($signature);
7071

71-
list($signature, $output) = $this->socket->read($this->unpacker);
7272
if ($signature == self::FAILURE) {
7373
$this->ackFailure();
7474
Bolt::error($output['message'], $output['code']);
7575
}
76+
7677
return $signature == self::SUCCESS ? $output : false;
7778
}
7879

@@ -85,21 +86,20 @@ public function pullAll(...$args)
8586
return false;
8687
}
8788

88-
$this->socket->write($msg);
89+
$this->write($msg);
8990

9091
$output = [];
9192
do {
92-
list($signature, $ret) = $this->socket->read($this->unpacker);
93+
$ret = $this->read($signature);
9394
$output[] = $ret;
9495
} while ($signature == self::RECORD);
9596

9697
if ($signature == self::FAILURE) {
9798
$this->ackFailure();
9899
Bolt::error($ret['message'], $ret['code']);
99-
$output = false;
100100
}
101101

102-
return $output;
102+
return $signature == self::SUCCESS ? $output : false;
103103
}
104104

105105
public function discardAll(...$args): bool
@@ -111,9 +111,9 @@ public function discardAll(...$args): bool
111111
return false;
112112
}
113113

114-
$this->socket->write($msg);
114+
$this->write($msg);
115+
$this->read($signature);
115116

116-
list($signature,) = $this->socket->read($this->unpacker);
117117
return $signature == self::SUCCESS;
118118
}
119119

@@ -131,9 +131,9 @@ private function ackFailure(): bool
131131
return false;
132132
}
133133

134-
$this->socket->write($msg);
134+
$this->write($msg);
135+
$this->read($signature);
135136

136-
list($signature,) = $this->socket->read($this->unpacker);
137137
return $signature == self::SUCCESS;
138138
}
139139

@@ -146,9 +146,9 @@ public function reset(...$args)
146146
return false;
147147
}
148148

149-
$this->socket->write($msg);
149+
$this->write($msg);
150+
$this->read($signature);
150151

151-
list($signature,) = $this->socket->read($this->unpacker);
152152
return $signature == self::SUCCESS;
153153
}
154154

0 commit comments

Comments
 (0)