Files
scylladb/tests/udp_server.cc
2014-12-15 19:39:33 +08:00

55 lines
1.5 KiB
C++

/*
* Copyright (C) 2014 Cloudius Systems, Ltd.
*/
#include "core/smp.hh"
#include "core/app-template.hh"
#include "core/future-util.hh"
using namespace net;
using namespace std::chrono_literals;
class udp_server {
private:
udp_channel _chan;
timer<> _stats_timer;
uint64_t _n_sent {};
public:
void start(uint16_t port) {
ipv4_addr listen_addr{port};
_chan = engine.net().make_udp_channel(listen_addr);
_stats_timer.set_callback([this] {
std::cout << "Out: " << _n_sent << " pps" << std::endl;
_n_sent = 0;
});
_stats_timer.arm_periodic(1s);
keep_doing([this] {
return _chan.receive().then([this] (udp_datagram dgram) {
return _chan.send(dgram.get_src(), std::move(dgram.get_data())).then([this] {
_n_sent++;
});
});
});
}
};
namespace bpo = boost::program_options;
int main(int ac, char ** av) {
app_template app;
app.add_options()
("port", bpo::value<uint16_t>()->default_value(10000), "UDP server port") ;
return app.run(ac, av, [&] {
auto&& config = app.configuration();
uint16_t port = config["port"].as<uint16_t>();
auto server = new distributed<udp_server>;
server->start().then([server = std::move(server), port] () mutable {
server->invoke_on_all(&udp_server::start, port);
}).then([port] {
std::cout << "Seastar UDP server listening on port " << port << " ...\n";
});
});
}