服务器和客户端之间的同步

Synchronization between Server & Client

我有一个基于 gsoap 的服务器。 Gsoap 也是在 Qt 上编写的。所以我可以使用 Qt classes.

我从客户端向服务器发送请求。

请求如下:服务器提供了一个phone号码和应该发送给他的消息。这个列表可能有 10 000 个或更多,这正是问题所在!我将使用 QThread 向每个号码发送一条消息。当QThread结束工作的时候,应该记录数据库的历史。。问题是我们无法理解什么时候所有的历史都记录在数据库中。

我知道这是对问题的非常糟糕的解释!请原谅我的英语。

现在我将尝试向您展示程序的问题。

我有一个 QThread class,用于发送消息。 One History class,在数据库中写入历史。

void MainWindow::on_pushButton_clicked() //Imagine that this is a server function.
{
    QSet<int> st; //List to who sends a message.
    st << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10;
    QSet<int> clientAnswer = this->start(st);
    qDebug() << "newSet: " << clientAnswer;
    //return ClientAnswer list ot client.
}


 QSet<int> MainWindow::start(QSet<int> st)
 {
     for (auto it : st) {
         MyThread *thrd = new MyThread(it);
         History *hist = new History();
         connect(thrd, &MyThread::smsSended, hist, &History::insertHistory);
         connect(hist, &History::historyAdded, this, [=](int historyUID){
             qDebug() << "history inserted: " << historyUID;
             this->ansSet.insert(historyUID);
             if (this->ansSet.size() == st.size()) {
                 qDebug() << "mainwindow finished!";
                 emit alreadyDone();
             }
         });
         connect(thrd, &MyThread::finished, hist, &History::deleteLater);
         connect(thrd, &MyThread::finished, thrd, &MyThread::deleteLater);
         thrd->start();
     }

     return this->ansSet;
 }

MainWindow.h

private:
Ui::MainWindow *ui;
QSet<int> ansSet; //The list is to send a client.

MyThread.cpp:

class MyThread : public QThread
{
    Q_OBJECT
public:
    explicit MyThread(int tInt, QObject *parent = 0);

signals:
    void smsSended(int);

public slots:
    void run();

private:
    int m_int;
};

MyThread.h

MyThread::MyThread(int tInt, QObject *parent) : QThread(parent)
{
    this->m_int = tInt;
}

void MyThread::run()
{
    qDebug() << "thread started: " << this->m_int;
    emit smsSended(this->m_int * 10);
}

History.cpp

 History::History(QObject *parent) : QObject(parent)
 {

 }

 void History::insertHistory(int insertInt)
 {
     qDebug() << "insert History: " << insertInt;
     emit historyAdded(insertInt);
 }

History.h

class History : public QObject
{
    Q_OBJECT
public:
    explicit History(QObject *parent = 0);

signals:
    void historyAdded(int hInt);

public slots:
    void insertHistory(int insertInt);
};

应用程序输出如下:

thread started:  5
thread started:  1
thread started:  3
thread started:  2
thread started:  4
thread started:  7
thread started:  9
thread started:  6
newSet:  QSet()
thread started:  8
thread started:  10
insert History:  50
history inserted:  50
insert History:  10
history inserted:  10
insert History:  30
history inserted:  30
insert History:  20
history inserted:  20
insert History:  40
history inserted:  40
insert History:  70
history inserted:  70
insert History:  90
history inserted:  90
insert History:  60
history inserted:  60
insert History:  80
history inserted:  80
insert History:  100
history inserted:  100
mainwindow finished!

我知道这个输出是正确的。但是,如果 (this->ansSet.size() == st.size()) 发生时,我如何从 MainWindow :: start 函数中执行 return QSet ---> ansSet ?或者你有其他想法?

伙计们,我很抱歉我的英语:)

首先,让我们对MainWindow::start进行初步更正,以便我们对其进行推理:

QSet<int> MainWindow::start(QSet<int> numbers)
{
     History* history = new History();

     for (auto number : numbers) 
     {
         MyThread* sender = new MyThread(number);
         connect(sender, &MyThread::smsSent, history, &History::insertHistory);
         connect(history, &History::historyAdded, this, [=] (int historyUID) {
             qDebug() << "History inserted: " << historyUID;
             this->answers.insert(historyUID);
             if (this->answers.size() == numbers.size()) {
                 qDebug() << "MainWindow finished!";
                 emit alreadyDone();
                 history->deleteLater();
             }
         });
         connect(sender, &MyThread::finished, sender, &MyThread::deleteLater);
         sender->start();
     }

     return this->answers;
}

好的,完成后问题如下:您在一个地方编写异步(非阻塞)代码 (MainWindow::start),但同时尝试同步使用它(阻塞)在另一个地方 QSet<int> clientAnswer = this->start(st);.

通常,如果你要async 某处 - 你会async 无处不在,因为异步和同步不能很好地相处。虽然,您可以等待异步操作完成(例如 Qt::BlockingQueuedConnection),但为什么要这样做呢?如果 MainWindow::start 块然后 MainWindow::on_pushButton_clicked 块,如果 MainWindow::on_pushButton_clicked 块和 MainWindow 是您的用户界面 window,然后在按钮上单击所有 UI冻结,如果 on_pushButton_clicked 是服务器功能...您的整个服务器变得无响应。

接下来我建议你做的是:

MainWindow::MainWindow() 
{
    connect(this, &MainWindow::alreadyDone, this, &MainWindow::onAllAnswersReady); 
}


 void MainWindow::start(QSet<int> phoneNumbers)  
 {
     History* history = new History();
     for (auto number : phoneNumbers) 
     {
         MyThread* sender = new MyThread(number);
         connect(sender, &MyThread::smsSent, history, &History::insertHistory);
         connect(history, &History::historyAdded, this, [=] (int historyUID) {
             qDebug() << "History inserted: " << historyUID;
             this->answers.insert(historyUID);
             if (this->answers.size() == phoneNumbers.size()) { // all answers are ready
                 qDebug() << "MainWindow finished!";
                 emit alreadyDone();
                 history->deleteLater();
             }
         });
         connect(sender, &MyThread::finished, sender, &MyThread::deleteLater);
         sender->start();
     }  
 }

void MainWindow::on_pushButton_clicked() // Imagine that this is a server function. 
{
    QSet<int> phoneNumbers; //List to who sends a message.
    phoneNumbers << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10;
    this->start(phoneNumbers); // <-- doesn't block 
}

void MainWindow::onAllAnswersReady() // <-- a handler function, may belong to any other class 
{
    qDebug() << "Answers: " << this->answers;
    // Do whatever you want with the answers here.  
}

这样你就不会阻塞,而只是在所有答案准备就绪时处理它们(所有历史记录都已记录)。