mirror of https://github.com/PurpleI2P/i2pd.git
I2P: End-to-End encrypted and anonymous Internet
https://i2pd.website/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
110 lines
2.4 KiB
110 lines
2.4 KiB
5 years ago
|
/*
|
||
3 years ago
|
* Copyright (c) 2013-2022, The PurpleI2P Project
|
||
5 years ago
|
*
|
||
|
* This file is part of Purple i2pd project and licensed under BSD3
|
||
|
*
|
||
|
* See full license text in LICENSE file at top of project tree
|
||
|
*/
|
||
|
|
||
2 years ago
|
#ifdef _WIN32
|
||
|
|
||
9 years ago
|
#include <thread>
|
||
|
#include <clocale>
|
||
|
#include "Config.h"
|
||
|
#include "Daemon.h"
|
||
|
#include "util.h"
|
||
|
#include "Log.h"
|
||
|
|
||
3 years ago
|
#include "Win32Service.h"
|
||
9 years ago
|
#ifdef WIN32_APP
|
||
5 years ago
|
#include <windows.h>
|
||
4 years ago
|
#include "Win32App.h"
|
||
9 years ago
|
#endif
|
||
|
|
||
|
namespace i2p
|
||
|
{
|
||
7 years ago
|
namespace util
|
||
|
{
|
||
|
bool DaemonWin32::init(int argc, char* argv[])
|
||
9 years ago
|
{
|
||
7 years ago
|
setlocale(LC_CTYPE, "");
|
||
|
SetConsoleCP(1251);
|
||
|
SetConsoleOutputCP(1251);
|
||
|
setlocale(LC_ALL, "Russian");
|
||
7 years ago
|
setlocale(LC_TIME, "C");
|
||
9 years ago
|
|
||
5 years ago
|
i2p::log::SetThrowFunction ([](const std::string& s)
|
||
|
{
|
||
|
MessageBox(0, TEXT(s.c_str ()), TEXT("i2pd"), MB_ICONERROR | MB_TASKMODAL | MB_OK );
|
||
4 years ago
|
}
|
||
|
);
|
||
5 years ago
|
|
||
2 years ago
|
i2p::win32::SetIsGraceful (std::bind (&DaemonWin32::SetIsGraceful, this, std::placeholders::_1));
|
||
|
i2p::win32::GetIsGraceful (std::bind (&DaemonWin32::GetIsGraceful, this));
|
||
|
|
||
7 years ago
|
if (!Daemon_Singleton::init(argc, argv))
|
||
|
return false;
|
||
3 years ago
|
|
||
|
if (isDaemon)
|
||
|
{
|
||
|
LogPrint(eLogDebug, "Daemon: running as service");
|
||
|
I2PService service((PSTR)SERVICE_NAME);
|
||
2 years ago
|
service.SetDaemonStart (std::bind (&DaemonWin32::start, this));
|
||
|
service.SetDaemonStop (std::bind (&DaemonWin32::stop, this));
|
||
3 years ago
|
if (!I2PService::Run(service))
|
||
|
{
|
||
|
LogPrint(eLogError, "Daemon: Service failed to run w/err 0x%08lx\n", GetLastError());
|
||
|
return false;
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
7 years ago
|
return true;
|
||
|
}
|
||
9 years ago
|
|
||
7 years ago
|
bool DaemonWin32::start()
|
||
|
{
|
||
|
setlocale(LC_CTYPE, "");
|
||
|
SetConsoleCP(1251);
|
||
|
SetConsoleOutputCP(1251);
|
||
|
setlocale(LC_ALL, "Russian");
|
||
7 years ago
|
setlocale(LC_TIME, "C");
|
||
9 years ago
|
#ifdef WIN32_APP
|
||
7 years ago
|
if (!i2p::win32::StartWin32App ()) return false;
|
||
9 years ago
|
#endif
|
||
7 years ago
|
bool ret = Daemon_Singleton::start();
|
||
|
if (ret && i2p::log::Logger().GetLogType() == eLogFile)
|
||
|
{
|
||
|
// TODO: find out where this garbage to console comes from
|
||
|
SetStdHandle(STD_OUTPUT_HANDLE, INVALID_HANDLE_VALUE);
|
||
|
SetStdHandle(STD_ERROR_HANDLE, INVALID_HANDLE_VALUE);
|
||
9 years ago
|
}
|
||
7 years ago
|
bool insomnia; i2p::config::GetOption("insomnia", insomnia);
|
||
|
if (insomnia)
|
||
|
SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED);
|
||
|
return ret;
|
||
|
}
|
||
9 years ago
|
|
||
7 years ago
|
bool DaemonWin32::stop()
|
||
|
{
|
||
9 years ago
|
#ifdef WIN32_APP
|
||
7 years ago
|
i2p::win32::StopWin32App ();
|
||
9 years ago
|
#endif
|
||
7 years ago
|
return Daemon_Singleton::stop();
|
||
|
}
|
||
9 years ago
|
|
||
7 years ago
|
void DaemonWin32::run ()
|
||
|
{
|
||
9 years ago
|
#ifdef WIN32_APP
|
||
7 years ago
|
i2p::win32::RunWin32App ();
|
||
9 years ago
|
#else
|
||
7 years ago
|
while (running)
|
||
9 years ago
|
{
|
||
|
std::this_thread::sleep_for (std::chrono::seconds(1));
|
||
|
}
|
||
|
#endif
|
||
|
}
|
||
|
}
|
||
7 years ago
|
}
|
||
7 years ago
|
#endif //_WIN32
|