玉兔远程控制 0.0.31
载入中...
搜索中...
未找到
Connect.cpp
1// Author: Kang Lin <kl222@126.com>
2#include <QTimer>
3#include <QLoggingCategory>
4
5#include "Connect.h"
6
7static Q_LOGGING_CATEGORY(log, "Client.Connect")
8
9CConnect::CConnect(CConnecter *pConnecter) : QObject()
10{
11 SetConnecter(pConnecter);
12}
13
14CConnect::~CConnect()
15{
16 qDebug(log) << "CConnect::~CConnect()";
17}
18
19int CConnect::SetConnecter(CConnecter* pConnecter)
20{
21 qDebug(log) << "CConnect::SetConnecter" << pConnecter;
22 if(!pConnecter) return -1;
23
24 bool check = false;
25 check = connect(this, SIGNAL(sigConnected()),
26 pConnecter, SIGNAL(sigConnected()));
27 Q_ASSERT(check);
28 check = connect(this, SIGNAL(sigDisconnect()),
29 pConnecter, SIGNAL(sigDisconnect()));
30 Q_ASSERT(check);
31 check = connect(this, SIGNAL(sigDisconnected()),
32 pConnecter, SIGNAL(sigDisconnected()));
33 Q_ASSERT(check);
34 check = connect(this, SIGNAL(sigError(const int, const QString&)),
35 pConnecter, SIGNAL(sigError(const int, const QString&)));
36 Q_ASSERT(check);
37 check = connect(this, SIGNAL(sigInformation(const QString&)),
38 pConnecter, SIGNAL(sigInformation(const QString&)));
39 Q_ASSERT(check);
40 check = connect(
41 this, SIGNAL(sigShowMessageBox(const QString&, const QString&,
42 const QMessageBox::Icon&)),
43 pConnecter, SIGNAL(sigShowMessageBox(const QString&, const QString&,
44 const QMessageBox::Icon&)));
45 Q_ASSERT(check);
46 check = connect(this, SIGNAL(sigBlockShowMessageBox(
47 const QString&, const QString&,
48 QMessageBox::StandardButtons,
49 QMessageBox::StandardButton&,
50 bool&, QString)),
51 pConnecter, SLOT(slotBlockShowMessageBox(
52 const QString&, const QString&,
53 QMessageBox::StandardButtons,
54 QMessageBox::StandardButton&,
55 bool&, QString)),
56 Qt::BlockingQueuedConnection);
57 Q_ASSERT(check);
58 check = connect(this, SIGNAL(sigBlockInputDialog(const QString&,
59 const QString&,
60 const QString&,
61 QString&)),
62 pConnecter, SLOT(slotBlockInputDialog(const QString&,
63 const QString&,
64 const QString&,
65 QString&)),
66 Qt::BlockingQueuedConnection);
67 Q_ASSERT(check);
68 check = connect(
69 this,
70 SIGNAL(sigBlockShowWidget(const QString&, int&, void*)),
71 pConnecter, SLOT(slotBlockShowWidget(const QString&, int&, void*)),
72 Qt::BlockingQueuedConnection);
73 Q_ASSERT(check);
74 return 0;
75}
76
78{
79 qDebug(log) << "CConnect::Connect()";
80 int nRet = 0;
81 nRet = static_cast<int>(OnInit());
82 if(nRet < 0) return nRet;
83
84 if(0 == nRet)
85 QTimer::singleShot(0, this, SLOT(slotTimeOut()));
86 return 0;
87}
88
90{
91 qDebug(log) << "CConnect::Disconnect()";
92 int nRet = 0;
93 nRet = OnClean();
94 return nRet;
95}
96
98{
99 //qDebug(log) << "CConnect::slotTimeOut()";
100 try {
101 // >= 0: continue. Call interval
102 // = -1: stop
103 // < -1: error
104 int nTime = OnProcess();
105 if(nTime >= 0)
106 {
107 QTimer::singleShot(nTime, this, SLOT(slotTimeOut()));
108 return;
109 }
110 qDebug(log) << "Process fail(< -1) or stop(= -1):" << nTime;
111 if(nTime < -1) {
112 qCritical(log) << "Process fail:" << nTime;
113 emit sigError(nTime, "Process fail or stop");
114 }
115 } catch(std::exception e) {
116 qCritical(log) << "Process fail:" << e.what();
117 emit sigError(-2, e.what());
118 } catch (...) {
119 qCritical(log) << "Process fail";
120 emit sigError(-3, "Process fail");
121 }
122
123 // Error or stop, must notify user disconnect it
124 emit sigDisconnect();
125}
126
128{
129 qWarning(log) << "Need to implement CConnect::OnProcess()";
130 return 0;
131}
132
连接接口。它由协议插件实现。 它默认启动一个定时器来开启一个非 Qt 事件循环(就是普通的循环处理)。 详见: Connect()、 slotTimeOut()、 OnProcess() 。 当然,它仍...
Definition Connect.h:45
void sigError(const int nError, const QString &szError=QString())
当有错误产生时触发
void sigInformation(const QString &szInfo)
从后台线程中触发在主线程中显示信息,不阻塞后台线程
void sigBlockShowMessageBox(const QString &szTitle, const QString &szMessage, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton &nRet, bool &checkBox, QString checkBoxContext=QString())
阻塞后台线程,并在前台线程中显示消息对话框(QMessageBox)
void sigShowMessageBox(const QString &szTitle, const QString &szMessage, const QMessageBox::Icon &icon=QMessageBox::Information)
从后台线程中触发在主线程中显示消息对话框(QMessageBox),不阻塞后台线程
void sigBlockInputDialog(const QString &szTitle, const QString &szLable, const QString &szMessage, QString &szText)
阻塞后台线程,并在前台线程中显示输入对话框 (QInputDialog)
void sigConnected()
当插件连接成功后触发。仅由插件触发
void sigDisconnect()
通知用户断开连接。仅由插件触发。 当从插件中需要要断开连接时触发。例如:对端断开连接、重置连接或者连接出错。
virtual int Disconnect()
断开
Definition Connect.cpp:89
virtual int OnProcess()
插件连接的具体操作处理
Definition Connect.cpp:127
virtual OnInitReturnValue OnInit()=0
具体的插件实现连接初始化
virtual void slotTimeOut()
一个非 Qt 事件处理,它调用 OnProcess(),并根据其返回值开始新的定时器。 如果 CConnect 没有一个非 Qt 事件循环(就是普通的循环处理), 可以重载它,或者 OnInit() 返...
Definition Connect.cpp:97
virtual int Connect()
开始连接。根据 OnInit() 返回值来决定是否开始定时器来支持非 qt 事件
Definition Connect.cpp:77
virtual int OnClean()=0
清理
void sigDisconnected()
断开连接成功信号。仅由插件触发
void sigBlockShowWidget(const QString &className, int &nRet, void *pContext)
阻塞后台线程,并在前台线程中显示窗口。
连接者应用接口。
Definition Connecter.h:62