-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScopy.cpp
More file actions
345 lines (306 loc) · 6.66 KB
/
Scopy.cpp
File metadata and controls
345 lines (306 loc) · 6.66 KB
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#include <iostream>
#include <string>
#include <thread>
#include <errno.h>
#include <mutex>
#include <fstream>
#include <bits/stdc++.h>
#include <string.h>
#include <stdlib.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sqlite3.h>
#include <regex>
#include <csignal>
using namespace std;
bool server_quit = false;
struct sockaddr_in server; // structura folosita de server
struct sockaddr_in from;
struct utilizator
{
int fd;
char username[20];
bool setUser(int link)
{
fd = link;
}
bool setName(char name[20])
{
strcpy(username, name);
return true;
};
char getName()
{
return *username;
}
};
struct gameLobby
{
utilizator Utilizator1;
utilizator Utilizator2;
int playerNumber = 1;
char hostName[20];
bool updatePlayer2(utilizator &Utilizator)
{
Utilizator2 = Utilizator;
playerNumber = 2;
return true;
};
bool enoughPlayers()
{
if (playerNumber == 2)
return true;
return false;
};
bool emptyLobby()
{
if (hostName == "\0")
return true;
return false;
}
bool startLobby(utilizator &Utilizator)
{
Utilizator1 = Utilizator;
*hostName = Utilizator1.getName();
};
utilizator utilizator1()
{
return Utilizator1;
}
utilizator utilizator2()
{
return Utilizator2;
}
};
gameLobby Lobbys[6];
bool addInList(gameLobby &Lobby)
{
for (int i = 0; i < 6; i++)
if (Lobbys[i].emptyLobby())
Lobbys[i] = Lobby;
return true;
}
struct login
{
char username[200];
char password[200];
};
enum Protocol
{
_Exit1,
_LogIn,
_LogOut,
_Register,
_CreateGame,
_JoinGame
};
enum LOGIN
{
_Connectat,
_Eroare,
_Sloturi
};
enum REGISTER
{
_Username,
_Allok
};
struct loginn
{
char username[20];
char password[20];
};
void signalHandler(int signum)
{
printf("[server]Server closed..\n");
server_quit = true;
exit(signum);
}; //TODO: vezi despre ce e vb (semnal)
bool logInPlayer(int fd)
{
printf("\nI am in logIn\n");
char username[200], password[200];
int len;
FILE *log;
log = fopen("login12", "r");
struct loginn l;
int semnal;
read(fd, &semnal, sizeof(semnal));
read(fd, &username, semnal);
printf("Am primit username\n");
read(fd, &semnal, sizeof(semnal));
read(fd, &password, semnal);
printf("Am primit parola\n");
while (fread(&l, sizeof(l), 1, log))
{
if (strcmp(username, l.username) == 0 && strcmp(password, l.password) == 0)
{
int raspuns = LOGIN::_Connectat;
printf("Succesful login\n");
write(fd, &raspuns, sizeof(raspuns));
break;
}
else
{
int raspuns = LOGIN::_Eroare;
printf("Please Enter correct username and password");
write(fd, &raspuns, sizeof(raspuns));
}
}
fclose(log);
return true;
};
bool registerPlayer(int fd)
{
printf("\nI am in register\n");
int length;
char username[200], password[200];
FILE *log;
log = fopen("login12.txt", "r+w");
struct loginn l;
read(fd, &length, sizeof(length));
read(fd, &l.username, length);
read(fd, &length, sizeof(length));
read(fd, &l.password, length);
int raspuns = REGISTER::_Allok;
;
printf("got answers\n");
while (fread(&l, sizeof(l), 1, log))
{
if (strcmp(username, l.username) == 0)
{
printf("erroare\n");
raspuns = REGISTER::_Username;
write(fd, &raspuns, sizeof(raspuns));
fclose(log);
return false;
}
}
printf("a mers\n");
fwrite(&l, sizeof(l), 1, log);
raspuns = REGISTER::_Allok;
write(fd, &raspuns, sizeof(raspuns));
fclose(log);
return true;
};
bool setUser(int fd, utilizator &Utilizator)
{
char username[20];
int len;
read(fd, &len, sizeof(len));
read(fd, &username, len);
Utilizator.setName(username);
return true;
};
bool createGame(utilizator &Utilizator)
{
gameLobby lobby;
lobby.startLobby(Utilizator);
addInList(lobby);
while (!lobby.enoughPlayers())
;
};
bool joinGame(utilizator &Utilizator)
{
int game = 1;
Lobbys[game].updatePlayer2(Utilizator);
};
bool playGame(int fd1, int fd2)
{
int a = 1;
};
bool Servire(int fd, sockaddr_in c_socket)
{
utilizator Utilizator;
setUser(fd, Utilizator);
char ipAdr[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &c_socket.sin_addr, ipAdr, INET_ADDRSTRLEN);
printf("Clinetul cu IP-ul %s s-a conectat la file-descriptor-ul %d.\n", ipAdr, fd);
int rd;
bool quits = false;
while (!quits && !server_quit)
{
fflush(stdin);
int desc;
rd = read(fd, &desc, sizeof(desc));
if (!rd)
{
printf("Client disconnected\n");
quits = true;
}
printf("%d\n", desc);
if (desc == Protocol::_LogIn)
logInPlayer(fd);
else if (desc == Protocol::_Register)
registerPlayer(fd);
else if (desc == Protocol::_CreateGame)
;
else if (desc == Protocol::_JoinGame)
;
else if (desc == Protocol::_Exit1)
;
}
return true;
};
//..............................................................................................
int main()
{
char msg[100]; //mesajul primit de la client
char msgrasp[100] = " "; //mesaj de raspuns pentru client
signal(SIGINT, signalHandler);
std::cout << "[server]Starting server.\n";
// pregatirea structurilor de date
bzero(&server, sizeof(server));
bzero(&from, sizeof(from));
//socket
int server_socket = socket(AF_INET, SOCK_STREAM, 0); //descriptor socket
if (server_socket == -1)
{
cerr << "[server]Eroare la crearea socket-ului!\n";
return -2;
}
std::cout << "[server]Created the socket.\n";
int enable = 1;
setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &enable, sizeof(int));
sockaddr_in servera;
memset(&servera, 0, sizeof(servera));
servera.sin_family = AF_INET;
servera.sin_addr.s_addr = htonl(INADDR_ANY);
servera.sin_port = htons(2024);
if (bind(server_socket, (sockaddr *)(&servera), sizeof(servera)) == -1)
{
perror("[server]Eroare la bind!\n");
return errno;
}
std::cout << "[server]Sucessful bind.\n";
//asteptam clienti
if (listen(server_socket, 6) == -1)
{
cerr << "[server]Eroare la listen!\n";
return -2;
}
std::cout << "[server]Listening to clients...\n";
//accept
sockaddr_in client_socket;
memset(&client_socket, 0, sizeof(client_socket));
while (true)
{
int client;
socklen_t lenght = sizeof(from);
fflush(stdout);
client = accept(server_socket, (sockaddr *)(&client_socket), &lenght);
if (client < 0)
{
cerr << "[server]Eroare la acceptarea clientului!\n";
return -2;
}
std::thread client_thread(Servire, client, client_socket);
client_thread.join();
}
//close
printf("[server]Stopping...\n");
return (0);
}