-
Notifications
You must be signed in to change notification settings - Fork 0
/
CNetworking.cpp
187 lines (145 loc) · 4.25 KB
/
CNetworking.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
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <iostream>
#include <climits>
using namespace std;
#include "CMyMove.h"
#include "CCommand.h"
#include "CNetworking.h"
CNetworking::CNetworking() : host_ip(""),host_port(0) {
}
int openCliSocket ( 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" );
return -1;
}
int s = socket ( ai -> ai_family, SOCK_STREAM, 0 );
if ( s == -1 )
{
freeaddrinfo ( ai );
printf ( "socket" );
return -1;
}
if ( connect ( s, ai -> ai_addr, ai -> ai_addrlen ) != 0 )
{
close ( s );
freeaddrinfo ( ai );
printf ( "connect" );
return -1;
}
freeaddrinfo ( ai );
return s;
}
CMyMove CNetworking::getMove(const int cliSocket) const {
char buffer[10] = { 0 };
int l = recv ( cliSocket, buffer, sizeof ( buffer ), 0 );
if(l<=0){
cout << "Přijat špatný move." << endl;
return CMyMove(-1,-1,-1,-1);
}
if(strcmp(buffer,"EXIT")==0)
return CMyMove(-2,-2,-2,-2);
printf("Přijat následující move: %s|\n",buffer);
CMyMove m(buffer);
return m;
}
void CNetworking::sendCommand(const CCommand & command, int FromSock ) const {
if(FromSock == -1)
return;
fd_set rd;
FD_ZERO ( &rd );
FD_SET ( FromSock, &rd );
FD_SET ( STDIN_FILENO, &rd );
if ( FD_ISSET ( FromSock, &rd ) )
{
char buffer[100];
if(command.command==MAKEMOVE){
command.getMoveRef().tocstring(buffer);
printf("Odesílám tento move: %s|\n",buffer);
send ( FromSock, buffer, 5+1, 0 );
}
else if(command.command==EXIT){
cout << "Odeslán EXIT požadavek."<<endl<<"Konec hry." << endl;
strcpy(buffer,"EXIT");
send ( FromSock, buffer, 4+1, 0 );
}
}
}
int CNetworking::getSocket() const{
return openCliSocket ( host_ip.c_str(), host_port );
}
bool CNetworking::waitForStart(const int socket) const {
char buffer[10] = {0};
cout << "Čekám na připojení protivníka..." << endl;
int l = recv ( socket, buffer, sizeof ( buffer ), 0 );
if(l<0){
cout << "Ve funkci waitForStart nastala chyba." << endl;
printf("Obdržen řetězec: |%s|\n",buffer);
return false;
}
else if(buffer[0]=='G' && buffer[1]=='0' && buffer[2]==0 ){
cout << "Přijatý GO signál od serveru." << endl;
return true;
}
return false;
}
COLOR CNetworking::recvPlayerColor(const int socket) const{
char buffer[3] = { 0 };
int l = recv ( socket, buffer, sizeof ( buffer ), 0 );
if(l<=0){
cout << "Chyba ve čtení barvy!" << endl;
return WHITE;
}
else{
if(buffer[0] == 'W' && buffer[1] == 0)
return WHITE;
else if(buffer[0] == 'B' && buffer[1] == 0)
return BLACK;
else {
cout << "Přijata neznámá barva." << endl;
return WHITE;
}
}
}
void CNetworking::inputServerInfo() {
string host;
while(host != "localhost"){
cout <<"Zadejte IP serveru:"<<endl;
cin >> host;
if(!cin.good()){
cin.clear();
cin.ignore();
cout<<"Špatný vstup."<<endl;
}
if(host != "localhost")
cout << "Momentálně lze zadat pouze localhost." << endl;
}
host_ip = host;
int port = 0;
cout<<"Zadejte port pro hru na serveru:"<<endl;
while( !(port >= 2000 && port <= 3000) ){
cin>>port;
if(!cin.good()){
cin.clear();
cin.ignore(INT_MAX,'\n');
cout<<"Vstup je číslo."<<endl;
port = 0;
}
if(!(port >= 2000 && port <= 3000)){
cout<<"Zadejte port v rozmezí 2000-3000."<<endl;
port = 0;
}
}
host_port = port;
}