1#include "winpr/winsock.h"
2#include "ConnectLayerQTcpSocket.h"
3#include <QNetworkProxy>
4#include <QLoggingCategory>
6static Q_LOGGING_CATEGORY(log,
"FreeRDP.Connect.Layer.QTcpSocket")
13 qDebug(log) << Q_FUNC_INFO;
16CConnectLayerQTcpSocket::~CConnectLayerQTcpSocket()
18 qDebug(log) << Q_FUNC_INFO;
21void CConnectLayerQTcpSocket::slotError(QAbstractSocket::SocketError e)
24 szError = m_TcpSocket.errorString();
25 qDebug(log) <<
"CConnectLayerQTcpSocket::slotError()" << e << szError;
26 emit m_pConnect->
sigError(e, szError);
29void CConnectLayerQTcpSocket::slotConnected()
35 nRet = WSAEventSelect(m_TcpSocket.socketDescriptor(), m_hSocket, FD_READ | FD_CLOSE);
37 qCritical(log) <<
"WSAEventSelect fail:" << WSAGetLastError();
42 if(!m_pConnect)
return;
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());
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())
57 szErr = tr(
"The server is empty, please input it");
58 qCritical(log) << szErr;
62 freerdp_settings_set_string(
63 settings, FreeRDP_ServerHostname,
64 net.GetHost().toStdString().c_str());
65 freerdp_settings_set_uint32(
66 settings, FreeRDP_ServerPort,
69 nRet = freerdp_client_start(context);
72 qCritical(log) <<
"freerdp_client_start fail";
73 emit m_pConnect->
sigError(nRet,
"freerdp_client_start fail");
77 bool check = connect(&m_TcpSocket, SIGNAL(readyRead()),
78 this, SLOT(slotReadyRead()));
83void CConnectLayerQTcpSocket::slotReadyRead()
85 qDebug(log) <<
"readRead ......";
88 emit m_pConnect->
sigError(nRet,
"Process fail");
91int CConnectLayerQTcpSocket::OnInit(rdpContext *context)
97 check = connect(&m_TcpSocket, SIGNAL(connected()),
98 this, SLOT(slotConnected()));
101 check = connect(&m_TcpSocket, SIGNAL(disconnected()),
102 m_pConnect, SIGNAL(sigDisconnect()));
105 check = connect(&m_TcpSocket,
106#
if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
107 SIGNAL(errorOccurred(QAbstractSocket::SocketError)),
109 SIGNAL(error(QAbstractSocket::SocketError)),
111 this, SLOT(slotError(QAbstractSocket::SocketError)));
114 m_hSocket = WSACreateEvent();
116 qCritical(log) <<
"CreateEvent ssh socket event failed";
120 auto &net = m_pParameter->m_Net;
121 m_TcpSocket.connectToHost(net.GetHost(), net.GetPort());
126int CConnectLayerQTcpSocket::OnClean()
128 if(m_TcpSocket.isOpen())
133 WSACloseEvent(m_hSocket);
139int CConnectLayerQTcpSocket::OnLayerRead(
void *data,
int bytes)
143 WSAResetEvent(m_hSocket);
144 int nRet = m_TcpSocket.read((
char*)data, bytes);
145 qDebug(log) << Q_FUNC_INFO << nRet << bytes;
149int CConnectLayerQTcpSocket::OnLayerWrite(
const void *data,
int bytes)
151 int nRet = m_TcpSocket.write((
char*)data, bytes);
152 qDebug(log) << Q_FUNC_INFO << nRet << bytes;
156BOOL CConnectLayerQTcpSocket::OnLayerWait(BOOL waitWrite, DWORD timeout)
158 qDebug(log) << Q_FUNC_INFO << waitWrite << timeout;
160 if(!m_TcpSocket.isOpen())
return false;
162 bRet = m_TcpSocket.waitForBytesWritten(timeout);
164 bRet = m_TcpSocket.waitForReadyRead(timeout);
165 qDebug(log) << Q_FUNC_INFO << waitWrite << timeout << bRet;
169HANDLE CConnectLayerQTcpSocket::OnLayerGetEvent()
virtual int OnProcess() override
插件连接的具体操作处理。因为此插件是非Qt事件,所以在此函数中等待。
void sigError(const int nError, const QString &szError=QString())
当有错误产生时触发