- Reactor vs Proactor (text): http://didawiki.cli.di.unipi.it/lib/exe/fetch.php/magistraleinformatica/tdp/tpd_reactor_proactor.pdf
- Reactor (video): https://www.youtube.com/watch?v=pmtrUcPs4GQ
- Proactor (video): https://www.youtube.com/watch?v=2rGOX8Oy7-Q
Last active
March 24, 2020 18:37
-
-
Save thejohnfreeman/3495f758f901e696df898475cb61cbfc to your computer and use it in GitHub Desktop.
Reactor vs Proactor
This file contains 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
void proact(Proactor& proactor, Socket& socket) { | |
proactor.recv(socket, [](char* buffer, int size) { | |
// Call me when you've read the socket and give me what you read. | |
}); | |
} | |
class ReactorAsProactor : public Proactor { | |
public: | |
ReactorAsProactor(private Reactor& reactor) {} | |
template <typename Handler> | |
void recv(Socket& socket, Handler const& handler) { | |
this.reactor.recv(socket, [&]() { | |
char buffer[256]; | |
int size; | |
do { | |
size = socket.read(buffer, 256); | |
handler(buffer, size); | |
} while (size == 256); | |
}); | |
} | |
}; |
This file contains 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
void react(Reactor& reactor, Socket& socket) { | |
reactor.recv(socket, [&]() { | |
// Call me when the socket is ready for reading. | |
char buffer[256]; | |
int size; | |
do { | |
size = socket.read(buffer, 256); | |
} while (size == 256); | |
}); | |
} | |
class ProactorAsReactor : public Reactor { | |
public: | |
ProactorAsReactor(private Proactor& proactor) {} // C#/TypeScript-style shorthand | |
template <typename Handler> | |
void recv(Socket& socket, Handler const& handler) { | |
this.proactor.recv(socket, [](char* buffer, int size) { | |
socket.unread(buffer, size); // Socket is our own abstraction, so we can do this. | |
handler(); | |
}); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment