玉兔远程控制 0.0.36
载入中...
搜索中...
未找到
ConnectLayerQTcpSocket.cpp
1#include "winpr/winsock.h"
2#include "ConnectLayerQTcpSocket.h"
3#include <QNetworkProxy>
4#include <QLoggingCategory>
5
6static Q_LOGGING_CATEGORY(log, "FreeRDP.Connect.Layer.QTcpSocket")
7
9 : CConnectLayer(connect)
10 , m_hSocket(nullptr)
11 , m_TcpSocket(this)
12{
13 qDebug(log) << Q_FUNC_INFO;
14}
15
16CConnectLayerQTcpSocket::~CConnectLayerQTcpSocket()
17{
18 qDebug(log) << Q_FUNC_INFO;
19}
20
21void CConnectLayerQTcpSocket::slotError(QAbstractSocket::SocketError e)
22{
23 QString szError;
24 szError = m_TcpSocket.errorString();
25 qDebug(log) << "CConnectLayerQTcpSocket::slotError()" << e << szError;
26 emit m_pConnect->sigError(e, szError);
27}
28
29void CConnectLayerQTcpSocket::slotConnected()
30{
31 int nRet = 0;
32
33 //*
34 if(m_hSocket) {
35 nRet = WSAEventSelect(m_TcpSocket.socketDescriptor(), m_hSocket, FD_READ | FD_CLOSE);
36 if(nRet) {
37 qCritical(log) << "WSAEventSelect fail:" << WSAGetLastError();
38 return;
39 }
40 }//*/
41
42 if(!m_pConnect) return;
43
44 rdpContext* context = (rdpContext*)m_pConnect->m_pContext;
45 auto settings = context->settings;
46 auto &net = m_pParameter->m_Net;
47 qDebug(log) << "Connected to"
48 << net.GetHost() + ":" + QString::number(net.GetPort());
49
50 if(!m_pParameter->GetDomain().isEmpty())
51 freerdp_settings_set_string(
52 settings, FreeRDP_Domain,
53 m_pParameter->GetDomain().toStdString().c_str());
54 if(net.GetHost().isEmpty())
55 {
56 QString szErr;
57 szErr = tr("The server is empty, please input it");
58 qCritical(log) << szErr;
59 return;
60 }
61
62 freerdp_settings_set_string(
63 settings, FreeRDP_ServerHostname,
64 net.GetHost().toStdString().c_str());
65 freerdp_settings_set_uint32(
66 settings, FreeRDP_ServerPort,
67 net.GetPort());
68
69 nRet = freerdp_client_start(context);
70 if(nRet)
71 {
72 qCritical(log) << "freerdp_client_start fail";
73 emit m_pConnect->sigError(nRet, "freerdp_client_start fail");
74 return;
75 }
76
77 bool check = connect(&m_TcpSocket, SIGNAL(readyRead()),
78 this, SLOT(slotReadyRead()));
79
80 Q_ASSERT(check);
81}
82
83void CConnectLayerQTcpSocket::slotReadyRead()
84{
85 qDebug(log) << "readRead ......";
86 int nRet = m_pConnect->OnProcess(0);
87 if(-1 > nRet)
88 emit m_pConnect->sigError(nRet, "Process fail");
89}
90
91int CConnectLayerQTcpSocket::OnInit(rdpContext *context)
92{
93 int nRet = 0;
94
95 bool check = false;
96
97 check = connect(&m_TcpSocket, SIGNAL(connected()),
98 this, SLOT(slotConnected()));
99 Q_ASSERT(check);
100
101 check = connect(&m_TcpSocket, SIGNAL(disconnected()),
102 m_pConnect, SIGNAL(sigDisconnect()));
103 Q_ASSERT(check);
104
105 check = connect(&m_TcpSocket,
106#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
107 SIGNAL(errorOccurred(QAbstractSocket::SocketError)),
108#else
109 SIGNAL(error(QAbstractSocket::SocketError)),
110#endif
111 this, SLOT(slotError(QAbstractSocket::SocketError)));
112 Q_ASSERT(check);
113
114 m_hSocket = WSACreateEvent();
115 if(!m_hSocket) {
116 qCritical(log) << "CreateEvent ssh socket event failed";
117 return -1;
118 }
119
120 auto &net = m_pParameter->m_Net;
121 m_TcpSocket.connectToHost(net.GetHost(), net.GetPort());
122
123 return nRet;
124}
125
126int CConnectLayerQTcpSocket::OnClean()
127{
128 if(m_TcpSocket.isOpen())
129 m_TcpSocket.close();
130
131 if(m_hSocket)
132 {
133 WSACloseEvent(m_hSocket);
134 m_hSocket = nullptr;
135 }
136 return 0;
137}
138
139int CConnectLayerQTcpSocket::OnLayerRead(void *data, int bytes)
140{
141 //*
142 if(m_hSocket)
143 WSAResetEvent(m_hSocket);//*/
144 int nRet = m_TcpSocket.read((char*)data, bytes);
145 qDebug(log) << Q_FUNC_INFO << nRet << bytes;
146 return nRet;
147}
148
149int CConnectLayerQTcpSocket::OnLayerWrite(const void *data, int bytes)
150{
151 int nRet = m_TcpSocket.write((char*)data, bytes);
152 qDebug(log) << Q_FUNC_INFO << nRet << bytes;
153 return nRet;
154}
155
156BOOL CConnectLayerQTcpSocket::OnLayerWait(BOOL waitWrite, DWORD timeout)
157{
158 qDebug(log) << Q_FUNC_INFO << waitWrite << timeout;
159 bool bRet = false;
160 if(!m_TcpSocket.isOpen()) return false;
161 if(waitWrite)
162 bRet = m_TcpSocket.waitForBytesWritten(timeout);
163 else
164 bRet = m_TcpSocket.waitForReadyRead(timeout);
165 qDebug(log) << Q_FUNC_INFO << waitWrite << timeout << bRet;
166 return bRet;
167}
168
169HANDLE CConnectLayerQTcpSocket::OnLayerGetEvent()
170{
171 return m_hSocket;
172}
virtual int OnProcess() override
插件连接的具体操作处理。因为此插件是非Qt事件,所以在此函数中等待。
The connect layer class
void sigError(const int nError, const QString &szError=QString())
当有错误产生时触发