-
Notifications
You must be signed in to change notification settings - Fork 0
/
CServer.cpp
169 lines (128 loc) · 3.81 KB
/
CServer.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
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <iostream>
//------- network includes----------
using namespace std;
#include "CServer.h"
#include "CRemotePlayer.h"
// ========================= FUNCTIONS ================================
int openSrvSocket(const char * srvName, int srvPort) {
struct addrinfo * ai;
char srvPortText[10];
snprintf(srvPortText, sizeof ( srvPortText), "%d", srvPort);
if (getaddrinfo(srvName, srvPortText, NULL, &ai) != 0) {
printf("getaddrinfo\n");
return -1;
}
int s = socket(ai -> ai_family, SOCK_STREAM, 0);
if (s == -1) {
freeaddrinfo(ai);
printf("socket\n");
return -1;
}
if (bind(s, ai -> ai_addr, ai -> ai_addrlen) != 0) {
close(s);
freeaddrinfo(ai);
printf("bind\n");
return -1;
}
if (listen(s, 5) != 0) {
close(s);
freeaddrinfo(ai);
printf("listen\n");
return -1;
}
freeaddrinfo(ai);
return s;
}
int CServer::serveClient(int fromSock) {
char buffer[10] = {0};
cout << "Čekám na tah od: " << (fromSock == cliSocket1 ? "HRÁČE 1" : "HRÁČE 2") << endl;
int l = recv(fromSock, buffer, sizeof ( buffer), 0);
if (l <= 0 || strcmp(buffer, "EXIT") == 0) {
return 1;
}
cout << "Přijal jsem následující řetězec:" << endl;
printf("%c|%c|%c|%c|%c...\n", buffer[0], buffer[1], buffer[2], buffer[3], buffer[4]);
cout << "\n... OD " << fromSock << " a odesílám ho na : " << theOtherSocket(fromSock) << endl;
send(theOtherSocket(fromSock), buffer, l, 0);
return 0;
}
// ===============================================================
int CServer::startServer() {
srvSocket = openSrvSocket("localhost", 2666);
if (srvSocket == -1) {
cout << "Nebylo možné vytvořit server socket." << endl;
return -1;
}
cout << "Server spuštěn." << endl;
waitForPlayers();
while (true) {
if (serveClient(cliSocket1) == 1) {
cout << "Hráč 1 odpadl nebo ukončil hru." << endl;
break;
}
if (serveClient(cliSocket2) == 1) {
cout << "Hráč 2 odpadl nebo ukončil hru." << endl;
break;
}
}
cout << "Ukončuji server." << endl;
stopServer();
return 0;
}
void CServer::stopServer() {
send(cliSocket1, "EXIT", 5, 0);
send(cliSocket2, "EXIT", 5, 0);
close(cliSocket1);
close(cliSocket2);
close(srvSocket);
}
void CServer::closeClient(int & cliSocket) {
if (cliSocket != -1) {
close(cliSocket);
}
}
int CServer::theOtherSocket(int sock) const {
return (sock == cliSocket1 ? cliSocket2 : cliSocket1);
}
void CServer::sendGoSignal() const {
send(cliSocket1, "GO", 3, 0);
send(cliSocket2, "GO", 3, 0);
}
void CServer::setPlayerSocket(int sock) {
if (cliSocket1 == -1) {
cliSocket1 = sock;
cout << "Hráč 1 připojen." << endl;
send(sock, "W", 2, 0);
return;
} else if (cliSocket2 == -1) {
cliSocket2 = sock;
cout << "Hráč 2 připojen." << endl;
send(sock, "B", 2, 0);
playersConnected = true;
return;
}
}
void CServer::waitForPlayers() {
struct sockaddr addr;
socklen_t addrLen;
int sock;
cout << "Čekám na připojení hráčů..." << endl;
while (!playersConnected) {
addrLen = sizeof ( addr);
sock = accept(srvSocket, &addr, &addrLen);
setPlayerSocket(sock);
}
cout << "Posílám klientům GO signál. " << endl;
sendGoSignal();
cout << "========== Zahajuji hru! =========" << endl;
}
CServer::CServer() : cliSocket1(-1), cliSocket2(-1), srvSocket(-1), playersConnected(false) {
}