feat(Core/Time): remove inherited ACE Time (#3455)

Co-authored-by: Viste <viste02@gmail.com>
This commit is contained in:
Kargatum
2020-09-11 19:03:26 +07:00
committed by GitHub
parent 8b3621779e
commit e15a493927
20 changed files with 157 additions and 58 deletions

View File

@@ -83,7 +83,6 @@
#include <ace/Guard_T.h>
#include <ace/RW_Thread_Mutex.h>
#include <ace/Thread_Mutex.h>
#include <ace/OS_NS_time.h>
#include <ace/Stack_Trace.h>
#if AC_PLATFORM == AC_PLATFORM_WINDOWS

View File

@@ -4,18 +4,7 @@
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*/
#include "Common.h"
#ifdef _WIN32
#include <winsock2.h>
#endif
#include <mysql.h>
#include <mysqld_error.h>
#include <errmsg.h>
#include <chrono>
#include <thread>
#include "MySQLConnection.h"
#include "MySQLThreading.h"
#include "QueryResult.h"
@@ -24,9 +13,15 @@
#include "DatabaseWorker.h"
#include "Timer.h"
#include "Log.h"
#include "Duration.h"
#include <mysql.h>
#include <mysqld_error.h>
#include <errmsg.h>
#include <thread>
using namespace std::this_thread;
using namespace std::chrono;
#ifdef _WIN32
#include <winsock2.h>
#endif
MySQLConnection::MySQLConnection(MySQLConnectionInfo& connInfo) :
m_reconnecting(false),
@@ -113,7 +108,6 @@ bool MySQLConnection::Open()
// Possible improvement for future: make ATTEMPTS and SECONDS configurable values
uint32 const ATTEMPTS = 180;
uint32 const SECONDS = 10;
uint32 count = 0;
do {
@@ -153,7 +147,7 @@ bool MySQLConnection::Open()
count++;
sLog->outError("Could not connect to MySQL database at %s: %s\n", m_connectionInfo.host.c_str(), mysql_error(mysqlInit));
sLog->outError("Retrying in 10 seconds...\n\n");
sleep_for(seconds(SECONDS));
std::this_thread::sleep_for(10s);
}
} while (!m_Mysql && count < ATTEMPTS);
@@ -537,7 +531,7 @@ bool MySQLConnection::_HandleMySQLErrno(uint32 errNo)
}
uint32 lErrno = mysql_errno(GetHandle()); // It's possible this attempted reconnect throws 2006 at us. To prevent crazy recursive calls, sleep here.
ACE_OS::sleep(3); // Sleep 3 seconds
std::this_thread::sleep_for(3s); // Sleep 3 seconds
return _HandleMySQLErrno(lErrno); // Call self (recursive)
}
@@ -552,12 +546,12 @@ bool MySQLConnection::_HandleMySQLErrno(uint32 errNo)
case ER_BAD_FIELD_ERROR:
case ER_NO_SUCH_TABLE:
sLog->outError("Your database structure is not up to date. Please make sure you've executed all queries in the sql/updates folders.");
ACE_OS::sleep(10);
std::this_thread::sleep_for(10s);
std::abort();
return false;
case ER_PARSE_ERROR:
sLog->outError("Error while parsing SQL. Core fix required.");
ACE_OS::sleep(10);
std::this_thread::sleep_for(10s);
std::abort();
return false;
default:

View File

