This commit is contained in:
Timur A. Fatkhullin 2024-11-04 23:22:15 +03:00
parent fb43a2b378
commit 39b0fad13d

View File

@ -221,7 +221,8 @@ public:
: _ident(), : _ident(),
_netService(std::move(srv)), _netService(std::move(srv)),
_serverPtr(srv_ptr), _serverPtr(srv_ptr),
_bindDevice(srv_ptr->_devices.size() ? srv_ptr->_devices.begin()->second : AdcDeviceNetServer::nullDevice) _bindDevice(srv_ptr->_devices.size() ? &srv_ptr->_devices.begin()->second
: &AdcDeviceNetServer::nullDevice)
{ {
if constexpr (std::is_array_v<std::remove_cvref_t<R>>) { if constexpr (std::is_array_v<std::remove_cvref_t<R>>) {
_ident = id; _ident = id;
@ -274,7 +275,7 @@ public:
netsession_ident_t _ident; netsession_ident_t _ident;
netservice_t _netService; netservice_t _netService;
AdcDeviceNetServer* _serverPtr; AdcDeviceNetServer* _serverPtr;
AdcDeviceNetServer::DeviceWrapper& _bindDevice; AdcDeviceNetServer::DeviceWrapper* _bindDevice;
std::chrono::duration<size_t> _recvTimeout = std::chrono::seconds(3600); std::chrono::duration<size_t> _recvTimeout = std::chrono::seconds(3600);
std::chrono::duration<size_t> _sendTimeout = std::chrono::seconds(5); std::chrono::duration<size_t> _sendTimeout = std::chrono::seconds(5);
@ -290,8 +291,6 @@ public:
auto get_elem = [&attrs](size_t idx) { auto get_elem = [&attrs](size_t idx) {
if (idx < attrs.size()) { if (idx < attrs.size()) {
return attrs[idx]; return attrs[idx];
// auto& el = attrs[idx];
// return serialized_t(el.begin(), el.end());
} else { } else {
return serialized_t(); return serialized_t();
} }
@ -307,13 +306,13 @@ public:
bool found = false; bool found = false;
for (auto& [ptr, dev_wr] : _serverPtr->_devices) { for (auto& [ptr, dev_wr] : _serverPtr->_devices) {
if (std::ranges::equal(dev_wr.ident(), dev_name)) { if (std::ranges::equal(dev_wr.ident(), dev_name)) {
_bindDevice = dev_wr; _bindDevice = &dev_wr;
found = true; found = true;
break; break;
} }
} }
if (found) { if (found) {
msg.ack(attrs); msg.ack(msg_t::DEVICE_KEY, attrs);
} else { } else {
msg.err(std::make_error_code(AdcDeviceNetServerSessionError::ERROR_UNKNOWN_DEVICE_ID)); msg.err(std::make_error_code(AdcDeviceNetServerSessionError::ERROR_UNKNOWN_DEVICE_ID));
} }
@ -325,14 +324,14 @@ public:
for (auto& [ptr, dev_wr] : _serverPtr->_devices) { for (auto& [ptr, dev_wr] : _serverPtr->_devices) {
names.emplace_back(dev_wr.ident()); names.emplace_back(dev_wr.ident());
} }
msg.ack(names); msg.ack(msg_t::NAMES_KEY, names);
} else if (msg.isHELLO()) { } else if (msg.isHELLO()) {
msg.ack(msg_t::HELLO_KEY, _serverPtr->_serverIdent);
} else if (msg.isGET()) { // get attribute value } else if (msg.isGET()) { // get attribute value
attrs = msg.template attrs<attr_vec_t>(0, 1); attrs = msg.template attrs<attr_vec_t>(0, 1);
if (attrs.size()) { if (attrs.size()) {
auto val = _bindDevice.getAttr(get_elem(0)); auto val = _bindDevice->getAttr(get_elem(0));
// msg.ack(get_elem(0), serialized_t{val.begin(), val.end()}); msg.ack(msg_t::GET_KEY, get_elem(0), val);
msg.ack(get_elem(0), val);
} else { // no attr name! } else { // no attr name!
msg.err(std::make_error_code(AdcDeviceNetServerSessionError::ERROR_NO_PROTO_ATTRNAME)); msg.err(std::make_error_code(AdcDeviceNetServerSessionError::ERROR_NO_PROTO_ATTRNAME));
} }
@ -340,9 +339,8 @@ public:
attrs = msg.template attrs<attr_vec_t>(0); attrs = msg.template attrs<attr_vec_t>(0);
if (attrs.size() >= 2) { if (attrs.size() >= 2) {
auto val = msg.template joinAttrs<serialized_t>(1); auto val = msg.template joinAttrs<serialized_t>(1);
_bindDevice.setAttr(get_elem(0), val); _bindDevice->setAttr(get_elem(0), val);
// msg.ack(get_elem(0), serialized_t{val.begin(), val.end()}); msg.ack(msg_t::SET_KEY, get_elem(0), val);
msg.ack(get_elem(0), val);
} else { // no attr name or its value! } else { // no attr name or its value!
if (attrs.size() == 1) { if (attrs.size() == 1) {
msg.err(std::make_error_code(AdcDeviceNetServerSessionError::ERROR_NO_PROTO_ATTRVALUE)); msg.err(std::make_error_code(AdcDeviceNetServerSessionError::ERROR_NO_PROTO_ATTRVALUE));
@ -354,8 +352,8 @@ public:
attrs = msg.template attrs<attr_vec_t>(0, 1); attrs = msg.template attrs<attr_vec_t>(0, 1);
if (attrs.size()) { if (attrs.size()) {
auto cmd_name = get_elem(0); auto cmd_name = get_elem(0);
_bindDevice.exec(cmd_name); _bindDevice->exec(cmd_name);
msg.ack(cmd_name); msg.ack(msg_t::CMD_KEY, cmd_name);
} else { // no cmd name! } else { // no cmd name!
msg.err(std::make_error_code(AdcDeviceNetServerSessionError::ERROR_NO_PROTO_CMDNAME)); msg.err(std::make_error_code(AdcDeviceNetServerSessionError::ERROR_NO_PROTO_CMDNAME));
} }