Last active
March 25, 2016 16:34
-
-
Save mycure/2a8e974bc47bbef10add to your computer and use it in GitHub Desktop.
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
#include <reactor/network/exception.hh> | |
#include <reactor/network/tcp-server.hh> | |
#include <reactor/scheduler.hh> | |
int main(int argc, char* argv[]) | |
{ | |
try | |
{ | |
if (argc != 2) | |
{ | |
std::cerr << "Usage: " << argv[0] << " <port>" << std::endl; | |
return 1; | |
} | |
reactor::Scheduler sched; | |
reactor::Thread acceptor( | |
sched, "acceptor", [&] | |
{ | |
reactor::network::TCPServer server; | |
server.listen(std::atoi(argv[1])); | |
std::vector<std::unique_ptr<reactor::Thread>> client_threads; | |
while (true) | |
{ | |
std::shared_ptr<reactor::network::Socket> socket = server.accept(); | |
client_threads.emplace_back( | |
new reactor::Thread( | |
sched, "serve", | |
[socket] | |
{ | |
try | |
{ | |
while (true) | |
{ | |
elle::Buffer line = socket->read_until("\n"); | |
socket->write(line); | |
} | |
} | |
catch (reactor::network::ConnectionClosed const&) | |
{} | |
})); | |
} | |
}); | |
sched.run(); | |
} | |
catch (std::exception const& e) | |
{ | |
std::cerr << "fatal error: " << e.what() << std::endl; | |
return 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment