Rabbit Remote Control 0.0.30
Loading...
Searching...
No Matches
ServiceRabbitVNC.cpp
1// Author: Kang Lin <kl222@126.com>
2
3#include "ServiceRabbitVNC.h"
4#include "RabbitCommonTools.h"
5#include "network/Socket.h"
6#include <stdexcept>
7#include <QHostAddress>
8#include <QTcpSocket>
9#include "ParameterServiceRabbitVNC.h"
10#ifdef HAVE_GUI
11 #include "FrmParameterRabbitVNC.h"
12#endif
13#include "Connection.h"
14#include "PluginService.h"
15
16#ifdef HAVE_ICE
17#include "ICE/ChannelIce.h"
18#include "Ice.h"
19#endif
20
21#include <QLoggingCategory>
22Q_DECLARE_LOGGING_CATEGORY(RabbitVNC)
23
24CServiceRabbitVNC::CServiceRabbitVNC(CPluginService *plugin) : CService(plugin)
25{
26 bool check = false;
27 check = connect(&m_Lister, SIGNAL(newConnection()),
28 this, SLOT(slotNewConnection()));
29 Q_ASSERT(check);
30
31 m_pPara = new CParameterServiceRabbitVNC(this);
32}
33
34CServiceRabbitVNC::~CServiceRabbitVNC()
35{
36 qDebug(RabbitVNC) << "CServiceRabbitVNC::~CServiceRabbitVNC";
37}
38
39#ifdef HAVE_GUI
40QWidget* CServiceRabbitVNC::GetParameterWidget(QWidget *parent)
41{
42 return new CFrmParameterRabbitVNC(
43 dynamic_cast<CParameterServiceRabbitVNC*>(GetParameters()), parent);
44}
45#endif
46
48{
49 bool check = false;
51 dynamic_cast<CParameterServiceRabbitVNC*>(GetParameters());
52 if(!p)
53 Q_ASSERT(false);
54
55 if(p->GetEnableSocket())
56 {
57 if(!m_Lister.listen(QHostAddress::Any, p->getPort()))
58 {
59 qCritical(RabbitVNC) << "Lister fail: Port" << GetParameters()->getPort()
60 << "; %s" << m_Lister.errorString().toStdString().c_str();
61 return -1;
62 }
63 qInfo(RabbitVNC) << "Lister at:", p->getPort();
64 }
65
66#if defined(HAVE_ICE)
67 if(p->getIce())
68 {
69 m_Signal = CICE::Instance()->GetSignal();
70 if(m_Signal)
71 {
72 // check = connect(m_Signal.data(), SIGNAL(sigConnected()),
73 // this, SLOT(slotSignalConnected()));
74 // Q_ASSERT(check);
75 // check = connect(m_Signal.data(), SIGNAL(sigDisconnected()),
76 // this, SLOT(slotSignalDisConnected()));
77 // Q_ASSERT(check);
78 // check = connect(m_Signal.data(), SIGNAL(sigError(int, const QString&)),
79 // this, SLOT(slotSignalError(int, const QString&)));
80 // Q_ASSERT(check);
81 check = connect(m_Signal.data(), SIGNAL(sigOffer(const QString&,
82 const QString&,
83 const QString&,
84 const QString&,
85 const QString&)),
86 this, SLOT(slotSignalOffer(const QString&,
87 const QString&,
88 const QString&,
89 const QString&,
90 const QString&)));
91 Q_ASSERT(check);
92 qInfo(RabbitVNC) << "Connect ICE signal";
93 }
94 }
95#endif
96
97 return 1; // Don't use OnProcess (qt event loop)
98}
99
101{
102#if defined(HAVE_ICE)
104 dynamic_cast<CParameterServiceRabbitVNC*>(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 CServiceRabbitVNC::slotNewConnection()
119{
120 if(!m_Lister.hasPendingConnections())
121 return;
122 QTcpSocket* pSocket = m_Lister.nextPendingConnection();
123 if(!pSocket) return;
124 qInfo(RabbitVNC) << "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(RabbitVNC) << "Don't open channel";
132 throw std::runtime_error("Don't open channel");
133 }
134 QSharedPointer<CConnection> c(new CConnection(channel,
135 dynamic_cast<CParameterServiceRabbitVNC*>(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(RabbitVNC) << "New connection exception:" << e.what();
148 } catch(...) {
149 qCritical(RabbitVNC) << "New connection exception";
150 }
151}
152
153void CServiceRabbitVNC::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 CServiceRabbitVNC::slotError(int nErr, QString szErr)
163{
164 slotDisconnected();
165}
166
167#ifdef HAVE_ICE
168void CServiceRabbitVNC::slotSignalConnected()
169{
170 CParameterICE* p = CICE::Instance()->GetParameter();
171 if(!p) return;
172 qInfo(RabbitVNC, "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 CServiceRabbitVNC::slotSignalDisConnected()
179{
180 CParameterICE* p = CICE::Instance()->GetParameter();
181 if(!p) return;
182 qInfo(RabbitVNC, "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 CServiceRabbitVNC::slotSignalError(int nErr, const QString& szErr)
189{
190 CParameterICE* p = CICE::Instance()->GetParameter();
191 if(!p) return;
192 qCritical(RabbitVNC, "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 CServiceRabbitVNC::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(RabbitVNC,
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(RabbitVNC) << "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<CParameterServiceRabbitVNC*>(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(RabbitVNC) << "New connection exception" << e.what();
245 } catch(...) {
246 qCritical(RabbitVNC) << "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