- Notifications
You must be signed in to change notification settings - Fork1
Networking library on top of uvw (respectively libuv). Includes http, modbus and sunspec client. Also: dns-sd / mdns.
License
NotificationsYou must be signed in to change notification settings
mincequi/uvw_net
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
uvw_net
is an event based networking library foruvw
written in modern C++.It is available as a compilable static library.
- HTTP Client
- Modbus Client
- SunSpec Client
- DNS Service Discovery
- Modbus Device Discovery
- SunSpec Device Discovery
API is attempted to be consistent withuvw API
#include<iostream>#include<uvw/timer.h>#include<uvw_net/http/HttpClient.h>#include<uvw_net/http/HttpRequest.h>#include<uvw_net/http/HttpResponse.h>usingnamespaceuvw_net::http;intmain() {// Create a HttpClient HttpClient client; client.on<HttpResponse>([](const HttpResponse& response,const HttpClient&) { std::cout << response.body << std::endl; }); HttpRequest request; request.host ="go-echarger_876432"; request.path ="/api/status"; client.get(request);// Start a timeoutauto timer =uvw::loop::get_default()->resource<uvw::timer_handle>(); timer->on<uvw::timer_event>([](constauto&,auto&) {uvw::loop::get_default()->stop(); }); timer->start(uvw::timer_handle::time{2000}, uvw::timer_handle::time{0});// Run the event loopreturnuvw::loop::get_default()->run();}
#include<iostream>#include<magic_enum_iostream.hpp>#include<uvw/timer.h>#include<uvw_net/dns_sd/DnsServiceDiscovery.h>#include<uvw_net/modbus/ModbusDiscovery.h>#include<uvw_net/sunspec/SunSpecDiscovery.h>usingnamespaceuvw_net::dns_sd;usingnamespaceuvw_net::modbus;usingnamespaceuvw_net::sunspec;using magic_enum::iostream_operators::operator<<;intmain() { DnsServiceDiscovery dnsDiscovery; dnsDiscovery.discover("_http._tcp.local"); dnsDiscovery.on<DnsRecordDataSrv>([](const DnsRecordDataSrv& data,const DnsServiceDiscovery&) {constauto host = data.target.substr(0, data.target.find(".")); std::cout <<"http service found> host:" << host <<", port:" << data.port << std::endl; }); SunSpecDiscovery sunspecDiscovery; sunspecDiscovery.on<SunSpecClientPtr>([](SunSpecClientPtr thing,const SunSpecDiscovery&) { std::stringstream supportedModels; std::stringstream unsupportedModels;for (constauto& kv : thing->models()) {auto m = magic_enum::enum_cast<SunSpecModel::Id>(kv.first);if (m.has_value()) supportedModels <<magic_enum::enum_name(m.value()) <<" (" << kv.first <<"),";//else// unsupportedModels << kv.first << ", "; } std::cout <<"sunspec thing found> id:" << thing->sunSpecId() <<", host:" << thing->host() <<", unitId:" << (uint32_t)thing->unitId() <<", supportedModels:" << supportedModels.str() << std::endl;//<< "unsupportedModels: " << unsupportedModels.str(); }); ModbusDiscovery modbusDiscovery; modbusDiscovery.discover();// ModbusDiscovery spawns ready/connected ModbusThings modbusDiscovery.on<ModbusClientPtr>([&](ModbusClientPtr thing,const ModbusDiscovery&) { std::cout <<"modbus thing found> ip:" << thing->ip() << std::endl; sunspecDiscovery.discover(thing); });// Start a timeoutauto timer =uvw::loop::get_default()->resource<uvw::timer_handle>(); timer->on<uvw::timer_event>([](constauto&,auto&) {uvw::loop::get_default()->stop(); }); timer->start(uvw::timer_handle::time{30000}, uvw::timer_handle::time{0});// Run the event loopreturnuvw::loop::get_default()->run();}
To be able to build and useuvw_net
you need a C++20 compiler.
Note thatuvw
is part of the dependencies of the project and is clonedbyCMake
. So, you don't have to install it whenuvw_net
libraries are compiled throughCMake
.
About
Networking library on top of uvw (respectively libuv). Includes http, modbus and sunspec client. Also: dns-sd / mdns.
Topics
Resources
License
Stars
Watchers
Forks
Packages0
No packages published