ddnet/src/engine/shared/engine.cpp

120 lines
3.1 KiB
C++
Raw Normal View History

2010-11-20 10:37:14 +00:00
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
/* If you are missing that file, acquire a complete release at teeworlds.com. */
2010-05-29 07:25:38 +00:00
#include <base/system.h>
#include <engine/console.h>
2011-02-27 14:03:57 +00:00
#include <engine/engine.h>
#include <engine/storage.h>
2010-05-29 07:25:38 +00:00
#include <engine/shared/config.h>
#include <engine/shared/network.h>
CHostLookup::CHostLookup()
{
}
2010-05-29 07:25:38 +00:00
CHostLookup::CHostLookup(const char *pHostname, int Nettype)
2010-05-29 07:25:38 +00:00
{
str_copy(m_aHostname, pHostname, sizeof(m_aHostname));
m_Nettype = Nettype;
}
void CHostLookup::Run()
{
m_Result = net_host_lookup(m_aHostname, &m_Addr, m_Nettype);
2010-05-29 07:25:38 +00:00
}
2011-02-27 14:03:57 +00:00
class CEngine : public IEngine
2010-05-29 07:25:38 +00:00
{
2011-02-27 14:03:57 +00:00
public:
IConsole *m_pConsole;
IStorage *m_pStorage;
bool m_Logging;
2010-05-29 07:25:38 +00:00
static void Con_DbgDumpmem(IConsole::IResult *pResult, void *pUserData)
2011-02-27 14:03:57 +00:00
{
CEngine *pEngine = static_cast<CEngine *>(pUserData);
char aBuf[32];
str_timestamp(aBuf, sizeof(aBuf));
char aFilename[128];
str_format(aFilename, sizeof(aFilename), "dumps/memory_%s.txt", aBuf);
mem_debug_dump(pEngine->m_pStorage->OpenFile(aFilename, IOFLAG_WRITE, IStorage::TYPE_SAVE));
2011-02-27 14:03:57 +00:00
}
2010-05-29 07:25:38 +00:00
static void Con_DbgLognetwork(IConsole::IResult *pResult, void *pUserData)
2011-02-27 14:03:57 +00:00
{
CEngine *pEngine = static_cast<CEngine *>(pUserData);
2010-05-29 07:25:38 +00:00
if(pEngine->m_Logging)
{
CNetBase::CloseLog();
pEngine->m_Logging = false;
}
else
{
char aBuf[32];
str_timestamp(aBuf, sizeof(aBuf));
char aFilenameSent[128], aFilenameRecv[128];
str_format(aFilenameSent, sizeof(aFilenameSent), "dumps/network_sent_%s.txt", aBuf);
str_format(aFilenameRecv, sizeof(aFilenameRecv), "dumps/network_recv_%s.txt", aBuf);
CNetBase::OpenLog(pEngine->m_pStorage->OpenFile(aFilenameSent, IOFLAG_WRITE, IStorage::TYPE_SAVE),
pEngine->m_pStorage->OpenFile(aFilenameRecv, IOFLAG_WRITE, IStorage::TYPE_SAVE));
pEngine->m_Logging = true;
}
}
2010-05-29 07:25:38 +00:00
2017-09-03 08:37:24 +00:00
CEngine(const char *pAppname, bool Silent)
2011-02-27 14:03:57 +00:00
{
2017-09-03 08:37:24 +00:00
if(!Silent)
dbg_logger_stdout();
2011-02-27 14:03:57 +00:00
dbg_logger_debugger();
2011-02-27 14:03:57 +00:00
//
dbg_msg("engine", "running on %s-%s-%s", CONF_FAMILY_STRING, CONF_PLATFORM_STRING, CONF_ARCH_STRING);
#ifdef CONF_ARCH_ENDIAN_LITTLE
dbg_msg("engine", "arch is little endian");
#elif defined(CONF_ARCH_ENDIAN_BIG)
dbg_msg("engine", "arch is big endian");
#else
dbg_msg("engine", "unknown endian");
#endif
2010-05-29 07:25:38 +00:00
2011-02-27 14:03:57 +00:00
// init the network
net_init();
CNetBase::Init();
2011-02-27 16:56:03 +00:00
m_JobPool.Init(1);
2010-05-29 07:25:38 +00:00
m_Logging = false;
}
2010-05-29 07:25:38 +00:00
void Init()
{
m_pConsole = Kernel()->RequestInterface<IConsole>();
m_pStorage = Kernel()->RequestInterface<IStorage>();
2010-05-29 07:25:38 +00:00
if(!m_pConsole || !m_pStorage)
return;
m_pConsole->Register("dbg_dumpmem", "", CFGFLAG_SERVER|CFGFLAG_CLIENT, Con_DbgDumpmem, this, "Dump the memory");
m_pConsole->Register("dbg_lognetwork", "", CFGFLAG_SERVER|CFGFLAG_CLIENT, Con_DbgLognetwork, this, "Log the network");
2011-02-27 14:03:57 +00:00
}
2010-05-29 07:25:38 +00:00
2011-02-27 14:03:57 +00:00
void InitLogfile()
{
// open logfile if needed
if(g_Config.m_Logfile[0])
dbg_logger_file(g_Config.m_Logfile);
}
2010-05-29 07:25:38 +00:00
void AddJob(std::shared_ptr<IJob> pJob)
2011-02-27 16:56:03 +00:00
{
if(g_Config.m_Debug)
dbg_msg("engine", "job added");
m_JobPool.Add(pJob);
2011-02-27 14:03:57 +00:00
}
};
2010-05-29 07:25:38 +00:00
2017-09-03 08:37:24 +00:00
IEngine *CreateEngine(const char *pAppname, bool Silent) { return new CEngine(pAppname, Silent); }