Rabbit Remote Control 0.0.30
Loading...
Searching...
No Matches
ServiceTigerVNC.cpp
1// Author: Kang Lin <kl222@126.com>
2
3#include "ServiceTigerVNC.h"
4#include "RabbitCommonTools.h"
5#include "network/Socket.h"
6#include <QHostAddress>
7#include <QTcpSocket>
8#include "ParameterServiceTigerVNC.h"
9#include "Connection.h"
10#include "PluginService.h"
11#include <stdexcept>
12
13#ifdef HAVE_GUI
14#include "FrmParameterTigerVNC.h"
15#endif
16
17#ifdef HAVE_ICE
18 #include "Ice.h"
19 #include "ICE/ChannelIce.h"
20#endif
21
22Q_DECLARE_LOGGING_CATEGORY(TigerVNC)
23CServiceTigerVNC::CServiceTigerVNC(CPluginService *plugin) : CService(plugin)
24{
25 bool check = false;
26 check = connect(&m_Lister, SIGNAL(newConnection()),
27 this, SLOT(slotNewConnection()));
28 Q_ASSERT(check);
29
30 m_pPara = new CParameterServiceTigerVNC(this);
31}
32
33CServiceTigerVNC::~CServiceTigerVNC()
34{
35 qDebug(TigerVNC) << "CServiceTigerVNC::~CServiceTigerVNC";
36}
37
38#ifdef HAVE_GUI
39QWidget* CServiceTigerVNC::GetParameterWidget(QWidget* parent)
40{
41 return new CFrmParameterTigerVNC(
42 dynamic_cast<CParameterServiceTigerVNC*>(GetParameters()), parent);
43}
44#endif
45
47{
48 bool check = false;
50 dynamic_cast<CParameterServiceTigerVNC*>(GetParameters());
51 if(!p)
52 Q_ASSERT(false);
53
54#if defined(HAVE_ICE)
55 if(p->getIce())
56 {
57 m_Signal = CICE::Instance()->GetSignal();
58 if(m_Signal)
59 {
60 // check = connect(m_Signal.data(), SIGNAL(sigConnected()),
61 // this, SLOT(slotSignalConnected()));
62 // Q_ASSERT(check);
63 // check = connect(m_Signal.data(), SIGNAL(sigDisconnected()),
64 // this, SLOT(slotSignalDisConnected()));
65 // Q_ASSERT(check);
66 // check = connect(m_Signal.data(), SIGNAL(sigError(int, const QString&)),
67 // this, SLOT(slotSignalError(int, const QString&)));
68 // Q_ASSERT(check);
69 check = connect(m_Signal.data(), SIGNAL(sigOffer(const QString&,
70 const QString&,
71 const QString&,
72 const QString&,
73 const QString&)),
74 this, SLOT(slotSignalOffer(const QString&,
75 const QString&,
76 const QString&,
77 const QString&,
78 const QString&)));
79 Q_ASSERT(check);
80 qInfo(TigerVNC) << "Connect ICE signal";
81 }
82 }
83#endif
84
85 if(p->GetEnableSocket())
86 {
87 if(!m_Lister.listen(QHostAddress::Any, p->getPort()))
88 {
89 qCritical(TigerVNC, "Lister fail: Port [%d]; %s",
90 GetParameters()->getPort(),
91 m_Lister.errorString().toStdString().c_str());
92 return -1;
93 }
94 qInfo(TigerVNC) << "Lister at:" << p->getPort();
95 }
96
97 return 1; //Don't use OnProcess (qt event loop)
98}
99
101{
102#if defined(HAVE_ICE)
104 dynamic_cast<CParameterServiceTigerVNC*>(GetParameters());
105 if(p)
106 {
107 m_Signal = CICE::Instance()->GetSignal();
108 if(m_Signal)
109 m_Signal->disconnect(this);
110 }
111#endif
112
113 m_Lister.close();
114 m_lstConnection.clear();
115 return 0;
116}
117
118void CServiceTigerVNC::slotNewConnection()
119{
120 if(!m_Lister.hasPendingConnections())
121 return;
122 QTcpSocket* pSocket = m_Lister.nextPendingConnection();
123 if(!pSocket) return;
124 qInfo(TigerVNC) << "New connection:"
125 << pSocket->peerAddress() << ":" << pSocket->peerPort();
126 try {
127 QSharedPointer<CChannel> channel(new CChannel(pSocket), &QObject::deleteLater);
128 if(!channel->isOpen())
129 if(!channel->open(QIODevice::ReadWrite))
130 {
131 qCritical(TigerVNC) << "Don't open channel";
132 throw std::runtime_error("Don't open channel");
133 }
134 QSharedPointer<CConnection> c(new CConnection(channel,
135 dynamic_cast<CParameterServiceTigerVNC*>(this->GetParameters())),
136 &QObject::deleteLater);
137 m_lstConnection.push_back(c);
138 bool check = connect(c.data(), SIGNAL(sigDisconnected()),
139 this, SLOT(slotDisconnected()));
140 Q_ASSERT(check);
141 check = connect(c.data(), SIGNAL(sigError(int, QString)),
142 this, SLOT(slotError(int, QString)));
143 Q_ASSERT(check);
144 // Because the socket is connected, so emit sigConnected()
145 emit channel->sigConnected();
146 } catch (std::exception e) {
147 qCritical(TigerVNC) << "New connection exception" << e.what();
148 } catch(...) {
149 qCritical(TigerVNC) << "New connection exception";
150 }
151}
152
153void CServiceTigerVNC::slotDisconnected()
154{
155 CConnection* pConnect = dynamic_cast<CConnection*>(sender());
156 pConnect->close(tr("Exit").toStdString().c_str());
157 foreach(auto c, m_lstConnection)
158 if(c == pConnect)
159 m_lstConnection.removeOne(c);
160}
161
162void CServiceTigerVNC::slotError(int nErr, QString szErr)
163{
164 slotDisconnected();
165}
166
167#ifdef HAVE_ICE
168void CServiceTigerVNC::slotSignalConnected()
169{
170 CParameterICE* p = CICE::Instance()->GetParameter();
171 if(!p) return;
172 qInfo(TigerVNC, "Connected to signal server: %s:%d; user:%s",
173 p->getSignalServer().toStdString().c_str(),
174 p->getSignalPort(),
175 p->getSignalUser().toStdString().c_str());
176}
177
178void CServiceTigerVNC::slotSignalDisConnected()
179{
180 CParameterICE* p = CICE::Instance()->GetParameter();
181 if(!p) return;
182 qInfo(TigerVNC, "Disconnect signal server: %s:%d; user:%s",
183 p->getSignalServer().toStdString().c_str(),
184 p->getSignalPort(),
185 p->getSignalUser().toStdString().c_str());
186}
187
188void CServiceTigerVNC::slotSignalError(int nErr, const QString& szErr)
189{
190 CParameterICE* p = CICE::Instance()->GetParameter();
191 if(!p) return;
192 qCritical(TigerVNC, "signal: %s:%d; user:%s; error: %d: %s",
193 p->getSignalServer().toStdString().c_str(),
194 p->getSignalPort(),
195 p->getSignalUser().toStdString().c_str(),
196 nErr, szErr.toStdString().c_str());
197}
198
199void CServiceTigerVNC::slotSignalOffer(const QString& fromUser,
200 const QString& toUser,
201 const QString& channelId,
202 const QString& type,
203 const QString& sdp)
204{
205 try {
206 qInfo(TigerVNC,
207 "New connection: from:%s; to:%s; channelId:%s",
208 fromUser.toStdString().c_str(),
209 toUser.toStdString().c_str(),
210 channelId.toStdString().c_str());
211 CParameterICE* p = CICE::Instance()->GetParameter();
212 if(!p) return;
213 QSharedPointer<CChannelIce> channel(new CChannelIce(m_Signal.data()),
214 &QObject::deleteLater);
215 if(!channel->isOpen())
216 {
217 rtc::IceServer stun(p->getStunServer().toStdString().c_str(),
218 p->getStunPort());
219 rtc::IceServer turn(p->getTurnServer().toStdString().c_str(),
220 p->getTurnPort(),
221 p->getTurnUser().toStdString().c_str(),
222 p->getTurnPassword().toStdString().c_str());
223 rtc::Configuration config;
224 config.iceServers.push_back(stun);
225 config.iceServers.push_back(turn);
226 channel->SetConfigure(config);
227 if(!channel->open(fromUser, toUser, channelId, type, sdp))
228 {
229 qCritical(TigerVNC) << "Don't open channel";
230 throw std::runtime_error("Don't open channel");
231 }
232 }
233 QSharedPointer<CConnection> c(new CConnection(channel,
234 dynamic_cast<CParameterServiceTigerVNC*>(this->GetParameters())),
235 &QObject::deleteLater);
236 m_lstConnection.push_back(c);
237 bool check = connect(c.data(), SIGNAL(sigDisconnected()),
238 this, SLOT(slotDisconnected()));
239 Q_ASSERT(check);
240 check = connect(c.data(), SIGNAL(sigError(int, QString)),
241 this, SLOT(slotError(int, QString)));
242 Q_ASSERT(check);
243 } catch (std::exception e) {
244 qCritical(TigerVNC) << "New connection exception" << e.what();
245 } catch(...) {
246 qCritical(TigerVNC) << "New connection exception";
247 }
248}
249#endif //HAVE_ICE
250
The ICE channel interface class.
Definition ChannelIce.h:27
The channel interface class.
Definition Channel.h:25
static CICE * Instance()
Single instance.
Definition Ice.cpp:55
ICE parameter helper class.
The service plugin interface.
virtual int OnInit() override
Init service.
virtual int OnClean() override
Clean service.
The service interface.
Definition Service.h:38