Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ php:
- 7.4
- 7.3
- 7.2
- 7.1

before_script:
- make install build
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ cs-check: composer.lock
./vendor/bin/phpcs --standard=codestandard.xml lib tests examples

coverage: composer.lock build
./vendor/bin/phpunit --coverage-clover build/logs/clover.xml
XDEBUG_MODE=coverage ./vendor/bin/phpunit --coverage-clover build/logs/clover.xml
./vendor/bin/php-coveralls -v

composer.phar:
Expand Down
5 changes: 5 additions & 0 deletions docs/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

> PHP version `^7.1`

#### `1.4.3`

* Solve stream closure/get meta conflict (@sirn-se)
* Examples and documentation overhaul (@sirn-se)

#### `1.4.2`

* Force stream close on read error (@sirn-se)
Expand Down
4 changes: 4 additions & 0 deletions examples/echoserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
while (true) {
$message = $server->receive();
$opcode = $server->getLastOpcode();
if ($opcode == 'close') {
echo "> Closed connection\n";
continue;
}
echo "> Got '{$message}' [opcode: {$opcode}]\n";

switch ($message) {
Expand Down
8 changes: 3 additions & 5 deletions lib/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,8 @@ protected function receiveFragment(): array

if ($opcode === 'close') {
// Get the close status.
$status_bin = '';
$status = '';
if ($payload_length > 0) {
$status_bin = $payload[0] . $payload[1];
$status = bindec(sprintf("%08b%08b", ord($payload[0]), ord($payload[1])));
Expand Down Expand Up @@ -305,12 +307,9 @@ protected function write($data): void
$length = strlen($data);
$written = fwrite($this->socket, $data);
if ($written === false) {
fclose($this->socket);
$this->throwException("Failed to write {$length} bytes.");
}

if ($written < strlen($data)) {
fclose($this->socket);
$this->throwException("Could only write {$written} out of {$length} bytes.");
}
$this->logger->debug("Wrote {$written} of {$length} bytes.");
Expand All @@ -323,11 +322,9 @@ protected function read($length): string
$buffer = fread($this->socket, $length - strlen($data));
if ($buffer === false) {
$read = strlen($data);
fclose($this->socket);
$this->throwException("Broken frame, read {$read} of stated {$length} bytes.");
}
if ($buffer === '') {
fclose($this->socket);
$this->throwException("Empty read; connection dead?");
}
$data .= $buffer;
Expand All @@ -339,6 +336,7 @@ protected function throwException($message, $code = 0): void
{
$meta = $this->isConnected() ? stream_get_meta_data($this->socket) : [];
$json_meta = json_encode($meta);
fclose($this->socket);
if (!empty($meta['timed_out'])) {
$code = ConnectionException::TIMED_OUT;
$this->logger->warning("{$message}", (array)$meta);
Expand Down
2 changes: 1 addition & 1 deletion lib/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ function ($key, $value) {
throw new ConnectionException($error);
}

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

/**
Expand Down
3 changes: 2 additions & 1 deletion lib/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ protected function connect(): void
stream_set_timeout($this->socket, $this->options['timeout']);
}

$this->logger->info("Client has connected to port {$this->port}");
$this->performHandshake();
$this->logger->info("Server connected to port {$this->port}");
}

protected function performHandshake(): void
Expand Down Expand Up @@ -153,5 +153,6 @@ protected function performHandshake(): void
. "\r\n";

$this->write($header);
$this->logger->debug("Handshake on {$get_uri}");
}
}
14 changes: 7 additions & 7 deletions tests/scripts/receive-broken-read.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,6 @@
"params": [],
"return": false
},
{
"function": "fclose",
"params": [
"@mock-stream"
],
"return":true
},
{
"function": "get_resource_type",
"params": [
Expand All @@ -39,5 +32,12 @@
"unread_bytes": 2,
"seekable": false
}
},
{
"function": "fclose",
"params": [
"@mock-stream"
],
"return":true
}
]
14 changes: 7 additions & 7 deletions tests/scripts/receive-empty-read.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,6 @@
"params": [],
"return": ""
},
{
"function": "fclose",
"params": [
"@mock-stream"
],
"return":true
},
{
"function": "get_resource_type",
"params": [
Expand All @@ -39,5 +32,12 @@
"unread_bytes": 2,
"seekable": false
}
},
{
"function": "fclose",
"params": [
"@mock-stream"
],
"return":true
}
]
10 changes: 5 additions & 5 deletions tests/scripts/send-broken-write.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@
],
"return": 18
},
{
"function": "fclose",
"params": [],
"return": true
},
{
"function": "get_resource_type",
"params": [
Expand All @@ -39,5 +34,10 @@
"unread_bytes": 2,
"seekable": false
}
},
{
"function": "fclose",
"params": [],
"return": true
}
]
10 changes: 5 additions & 5 deletions tests/scripts/send-failed-write.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@
],
"return": false
},
{
"function": "fclose",
"params": [],
"return": true
},
{
"function": "get_resource_type",
"params": [
Expand All @@ -39,5 +34,10 @@
"unread_bytes": 2,
"seekable": false
}
},
{
"function": "fclose",
"params": [],
"return": true
}
]