m_beforeTariffConn = properties.tariffName.beforeChange([this](const auto& oldVal, const auto& newVal){ onTariffChange(oldVal, newVal); });
m_beforeCashConn = properties.cash.beforeChange([this](auto oldVal, auto newVal){ onCashChange(oldVal, newVal); });
m_afterIPConn = ips.afterChange([this](const auto& oldVal, const auto& newVal){ onIPChange(oldVal, newVal); });
-
-pthread_mutexattr_t attr;
-pthread_mutexattr_init(&attr);
-pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
-pthread_mutex_init(&mutex, &attr);
}
//-----------------------------------------------------------------------------
UserImpl::UserImpl(const UserImpl & u)
m_afterIPConn = ips.afterChange([this](const auto& oldVal, const auto& newVal){ onIPChange(oldVal, newVal); });
properties.SetProperties(u.properties);
-
- pthread_mutexattr_t attr;
- pthread_mutexattr_init(&attr);
- pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
- pthread_mutex_init(&mutex, &attr);
-}
-//-----------------------------------------------------------------------------
-UserImpl::~UserImpl()
-{
-pthread_mutex_destroy(&mutex);
}
//-----------------------------------------------------------------------------
void UserImpl::SetLogin(const std::string & l)
{
-STG_LOCKER lock(&mutex);
+std::lock_guard lock(m_mutex);
static int idGen = 0;
assert(login.empty() && "Login is already set");
login = l;
//-----------------------------------------------------------------------------
int UserImpl::ReadConf()
{
-STG_LOCKER lock(&mutex);
+std::lock_guard lock(m_mutex);
UserConf conf;
if (store->RestoreUserConf(&conf, login))
//-----------------------------------------------------------------------------
int UserImpl::ReadStat()
{
-STG_LOCKER lock(&mutex);
+std::lock_guard lock(m_mutex);
UserStat stat;
if (store->RestoreUserStat(&stat, login))
//-----------------------------------------------------------------------------
int UserImpl::WriteConf()
{
-STG_LOCKER lock(&mutex);
+std::lock_guard lock(m_mutex);
UserConf conf(properties.GetConf());
printfd(__FILE__, "UserImpl::WriteConf()\n");
//-----------------------------------------------------------------------------
int UserImpl::WriteStat()
{
-STG_LOCKER lock(&mutex);
+std::lock_guard lock(m_mutex);
UserStat stat(properties.GetStat());
if (store->SaveUserStat(stat, login))
//-----------------------------------------------------------------------------
int UserImpl::WriteMonthStat()
{
-STG_LOCKER lock(&mutex);
+std::lock_guard lock(m_mutex);
time_t tt = stgTime - 3600;
struct tm t1;
localtime_r(&tt, &t1);
//-----------------------------------------------------------------------------
int UserImpl::Authorize(uint32_t ip, uint32_t dirs, const Auth * auth)
{
-STG_LOCKER lock(&mutex);
+std::lock_guard lock(m_mutex);
/*
* Authorize user. It only means that user will be authorized. Nothing more.
* User can be connected or disconnected while authorized.
//-----------------------------------------------------------------------------
void UserImpl::Unauthorize(const Auth * auth, const std::string & reason)
{
-STG_LOCKER lock(&mutex);
+std::lock_guard lock(m_mutex);
/*
* Authorizer tries to unauthorize user, that was not authorized by it
*/
//-----------------------------------------------------------------------------
bool UserImpl::IsAuthorizedBy(const Auth * auth) const
{
-STG_LOCKER lock(&mutex);
+std::lock_guard lock(m_mutex);
// Is this user authorized by specified authorizer?
return authorizedBy.find(auth) != authorizedBy.end();
}
//-----------------------------------------------------------------------------
std::vector<std::string> UserImpl::GetAuthorizers() const
{
- STG_LOCKER lock(&mutex);
+ std::lock_guard lock(m_mutex);
std::vector<std::string> list;
std::transform(authorizedBy.begin(), authorizedBy.end(), std::back_inserter(list), [](const auto auth){ return auth->GetVersion(); });
return list;
* Connect user to Internet. This function is differ from Authorize() !!!
*/
-STG_LOCKER lock(&mutex);
-
if (!fakeConnect)
{
std::string scriptOnConnect = settings->GetScriptsDir() + "/OnConnect";
* Disconnect user from Internet. This function is differ from UnAuthorize() !!!
*/
-STG_LOCKER lock(&mutex);
-
if (!lastIPForDisconnect)
{
printfd(__FILE__, "lastIPForDisconnect\n");
//-----------------------------------------------------------------------------
void UserImpl::Run()
{
-STG_LOCKER lock(&mutex);
+std::lock_guard lock(m_mutex);
if (stgTime > lastWriteStat + settings->GetStatWritePeriod())
{
//-----------------------------------------------------------------------------
void UserImpl::UpdatePingTime(time_t t)
{
-STG_LOCKER lock(&mutex);
+std::lock_guard lock(m_mutex);
if (t)
pingTime = t;
else
void UserImpl::AddTraffStatU(int dir, uint32_t ip, uint32_t len)
#endif
{
-STG_LOCKER lock(&mutex);
+std::lock_guard lock(m_mutex);
if (!m_connected || tariff == NULL)
return;
void UserImpl::AddTraffStatD(int dir, uint32_t ip, uint32_t len)
#endif
{
-STG_LOCKER lock(&mutex);
+std::lock_guard lock(m_mutex);
if (!m_connected || tariff == NULL)
return;
//-----------------------------------------------------------------------------
void UserImpl::OnAdd()
{
-STG_LOCKER lock(&mutex);
+std::lock_guard lock(m_mutex);
std::string scriptOnAdd = settings->GetScriptsDir() + "/OnUserAdd";
//-----------------------------------------------------------------------------
void UserImpl::OnDelete()
{
-STG_LOCKER lock(&mutex);
+std::lock_guard lock(m_mutex);
std::string scriptOnDel = settings->GetScriptsDir() + "/OnUserDel";
TraffStat ts;
{
- STG_LOCKER lock(&mutex);
+ std::lock_guard lock(m_mutex);
ts.swap(traffStat);
}
if (!hard)
{
printfd(__FILE__, "UserImpl::WriteDetailStat() - pushing detail stat to queue\n");
- STG_LOCKER lock(&mutex);
+ std::lock_guard lock(m_mutex);
traffStatSaved.second.swap(ts);
traffStatSaved.first = lastWriteDetailedStat;
}
//-----------------------------------------------------------------------------
double UserImpl::GetPassiveTimePart() const
{
-STG_LOCKER lock(&mutex);
+std::lock_guard lock(m_mutex);
+return getPassiveTimePart();
+}
+double UserImpl::getPassiveTimePart() const
+{
static const std::array<unsigned, 12> daysInMonth{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
struct tm tms;
//-----------------------------------------------------------------------------
void UserImpl::SetPassiveTimeAsNewUser()
{
-STG_LOCKER lock(&mutex);
+std::lock_guard lock(m_mutex);
time_t t = stgTime;
struct tm tm;
//-----------------------------------------------------------------------------
void UserImpl::MidnightResetSessionStat()
{
-STG_LOCKER lock(&mutex);
+std::lock_guard lock(m_mutex);
if (m_connected)
{
//-----------------------------------------------------------------------------
void UserImpl::ProcessNewMonth()
{
-STG_LOCKER lock(&mutex);
+std::lock_guard lock(m_mutex);
// Reset traff
if (m_connected)
Disconnect(true, "fake");
//-----------------------------------------------------------------------------
void UserImpl::ProcessDayFeeSpread()
{
-STG_LOCKER lock(&mutex);
+std::lock_guard lock(m_mutex);
if (passive.ConstData() || tariff == NULL)
return;
//-----------------------------------------------------------------------------
void UserImpl::ProcessDayFee()
{
-STG_LOCKER lock(&mutex);
+std::lock_guard lock(m_mutex);
if (tariff == NULL)
return;
double passiveTimePart = 1.0;
if (!settings->GetFullFee())
{
- passiveTimePart = GetPassiveTimePart();
+ passiveTimePart = getPassiveTimePart();
}
else
{
//-----------------------------------------------------------------------------
void UserImpl::ProcessDailyFee()
{
-STG_LOCKER lock(&mutex);
+std::lock_guard lock(m_mutex);
if (passive.ConstData() || tariff == NULL)
return;
time_t t = stgTime;
localtime_r(&t, &tms);
+std::lock_guard lock(m_mutex);
+
double passiveTimePart = 1.0;
if (!settings->GetFullFee())
{
- passiveTimePart = GetPassiveTimePart();
+ passiveTimePart = getPassiveTimePart();
}
else
{
//-----------------------------------------------------------------------------
int UserImpl::AddMessage(Message * msg)
{
-STG_LOCKER lock(&mutex);
+std::lock_guard lock(m_mutex);
if (SendMessage(*msg))
{
//-----------------------------------------------------------------------------
void UserImpl::onPassiveChange(int oldVal, int newVal)
{
+ std::lock_guard lock(m_mutex);
if (newVal && !oldVal && tariff != NULL)
properties.cash.Set(cash - tariff->GetPassiveCost(),
*sysAdmin,
//-----------------------------------------------------------------------------
void UserImpl::onDisabledChange(int oldVal, int newVal)
{
+ std::lock_guard lock(m_mutex);
if (oldVal && !newVal && GetConnected())
Disconnect(false, "disabled");
else if (!oldVal && newVal && IsInetable())
//-----------------------------------------------------------------------------
void UserImpl::onTariffChange(const std::string& /*oldVal*/, const std::string& newVal)
{
- STG_LOCKER lock(&mutex);
+ std::lock_guard lock(m_mutex);
if (settings->GetReconnectOnTariffChange() && m_connected)
Disconnect(false, "Change tariff");
tariff = tariffs->FindByName(newVal);
//-----------------------------------------------------------------------------
void UserImpl::onIPChange(const UserIPs& oldVal, const UserIPs& newVal)
{
+ std::lock_guard lock(m_mutex);
printfd(__FILE__, "Change IP from '%s' to '%s'\n", oldVal.toString().c_str(), newVal.toString().c_str());
if (m_connected)
Disconnect(false, "Change IP");