diff --git a/net/adc_device_netserver.h b/net/adc_device_netserver.h index 78950ad..4b0cb1c 100644 --- a/net/adc_device_netserver.h +++ b/net/adc_device_netserver.h @@ -344,7 +344,8 @@ public: } } if (found) { - msg.ack(msg_t::DEVICE_KEY, attrs); + // msg.ack(msg_t::DEVICE_KEY, attrs); + msg.ack(constants::ADC_DEVICE_NETPROTO_KEY_DEVICE, attrs); } else { msg.err(std::make_error_code(AdcDeviceNetServerSessionError::ERROR_UNKNOWN_DEVICE_ID)); } @@ -356,14 +357,17 @@ public: for (auto& [ptr, dev_wr] : _serverPtr->_devices) { names.emplace_back(dev_wr.ident()); } - msg.ack(msg_t::NAMES_KEY, names); + // msg.ack(msg_t::NAMES_KEY, names); + msg.ack(constants::ADC_DEVICE_NETPROTO_KEY_NAMES, names); } else if (msg.isHELLO()) { - msg.ack(msg_t::HELLO_KEY, _serverPtr->_serverIdent); + // msg.ack(msg_t::HELLO_KEY, _serverPtr->_serverIdent); + msg.ack(constants::ADC_DEVICE_NETPROTO_KEY_HELLO, _serverPtr->_serverIdent); } else if (msg.isGET()) { // get attribute value attrs = msg.template attrs(0, 1); if (attrs.size()) { auto val = _bindDevice->getAttr(get_elem(0)); - msg.ack(msg_t::GET_KEY, get_elem(0), val); + // msg.ack(msg_t::GET_KEY, get_elem(0), val); + msg.ack(constants::ADC_DEVICE_NETPROTO_KEY_GET, get_elem(0), val); } else { // no attr name! msg.err(std::make_error_code(AdcDeviceNetServerSessionError::ERROR_NO_PROTO_ATTRNAME)); } @@ -372,7 +376,8 @@ public: if (attrs.size() >= 2) { auto val = msg.template joinAttrs(1); _bindDevice->setAttr(get_elem(0), val); - msg.ack(msg_t::SET_KEY, get_elem(0), val); + // msg.ack(msg_t::SET_KEY, get_elem(0), val); + msg.ack(constants::ADC_DEVICE_NETPROTO_KEY_SET, get_elem(0), val); } else { // no attr name or its value! if (attrs.size() == 1) { msg.err(std::make_error_code(AdcDeviceNetServerSessionError::ERROR_NO_PROTO_ATTRVALUE)); @@ -385,7 +390,8 @@ public: if (attrs.size()) { auto cmd_name = get_elem(0); _bindDevice->exec(cmd_name); - msg.ack(msg_t::CMD_KEY, cmd_name); + // msg.ack(msg_t::CMD_KEY, cmd_name); + msg.ack(constants::ADC_DEVICE_NETPROTO_KEY_CMD, cmd_name); } else { // no cmd name! msg.err(std::make_error_code(AdcDeviceNetServerSessionError::ERROR_NO_PROTO_CMDNAME)); } diff --git a/net/adc_endpoint.h b/net/adc_endpoint.h index 4f79844..7516125 100644 --- a/net/adc_endpoint.h +++ b/net/adc_endpoint.h @@ -304,6 +304,36 @@ public: return proto() == protoMarkWSS; } + // add '\0' char (or replace special-meaning char/char-sequence) to construct UNIX abstract namespace + // endpoint path + template + AdcEndpoint& makeAbstract(const T& mark = nullptr) + requires(traits::adc_input_char_range || std::same_as, char> || + std::is_null_pointer_v>) + { + if (!(isLocalStream() || isLocalSeqpacket())) { // only local proto is valid! + return *this; + } + + if constexpr (std::is_null_pointer_v) { // just insert '\0' + auto it = _endpoint.insert(std::string::const_iterator(_path.begin()), '\0'); + _path = std::string_view(it, _endpoint.end()); + } else if constexpr (std::same_as, char>) { // replace a character (mark) + auto pos = std::distance(_endpoint.cbegin(), std::string::const_iterator(_path.begin())); + if (_endpoint[pos] == mark) { + _endpoint[pos] = '\0'; + } + } else { // replace a character range (mark) + if (std::ranges::equal(_path | std::views::take(std::ranges::size(mark), mark))) { + auto pos = std::distance(_endpoint.cbegin(), std::string::const_iterator(_path.begin())); + _endpoint.replace(pos, std::ranges::size(mark), 1, '\0'); + _path = std::string_view(_endpoint.begin() + pos, _endpoint.end()); + } + } + + return *this; + } + protected: std::string _endpoint; std::string_view _proto, _host, _path, _portView; diff --git a/tests/adc_asio_netserver_test.cpp b/tests/adc_asio_netserver_test.cpp index b3adf3a..8fe2c3d 100644 --- a/tests/adc_asio_netserver_test.cpp +++ b/tests/adc_asio_netserver_test.cpp @@ -162,13 +162,14 @@ int main(int argc, char* argv[]) for (auto& ep : epnt) { adc::AdcEndpoint epn(ep); if (epn.isValid()) { - if (epn.isLocalSeqpacket() || epn.isLocalStream()) { - if (epn.path()[0] == '@') { // replace '@' to '\0' (use of UNIX abstract namespace) - auto it = std::ranges::find(ep, '@'); - *it = '\0'; - epn = adc::AdcEndpoint(ep); - } - } + epn.makeAbstract('@'); + // if (epn.isLocalSeqpacket() || epn.isLocalStream()) { + // if (epn.path()[0] == '@') { // replace '@' to '\0' (use of UNIX abstract namespace) + // auto it = std::ranges::find(ep, '@'); + // *it = '\0'; + // epn = adc::AdcEndpoint(ep); + // } + // } // std::cout << "try to start listenning at '" << ep << "' ...";