diff --git a/CMakeLists.txt b/CMakeLists.txt index fd969cf..75c2e2e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -52,14 +52,14 @@ endif() find_package(debugstream) find_package(Boost REQUIRED) -# find_package(ZeroMQ_INCLUDE_DIR) include_directories(include) add_executable(send src/send.cc) -target_link_libraries (send ${Boost_LIBRARIES}) #链接boost -target_link_libraries(send debugstream) +target_link_libraries (send debugstream ${Boost_LIBRARIES}) #链接boost add_executable(receive src/receive.cc) -target_link_libraries (receive ${Boost_LIBRARIES}) #链接boost -target_link_libraries(receive debugstream) -# target_link_libraries(demo ZeroMQ_LIBRARY) +target_link_libraries (receive debugstream ${Boost_LIBRARIES}) #链接boost +add_executable(send_mulport src/send_mulport.cc) +target_link_libraries (send_mulport debugstream ${Boost_LIBRARIES}) #链接boost +add_executable(receive_mulport src/receive_mulport.cc) +target_link_libraries (receive_mulport debugstream ${Boost_LIBRARIES}) #链接boost diff --git a/src/receive_mulport.cc b/src/receive_mulport.cc new file mode 100644 index 0000000..6e23c19 --- /dev/null +++ b/src/receive_mulport.cc @@ -0,0 +1,91 @@ +#include +#include +#include +#include + +class BoostUdpServer { + public: + BoostUdpServer(int port, std::function fun) + : socket_(io_context_, boost::asio::ip::udp::endpoint( + boost::asio::ip::udp::v4(), port)) { + start_receive(); + fun_ = fun; + } + void Run() { io_context_.run(); } + + private: + void start_receive() { + socket_.async_receive_from( + boost::asio::buffer(recv_buffer_), sender_endpoint_, + [this](boost::system::error_code ec, std::size_t bytes_recvd) { + handle_receive(ec, bytes_recvd); + }); + } + + void handle_receive(const boost::system::error_code& ec, + std::size_t bytes_recvd) { + if (!ec) { + std::string received_message(recv_buffer_.data(), bytes_recvd); + + fun_(received_message); + + // 继续接收下一个数据包 + start_receive(); + } else { + std::cerr << "Receive error: " << ec.message() << std::endl; + } + } + + boost::asio::io_context io_context_; + boost::asio::ip::udp::socket socket_; + boost::asio::ip::udp::endpoint sender_endpoint_; + std::array recv_buffer_; + std::function fun_; +}; + +class Server { + public: + Server(int n = 1, std::function fun = nullptr) { + // static std::string client_ip = + // MyYAMLConfig::Get()["client_ip"].as(); + // static std::string client_port = + // MyYAMLConfig::Get()["client_port"].as(); + int port = 12345; + fun_ = fun; + for (int i = 0; i < n; i++) { + threads_.emplace_back(std::thread([&, listen_port = port + i]() { + BoostUdpServer server(listen_port, fun_); + std::cout << "Boost Udp ready receive to port " << ":" << listen_port + << std::endl; + server.Run(); + })); + } + } + + private: + std::vector threads_; + // std::vector> clis_; + std::function fun_; +}; + +void CallBackFun(std::string& str) { + // 解码时间字符串为微秒 + long long received_time_us = std::stoll(str); + auto now = std::chrono::high_resolution_clock::now(); + auto current_time_us = std::chrono::duration_cast( + now.time_since_epoch()) + .count(); + + // 计算时间差 + long long time_difference = current_time_us - received_time_us; + + std::cout << "Received: " << str << " | Time difference: " << time_difference + << " us" << std::endl; +} + +int main() { + Server server(10, CallBackFun); + while (true); + + return 0; +} diff --git a/src/send_mulport.cc b/src/send_mulport.cc new file mode 100644 index 0000000..09d0013 --- /dev/null +++ b/src/send_mulport.cc @@ -0,0 +1,104 @@ +#include +#include +#include +#include +#include + +class BoostUdpClient { + public: + BoostUdpClient(const std::string &ip, int port) : socket(io_context) { + try { + socket.open(boost::asio::ip::udp::v4()); + // 设置目标地址和端口 + receiver_endpoint = boost::asio::ip::udp::endpoint( + boost::asio::ip::make_address(ip), port); + } catch (std::exception &e) { + std::cerr << "Exception: " << e.what() << std::endl; + } + } + void Send(const std::string &str) { + socket.send_to(boost::asio::buffer(str), receiver_endpoint); + } + void Send(const char *str, size_t size) { + std::string send_str(str, size); + socket.send_to(boost::asio::buffer(send_str), receiver_endpoint); + } + void Send(const char *str) { + std::string send_str(str); + socket.send_to(boost::asio::buffer(send_str), receiver_endpoint); + } + + private: + boost::asio::io_context io_context; + boost::asio::ip::udp::socket socket; + boost::asio::ip::udp::endpoint receiver_endpoint; +}; + +template +class Client { + public: + Client(int n = 1) : all_ports_n_(n) { + // static std::string client_ip = + // MyYAMLConfig::Get()["client_ip"].as(); + // static std::string client_port = + // MyYAMLConfig::Get()["client_port"].as(); + std::string ip = "127.0.0.1"; + int port = 12345; + + for (int i = 0; i < n; i++) { + auto ptr = std::make_unique(ip, port + i); + clis_.emplace_back(std::move(ptr)); + std::cout << "Boost Udp ready send to " << ip << ":" << port << std::endl; + } + } + + void Send(const T &val) { + clis_.at(cnt_port_)->Send(&val, sizeof(T)); + cnt_port_++; + cnt_port_ = cnt_port_ % all_ports_n_; + } + void Send(const std::string &str) { + clis_.at(cnt_port_)->Send(str); + cnt_port_++; + cnt_port_ = cnt_port_ % all_ports_n_; + } + void Send(const char *str, size_t size) { + clis_.at(cnt_port_)->Send(str, size); + cnt_port_++; + cnt_port_ = cnt_port_ % all_ports_n_; + } + void Send(const char *str) { + clis_.at(cnt_port_)->Send(str); + cnt_port_++; + cnt_port_ = cnt_port_ % all_ports_n_; + } + + private: + std::vector> clis_; + const int all_ports_n_ = 0; + std::atomic_int cnt_port_ = 0; +}; + +int main() { + Client client(10); + while (true) { + // 获取当前时间,转换为微秒 + auto now = std::chrono::high_resolution_clock::now(); + auto duration = now.time_since_epoch(); + auto microseconds = + std::chrono::duration_cast(duration).count(); + + // 转换为字符串 + std::string message = std::to_string(microseconds); + + client.Send(message); + // 发送数据 + // socket.send_to(boost::asio::buffer(message), receiver_endpoint); + std::cout << "Sent: " << message << std::endl; + + // 等待 50 毫秒 + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } + + return 0; +}