Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
setvisible committed Jan 15, 2023
2 parents 3ecb5b1 + 5ce23b8 commit 03511e6
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 61 deletions.
40 changes: 40 additions & 0 deletions src/ipc/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#define IPC_CONSTANTS_H

#include <QtCore/QString>
#include <QtCore/QSharedMemory>

/*!
* \class InterProcessCommunication
Expand Down Expand Up @@ -92,4 +93,43 @@ static const std::string C_STR_DOWNLOAD_LINK( C_KEYWORD_DOWNLOAD_LINK.toStdStr
static const std::string C_STR_ERROR( C_PACKET_ERROR.toStdString());


static inline QString shm_read(QSharedMemory *sharedMemory)
{
// Reads the shared memory.
QByteArray bytes;
if (sharedMemory->lock()) {
bytes.setRawData((char*)sharedMemory->constData(), sharedMemory->size());
sharedMemory->unlock();
}

// Qt5 to Qt6
// QString::QString(const QByteArray &ba)
// Note: : any null ('\0') bytes in the byte array will be included in this string,
// converted to Unicode null characters (U+0000).
// This behavior is different from Qt 5.x.
auto real_size_excluding_null = bytes.indexOf(QChar::Null);
bytes.truncate(real_size_excluding_null);
return QString(bytes);
}

static inline void shm_write(QSharedMemory *sharedMemory, const QString &message)
{
// Qt5 to Qt6
// QString::QString(const QByteArray &ba)
// Note: : any null ('\0') bytes in the byte array will be included in this string,
// converted to Unicode null characters (U+0000).
// This behavior is different from Qt 5.x.
QByteArray bytes = message.toUtf8();
bytes.append(QChar::Null);

// Write message into shared memory.
if (sharedMemory->lock()) {
char *to = (char*)sharedMemory->data();
const char *from = bytes.constData();
qsizetype max_count = qMin(sharedMemory->size(), bytes.size());
memcpy(to, from, max_count);
sharedMemory->unlock();
}
}

#endif // IPC_CONSTANTS_H
29 changes: 4 additions & 25 deletions src/ipc/interprocesscommunication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,33 +37,12 @@ QString InterProcessCommunication::readMessageFromLauncher()

if (sharedMemory.attach(QSharedMemory::ReadWrite)) {

QByteArray bytes;

if (sharedMemory.lock()) {
// Reads the shared memory
auto ptr = static_cast<const char*>(sharedMemory.constData());
auto n = static_cast<uint>(sharedMemory.size());
bytes.setRawData(ptr, n);
sharedMemory.unlock();
}

message += QString(bytes);
// Read message from Launcher
message += shm_read(&sharedMemory);
message += QChar::Space;

if (sharedMemory.lock()) {
// Replies the ACK message
bytes = C_SHARED_MEMORY_ACK_REPLY.toUtf8();

const char *from = bytes.constData();
void *to = sharedMemory.data();
size_t size = static_cast<size_t>(qMin(bytes.size() + 1, sharedMemory.size()));
memcpy(to, from, size);

char *d_ptr = static_cast<char*>(sharedMemory.data());
d_ptr[sharedMemory.size() - 1] = '\0';

sharedMemory.unlock();
}
// Write Acknowledge to Launcher
shm_write(&sharedMemory, C_SHARED_MEMORY_ACK_REPLY);

sharedMemory.detach();

Expand Down
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.0.1
3.0.2
49 changes: 14 additions & 35 deletions web-extension/launcher/launcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
# include <QtCore/QFileInfo>
#endif

#if defined(DEBUG_LAUNCHER)
#if defined(DEBUG_LAUNCHER_TO_APP)
# include <QtCore/QDebug>
#endif

Expand All @@ -66,16 +66,16 @@ void log(const QString &label, const std::string &message);

// Constants: Launcher <-> Browser
#ifdef Q_OS_WIN
static const std::string C_PROCESS ("./DownZemAll.exe");
std::string C_PROCESS {"./DownZemAll.exe"};
#elif defined(Q_OS_UNIX)
static const std::string C_PROCESS ("./DownZemAll");
std::string C_PROCESS {"./DownZemAll"};
#else
#endif
static const std::string C_HAND_SHAKE_QUESTION ("\"areyouthere");
static const std::string C_HAND_SHAKE_ANSWER ("somewhere");
static const std::string C_LAUNCH ("\"launch ");
static const std::string C_CHROMIUM_HEADER ("{\"text\":");
static const std::string C_CHROMIUM_FOOTER ("}");
std::string C_HAND_SHAKE_QUESTION {"\"areyouthere"};
std::string C_HAND_SHAKE_ANSWER {"somewhere"};
std::string C_LAUNCH {"\"launch "};
std::string C_CHROMIUM_HEADER {"{\"text\":"};
std::string C_CHROMIUM_FOOTER {"}"};


static std::string unquote(const std::string &str)
Expand Down Expand Up @@ -106,12 +106,12 @@ void log(const char* label, const std::string &message)

void log(const char* label, const QString &message1, const QString &message2)
{
log(QString("%0: %1 %2").arg(label).arg(message1).arg(message2));
log(QString("%0: %1 %2").arg(QString::fromUtf8(label), message1, message2));
}

void log(const QString &label, const std::string &message)
{
log(QString("%0: %1").arg(label).arg(QString::fromStdString(message)));
log(QString("%0: %1").arg(label, QString::fromStdString(message)));
}

void log(const QString &message)
Expand Down Expand Up @@ -260,20 +260,7 @@ static bool sendCommandToProcess(const QString &program, const QString &argument

if (sharedMemory.isAttached()) {

if (sharedMemory.lock()) {
// Write the message into the shared memory
QByteArray bytes = arguments.toUtf8();

const char *from = bytes.constData();
void *to = sharedMemory.data();
size_t size = static_cast<size_t>(qMin(bytes.size() + 1, sharedMemory.size()));
memcpy(to, from, size);

char *d_ptr = static_cast<char*>(sharedMemory.data());
d_ptr[sharedMemory.size() - 1] = '\0';

sharedMemory.unlock();
}
shm_write(&sharedMemory, arguments);

} else {
log(Q_FUNC_INFO, QString("Unable to attach to the shared memory segment"));
Expand Down Expand Up @@ -313,17 +300,9 @@ static bool sendCommandToProcess(const QString &program, const QString &argument
counter++;

mSleep(1000);
QByteArray answer;

if (sharedMemory.lock()) {
// Reads the shared memory
const char* ptr = static_cast<const char*>(sharedMemory.constData());
uint n = static_cast<uint>(sharedMemory.size());
answer.setRawData(ptr, n);
sharedMemory.unlock();
}
QString answer = shm_read(&sharedMemory);

if (QString(answer) == C_SHARED_MEMORY_ACK_REPLY) {
if (answer == C_SHARED_MEMORY_ACK_REPLY) {
sharedMemory.detach();
return true;
}
Expand Down Expand Up @@ -367,7 +346,7 @@ std::string compress(const std::string &command)

int main(int argc, char* argv[])
{
#if defined(DEBUG_LAUNCHER)
#if defined(DEBUG_LAUNCHER_TO_APP)
/*
* The code below permits to step-by-step debug the Launcher,
* launching the Application and passing a dummy message
Expand Down

0 comments on commit 03511e6

Please sign in to comment.