-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.cpp
More file actions
342 lines (282 loc) · 10.5 KB
/
Server.cpp
File metadata and controls
342 lines (282 loc) · 10.5 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
#pragma once
/*
* Sarah Asad
* Term Project: Tic-Tac-Toe
* Server.cpp
*
* The server is built using a TCP socket and multithreading allowing multiple games to be played at once. the server
* socket connection is set up where it then listens for incoming connection. The server also stores all of the player
* information (player ID, username), game information (gameID, game board), which players turn it is, and the outcome
* of the game.
*/
#include <cstdio>
#include <iostream>
#include <memory>
#include <sys/types.h> //socket, bind
#include <sys/socket.h> //coket, bind, listen, inet_ntoa
#include <netinet/in.h> //hton1, htons, inet_ntoa
#include <arpa/inet.h> //intet_ntoa
#include <netdb.h> //gethostbyname
#include <unistd.h> //read, write, close
#include <strings.h> //bzero
#include <netinet/tcp.h> //SO_REUSEADDR
#include <sys/uio.h> //writev
#include <pthread.h> //pthread
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sstream>
#include <fstream>
#include <fcntl.h>
#include <map>
#include "Server.h"
#include "CommandType.h"
#include "Client.h"
#include "GameOutcome.h"
//sets up the server side socket and listens for incoming connections.
void Server::Start(int port) {
//set up addr atructure
struct sockaddr_in hints;
memset(&hints, 0, sizeof(hints));
hints.sin_family = AF_INET; // IPv4 or v6
hints.sin_port = htons(port);
//create socket
this->sd = socket(AF_INET, SOCK_STREAM, 0);
if (this->sd < 0) {
throw new SocketException("Error creating server socket");
}
//lose the "addres already in use" error message
const int yes = 1;
setsockopt(this->sd, SOL_SOCKET, SO_REUSEADDR, (char*)&yes, sizeof(yes));
//Bind socket
int err = bind(this->sd, (sockaddr*)&hints, sizeof(hints));
if (err < 0) {
throw new SocketException("Unable to bind", err);
}
//listen for connection requests
err = listen(this->sd, 5);
if (err < 0) {
throw new SocketException("Unablle to listen", err);
}
std::cout << "Server ready and accepting connections\n";
//keep accepting connections until forceful quit
while (1) {
sockaddr_in client;
socklen_t clientSize = sizeof(client);
//accept connections
int clientSd = accept(this->sd, (sockaddr*)&client, &clientSize);
std::cout << "Connection accepted: Sd=" << clientSd << std::endl;
// Create a new thread to process requests from this client.
ClientContext* context = new ClientContext(this, clientSd);
int err = pthread_create(&context->threadId, NULL, &Server::threadProc, context);
/*if (err < 0) {
delete context;
throw new SocketException("Unable to create new thread for client", err);
}
context = NULL;*/
}
}
//register a user
void Server::registerUser(std::shared_ptr<ClientContext> context) {
// Get the name of the user.
std::string name = context->client.readString();
// Assign a unique id.
std::shared_ptr<User> user = std::make_shared<User>();
user->id = this->nextUserId++;
user->name = name;
// Add to the map.
this->users.insert({ user->id, user });
// Display that user has been registered.
std::cout << "Register User: Id=" << user->id << ", Name = " << name << std::endl;
// Save the user id and send it to the user.
context->userId = user->id;
context->client.sendInt(user->id);
}
//start a new game
void Server::startNewGame(std::shared_ptr<ClientContext> context) {
std::shared_ptr<User> user = this->users.at(context->userId);
std::shared_ptr<Game> game = std::make_shared<Game>(this->nextGameId++, user->id, user->name);
//insert into waiting games map
this->waitingGames.insert({ game->id, game });
std::cout << "Created Game " << game->id << " for user " << user->id << std::endl;
//send the game to the client
context->client.sendGame(game);
}
//get all the waiting games
void Server::getWaitingGames(std::shared_ptr<ClientContext> context) {
//send client the sise of map of waiting games
context->client.sendInt(this->waitingGames.size());
for (auto game : this->waitingGames) {
//send the game
context->client.sendGame(game.second);
}
}
//join a game
void Server::joinGame(std::shared_ptr<ClientContext> context) {
int id = context->client.readInt();
// Get the user who will be joining.
std::shared_ptr<User> user = this->users.at(context->userId);
// Remove the game from waiting list.
std::shared_ptr<Game> game = this->waitingGames.at(id);
this->waitingGames.erase(id);
// Add it to the active list after updating it.
game->user2 = user->id;
game->userName2 = user->name;
// Now add the game to the active list.
this->activeGames.insert({ id, game });
//send the game
context->client.sendGame(game);
}
//see if the game has started
void Server::isGameStarted(std::shared_ptr<ClientContext> context) {
int gameId = context->client.readInt();
int result = (this->activeGames.find(gameId) != this->activeGames.end());
context->client.sendInt(result);
}
//check to see whos turn it is
void Server::isMyTurn(std::shared_ptr<ClientContext> context) {
//get the game ID
int gameId = context->client.readInt();
//see if it is in the active games map
auto game = this->activeGames.find(gameId);
//send whos turn it is - send user ID
int result = game->second->turnOfUser == context->userId;
//check to see if someone has won or tie
int outcome = checkOutcome(game->second);
//check to see who won
if (outcome == GameOutcome::User1Win || outcome == GameOutcome::User2Win) {
outcome = context->userId == game->second->winnerId ? GameOutcome::YouWin : GameOutcome::YouLose;
}
//send the outcome
context->client.sendInt(outcome);
//send the result
context->client.sendInt(result);
//send the game
context->client.sendGame(game->second);
}
//helper method to see if there is a win or tie
bool Server::didWin(std::shared_ptr<Game> game, int userId) {
// Check rows.
for (int i = 0; i < 3; i++) {
if (game->board[i][0] == userId && game->board[i][1] == userId && game->board[i][2] == userId) {
return true;
}
}
// Check columns.
for (int i = 0; i < 3; i++) {
if (game->board[0][i] == userId && game->board[1][i] == userId && game->board[2][i] == userId) {
return true;
}
}
//check diagonals
if (game->board[0][0] == userId && game->board[1][1] == userId && game->board[2][2] == userId) {
return true;
}
if (game->board[0][2] == userId && game->board[1][1] == userId && game->board[2][0] == userId) {
return true;
}
return false;
}
//check the outcome of the game
GameOutcome Server::checkOutcome(std::shared_ptr<Game> game) {
//use 1 won
if (didWin(game, game->user1)) {
game->winnerId = game->user1;
return GameOutcome::User1Win;
}
//user2 won
else if (didWin(game, game->user2)) {
game->winnerId = game->user2;
return GameOutcome::User2Win;
}
// Check for a tie.
int emptyBoxes = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (game->board[i][j] == 0) {
emptyBoxes += 1;
}
}
}
return emptyBoxes == 0 ? GameOutcome::Tie : GameOutcome::Continue;
}
//see whos turn it is to play
void Server::playTurn(std::shared_ptr<ClientContext> context) {
int id = context->client.readInt();
auto game = this->activeGames.at(id);
int pos = context->client.readInt() - 1;
int x = pos / 3;
int y = pos % 3;
std::cout << "PlayTurn: GameId=" << id << ", User: " << context->userId << ", Position: " << pos << std::endl;
if (x < 0 || x > 2 || y < 0 || y > 2) {
context->client.sendInt(GameOutcome::InvalidMove);
context->client.sendGame(game);
return;
}
if (game->board[x][y] != 0 || game->turnOfUser != context->userId) {
context->client.sendInt(GameOutcome::InvalidMove);
context->client.sendGame(game);
return;
}
game->board[x][y] = context->userId;
// Check if the game has concluded.
auto outcome = checkOutcome(game);
// Change the player.
if (game->user1 == context->userId) {
game->turnOfUser = game->user2;
}
else {
game->turnOfUser = game->user1;
}
if (outcome == GameOutcome::User1Win || outcome == GameOutcome::User2Win) {
outcome = context->userId == game->winnerId ? GameOutcome::YouWin : GameOutcome::YouLose;
}
context->client.sendInt(outcome);
context->client.sendGame(game);
return;
}
//used to see what the client would like to do
int Server::processClient(std::shared_ptr<ClientContext> context)
{
std::cout << "Processing request for client using thread " << context->client.getSd() << std::endl;
try {
while (true) {
int command = context->client.readInt();
switch (command) {
case CommandType::RegisterUser:
registerUser(context);
break;
case CommandType::StartNewGame:
startNewGame(context);
break;
case CommandType::GetWaitingGames:
getWaitingGames(context);
break;
case CommandType::JoinExistingGame:
joinGame(context);
break;
case CommandType::IsGameStarted:
isGameStarted(context);
break;
case CommandType::IsMyTurn:
isMyTurn(context);
break;
case CommandType::PlayTurn:
playTurn(context);
break;
default:
std::cout << "Unknown command: " << command << std::endl;
break;
}
}
}
catch (...) {
std::cout << "Client connection terminated" << std::endl;
}
}
//multithreading used by server so that multiple games can be played at once
void* Server::threadProc(void* arg) {
std::shared_ptr<ClientContext> context((ClientContext*)arg);
context->server->processClient(context);
return NULL;
}