Skip to content

Commit

Permalink
Merge pull request #101 from Textalk/ping_read
Browse files Browse the repository at this point in the history
Ping & Pong, handle internally, continue reading
  • Loading branch information
sirn-se authored Oct 30, 2020
2 parents eaa1b58 + d3461d4 commit 0e1ffc4
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 55 deletions.
19 changes: 13 additions & 6 deletions lib/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,6 @@ protected function receiveFragment()
}
$opcode = $opcode_ints[$opcode_int];

// Record the opcode if we are not receiving a continutation fragment
if ($opcode !== 'continuation') {
$this->last_opcode = $opcode;
}

// Masking?
$mask = (bool) (ord($data[1]) >> 7); // Bit 0 in byte 1

Expand Down Expand Up @@ -234,10 +229,22 @@ protected function receiveFragment()
}
}

// if we received a ping, send a pong
// if we received a ping, send a pong and wait for the next message
if ($opcode === 'ping') {
$this->logger->debug("Received 'ping', sending 'pong'.");
$this->send($payload, 'pong', true);
return [null, false];
}

// if we received a pong, wait for the next message
if ($opcode === 'pong') {
$this->logger->debug("Received 'pong'.");
return [null, false];
}

// Record the opcode if we are not receiving a continutation fragment
if ($opcode !== 'continuation') {
$this->last_opcode = $opcode;
}

if ($opcode === 'close') {
Expand Down
12 changes: 2 additions & 10 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,19 +162,11 @@ public function testPingPong(): void

MockSocket::initialize('ping-pong', $this);
$client->send('Server ping', 'ping');
$message = $client->receive();
$this->assertEquals('Server ping', $message);
$this->assertEquals('pong', $client->getLastOpcode());

$client->send('', 'ping');
$message = $client->receive();
$this->assertEquals('', $message);
$this->assertEquals('pong', $client->getLastOpcode());

$message = $client->receive();
$this->assertEquals('Client ping', $message);
$this->assertEquals('Receiving a message', $message);
$this->assertEquals('text', $client->getLastOpcode());
$this->assertTrue(MockSocket::isEmpty());
$this->assertEquals('ping', $client->getLastOpcode());
}

public function testRemoteClose(): void
Expand Down
13 changes: 2 additions & 11 deletions tests/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,20 +165,11 @@ public function testPingPong(): void

MockSocket::initialize('ping-pong', $this);
$server->send('Server ping', 'ping');
$message = $server->receive();
$this->assertEquals('Server ping', $message);
$this->assertEquals('pong', $server->getLastOpcode());

$server->send('', 'ping');
$message = $server->receive();
$this->assertEquals('', $message);
$this->assertEquals('pong', $server->getLastOpcode());

$message = $server->receive();
$this->assertEquals('Client ping', $message);

$this->assertEquals('Receiving a message', $message);
$this->assertEquals('text', $server->getLastOpcode());
$this->assertTrue(MockSocket::isEmpty());
$this->assertEquals('ping', $server->getLastOpcode());
}

public function testRemoteClose(): void
Expand Down
69 changes: 41 additions & 28 deletions tests/scripts/ping-pong.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@
],
"return": "stream"
},
{
"function": "fwrite",
"params": [
"@mock-stream"
],
"return": 6
},
{
"function": "get_resource_type",
"params": [
"@mock-stream"
],
"return": "stream"
},
{
"function": "fread",
"params": [
Expand Down Expand Up @@ -47,27 +61,6 @@
"return-op": "chr-array",
"return": [82, 100, 115, 119, 100, 115, 33, 113, 104, 111, 102]
},
{
"function": "get_resource_type",
"params": [
"@mock-stream"
],
"return": "stream"
},
{
"function": "fwrite",
"params": [
"@mock-stream"
],
"return": 6
},
{
"function": "get_resource_type",
"params": [
"@mock-stream"
],
"return": "stream"
},
{
"function": "fread",
"params": [
Expand All @@ -86,13 +79,6 @@
"return-op": "chr-array",
"return": [1, 1, 1, 1]
},
{
"function": "get_resource_type",
"params": [
"@mock-stream"
],
"return": "stream"
},
{
"function": "fread",
"params": [
Expand Down Expand Up @@ -133,5 +119,32 @@
"@mock-stream"
],
"return": 17
},
{
"function": "fread",
"params": [
"@mock-stream",
2
],
"return-op": "chr-array",
"return": [129, 147]
},
{
"function": "fread",
"params": [
"@mock-stream",
4
],
"return-op": "chr-array",
"return": [33, 111, 149, 174]
},
{
"function": "fread",
"params": [
"@mock-stream",
19
],
"return-op": "chr-array",
"return": [115, 10, 246, 203, 72, 25, 252, 192, 70, 79, 244, 142, 76, 10, 230, 221, 64, 8, 240]
}
]

0 comments on commit 0e1ffc4

Please sign in to comment.