-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.cpp
311 lines (222 loc) · 6.33 KB
/
Server.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include "Server.h"
#include "Network.h"
#include "World.h"
#include "Initiate.h"
extern Entity *player;
extern std::string level_name;
typedef std::vector<Entity *> ENTITYS;
int ClientUpdate(SOCKET sock, ENTITYS *entitys);
int Connect(SOCKET sock, int &client_id, ENTITYS *entitys);
int ClientDisconnect(SOCKET sock, ENTITYS *entitys);
DWORD WINAPI WaitClients(void *param)
{
WSAData wsad;
if(!WSAStartup(MAKEWORD(2,0),&wsad))
{
SOCKET sock1;//íà í¸ì áóäåì ïðèíèìàòü
SOCKET sock2;//íà íåãî áóäåì îòñûëàòü
int read = 0;
sock1 = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
SOCKADDR_IN sock_addr;
sock_addr.sin_family = AF_INET;
sock_addr.sin_port = htons(3000);
sock_addr.sin_addr.S_un.S_addr = htons(INADDR_ANY);
bind(sock1, (SOCKADDR*)&sock_addr, sizeof(sock_addr));
while(true)
{
char buf[101];
int size = sizeof(sock_addr);
read = recvfrom(sock1, buf, 100, 0, (SOCKADDR*)&sock_addr, &size);
if(read != SOCKET_ERROR)
{
buf[read] = '\0';
if(strcmp(buf, FIND_SRV) == 0)
{
sock2 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
sock_addr.sin_port = htons(2000);
sock_addr.sin_family = AF_INET;
int con = 0;
con = connect(sock2, (SOCKADDR*)&sock_addr, sizeof(sock_addr));
if(con != SOCKET_ERROR)
{
//send server ;
std::string send_str = SRV_STR;
send_str += "|";
send_str += level_name;
send(sock2, send_str.c_str(), send_str.length(), 0);
}
closesocket(sock2);
}
}
}
}
return 0;
}
DWORD WINAPI Accept(void *param)
{
WSAData wsad;
if(!WSAStartup(MAKEWORD(2,0),&wsad))
{
SOCKET sock;
sock = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
SOCKADDR_IN addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(2008);
addr.sin_addr.S_un.S_addr = htons(INADDR_ANY);
bind(sock,(SOCKADDR*)&addr,sizeof(addr));
listen(sock, 20);
NET_DATA *nd;
while(1)
{
int size = sizeof(addr);
SOCKET s = accept(sock,(SOCKADDR*)&addr,&size);
nd = new NET_DATA;
nd->data = param;
nd->sock = s;
CreateThread(NULL, 0, &Server, nd, 0, NULL);
}
}
return 0;
}
DWORD WINAPI Server(void *param)
{
NET_DATA *nd = (NET_DATA *)param;
SOCKET sock = nd->sock;
std::vector<Entity *> *entitys = (std::vector<Entity *>*)(nd->data);
delete nd;
int cmd = -1;
int result = -1;
HANDLE mutex = OpenMutex(MUTEX_ALL_ACCESS, FALSE, NAME_MUTEX2);
int client_id = 0;
if (mutex)
{
do
{
int cmd = -1;
result = recv(sock, (char *)&cmd, sizeof(int), 0);
if (result == sizeof(int))
{
switch(cmd)
{
case CONNECT:
result = Connect(sock, client_id, entitys);
break;
case CLIENT_UPDATE:
result = ClientUpdate(sock, entitys);
break;
case CLIENT_DISCONNECT:
result = ClientDisconnect(sock, entitys);
break;
}
}
} while(cmd != CLIENT_DISCONNECT && result != SOCKET_ERROR && result != 0);
closesocket(sock);
WaitForSingleObject(mutex, INFINITE);
if (entitys->size() >= (client_id-1))
{
if ((*entitys)[(client_id-1)] != NULL)
(*entitys)[(client_id-1)]->is_disconnect = true;
}
ReleaseMutex(mutex);
}
return 0;
}
int Connect(SOCKET sock, int &client_id, ENTITYS *entitys)
{
HANDLE mutex = OpenMutex(MUTEX_ALL_ACCESS, FALSE, NAME_MUTEX2);
ENTITY_NET_DATA data;
int result = -1;
int size = 0;
int end_size = sizeof(ENTITY_NET_DATA);
WaitForSingleObject(mutex, INFINITE);
size = entitys->size()+1;
client_id = entitys->size()+1;
entitys->resize(size);
ReleaseMutex(mutex);
result = send(sock, (char *)&client_id, sizeof(int), 0);
result = recv(sock, (char *)&data, end_size, 0);
if (result != 0 && result == end_size)
{
WaitForSingleObject(mutex, INFINITE);
Entity *e = new Entity(data.pos, GENERAL_MAP_LAYER);
e->SetEntityData(data);
(*entitys)[(data.id-1)] = e;
player->GetEntityData(data);
ReleaseMutex(mutex);
}
result = send(sock, (char *)&size, sizeof(int), 0);
result = send(sock, (char *)&data, end_size, 0);
for (int i = 0; i < (size-1); i++)
{
WaitForSingleObject(mutex, INFINITE);
(*entitys)[i]->GetEntityData(data);
ReleaseMutex(mutex);
result = send(sock, (char *)&data, end_size, 0);
}
return result;
}
int ClientUpdate(SOCKET sock, ENTITYS *entitys)
{
HANDLE mutex = OpenMutex(MUTEX_ALL_ACCESS, FALSE, NAME_MUTEX2);
int client_id = -1;
ENTITY_NET_DATA_SMALL data = {0};
int size1 = 0, size2 = 0;
int result = -1;
int end_size = sizeof(ENTITY_NET_DATA);
int ends_size = sizeof(ENTITY_NET_DATA_SMALL);
result = recv(sock, (char *)&data, ends_size, 0);
if (result != 0 && result == ends_size)
{
WaitForSingleObject(mutex, INFINITE);
client_id = data.id;
size2 = entitys->size();
(*entitys)[(data.id-1)]->SetEntityData(data);
ReleaseMutex(mutex);
}
result = recv(sock, (char *)&size1, sizeof(int), 0);
if (result == 0 && result != sizeof(int))
{
return result;
}
result = send(sock, (char *)&size2, sizeof(int), 0);
WaitForSingleObject(mutex, INFINITE);
player->GetEntityData(data);
ReleaseMutex(mutex);
send(sock, (char *)&data, ends_size, 0);
for (int i = 0; i < size1; i++)
{
WaitForSingleObject(mutex, INFINITE);
(*entitys)[i]->GetEntityData(data);
ReleaseMutex(mutex);
if (client_id != data.id)
result = send(sock, (char *)&data, sizeof(ENTITY_NET_DATA_SMALL), 0);
}
if (size1 < size2)
{
ENTITY_NET_DATA ed = {0};
std::vector<ENTITY_NET_DATA> temp;
temp.resize(size2-size1);
WaitForSingleObject(mutex, INFINITE);
for (int i = size1; i < size2; i++)
{
(*entitys)[i]->GetEntityData(ed);
temp.push_back(ed);
}
ReleaseMutex(mutex);
for (int i = 0; i < temp.size(); i++)
{
result = send(sock, (char *)&temp[i], sizeof(ENTITY_NET_DATA), 0);
}
}
}
int ClientDisconnect(SOCKET sock, ENTITYS *entitys)
{
HANDLE mutex = OpenMutex(MUTEX_ALL_ACCESS, FALSE, NAME_MUTEX2);
int id = 0;
int result = -1;
result = recv(sock, (char *)&id, sizeof(int), 0);
WaitForSingleObject(mutex, INFINITE);
(*entitys)[(id-1)]->is_disconnect = true;
ReleaseMutex(mutex);
return result;
}