玉兔远程控制
0.1.0-bate11
Toggle main menu visibility
载入中...
搜索中...
未找到
Plugins
Telnet
QTelnet.h
1
#ifndef QTELNET_H
2
#define QTELNET_H
3
4
#include <QObject>
5
#include <QTcpSocket>
6
#include <QWebSocket>
7
#include <QSize>
8
#include <QString>
9
10
#define IncommingBufferSize (1500)
11
12
class
QTelnet :
public
QObject
13
{
14
Q_OBJECT
15
16
public
:
17
enum
SocketStatus
18
{
19
Disconnected,
20
Resolving,
// Resolving host
21
Connecting,
// Connecting to host.
22
Connected
// Connected to host.
23
};
24
enum
SocketType
25
{
26
TCP = 0,
27
WEBSOCKET,
28
SECUREWEBSOCKET
29
};
30
31
protected
:
32
enum
TelnetStateCodes
33
{
34
STATE_DATA = (char)0,
35
STATE_IAC = (
char
)1,
36
STATE_IACSB = (char)2,
37
STATE_IACWILL = (
char
)3,
38
STATE_IACDO = (char)4,
39
STATE_IACWONT = (
char
)5,
40
STATE_IACDONT = (char)6,
41
STATE_IACSBIAC = (
char
)7,
42
STATE_IACSBDATA = (char)8,
43
STATE_IACSBDATAIAC = (
char
)9,
44
STATE_DATAR = (char)10,
45
STATE_DATAN = (
char
)11
46
};
47
enum
TelnetCodes
48
{
49
// Negociación entrada/salida (client<->servidor)
50
IAC = (char)255,
// Inicia la secuencia para la negociación telnet.
51
EOR = (
char
)239,
// Estando en la negociación, End Of Record.
52
WILL = (char)251,
// Estando en la negociación, Acepta el protocolo?
53
WONT = (
char
)252,
// Estando en la negociación, Acepta el protocolo?
54
DO = (char)253,
// Estando en la negociación, Protocolo aceptado.
55
DONT = (
char
)254,
// Estando en la negociación, Protocolo denegado.
56
SB = (char)250,
// Estando en la negociación, inicia secuencia de sub-negociación.
57
SE = (
char
)240,
// Estando en la sub-negociación, fin de sub-negociación.
58
59
// Negociación de salida (client->servidor)
60
TELOPT_BINARY = (char)0,
// Estando en la negociación, pide modo binario.
61
TELOPT_ECHO = (
char
)1,
// Estando en la negociación, pide echo local.
62
TELOPT_SGA = (char)2,
// Estando en la negociación, pide Suppress Go Ahead.
63
TELOPT_EOR = (
char
)25,
// Estando en la negociación, informa End Of Record.
64
TELOPT_NAWS = (char)31,
// Estando en la negociación, Negotiate Abaut Window Size.
65
TELOPT_TTYPE = (
char
)24
// Estando en la negociación, Terminal Type.
66
};
67
enum
TelnetQualifiers
68
{
69
TELQUAL_IS = (char)0,
70
TELQUAL_SEND = (
char
)1
71
};
72
73
private
:
74
QTcpSocket m_tcpSocket;
75
QWebSocket m_webSocket;
76
SocketType m_socketType;
77
static
const
char
IACWILL[2];
78
static
const
char
IACWONT[2];
79
static
const
char
IACDO[2];
80
static
const
char
IACDONT[2];
81
static
const
char
IACSB[2];
82
static
const
char
IACSE[2];
83
static
char
_sendCodeArray[2];
84
static
char
_arrCRLF[2];
85
static
char
_arrCR[2];
86
87
QSize m_winSize;
// Tamaño de la pantalla en caracteres.
88
QSize m_oldWinSize;
// Tamaño de la pantalla que se envió por última vez al server. Para no enviar el mismo dato.
89
enum
TelnetStateCodes m_negotiationState;
90
char
m_receivedDX[256];
// What IAC DO(NT) request do we have received already ?
91
char
m_receivedWX[256];
// What IAC WILL/WONT request do we have received already ?
92
char
m_sentDX[256];
// What IAC DO/DONT request do we have sent already ?
93
char
m_sentWX[256];
// What IAC WILL/WONT request do we have sent already ?
94
void
resetProtocol
();
95
96
char
m_buffIncoming[IncommingBufferSize];
97
char
m_buffProcessed[IncommingBufferSize];
98
QByteArray m_buffSB;
99
int
m_actualSB;
100
101
void
emitEndOfRecord() { emit endOfRecord(); }
102
void
emitEchoLocal(
bool
bEcho) { emit echoLocal(bEcho); }
103
104
void
sendTelnetControl(
char
codigo);
105
void
handleSB(
void
);
106
void
transpose(
const
char
*buf,
int
iLen);
107
108
void
willsReply(
char
action,
char
reply);
109
void
wontsReply(
char
action,
char
reply);
110
void
doesReply(
char
action,
char
reply);
111
void
dontsReply(
char
action,
char
reply);
112
113
void
sendSB(
char
code,
char
*arr,
int
iLen);
114
qint64 doTelnetInProtocol(qint64 buffSize);
115
116
public
:
117
explicit
QTelnet(SocketType type = TCP, QObject *parent = 0);
118
explicit
QTelnet(QObject *parent = 0) : QTelnet(TCP, parent) {}
119
120
void
setType(SocketType type);
121
void
connectToHost(
const
QString &hostName, quint16 port);
122
void
disconnectFromHost(
void
);
123
void
sendData(
const
QByteArray &ba);
124
void
sendData(
const
char
*data,
int
len);
125
void
setCustomCRLF(
char
lf = 13,
char
cr = 10);
126
void
setCustomCR(
char
cr = 10,
char
cr2 = 0);
127
128
void
writeCustomCRLF();
129
void
writeCustomCR();
130
131
void
write(
const
char
c);
132
qint64 write(
const
char
*data, qint64 len);
133
qint64 read(
char
*data, qint64 maxlen);
134
135
bool
isConnected()
const
;
136
bool
testBinaryMode()
const
;
137
void
setWindSize(QSize s) {m_winSize = s;}
138
void
sendWindowSize();
139
140
QString peerInfo()
const
;
141
QString peerName()
const
;
142
143
QString errorString();
144
145
signals:
146
void
newData(
const
char
*buff,
int
len);
147
void
endOfRecord();
148
void
echoLocal(
bool
echo);
149
void
stateChanged(QAbstractSocket::SocketState s);
150
void
error(QAbstractSocket::SocketError err);
151
152
private
slots:
153
void
socketError(QAbstractSocket::SocketError err);
154
void
onTcpReadyRead();
155
void
binaryMessageReceived(
const
QByteArray &message);
156
void
onStateChanged(QAbstractSocket::SocketState s);
157
};
158
159
#endif
// QTELNET_H
QTelnet::resetProtocol
void resetProtocol()
Resetea los datos del protocolo. Debe llamarse cada vez que se inicia una conexión nueva.
定义
QTelnet.cpp:190
作者:康林 (kl222@126.com)
版权所有(c) 康林工作室 保留所有权力