Rabbit Remote Control 0.0.30
Loading...
Searching...
No Matches
ConnecterTelnet.cpp
1#include "ConnecterTelnet.h"
2#include "DlgSettingsTelnet.h"
3
4#include <unistd.h>
5#include <QLoggingCategory>
6
7Q_DECLARE_LOGGING_CATEGORY(Telnet)
8
9CConnecterTelnet::CConnecterTelnet(CPluginClient *parent)
10 : CConnecterTerminal(parent),
11 m_pSocket(nullptr)
12{
13 m_pPara = new CParameterTelnet();
14 if(m_pPara)
15 {
16 m_pPara->SetPort(23);
17 }
18}
19
20CConnecterTelnet::~CConnecterTelnet()
21{}
22
23QDialog *CConnecterTelnet::GetDialogSettings(QWidget *parent)
24{
25 return new CDlgSettingsTelnet(m_pPara, parent);
26}
27
28int CConnecterTelnet::SetParameter()
29{
30 return CConnecterTerminal::SetParameter();
31}
32
34{
35 int nRet = 0;
36
37 Q_ASSERT(!m_pSocket);
38 m_pSocket = new QTcpSocket(this);
39
40 // Write what we input to remote terminal via socket
41 bool check = connect(m_pConsole, SIGNAL(sendData(const char *,int)),
42 this, SLOT(slotSendData(const char *,int)));
43 Q_ASSERT(check);
44
45 // Read anything from remote terminal via socket and show it on widget.
46 check = connect(m_pSocket, SIGNAL(readyRead()),
47 this, SLOT(slotReadyRead()));
48 Q_ASSERT(check);
49
50 check = connect(m_pSocket, SIGNAL(error(QAbstractSocket::SocketError)),
51 this, SLOT(slotError(QAbstractSocket::SocketError)));
52 Q_ASSERT(check);
53
54 check = connect(m_pSocket, SIGNAL(disconnected()),
55 this, SLOT(DisConnect()));
56 Q_ASSERT(check);
57
58 // Here we start an empty pty.
59 m_pConsole->startTerminalTeletype();
60
61 m_pSocket->connectToHost(m_pPara->GetHost(), m_pPara->GetPort());
62
63 return nRet;
64}
65
66int CConnecterTelnet::OnDisConnect()
67{
68 int nRet = 0;
69
70 if(m_pSocket)
71 {
72 if(m_pSocket->state() == QTcpSocket::ConnectedState)
73 m_pSocket->close();
74 delete m_pSocket;
75 m_pSocket = nullptr;
76 }
77 return nRet;
78}
79
80void CConnecterTelnet::slotSendData(const char *data, int size)
81{
82 if(m_pSocket)
83 m_pSocket->write(data, size);
84}
85
86void CConnecterTelnet::slotReadyRead()
87{
88 if(m_pSocket && m_pConsole)
89 {
90 QByteArray data = m_pSocket->readAll();
91 if(!data.isEmpty())
92 write(m_pConsole->getPtySlaveFd(), data.data(), data.size());
93 }
94}
95
96void CConnecterTelnet::slotError(QAbstractSocket::SocketError err)
97{
98 QString szErr;
99 if(!m_pSocket) return;
100 szErr = m_pSocket->errorString();
101 emit sigError(err, szErr);
102 qCritical(Telnet) << "error:" << err << "-" << szErr;
103 if(err == QTcpSocket::ConnectionRefusedError
104 || err == QTcpSocket::HostNotFoundError)
105 DisConnect();
106}
virtual int OnConnect() override
OnConnect.
void sigError(const int nError, const QString &szError)
Triggered when an error is generated.
The plugin interface.