-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcpmanger.cpp
151 lines (101 loc) · 3.75 KB
/
tcpmanger.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "tcpmanger.h"
TcpManger::TcpManger(QObject *parent) : QObject(parent)
{
mSetupTcpDialog = new SetupTcpDialog();
connect(mSetupTcpDialog, SIGNAL(newIpAndPort(QString,quint16)),this, SLOT(newIpAndPort(QString,quint16)));
mTcpSocket = new QTcpSocket(this);
connect(mTcpSocket, SIGNAL(readyRead()),this, SLOT(readyRead()));
connect(mTcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(error(QAbstractSocket::SocketError)));
mProcessIncommingData = new processIncommingData(this);
IpAdress = new QHostAddress();
// QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
IpString = mSetupTcpDialog->getIpAdress();
PortNumber = mSetupTcpDialog->getPortNumber();
connectToServer();
}
TcpManger::~TcpManger()
{
}
void TcpManger::showSetupDialog()
{
mSetupTcpDialog->show();
mSetupTcpDialog->setModal(true);
mTcpSocket->abort();
}
void TcpManger::connectToServer()
{
try
{
IpAdress->setAddress(IpString);
mTcpSocket->connectToHost(*IpAdress, PortNumber, QIODevice::ReadWrite);
//mTcpSocket->connectToHost(IpString,PortNumber, QIODevice::ReadWrite);
}
catch(...)
{
QMessageBox::warning(qobject_cast<QWidget *>(parent()), "Error Connecting", "Could Not Connect To The Server");
}
}
void TcpManger::sendData(QByteArray SendSomeData)
{
if(mTcpSocket->isOpen())
{
mTcpSocket->write(SendSomeData);
}
else
{
QMessageBox::warning(qobject_cast<QWidget *>(parent()), "No Connection Error", "We are not connected to the server\nCheck IP and Port");
}
}
void TcpManger::error(QAbstractSocket::SocketError socketError)
{
switch (socketError) {
case QAbstractSocket::RemoteHostClosedError:
break;
case QAbstractSocket::HostNotFoundError:
QMessageBox::information(qobject_cast<QWidget *>(parent()), tr("Client"), tr("The host was not found. Please check the host name and port settings."));
break;
case QAbstractSocket::ConnectionRefusedError:
QMessageBox::information(qobject_cast<QWidget *>(parent()), tr("Client"), tr("The connection was refused by the Server. "
"Make sure the WD electronic server is running, "
"and check that the host name and port "
"settings are correct."));
break;
default:
QMessageBox::information(qobject_cast<QWidget *>(parent()), tr("Client"),
tr("The following error occurred: %1.")
.arg(mTcpSocket->errorString()));
}
}
// /////////////////////////////////////////////////
// PUBLIC SLOTS
// /////////////////////////////////////////////////
void TcpManger::newIpAndPort(QString ipAdress, quint16 portNumber)
{
IpAdress->clear();
IpString = ipAdress;
PortNumber = portNumber;
connectToServer();
qDebug() << IpString << PortNumber;
}
// /////////////////////////////////////////////////
// PUBLIC SLOTS END
// /////////////////////////////////////////////////
// /////////////////////////////////////////////////
// Request data
// /////////////////////////////////////////////////
void TcpManger::sendRequest(quint16 blockSize, quint8 command, quint8 readOrWrite, quint16 elements)
{
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_5_3);
out << blockSize << command << readOrWrite << elements;
out.device()->seek(0);
out << quint16(block.size() - sizeof(quint16));
mTcpSocket->write(block);
}
void TcpManger::readyRead()
{
}
// /////////////////////////////////////////////////
// Request data END
// /////////////////////////////////////////////////