Skip to content

Commit 6bc13b2

Browse files
authored
Merge pull request #120 from Textalk/v1.4.3
Version 1.4.3
2 parents 264ad4f + 0733ec6 commit 6bc13b2

File tree

11 files changed

+41
-32
lines changed

11 files changed

+41
-32
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ php:
44
- 7.4
55
- 7.3
66
- 7.2
7+
- 7.1
78

89
before_script:
910
- make install build

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ cs-check: composer.lock
1212
./vendor/bin/phpcs --standard=codestandard.xml lib tests examples
1313

1414
coverage: composer.lock build
15-
./vendor/bin/phpunit --coverage-clover build/logs/clover.xml
15+
XDEBUG_MODE=coverage ./vendor/bin/phpunit --coverage-clover build/logs/clover.xml
1616
./vendor/bin/php-coveralls -v
1717

1818
composer.phar:

docs/Changelog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
> PHP version `^7.1`
88
9+
#### `1.4.3`
10+
11+
* Solve stream closure/get meta conflict (@sirn-se)
12+
* Examples and documentation overhaul (@sirn-se)
13+
914
#### `1.4.2`
1015

1116
* Force stream close on read error (@sirn-se)

examples/echoserver.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@
4141
while (true) {
4242
$message = $server->receive();
4343
$opcode = $server->getLastOpcode();
44+
if ($opcode == 'close') {
45+
echo "> Closed connection\n";
46+
continue;
47+
}
4448
echo "> Got '{$message}' [opcode: {$opcode}]\n";
4549

4650
switch ($message) {

lib/Base.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,8 @@ protected function receiveFragment(): array
249249

250250
if ($opcode === 'close') {
251251
// Get the close status.
252+
$status_bin = '';
253+
$status = '';
252254
if ($payload_length > 0) {
253255
$status_bin = $payload[0] . $payload[1];
254256
$status = bindec(sprintf("%08b%08b", ord($payload[0]), ord($payload[1])));
@@ -305,12 +307,9 @@ protected function write($data): void
305307
$length = strlen($data);
306308
$written = fwrite($this->socket, $data);
307309
if ($written === false) {
308-
fclose($this->socket);
309310
$this->throwException("Failed to write {$length} bytes.");
310311
}
311-
312312
if ($written < strlen($data)) {
313-
fclose($this->socket);
314313
$this->throwException("Could only write {$written} out of {$length} bytes.");
315314
}
316315
$this->logger->debug("Wrote {$written} of {$length} bytes.");
@@ -323,11 +322,9 @@ protected function read($length): string
323322
$buffer = fread($this->socket, $length - strlen($data));
324323
if ($buffer === false) {
325324
$read = strlen($data);
326-
fclose($this->socket);
327325
$this->throwException("Broken frame, read {$read} of stated {$length} bytes.");
328326
}
329327
if ($buffer === '') {
330-
fclose($this->socket);
331328
$this->throwException("Empty read; connection dead?");
332329
}
333330
$data .= $buffer;
@@ -339,6 +336,7 @@ protected function throwException($message, $code = 0): void
339336
{
340337
$meta = $this->isConnected() ? stream_get_meta_data($this->socket) : [];
341338
$json_meta = json_encode($meta);
339+
fclose($this->socket);
342340
if (!empty($meta['timed_out'])) {
343341
$code = ConnectionException::TIMED_OUT;
344342
$this->logger->warning("{$message}", (array)$meta);

lib/Client.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ function ($key, $value) {
186186
throw new ConnectionException($error);
187187
}
188188

189-
$this->logger->info("Client connected to to {$address}");
189+
$this->logger->info("Client connected to {$address}");
190190
}
191191

192192
/**

lib/Server.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ protected function connect(): void
110110
stream_set_timeout($this->socket, $this->options['timeout']);
111111
}
112112

113+
$this->logger->info("Client has connected to port {$this->port}");
113114
$this->performHandshake();
114-
$this->logger->info("Server connected to port {$this->port}");
115115
}
116116

117117
protected function performHandshake(): void
@@ -153,5 +153,6 @@ protected function performHandshake(): void
153153
. "\r\n";
154154

155155
$this->write($header);
156+
$this->logger->debug("Handshake on {$get_uri}");
156157
}
157158
}

tests/scripts/receive-broken-read.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,6 @@
1111
"params": [],
1212
"return": false
1313
},
14-
{
15-
"function": "fclose",
16-
"params": [
17-
"@mock-stream"
18-
],
19-
"return":true
20-
},
2114
{
2215
"function": "get_resource_type",
2316
"params": [
@@ -39,5 +32,12 @@
3932
"unread_bytes": 2,
4033
"seekable": false
4134
}
35+
},
36+
{
37+
"function": "fclose",
38+
"params": [
39+
"@mock-stream"
40+
],
41+
"return":true
4242
}
4343
]

tests/scripts/receive-empty-read.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,6 @@
1111
"params": [],
1212
"return": ""
1313
},
14-
{
15-
"function": "fclose",
16-
"params": [
17-
"@mock-stream"
18-
],
19-
"return":true
20-
},
2114
{
2215
"function": "get_resource_type",
2316
"params": [
@@ -39,5 +32,12 @@
3932
"unread_bytes": 2,
4033
"seekable": false
4134
}
35+
},
36+
{
37+
"function": "fclose",
38+
"params": [
39+
"@mock-stream"
40+
],
41+
"return":true
4242
}
4343
]

tests/scripts/send-broken-write.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@
1313
],
1414
"return": 18
1515
},
16-
{
17-
"function": "fclose",
18-
"params": [],
19-
"return": true
20-
},
2116
{
2217
"function": "get_resource_type",
2318
"params": [
@@ -39,5 +34,10 @@
3934
"unread_bytes": 2,
4035
"seekable": false
4136
}
37+
},
38+
{
39+
"function": "fclose",
40+
"params": [],
41+
"return": true
4242
}
4343
]

0 commit comments

Comments
 (0)