map/das-dn/third_party/AdsLib/Standalone/AmsPort.cpp

61 lines
1.2 KiB
C++
Raw Normal View History

2024-12-03 10:36:06 +08:00
// SPDX-License-Identifier: MIT
/**
Copyright (c) 2015 - 2022 Beckhoff Automation GmbH & Co. KG
*/
#include "AmsPort.h"
2024-12-05 11:04:47 +08:00
namespace std
2024-12-03 10:36:06 +08:00
{
2024-12-05 11:04:47 +08:00
bool operator==(const AmsAddr& lhs, const AmsAddr& rhs)
2024-12-03 10:36:06 +08:00
{
2024-12-05 11:04:47 +08:00
return 0 == memcmp(&lhs, &rhs, sizeof(lhs));
}
}
AmsPort::AmsPort()
: tmms(DEFAULT_TIMEOUT),
port(0)
2024-12-03 10:36:06 +08:00
{}
void AmsPort::AddNotification(const AmsAddr ams, const uint32_t hNotify, SharedDispatcher dispatcher)
{
std::lock_guard<std::mutex> lock(mutex);
dispatcherList.emplace(NotifyUUID {ams, hNotify}, dispatcher);
}
2024-12-05 11:04:47 +08:00
void AmsPort::Close()
{
std::lock_guard<std::mutex> lock(mutex);
for (auto& d: dispatcherList) {
d.second->Erase(d.first.second, tmms);
}
dispatcherList.clear();
tmms = DEFAULT_TIMEOUT;
port = 0;
}
2024-12-03 10:36:06 +08:00
long AmsPort::DelNotification(const AmsAddr ams, uint32_t hNotify)
{
std::lock_guard<std::mutex> lock(mutex);
auto it = dispatcherList.find({ams, hNotify});
if (it != dispatcherList.end()) {
const auto status = it->second->Erase(hNotify, tmms);
dispatcherList.erase(it);
return status;
}
return ADSERR_CLIENT_REMOVEHASH;
}
bool AmsPort::IsOpen() const
{
return !!port;
}
uint16_t AmsPort::Open(uint16_t __port)
{
port = __port;
return port;
}