51 lines
1.3 KiB
C++
51 lines
1.3 KiB
C++
#include "libhv_udp_server.h"
|
|
#include "test_struct.h"
|
|
|
|
using namespace hv;
|
|
|
|
int main(int argc, char* argv[]) {
|
|
static std::string server_port =
|
|
MyYAMLConfig::Get()["server_port"].as<std::string>();
|
|
UdpServer srv;
|
|
int bindfd = srv.createsocket(std::stol(server_port));
|
|
if (bindfd < 0) {
|
|
return -20;
|
|
}
|
|
gDebug() << gxt::format("server bind port:{}, bindfd={}", server_port,
|
|
bindfd);
|
|
TestStruct test;
|
|
srv.onMessage = [&](const SocketChannelPtr& channel, Buffer* buf) {
|
|
if (buf->size() != sizeof(TestStruct)) {
|
|
gDebugWarn() << "error size buf" << buf->size();
|
|
std::terminate();
|
|
}
|
|
memcpy(&test, buf->data(), buf->size());
|
|
std::string time_str = test.time;
|
|
auto time = stol(time_str);
|
|
auto cur_time = gxt::GetTimeUs();
|
|
gDebugLog(cur_time - time);
|
|
// printf("< %.*s\n", (int)buf->size(), (char*)buf->data());
|
|
// channel->write(buf);
|
|
};
|
|
srv.start();
|
|
while (true) {
|
|
gxt::Sleep(1);
|
|
}
|
|
|
|
// std::string str;
|
|
// while (std::getline(std::cin, str)) {
|
|
// if (str == "close") {
|
|
// srv.closesocket();
|
|
// } else if (str == "start") {
|
|
// srv.start();
|
|
// } else if (str == "stop") {
|
|
// srv.stop();
|
|
// break;
|
|
// } else {
|
|
// srv.sendto(str);
|
|
// }
|
|
// }
|
|
|
|
return 0;
|
|
}
|