Conversation
It supports protocols where handleData() returns a generator function to yield back control to the server, e.g. during lengthy up- or download operations. For backwards compatibility, this is implemented in a new server class called peer.server.AsyncServer. However, this will become the default with the next major release
thekid
commented
Mar 29, 2021
| [$protocol, 'handleDisconnect'], | ||
| [$protocol, 'handleError'], | ||
| [$protocol, 'initialize'] | ||
| ]; |
Member
Author
There was a problem hiding this comment.
"Isn't this slow?" - I thought - indirectly invoking methods via an array?
use peer\Socket;
use util\profiling\Timer;
class Protocol {
public function handleData($socket) {
return $socket->toString();
}
}
$protocol= new Protocol();
$invoke= [[$protocol, 'handleData']];
$socket= new Socket('127.0.0.1', 8080);
$timer= (new Timer())->start();
for ($i= 0; $i < 10_000_000; $i++) {
// $result= ($f= $invoke[0] ?? null) ? $f($socket) : null;
$result= $protocol->handleData($socket);
}
printf("%.3f seconds: %s\n", $timer->elapsedTime(), $result);- Indirect: 5.791 seconds: peer.Socket((closed) -> tcp://127.0.0.1:8080)
- Direct: 4.877 seconds: peer.Socket((closed) -> tcp://127.0.0.1:8080)
So yes, this is one second difference, but for 10 million invocations (so only 1_000 / 10_000_000 = 0.0001 milliseconds difference per invocation)! Compared to the socket I/O we're handling
thekid
commented
Mar 29, 2021
thekid
commented
Mar 30, 2021
Member
Author
thekid
added a commit
to xp-forge/web
that referenced
this pull request
Jul 6, 2025
See xp-framework/networking#17 (released in 10.1.0, the minimum version we depend on)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
It supports protocols where
handleData()returns a generator function to yield back control to the server, e.g. during lengthy up- or download operations. Here's a web application showcasing this:For backwards compatibility, this is implemented in a new server class called
peer.server.AsyncServer. However, this will become the default with the next major release.