{"id":18290565,"url":"https://github.com/shuai132/asio_net","last_synced_at":"2025-06-23T17:34:05.414Z","repository":{"id":124277028,"uuid":"410793484","full_name":"shuai132/asio_net","owner":"shuai132","description":"a tiny C++14 Async TCP, UDP, RPC,DDS library based on asio and rpc_core","archived":false,"fork":false,"pushed_at":"2025-04-02T03:49:36.000Z","size":253,"stargazers_count":16,"open_issues_count":1,"forks_count":4,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-04-02T04:30:11.467Z","etag":null,"topics":["asio","dds","esp32","rpc","socket","tcp","udp"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/shuai132.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-09-27T07:59:19.000Z","updated_at":"2025-04-02T03:49:39.000Z","dependencies_parsed_at":"2024-01-18T07:23:48.822Z","dependency_job_id":"51f70d71-a9bb-4282-b88f-6f8bfa6f809b","html_url":"https://github.com/shuai132/asio_net","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shuai132%2Fasio_net","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shuai132%2Fasio_net/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shuai132%2Fasio_net/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shuai132%2Fasio_net/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shuai132","download_url":"https://codeload.github.com/shuai132/asio_net/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247324374,"owners_count":20920629,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["asio","dds","esp32","rpc","socket","tcp","udp"],"created_at":"2024-11-05T14:11:27.567Z","updated_at":"2025-04-05T10:30:49.445Z","avatar_url":"https://github.com/shuai132.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# asio_net\n\n[![Build Status](https://github.com/shuai132/asio_net/workflows/CI/badge.svg)](https://github.com/shuai132/asio_net/actions?workflow=CI)\n[![Release](https://img.shields.io/github/release/shuai132/asio_net.svg)](https://github.com/shuai132/asio_net/releases)\n[![License](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)\n\na tiny C++14 Async TCP/UDP/RPC/DDS library based on [asio](http://think-async.com/Asio/)\nand [rpc_core](https://github.com/shuai132/rpc_core)\n\n## Features\n\n* Header-Only\n* TCP/UDP: support auto_pack option for tcp, will ensure packets are complete, just like websocket\n* RPC: via socket(with SSL/TLS), domain socket, support c++20 coroutine for asynchronous operations\n* DDS: via socket(with SSL/TLS), domain socket\n* Service Discovery: based on UDP multicast\n* IPv4 and IPv6\n* SSL/TLS: depend OpenSSL\n* Serial Port\n* Automatic reconnection\n* Comprehensive unittests\n\n## Requirements\n\n* [asio](http://think-async.com/Asio/)\n* C++14\n* Optional: C++20 (for rpc coroutine api, co_await co_call)\n\n## Usage\n\n* clone\n\n```shell\ngit clone --recurse-submodules git@github.com:shuai132/asio_net.git\n```\n\nor\n\n```shell\ngit clone git@github.com:shuai132/asio_net.git \u0026\u0026 cd asio_net\ngit submodule update --init --recursive\n```\n\nThe following are examples of using each module. For complete unit tests,\nplease refer to the source code: [test](test)\n\n### RPC\n\nrpc based on tcp and [rpc_core](https://github.com/shuai132/rpc_core), and also support ipv6 and ssl.\ninspect the code for more details [rpc.cpp](test/rpc.cpp)\n\n```c++\n// server\nasio::io_context context;\nrpc_server server(context, PORT/*, rpc_config*/);\nserver.on_session = [](const std::weak_ptr\u003crpc_session\u003e\u0026 rs) {\n  auto session = rs.lock();\n  session-\u003eon_close = [] {};\n  session-\u003erpc-\u003esubscribe(\"cmd\", [](const std::string\u0026 data) -\u003e std::string {\n    return \"world\";\n  });\n};\nserver.start(true);\n```\n\n```c++\n// client\nasio::io_context context;\nrpc_client client(context/*, rpc_config*/);\nclient.on_open = [](const std::shared_ptr\u003crpc_core::rpc\u003e\u0026 rpc) {\n  rpc-\u003ecmd(\"cmd\")\n     -\u003emsg(std::string(\"hello\"))\n     -\u003ersp([](const std::string\u0026 data) {\n       assert(data == \"world\");\n     })\n     -\u003ecall();\n};\nclient.on_close = [] {};\nclient.open(\"localhost\", PORT);\nclient.run();\n```\n\nand, you can create rpc first, for more details: [rpc_config.cpp](test/rpc_config.cpp)\n\n```c++\n// server\nauto rpc = rpc_core::rpc::create();\nrpc-\u003esubscribe(\"cmd\", [](const std::string\u0026 data) -\u003e std::string {\n  assert(data == \"hello\");\n  return \"world\";\n});\n\nasio::io_context context;\nrpc_server server(context, PORT, rpc_config{.rpc = rpc});\nserver.start(true);\n```\n\n```c++\n// client\nauto rpc = rpc_core::rpc::create();\nasio::io_context context;\nrpc_client client(context, rpc_config{.rpc = rpc});\nclient.open(\"localhost\", PORT);\nclient.run();\n\nrpc-\u003ecmd(\"cmd\")-\u003emsg(std::string(\"hello\"))-\u003ecall();\n// or: rpc-\u003ecall(\"cmd\", std::string(\"hello\"));\n```\n\nand you can use C++20 coroutine:\n\n```c++\n// server\nrpc-\u003esubscribe(\"cmd\", [\u0026](request_response\u003cstd::string, std::string\u003e rr) -\u003e asio::awaitable\u003cvoid\u003e {\n  assert(rr-\u003ereq == \"hello\");\n  asio::steady_timer timer(context);\n  timer.expires_after(std::chrono::seconds(1));\n  co_await timer.async_wait();\n  rr-\u003ersp(\"world\");\n}, scheduler_asio_coroutine);\n\n// client\n// use C++20 co_await with asio, or you can use custom async implementation, and co_await it!\nauto rsp = co_await rpc-\u003ecmd(\"cmd\")-\u003emsg(std::string(\"hello\"))-\u003eco_call\u003cstd::string\u003e();\n// or: auto rsp = co_await rpc-\u003eco_call\u003cstd::string\u003e(\"cmd\", std::string(\"hello\"));\nassert(rsp.data == \"world\");\n```\n\ninspect the code for more\ndetails: [rpc_s_coroutine.cpp](test/rpc_s_coroutine.cpp)\nand [rpc_c_coroutine.cpp](test/rpc_c_coroutine.cpp)\n\n### DDS\n\n```c++\n// run a server as daemon\nasio::io_context context;\ndds_server server(context, PORT);\nserver.start(true);\n```\n\n```c++\n// client\nasio::io_context context;\ndds_client client(context);\nclient.open(\"localhost\", PORT);\nclient.subscribe(\"topic\", [](const std::string\u0026 data) {\n});\nclient.publish\u003cstd::string\u003e(\"topic\", \"string/binary\");\nclient.run();\n```\n\n### Server Discovery\n\n```c++\n// receiver\nasio::io_context context;\nserver_discovery::receiver receiver(context, [](const std::string\u0026 name, const std::string\u0026 message) {\n  printf(\"receive: name: %s, message: %s\\n\", name.c_str(), message.c_str());\n});\ncontext.run();\n```\n\n```c++\n// sender\nasio::io_context context;\nserver_discovery::sender sender_ip(context, \"ip\", \"message\");\ncontext.run();\n```\n\n### TCP\n\nYou can enable automatic handling of packet fragmentation using `tcp_config`.\nSubsequent send and receive will be complete data packets.\n\nBy default, this feature is disabled.\n\n```c++\n// echo server\nasio::io_context context;\ntcp_server server(context, PORT/*, tcp_config*/);\nserver.on_session = [](const std::weak_ptr\u003ctcp_session\u003e\u0026 ws) {\n  auto session = ws.lock();\n  session-\u003eon_close = [] {\n  };\n  session-\u003eon_data = [ws](std::string data) {\n    ws.lock()-\u003esend(std::move(data));\n  };\n};\nserver.start(true);\n```\n\n```c++\n// echo client\nasio::io_context context;\ntcp_client client(context/*, tcp_config*/);\nclient.on_data = [](const std::string\u0026 data) {\n};\nclient.on_close = [] {\n};\nclient.open(\"localhost\", PORT);\nclient.run();\n```\n\n### UDP\n\n```c++\n// server\nasio::io_context context;\nudp_server server(context, PORT);\nserver.on_data = [](uint8_t* data, size_t size, const udp::endpoint\u0026 from) {\n};\nserver.start();\n```\n\n```c++\n// client\nasio::io_context context;\nudp_client client(context);\nauto endpoint = udp::endpoint(asio::ip::address_v4::from_string(\"127.0.0.1\"), PORT);\nclient.send_to(\"hello\", endpoint);\ncontext.run();\n```\n\n### Serial Port\n\n```c++\nasio::io_context context;\nserial_port serial(context);\nserial.on_open = [\u0026] {\n  /// set_option\n  serial.set_option(asio::serial_port::baud_rate(115200));\n  serial.set_option(asio::serial_port::flow_control(asio::serial_port::flow_control::none));\n  serial.set_option(asio::serial_port::parity(asio::serial_port::parity::none));\n  serial.set_option(asio::serial_port::stop_bits(asio::serial_port::stop_bits::one));\n  serial.set_option(asio::serial_port::character_size(asio::serial_port::character_size(8)));\n\n  /// test\n  serial.send(\"hello world\");\n};\nserial.on_data = [](const std::string\u0026 data) {\n};\nserial.on_open_failed = [](std::error_code ec) {\n};\nserial.on_close = [] {\n};\nserial.open(\"/dev/tty.usbserial-xx\");\nserial.run();\n```\n\n# Links\n\n* RPC library for MCU\n\nmost MCU not support asio, there is a library can be ported easily: [esp_rpc](https://github.com/shuai132/esp_rpc)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshuai132%2Fasio_net","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshuai132%2Fasio_net","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshuai132%2Fasio_net/lists"}