Skip to content

Instantly share code, notes, and snippets.

@christianparpart
Created August 20, 2014 13:57
Show Gist options
  • Save christianparpart/41759194a77cb61ab64a to your computer and use it in GitHub Desktop.
Save christianparpart/41759194a77cb61ab64a to your computer and use it in GitHub Desktop.
Xzero (local) Echo Server Demo
#include <xzero/Server.h>
#include <xzero/ConnectionFactory.h>
#include <xzero/Connection.h>
#include <xzero/LocalConnector.h>
#include <xzero/SequencedExecutor.h>
#include <base/Buffer.h>
using base::Buffer;
class EchoConnection : public xzero::Connection {
public:
EchoConnection(xzero::Connector* connector, xzero::EndPoint* endpoint)
: xzero::Connection(connector, endpoint) {}
void onOpen() {
// we're using the executor service here only as a demo to flatten the
// call-stack off the onOpen callback
connector()->executor()->execute([this]() {
Buffer data;
endpoint()->fill(&data); // XXX readline(&data) instead
endpoint()->flush(data);
close();
});
}
};
class EchoFactory : public xzero::ConnectionFactory {
public:
EchoFactory() : xzero::ConnectionFactory("echo") {}
EchoConnection* create(xzero::Connector* connector,
xzero::EndPoint* endpoint) override {
return new EchoConnection(connector, endpoint);
}
};
int main(int argc, const char* argv[]) {
xzero::SequencedExecutor executor;
xzero::Server server(&executor, nullptr /*scheduler*/);
auto localConnector = server.addConnector<xzero::LocalConnector>();
localConnector->addConnectionFactory<EchoFactory>();
server.start();
xzero::LocalEndPoint* ep = localConnector->createClient("Hello, World!\n");
printf("response:\n%s\n", ep->output().c_str());
server.stop();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment