-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerAndClient.cpp
More file actions
247 lines (206 loc) · 4.26 KB
/
ServerAndClient.cpp
File metadata and controls
247 lines (206 loc) · 4.26 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
#include <iostream>
#include "ServerAndClient.h"
#include "PacketOverloads.h"
static sf::Vector2u window;
Paddle player1;
Paddle player2;
float dt;
void setServerContext(sf::Vector2u newWindow)
{
setObjectContext(newWindow);
window = newWindow;
player1 = Paddle (window.x / 2, window.y - 10);
player2 = Paddle (window.x / 2, 10);
}
//Server
Server::Server(int port)
{
this->PORT = port;
}
Server::~Server()
{
for (auto client : this->players)
{
delete client;
}
}
//Sends data regading all objects (the two paddles and the ball)
void Server::SendData()
{
for (auto client : this->players)
{
sf::Packet packetData;
packetData << player1;
packetData << player2;
packetData << this->ball;
client->send(packetData);
client->receive(packetData);
}
}
void Server::Start()
{
sf::Clock dt_clock;
while (true)
{
dt = dt_clock.restart().asSeconds();
//Receivin player movement and moving the according paddle
for (auto client : this->players)
{
//The packet should contain and int indicating the player and a string indicating the direction in which he moved
sf::Packet input;
if (client->receive(input) == sf::Socket::Done)
{
int player;
std::string intermediate;
char movement;
input >> player;
input >> intermediate;
movement = intermediate[0];
if (player == 1)
{
switch (movement)
{
case 'L':
player1.MoveLeft();
client->send(input);
break;
case 'R':
player1.MoveRight();
client->send(input);
break;
case 'N':
client->send(input);
}
}
else
{
if (player == 2)
{
switch (movement)
{
case 'L':
player2.MoveLeft();
client->send(input);
break;
case 'R':
player2.MoveRight();
client->send(input);
break;
case 'N':
client->send(input);
}
}
else
{
client->send(input);
}
}
}
//Updating the ball object
this->ball.Move();
}
this->SendData();
}
}
void Server::WaitForPlayers()
{
if (this->listener.listen(PORT) != sf::Socket::Done)
{
std::cout << "Could not bind listener";
}
else
{
while (this->players.size() < 2)
{
sf::TcpSocket* client = new sf::TcpSocket;
if (listener.accept(*client) != sf::Socket::Done)
{
std::cout << "Could not connect";
}
else
{
//Sending information about which client is the respective player
int player;
sf::Packet packet;
player = this->players.size() + 1;
packet << player;
client->send(packet);
client->receive(packet);
this->players.push_back(client);
}
}
}
}
//Client
//Makes the initial connection to the server and waits for a player number to be assigend to it
Client::Client(std::string ip, int port)
{
sf::Packet packet;
this->serverSocket.connect(ip, port);
this->serverSocket.receive(packet);
packet >> this->playerNumber;
if (this->playerNumber == 1 || this->playerNumber == 2)
{
packet << "OK";
serverSocket.send(packet);
}
}
Client::~Client()
{
serverSocket.disconnect();
}
void Client::GetObjectPositions(Ball& ball)
{
sf::Packet objectsPacket;
this->serverSocket.receive(objectsPacket);
objectsPacket >> player1;
objectsPacket >> player2;
objectsPacket >> ball;
this->serverSocket.send(objectsPacket);
}
//Creates inputPacket and sends it to the server
void Client::SendInput(std::string direction)
{
sf::Packet inputPacket;
inputPacket << this->playerNumber;
inputPacket << direction;
this->serverSocket.send(inputPacket);
this->serverSocket.receive(inputPacket);
}
void Client::Start()
{
Ball ball;
sf::RenderWindow window(sf::VideoMode(500, 500), "PONG!!");
window.setFramerateLimit(60);
while (window.isOpen())
{
//windwo event loop
sf::Event evnt;
while (window.pollEvent(evnt))
{
switch (evnt.type)
{
case sf::Event::EventType::Closed:
window.close();
}
}
//Data sending and receiving
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
{
this->SendInput("L");
}
else
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
this->SendInput("R");
else
this->SendInput("N");
}
this->GetObjectPositions(ball);
window.clear();
window.draw(player1);
window.draw(player2);
window.draw(ball);
window.display();
}
}