[win-linux] update manager: transfer recurring functions to CSocket

This commit is contained in:
SimplestStudio
2023-09-01 06:20:17 +03:00
parent 59e4d4a0ef
commit e2aa2199af
6 changed files with 81 additions and 93 deletions

View File

@ -32,6 +32,7 @@
#include "csocket.h"
#include <future>
#include <sstream>
#ifdef _WIN32
# define _WINSOCK_DEPRECATED_NO_WARNINGS
@ -274,6 +275,14 @@ bool CSocket::sendMessage(void *data, size_t size)
return true;
}
bool CSocket::sendMessage(int cmd, const tstring &param1, const tstring &param2)
{
tstring str = std::to_tstring(cmd) + _T("|") + (param1.empty() ? _T("null") : param1) +
_T("|") + (param2.empty() ? _T("null") : param2);
size_t sz = str.size() * sizeof(str.front());
return sendMessage((void*)str.c_str(), sz);
}
void CSocket::onMessageReceived(FnVoidData callback)
{
pimpl->m_received_callback = callback;
@ -283,3 +292,12 @@ void CSocket::onError(FnVoidCharPtr callback)
{
pimpl->m_error_callback = callback;
}
int CSocket::parseMessage(void *data, std::vector<tstring> &params)
{
tstring str((const tchar*)data), tmp;
tstringstream wss(str);
while (std::getline(wss, tmp, _T('|')))
params.push_back(tmp == _T("null") ? _T("") : std::move(tmp));
return (int)params.size();
}

View File

@ -33,7 +33,22 @@
#ifndef CSOCKET_H
#define CSOCKET_H
#include <string>
#include <vector>
#include <functional>
#ifdef _WIN32
# include <tchar.h>
# define tchar wchar_t
# define tstringstream std::wstringstream
# define tstring std::wstring
# define to_tstring to_wstring
#else
# define _T(str) str
# define tchar char
# define tstringstream std::stringstream
# define tstring std::string
# define to_tstring to_string
#endif
using std::size_t;
@ -64,8 +79,10 @@ public:
/* callback */
bool isPrimaryInstance();
bool sendMessage(void *data, size_t size);
bool sendMessage(int cmd, const tstring &param1 = _T(""), const tstring &param2 = _T(""));
void onMessageReceived(FnVoidData callback);
void onError(FnVoidCharPtr callback);
int parseMessage(void *data, std::vector<tstring> &params);
private:
class CSocketPrv;

View File

@ -45,6 +45,7 @@
# endif
# include "platform_win/utils.h"
# include <codecvt>
# include <Windows.h>
# include <WinInet.h>
# include <shlwapi.h>
# define APP_LAUNCH_NAME L"/DesktopEditors.exe"
@ -66,15 +67,15 @@
# define APP_HELPER "/editors_helper"
# define DAEMON_NAME "/updatesvc"
# define SUBFOLDER "/desktopeditors"
# define ARCHIVE_EXT TEXT(".tar.xz")
# define ARCHIVE_PATTERN TEXT("*.tar.xz")
# define ARCHIVE_EXT _T(".tar.xz")
# define ARCHIVE_PATTERN _T("*.tar.xz")
# define sleep(a) usleep(a*1000)
#endif
#define UPDATE_PATH TEXT("/" REG_APP_NAME "Updates")
#define BACKUP_PATH TEXT("/" REG_APP_NAME "Backup")
#define PROVIDERS_PATH TEXT("/providers")
#define SUCCES_UNPACKED TEXT("/success_unpacked.txt")
#define UPDATE_PATH _T("/" REG_APP_NAME "Updates")
#define BACKUP_PATH _T("/" REG_APP_NAME "Backup")
#define PROVIDERS_PATH _T("/providers")
#define SUCCES_UNPACKED _T("/success_unpacked.txt")
#define __GLOBAL_LOCK if (m_lock) {NS_Logger::WriteLog(_T("Blocked in: ") + FUNCTION_INFO); return;} m_lock = true; \
NS_Logger::WriteLog(_T("Locking and further execution: ") + FUNCTION_INFO);
#define __UNLOCK m_lock = false; NS_Logger::WriteLog(_T("Unlocked in: ") + FUNCTION_INFO);
@ -91,7 +92,7 @@ auto currentArch()->tstring
return L"_x86";
# endif
#else
return TEXT("_x64");
return _T("_x64");
#endif
}
@ -113,7 +114,7 @@ auto generateTmpFileName(const tstring &ext)->tstring
uuid_unparse(uuid, uuid_str);
uuid_tstr = uuid_str;
#endif
return NS_File::tempPath() + TEXT("/") + TEXT(FILE_PREFIX) + uuid_tstr + currentArch() + ext;
return NS_File::tempPath() + _T("/") + _T(FILE_PREFIX) + uuid_tstr + currentArch() + ext;
}
auto isSuccessUnpacked(const tstring &successFilePath, const tstring &version)->bool
@ -202,32 +203,27 @@ void CSvcManager::init()
onCompleteUnzip(error);
});
m_socket->onMessageReceived([=](void *data, size_t) {
tstring str((const tchar*)data), tmp;
vector<tstring> params;
tstringstream wss(str);
while (std::getline(wss, tmp, TEXT('|')))
params.push_back(std::move(tmp));
if (params.size() == 4) {
if (m_socket->parseMessage(data, params) == 3) {
switch (std::stoi(params[0])) {
case MSG_CheckUpdates: {
__GLOBAL_LOCK
//DeleteUrlCacheEntry(params[1].c_str());
m_downloadMode = Mode::CHECK_UPDATES;
if (m_pDownloader)
m_pDownloader->downloadFile(params[1], generateTmpFileName(TEXT(".json")));
NS_Logger::WriteLog(TEXT("Received MSG_CheckUpdates, URL: ") + params[1]);
m_pDownloader->downloadFile(params[1], generateTmpFileName(_T(".json")));
NS_Logger::WriteLog(_T("Received MSG_CheckUpdates, URL: ") + params[1]);
break;
}
case MSG_LoadUpdates: {
__GLOBAL_LOCK
m_downloadMode = Mode::DOWNLOAD_UPDATES;
if (m_pDownloader) {
tstring ext = (params[2] == TEXT("iss")) ? TEXT(".exe") :
(params[2] == TEXT("msi")) ? TEXT(".msi") : ARCHIVE_EXT;
tstring ext = (params[2] == _T("iss")) ? _T(".exe") :
(params[2] == _T("msi")) ? _T(".msi") : ARCHIVE_EXT;
m_pDownloader->downloadFile(params[1], generateTmpFileName(ext));
}
NS_Logger::WriteLog(TEXT("Received MSG_LoadUpdates, URL: ") + params[1]);
NS_Logger::WriteLog(_T("Received MSG_LoadUpdates, URL: ") + params[1]);
break;
}
case MSG_StopDownload: {
@ -278,13 +274,13 @@ void CSvcManager::onCompleteUnzip(const int error)
if (!NS_File::writeToFile(updPath + SUCCES_UNPACKED, successList)) {
return;
}
if (!sendMessage(MSG_ShowStartInstallMessage))
if (!m_socket->sendMessage(MSG_ShowStartInstallMessage))
NS_Logger::WriteLog(DEFAULT_ERROR_MESSAGE);
} else
if (error == UNZIP_ERROR) {
tstring error(TEXT("An error occured while unpacking the archive"));
if (!sendMessage(MSG_OtherError, error))
tstring error(_T("An error occured while unpacking the archive"));
if (!m_socket->sendMessage(MSG_OtherError, error))
NS_Logger::WriteLog(DEFAULT_ERROR_MESSAGE);
} else
@ -299,10 +295,10 @@ void CSvcManager::onCompleteSlot(const int error, const tstring &filePath)
if (error == 0) {
switch (m_downloadMode) {
case Mode::CHECK_UPDATES:
sendMessage(MSG_LoadCheckFinished, filePath);
m_socket->sendMessage(MSG_LoadCheckFinished, filePath);
break;
case Mode::DOWNLOAD_UPDATES:
sendMessage(MSG_LoadUpdateFinished, filePath);
m_socket->sendMessage(MSG_LoadUpdateFinished, filePath);
break;
default:
break;
@ -312,19 +308,19 @@ void CSvcManager::onCompleteSlot(const int error, const tstring &filePath)
// Pause or Stop
} else
if (error == -1) {
sendMessage(MSG_OtherError, TEXT("Update download failed: out of memory!"));
m_socket->sendMessage(MSG_OtherError, _T("Update download failed: out of memory!"));
} else
if (error == -2) {
sendMessage(MSG_OtherError, TEXT("Update download failed: server connection error!"));
m_socket->sendMessage(MSG_OtherError, _T("Update download failed: server connection error!"));
} else {
sendMessage(MSG_OtherError, TEXT("Update download failed: network error!"));
m_socket->sendMessage(MSG_OtherError, _T("Update download failed: network error!"));
}
}
void CSvcManager::onProgressSlot(const int percent)
{
if (m_downloadMode == Mode::DOWNLOAD_UPDATES)
sendMessage(MSG_Progress, to_tstring(percent));
m_socket->sendMessage(MSG_Progress, to_tstring(percent));
}
void CSvcManager::unzipIfNeeded(const tstring &filePath, const tstring &newVersion)
@ -335,7 +331,7 @@ void CSvcManager::unzipIfNeeded(const tstring &filePath, const tstring &newVersi
const tstring updPath = NS_File::parentPath(NS_File::appPath()) + UPDATE_PATH;
auto unzip = [=]()->void {
if (!NS_File::dirExists(updPath) && !NS_File::makePath(updPath)) {
NS_Logger::WriteLog(TEXT("An error occurred while creating dir: ") + updPath);
NS_Logger::WriteLog(_T("An error occurred while creating dir: ") + updPath);
__UNLOCK
return;
}
@ -347,7 +343,7 @@ void CSvcManager::unzipIfNeeded(const tstring &filePath, const tstring &newVersi
} else {
if (isSuccessUnpacked(updPath + SUCCES_UNPACKED, newVersion)) {
__UNLOCK
if (!sendMessage(MSG_ShowStartInstallMessage))
if (!m_socket->sendMessage(MSG_ShowStartInstallMessage))
NS_Logger::WriteLog(DEFAULT_ERROR_MESSAGE);
} else {
@ -368,7 +364,7 @@ void CSvcManager::clearTempFiles(const tstring &prefix, const tstring &except)
tstring _error;
list<tstring> filesList;
if (!NS_File::GetFilesList(NS_File::tempPath(), &filesList, _error, true)) {
NS_Logger::WriteLog(DEFAULT_ERROR_MESSAGE + TEXT(" ") + _error);
NS_Logger::WriteLog(DEFAULT_ERROR_MESSAGE + _T(" ") + _error);
return;
}
for (auto &filePath : filesList) {
@ -394,7 +390,7 @@ void CSvcManager::startReplacingFiles()
tstring updSubPath = NS_File::fileExists(updPath + SUBFOLDER + APP_LAUNCH_NAME) ? updPath + SUBFOLDER : updPath;
tstring tmpPath = NS_File::parentPath(appPath) + BACKUP_PATH;
if (!NS_File::dirExists(updPath)) {
NS_Logger::WriteLog(TEXT("Update cancelled. Can't find folder: ") + updPath, true);
NS_Logger::WriteLog(_T("Update cancelled. Can't find folder: ") + updPath, true);
return;
}
@ -415,7 +411,7 @@ void CSvcManager::startReplacingFiles()
// Check backup folder
if (NS_File::dirExists(tmpPath) && !NS_File::removeDirRecursively(tmpPath)) {
NS_Logger::WriteLog(TEXT("Update cancelled. Can't delete folder: ") + tmpPath, true);
NS_Logger::WriteLog(_T("Update cancelled. Can't delete folder: ") + tmpPath, true);
return;
}
@ -434,7 +430,7 @@ void CSvcManager::startReplacingFiles()
sleep(500);
if (NS_File::isProcessRunning(app)) {
NS_Logger::WriteLog(TEXT("Update cancelled. The ") + app + TEXT(" is not closed!"), true);
NS_Logger::WriteLog(_T("Update cancelled. The ") + app + _T(" is not closed!"), true);
return;
}
}
@ -450,22 +446,22 @@ void CSvcManager::startReplacingFiles()
#else
if (!NS_File::replaceFolder(appPath, tmpPath, true)) {
#endif
NS_Logger::WriteLog(TEXT("Update cancelled. Can't replace files to backup: ") + NS_Utils::GetLastErrorAsString(), true);
NS_Logger::WriteLog(_T("Update cancelled. Can't replace files to backup: ") + NS_Utils::GetLastErrorAsString(), true);
if (NS_File::dirExists(tmpPath) && !NS_File::dirIsEmpty(tmpPath) && !NS_File::replaceFolder(tmpPath, appPath))
NS_Logger::WriteLog(TEXT("Can't restore files from backup!"), true);
NS_Logger::WriteLog(_T("Can't restore files from backup!"), true);
return;
}
// Move update path to app path
if (!NS_File::replaceFolder(updSubPath, appPath, true)) {
NS_Logger::WriteLog(TEXT("Update cancelled. Can't move updates to App path: ") + NS_Utils::GetLastErrorAsString(), true);
NS_Logger::WriteLog(_T("Update cancelled. Can't move updates to App path: ") + NS_Utils::GetLastErrorAsString(), true);
if (NS_File::dirExists(appPath) && !NS_File::removeDirRecursively(appPath)) {
NS_Logger::WriteLog(TEXT("An error occurred while remove App path: ") + NS_Utils::GetLastErrorAsString(), true);
NS_Logger::WriteLog(_T("An error occurred while remove App path: ") + NS_Utils::GetLastErrorAsString(), true);
return;
}
if (!NS_File::replaceFolder(tmpPath, appPath, true))
NS_Logger::WriteLog(TEXT("An error occurred while restore files from backup: ") + NS_Utils::GetLastErrorAsString(), true);
NS_Logger::WriteLog(_T("An error occurred while restore files from backup: ") + NS_Utils::GetLastErrorAsString(), true);
NS_File::removeDirRecursively(updPath);
return;
@ -538,18 +534,11 @@ void CSvcManager::startReplacingFiles()
NS_File::removeDirRecursively(tmpPath);
// Restart program
if (!NS_File::runProcess(appPath + APP_LAUNCH_NAME, TEXT("")))
NS_Logger::WriteLog(TEXT("An error occurred while restarting the program!"), true);
if (!NS_File::runProcess(appPath + APP_LAUNCH_NAME, _T("")))
NS_Logger::WriteLog(_T("An error occurred while restarting the program!"), true);
// Restart service
#ifdef _WIN32
restartService();
#endif
}
bool CSvcManager::sendMessage(int cmd, const tstring &param1, const tstring &param2, const tstring &param3)
{
tstring str = to_tstring(cmd) + TEXT("|") + param1 + TEXT("|") + param2 + TEXT("|") + param3;
size_t sz = str.size() * sizeof(str.front());
return m_socket->sendMessage((void*)str.c_str(), sz);
}

View File

@ -38,19 +38,9 @@
#ifdef _WIN32
# include "classes/platform_win/cdownloader.h"
# include "classes/platform_win/cunzip.h"
# include <Windows.h>
# define tstring std::wstring
# define tchar wchar_t
# define tstringstream std::wstringstream
# define to_tstring to_wstring
#else
# include "classes/platform_linux/cdownloader.h"
# include "classes/platform_linux/cunzip.h"
# define TEXT(str) str
# define tstring std::string
# define tchar char
# define tstringstream std::stringstream
# define to_tstring to_string
#endif
typedef std::function<void(void)> FnVoidVoid;
@ -74,8 +64,6 @@ private:
void unzipIfNeeded(const tstring &filePath, const tstring &newVersion);
void clearTempFiles(const tstring &prefix, const tstring &except = tstring());
void startReplacingFiles();
bool sendMessage(int cmd, const tstring &param1 = TEXT("null"), const tstring &param2 = TEXT("null"),
const tstring &param3 = TEXT("null"));
FnVoidVoid m_quit_callback = nullptr;
tstring m_newVersion;