@@ -340,7 +340,7 @@ std::string Log::GetTimestampStr()
{
time_t t = time(NULL);
tm aTm;
ACE_OS::localtime_r(&t, &aTm);
localtime_r(&t, &aTm);
// YYYY year
// MM month (2 digits 01-12)
// DD day (2 digits 01-31)

View File

@@ -9,16 +9,14 @@
#include "Common.h"
#include "Errors.h"
#include "Util.h"
#include "ByteConverter.h"
#include <ace/OS_NS_time.h>
#include <exception>
#include <list>
#include <map>
#include <string>
#include <vector>
#include <cstring>
#include <time.h>
// Root of ByteBuffer exception hierarchy
class ByteBufferException : public std::exception
@@ -489,7 +487,7 @@ class ByteBuffer
void AppendPackedTime(time_t time)
{
tm lt;
ACE_OS::localtime_r(&time, &lt);
localtime_r(&time, &lt);
append<uint32>((lt.tm_year - 100) << 24 | lt.tm_mon << 20 | (lt.tm_mday - 1) << 14 | lt.tm_wday << 11 | lt.tm_hour << 6 | lt.tm_min);
}

View File

@@ -0,0 +1,33 @@
/*
* Copyright (C) 2016+ AzerothCore <www.azerothcore.org>, released under GNU GPL v2 license: https://github.com/azerothcore/azerothcore-wotlk/blob/master/LICENSE-GPL2
* Copyright (C) 2008-2020 TrinityCore <http://www.trinitycore.org/>
*/
#ifndef _DURATION_H_
#define _DURATION_H_
#include <chrono>
/// Microseconds shorthand typedef.
typedef std::chrono::microseconds Microseconds;
/// Milliseconds shorthand typedef.
typedef std::chrono::milliseconds Milliseconds;
/// Seconds shorthand typedef.
typedef std::chrono::seconds Seconds;
/// Minutes shorthand typedef.
typedef std::chrono::minutes Minutes;
/// Hours shorthand typedef.
typedef std::chrono::hours Hours;
/// time_point shorthand typedefs
typedef std::chrono::steady_clock::time_point TimePoint;
typedef std::chrono::system_clock::time_point SystemTimePoint;
/// Makes std::chrono_literals globally available.
using namespace std::chrono_literals;
#endif

View File

@@ -7,13 +7,23 @@
#ifndef ACORE_TIMER_H
#define ACORE_TIMER_H
#include "ace/OS_NS_sys_time.h"
#include "Common.h"
#include "Duration.h"
inline TimePoint GetApplicationStartTime()
{
using namespace std::chrono;
static const steady_clock::time_point ApplicationStartTime = steady_clock::now();
return ApplicationStartTime;
}
inline uint32 getMSTime()
{
static const ACE_Time_Value ApplicationStartTime = ACE_OS::gettimeofday();
return (ACE_OS::gettimeofday() - ApplicationStartTime).msec();
using namespace std::chrono;
return uint32(duration_cast<milliseconds>(steady_clock::now() - GetApplicationStartTime()).count());
}
inline uint32 getMSTimeDiff(uint32 oldMSTime, uint32 newMSTime)
@@ -25,6 +35,14 @@ inline uint32 getMSTimeDiff(uint32 oldMSTime, uint32 newMSTime)
return newMSTime - oldMSTime;
}
inline uint32 getMSTimeDiff(uint32 oldMSTime, TimePoint newTime)
{
using namespace std::chrono;
uint32 newMSTime = uint32(duration_cast<milliseconds>(newTime - GetApplicationStartTime()).count());
return getMSTimeDiff(oldMSTime, newMSTime);
}
inline uint32 GetMSTimeDiffToNow(uint32 oldMSTime)
{
return getMSTimeDiff(oldMSTime, getMSTime());

View File

@@ -102,6 +102,45 @@ Tokenizer::Tokenizer(const std::string &src, const char sep, uint32 vectorReserv
}
}
#if (defined(WIN32) || defined(_WIN32) || defined(__WIN32__))
struct tm* localtime_r(time_t const* time, struct tm* result)
{
localtime_s(result, time);
return result;
}
#endif
tm TimeBreakdown(time_t time)
{
tm timeLocal;
localtime_r(&time, &timeLocal);
return timeLocal;
}
time_t LocalTimeToUTCTime(time_t time)
{
#if (defined(WIN32) || defined(_WIN32) || defined(__WIN32__))
return time + _timezone;
#else
return time + timezone;
#endif
}
time_t GetLocalHourTimestamp(time_t time, uint8 hour, bool onlyAfterTime)
{
tm timeLocal = TimeBreakdown(time);
timeLocal.tm_hour = 0;
timeLocal.tm_min = 0;
timeLocal.tm_sec = 0;
time_t midnightLocal = mktime(&timeLocal);
time_t hourLocal = midnightLocal + hour * HOUR;
if (onlyAfterTime && hourLocal <= time)
hourLocal += DAY;
return hourLocal;
}
void stripLineInvisibleChars(std::string &str)
{
static std::string const invChars = " \t\7\n";
@@ -227,7 +266,7 @@ uint32 TimeStringToSecs(const std::string& timestring)
std::string TimeToTimestampStr(time_t t)
{
tm aTm;
ACE_OS::localtime_r(&t, &aTm);
localtime_r(&t, &aTm);
// YYYY year
// MM month (2 digits 01-12)
// DD day (2 digits 01-31)
@@ -245,6 +284,15 @@ std::string TimeToTimestampStr(time_t t)
return std::string(buf);
}
std::string TimeToHumanReadable(time_t t)
{
tm time;
localtime_r(&t, &time);
char buf[30];
strftime(buf, 30, "%c", &time);
return std::string(buf);
}
/// Check if the string is a valid ip address representation
bool IsIPAddress(char const* ipaddress)
{

View File

@@ -56,6 +56,11 @@ private:
StorageType m_storage;
};
struct tm* localtime_r(time_t const* time, struct tm* result);
time_t LocalTimeToUTCTime(time_t time);
time_t GetLocalHourTimestamp(time_t time, uint8 hour, bool onlyAfterTime = true);
tm TimeBreakdown(time_t t);
void stripLineInvisibleChars(std::string &src);
int32 MoneyStringToMoney(const std::string& moneyString);
@@ -63,6 +68,7 @@ int32 MoneyStringToMoney(const std::string& moneyString);
std::string secsToTimeString(uint64 timeInSecs, bool shortText = false);
uint32 TimeStringToSecs(const std::string& timestring);
std::string TimeToTimestampStr(time_t t);
std::string TimeToHumanReadable(time_t t);
/* Return a random number in the range min..max. */
int32 irand(int32 min, int32 max);

View File

@@ -428,7 +428,7 @@ bool AchievementCriteriaData::Meets(uint32 criteria_id, Player const* source, Un
{
time_t birthday_start = time_t(sWorld->getIntConfig(CONFIG_BIRTHDAY_TIME));
tm birthday_tm;
ACE_OS::localtime_r(&birthday_start, &birthday_tm);
localtime_r(&birthday_start, &birthday_tm);
// exactly N birthday
birthday_tm.tm_year += birthday_login.nth_birthday;

View File

@@ -27411,7 +27411,7 @@ void Player::_LoadBrewOfTheMonth(PreparedQueryResult result)
time_t curtime = time(nullptr);
tm localTime;
ACE_OS::localtime_r(&curtime, &localTime);
localtime_r(&curtime, &localTime);
uint16 month = uint16(localTime.tm_mon);
uint16 eventId = month;

View File

@@ -32,6 +32,8 @@
#include "PacketLog.h"
#include "ScriptMgr.h"
#include "AccountMgr.h"
#include <thread>
#ifdef ELUNA
#include "LuaEngine.h"
#endif
@@ -90,7 +92,7 @@ struct ClientPktHeader
#endif
WorldSocket::WorldSocket(void): WorldHandler(),
m_LastPingTime(ACE_Time_Value::zero), m_OverSpeedPings(0), m_Session(0),
m_LastPingTime(SystemTimePoint::min()), m_OverSpeedPings(0), m_Session(0),
m_RecvWPct(0), m_RecvPct(), m_Header(sizeof (ClientPktHeader)),
m_OutBuffer(0), m_OutBufferSize(65536), m_OutActive(false),
m_Seed(static_cast<uint32> (rand32()))
@@ -1021,9 +1023,9 @@ int WorldSocket::HandleAuthSession(WorldPacket& recvPacket)
// Sleep this Network thread for
uint32 sleepTime = sWorld->getIntConfig(CONFIG_SESSION_ADD_DELAY);
ACE_OS::sleep (ACE_Time_Value (0, sleepTime));
std::this_thread::sleep_for(Microseconds(sleepTime));
sWorld->AddSession (m_Session);
sWorld->AddSession(m_Session);
return 0;
}
@@ -1037,20 +1039,19 @@ int WorldSocket::HandlePing(WorldPacket& recvPacket)
recvPacket >> ping;
recvPacket >> latency;
if (m_LastPingTime == ACE_Time_Value::zero)
m_LastPingTime = ACE_OS::gettimeofday(); // for 1st ping
if (m_LastPingTime == SystemTimePoint::min())
m_LastPingTime = std::chrono::system_clock::now(); // for 1st ping
else
{
ACE_Time_Value cur_time = ACE_OS::gettimeofday();
ACE_Time_Value diff_time (cur_time);
diff_time -= m_LastPingTime;
m_LastPingTime = cur_time;
auto now = std::chrono::system_clock::now();
Seconds seconds = std::chrono::duration_cast<Seconds>(now - m_LastPingTime);
m_LastPingTime = now;
if (diff_time < ACE_Time_Value (27))
if (seconds.count() < 27)
{
++m_OverSpeedPings;
uint32 max_count = sWorld->getIntConfig (CONFIG_MAX_OVERSPEED_PINGS);
uint32 max_count = sWorld->getIntConfig(CONFIG_MAX_OVERSPEED_PINGS);
if (max_count && m_OverSpeedPings > max_count)
{

View File

@@ -13,7 +13,6 @@
#ifndef _WORLDSOCKET_H
#define _WORLDSOCKET_H
#include <ace/Basic_Types.h>
#include <ace/Synch_Traits.h>
#include <ace/Svc_Handler.h>
#include <ace/SOCK_Stream.h>
@@ -28,6 +27,7 @@
#include "Common.h"
#include "AuthCrypt.h"
#include "Duration.h"
class ACE_Message_Block;
class WorldPacket;
@@ -151,7 +151,7 @@ class WorldSocket : public WorldHandler
private:
/// Time in which the last ping was received
ACE_Time_Value m_LastPingTime;
SystemTimePoint m_LastPingTime;
/// Keep track of over-speed pings, to prevent ping flood.
uint32 m_OverSpeedPings;

View File

@@ -84,7 +84,7 @@ bool Weather::ReGenerate()
// season source http://aa.usno.navy.mil/data/docs/EarthSeasons.html
time_t gtime = sWorld->GetGameTime();
struct tm ltime;
ACE_OS::localtime_r(&gtime, &ltime);
localtime_r(&gtime, &ltime);
uint32 season = ((ltime.tm_yday - 78 + 365)/91)%4;
#if defined(ENABLE_EXTRAS) && defined(ENABLE_EXTRA_LOGS)

View File

@@ -2838,7 +2838,7 @@ time_t World::GetNextTimeWithDayAndHour(int8 dayOfWeek, int8 hour)
hour = 0;
time_t curr = time(nullptr);
tm localTm;
ACE_OS::localtime_r(&curr, &localTm);
localtime_r(&curr, &localTm);
localTm.tm_hour = hour;
localTm.tm_min = 0;
localTm.tm_sec = 0;
@@ -2859,7 +2859,7 @@ time_t World::GetNextTimeWithMonthAndHour(int8 month, int8 hour)
hour = 0;
time_t curr = time(nullptr);
tm localTm;
ACE_OS::localtime_r(&curr, &localTm);
localtime_r(&curr, &localTm);
localTm.tm_mday = 1;
localTm.tm_hour = hour;
localTm.tm_min = 0;

View File

@@ -460,7 +460,7 @@ public:
{
time_t timeBan = time_t(fields2[0].GetUInt32());
tm tmBan;
ACE_OS::localtime_r(&timeBan, &tmBan);
localtime_r(&timeBan, &tmBan);
if (fields2[0].GetUInt32() == fields2[1].GetUInt32())
{
@@ -472,7 +472,7 @@ public:
{
time_t timeUnban = time_t(fields2[1].GetUInt32());
tm tmUnban;
ACE_OS::localtime_r(&timeUnban, &tmUnban);
localtime_r(&timeUnban, &tmUnban);
handler->PSendSysMessage("|%-15.15s|%02d-%02d-%02d %02d:%02d|%02d-%02d-%02d %02d:%02d|%-15.15s|%-15.15s|",
accountName.c_str(), tmBan.tm_year%100, tmBan.tm_mon+1, tmBan.tm_mday, tmBan.tm_hour, tmBan.tm_min,
tmUnban.tm_year%100, tmUnban.tm_mon+1, tmUnban.tm_mday, tmUnban.tm_hour, tmUnban.tm_min,
@@ -549,7 +549,7 @@ public:
{
time_t timeBan = time_t(banFields[0].GetUInt32());
tm tmBan;
ACE_OS::localtime_r(&timeBan, &tmBan);
localtime_r(&timeBan, &tmBan);
if (banFields[0].GetUInt32() == banFields[1].GetUInt32())
{
@@ -561,7 +561,7 @@ public:
{
time_t timeUnban = time_t(banFields[1].GetUInt32());
tm tmUnban;
ACE_OS::localtime_r(&timeUnban, &tmUnban);
localtime_r(&timeUnban, &tmUnban);
handler->PSendSysMessage("|%-15.15s|%02d-%02d-%02d %02d:%02d|%02d-%02d-%02d %02d:%02d|%-15.15s|%-15.15s|",
char_name.c_str(), tmBan.tm_year%100, tmBan.tm_mon+1, tmBan.tm_mday, tmBan.tm_hour, tmBan.tm_min,
tmUnban.tm_year%100, tmUnban.tm_mon+1, tmUnban.tm_mday, tmUnban.tm_hour, tmUnban.tm_min,
@@ -630,7 +630,7 @@ public:
Field* fields = result->Fetch();
time_t timeBan = time_t(fields[1].GetUInt32());
tm tmBan;
ACE_OS::localtime_r(&timeBan, &tmBan);
localtime_r(&timeBan, &tmBan);
if (fields[1].GetUInt32() == fields[2].GetUInt32())
{
handler->PSendSysMessage("|%-15.15s|%02d-%02d-%02d %02d:%02d| permanent |%-15.15s|%-15.15s|",
@@ -641,7 +641,7 @@ public:
{
time_t timeUnban = time_t(fields[2].GetUInt32());
tm tmUnban;
ACE_OS::localtime_r(&timeUnban, &tmUnban);
localtime_r(&timeUnban, &tmUnban);
handler->PSendSysMessage("|%-15.15s|%02d-%02d-%02d %02d:%02d|%02d-%02d-%02d %02d:%02d|%-15.15s|%-15.15s|",
fields[0].GetCString(), tmBan.tm_year%100, tmBan.tm_mon+1, tmBan.tm_mday, tmBan.tm_hour, tmBan.tm_min,
tmUnban.tm_year%100, tmUnban.tm_mon+1, tmUnban.tm_mday, tmUnban.tm_hour, tmUnban.tm_min,

View File

@@ -224,7 +224,7 @@ public:
char createdDateStr[20];
time_t createdDate = guild->GetCreatedDate();
tm localTm;
ACE_OS::localtime_r(&createdDate, &localTm);
localtime_r(&createdDate, &localTm);
strftime(createdDateStr, 20, "%Y-%m-%d %H:%M:%S", &localTm);
handler->PSendSysMessage(LANG_GUILD_INFO_CREATION_DATE, createdDateStr); // Creation Date

View File

@@ -2370,7 +2370,7 @@ public:
char buffer[80];
// set it to string
ACE_OS::localtime_r(&sqlTime, &timeInfo);
localtime_r(&sqlTime, &timeInfo);
strftime(buffer, sizeof(buffer), "%Y-%m-%d %I:%M%p", &timeInfo);
handler->PSendSysMessage(LANG_COMMAND_MUTEHISTORY_OUTPUT, buffer, fields[1].GetUInt32(), fields[2].GetCString(), fields[3].GetCString());

View File

@@ -846,7 +846,7 @@ public:
{
time_t curtime = time(nullptr);
tm strDate;
ACE_OS::localtime_r(&curtime, &strDate);
localtime_r(&curtime, &strDate);
if (strDate.tm_min == 0 || strDate.tm_min == 30)
return true;

View File

@@ -110,7 +110,7 @@ public:
{
time_t curtime = time(nullptr);
tm strdate;
ACE_OS::localtime_r(&curtime, &strdate);
localtime_r(&curtime, &strdate);
if (!preWarning && strdate.tm_hour == 13 && strdate.tm_min == 55)
{
@@ -255,7 +255,7 @@ public:
{
time_t curtime = time(nullptr);
tm strdate;
ACE_OS::localtime_r(&curtime, &strdate);
localtime_r(&curtime, &strdate);
if (!startWarning && strdate.tm_hour == 14 && strdate.tm_min == 0)
{
sCreatureTextMgr->SendChat(me, RIGGLE_SAY_START, 0, CHAT_MSG_MONSTER_YELL, LANG_UNIVERSAL, TEXT_RANGE_ZONE);

View File

@@ -15,9 +15,11 @@
#include "Log.h"
#include "RASocket.h"
#include "Util.h"
#include "Duration.h"
#include "World.h"
#include "SHA1.h"
#include "ServerMotd.h"
#include <thread>
RASocket::RASocket()
{
@@ -49,7 +51,7 @@ int RASocket::handle_close(ACE_HANDLE /*handle*/, ACE_Reactor_Mask /*mask*/)
// RASocket::commandfinished to be completed. Calling destroy() before the latter function ends
// will lead to using a freed pointer -> crash.
while (_commandExecuting)
ACE_OS::sleep(1);
std::this_thread::sleep_for(1s);
destroy();
return 0;