# asio_net **Repository Path**: shuai132/asio_net ## Basic Information - **Project Name**: asio_net - **Description**: No description available - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-06-23 - **Last Updated**: 2024-06-23 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # asio_net [![Build Status](https://github.com/shuai132/asio_net/workflows/build/badge.svg)](https://github.com/shuai132/asio_net/actions?workflow=build) a tiny Async TCP/UDP/RPC library based on [asio](http://think-async.com/Asio/) and [rpc_core](https://github.com/shuai132/rpc_core) ## Features * Header-Only * TCP/UDP support, rely on: [asio](http://think-async.com/Asio/) * RPC support, rely on: [rpc_core](https://github.com/shuai132/rpc_core) * Service discovery based on UDP multicast * Support IPv6 and SSL (with OpenSSL) * Support domain socket and rpc * Support serial port * Comprehensive unittests * Automatic reconnection Options: * TCP can be configured to automatically handle the problem of packet fragmentation to support the transmission of complete data packets. * Supports setting the maximum packet length, and will automatically disconnect if exceeded. ## Requirements * C++14 * [asio](http://think-async.com/Asio/) ## Usage * clone ```shell git clone --recurse-submodules git@github.com:shuai132/asio_net.git ``` or ```shell git clone git@github.com:shuai132/asio_net.git && cd asio_net git submodule update --init --recursive ``` The following are examples of using each module. For complete unit tests, please refer to the source code: [test](test) * TCP You can enable automatic handling of packet fragmentation using `tcp_config`. Subsequent send and receive will be complete data packets. By default, this feature is disabled. ```c++ // echo server asio::io_context context; tcp_server server(context, PORT/*, tcp_config*/); server.on_session = [](const std::weak_ptr& ws) { auto session = ws.lock(); session->on_close = [] { }; session->on_data = [ws](std::string data) { ws.lock()->send(std::move(data)); }; }; server.start(true); ``` ```c++ // echo client asio::io_context context; tcp_client client(context/*, tcp_config*/); client.on_data = [](const std::string& data) { }; client.on_close = [] { }; client.open("localhost", PORT); client.run(); ``` * UDP ```c++ // server asio::io_context context; udp_server server(context, PORT); server.on_data = [](uint8_t* data, size_t size, const udp::endpoint& from) { }; server.start(); ``` ```c++ // client asio::io_context context; udp_client client(context); auto endpoint = udp::endpoint(asio::ip::address_v4::from_string("127.0.0.1"), PORT); client.send_to("hello", endpoint); context.run(); ``` * RPC rpc based on tcp and [rpc_core](https://github.com/shuai132/rpc_core), and also support ipv6 and ssl. more usages, see [rpc.cpp](test/rpc.cpp) and [rpc_config.cpp](test/rpc_config.cpp) ```c++ // server asio::io_context context; rpc_server server(context, PORT/*, rpc_config*/); server.on_session = [](const std::weak_ptr& rs) { auto session = rs.lock(); session->on_close = [] { }; session->rpc->subscribe("cmd", [](const std::string& data) -> std::string { return "world"; }); }; server.start(true); ``` ```c++ // client asio::io_context context; rpc_client client(context/*, rpc_config*/); client.on_open = [](const std::shared_ptr& rpc) { rpc->cmd("cmd") ->msg(std::string("hello")) ->rsp([](const std::string& data) { }) ->call(); }; client.on_close = [] { }; client.open("localhost", PORT); client.run(); ``` * Server Discovery ```c++ // receiver asio::io_context context; server_discovery::receiver receiver(context, [](const std::string& name, const std::string& message) { printf("receive: name: %s, message: %s\n", name.c_str(), message.c_str()); }); context.run(); ``` ```c++ // sender asio::io_context context; server_discovery::sender sender_ip(context, "ip", "message"); context.run(); ``` * Serial Port ```c++ asio::io_context context; serial_port serial(context); serial.on_open = [&] { /// set_option serial.set_option(asio::serial_port::baud_rate(115200)); serial.set_option(asio::serial_port::flow_control(asio::serial_port::flow_control::none)); serial.set_option(asio::serial_port::parity(asio::serial_port::parity::none)); serial.set_option(asio::serial_port::stop_bits(asio::serial_port::stop_bits::one)); serial.set_option(asio::serial_port::character_size(asio::serial_port::character_size(8))); /// test serial.send("hello world"); }; serial.on_data = [](const std::string& data) { }; serial.on_open_failed = [](std::error_code ec) { }; serial.on_close = [] { }; serial.open("/dev/tty.usbserial-xx"); serial.run(); ``` # Links * RPC library for MCU most MCU not support asio, there is a library can be ported easily: [esp_rpc](https://github.com/shuai132/esp_rpc)