-
Notifications
You must be signed in to change notification settings - Fork 0
/
BasicServerTCP.cpp
197 lines (152 loc) · 4.6 KB
/
BasicServerTCP.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//----------------------------------------------------------------------------//
// *** МНОГОПОТОЧНАЯ КРОССПЛАТФОРМЕННАЯ СИСТЕМА ОБМЕНА ДАННЫМИ *** //
// //
// Файл BasicServerTCP.cpp //
// //
// *** TBASICSERVERTCP БАЗОВЫЙ СЕРВЕР TCP *** //
// //
// Автор ГЛУЩЕНКО Сергей //
// //
// Москва //
//----------------------------------------------------------------------------//
#include "BasicServerTCP.h"
TBasicServerTCP::TBasicServerTCP()
{
PortTCP = 0;
Server = new QTcpServer(this);
ClearClients();
TempCatalog = "";
connect(Server, SIGNAL(newConnection()), this, SLOT(OnNewConnection()));
}
TBasicServerTCP::~TBasicServerTCP()
{
TempCatalog = "";
Stop();
Server->deleteLater();
}
bool TBasicServerTCP::Start(int _PortTCP)
{
bool res;
Stop();
if (_PortTCP > 0)
{
if (TempCatalog != "")
{
PortTCP = _PortTCP;
res = Server->listen(QHostAddress::AnyIPv4, static_cast<quint16>(_PortTCP));
}
else
{
res = false;
}
}
else
{
res = false;
}
return res;
}
void TBasicServerTCP::Stop()
{
Server->close(); //Чтобы отпустить порт сразу, а не ждать окончания работы потоков
PortTCP = 0;
ClearClients();
}
void TBasicServerTCP::SetTempCatalog(QString _TempCatalog)
{
TempCatalog = _TempCatalog;
}
int TBasicServerTCP::GetPortTCP()
{
return PortTCP;
}
void TBasicServerTCP::OnNewConnection()
{
CreateThreadObject();
Clients[Clients.size()-1]->SetNumber(static_cast<int>(Clients.size()));
Clients[Clients.size()-1]->SetTempCatalog(TempCatalog);
Clients[Clients.size()-1]->SetSocket(Server->nextPendingConnection());
Clients[Clients.size()-1]->CheckStart();
connect(Clients[Clients.size()-1], SIGNAL(Success(int, int)), this, SLOT(OnSuccess(int, int)), Qt::QueuedConnection);
connect(Clients[Clients.size()-1], SIGNAL(Error(int, int, QString)), this, SLOT(OnError(int, int, QString)), Qt::QueuedConnection);
connect(Clients[Clients.size()-1], SIGNAL(Message(int, QString)), this, SLOT(OnMessage(int, QString)), Qt::QueuedConnection);
Clients[Clients.size()-1]->start();
}
void TBasicServerTCP::OnError(int Number, int Duration, QString Str)
{
QString Address;
Q_UNUSED(Duration);
Address = Clients[static_cast<unsigned int>(FindClient(Number))]->GetAddress();
DeleteClient(Number);
emit ErrorServer(Str);
emit MessageServer("IP-адрес клиента: " + Address);
}
void TBasicServerTCP::OnSuccess(int Number, int Duration)
{
QString Address;
Q_UNUSED(Duration);
Address = Clients[static_cast<unsigned int>(FindClient(Number))]->GetAddress();
DeleteClient(Number);
emit MessageServer("BasicServerTCP: Поток " + QString::number(Number) + " успешно завершен. IP-адрес клиента: " + Address);
}
void TBasicServerTCP::OnMessage(int Number, QString Str)
{
Q_UNUSED(Number);
emit MessageServer(Str);
}
void TBasicServerTCP::ClearClients()
{
unsigned int i;
for(i=0; i<Clients.size(); i++)
if (Clients[i] != NULL)
{
Clients[i]->quit();
Clients[i]->wait();
delete Clients[i];
Clients[i] = NULL;
}
Clients.clear();
}
void TBasicServerTCP::DeleteClient(int Number)
{
int index1;
unsigned int i, index2;
std::vector<TNetReceiveThread*> Temp;
index1 = FindClient(Number);
if (index1 != -1)
{
index2 = static_cast<unsigned int>(index1);
Clients[index2]->quit();
Clients[index2]->wait();
delete Clients[index2];
Temp.clear();
for(i=0; i<Clients.size(); i++)
if (i != index2)
Temp.push_back(Clients[i]);
Clients.clear();
for(i=0; i<Temp.size(); i++)
Clients.push_back(Temp[i]);
Temp.clear();
}
}
int TBasicServerTCP::FindClient(int Number)
{
int res;
unsigned int i;
res = -1;
for(i=0; i<Clients.size(); i++)
if (Clients[i]->GetNumber() == Number)
{
res = static_cast<int>(i);
break;
}
return res;
}
void TBasicServerTCP::CreateThreadObject()
{
Clients.push_back(new TNetReceiveThread());
}
QString TBasicServerTCP::GetTempCatalog()
{
return TempCatalog;
}