Skip to content

Commit

Permalink
Minor changes - Server
Browse files Browse the repository at this point in the history
	- modifico algunas cosas para seguir buenas practicas
	- falta mucho refactor TODO
	- funcionamiento sigue igual, de eso no toque nada
  • Loading branch information
maxogod committed Jun 8, 2024
1 parent 5975522 commit 5ffc840
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 32 deletions.
13 changes: 6 additions & 7 deletions server/protocol/accepter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,28 @@
#include "../game_logic/matches_manager.h"


ServerAccepter::ServerAccepter(const std::string& port):
skt(Socket(port.c_str())), online(true), matches_manager() {}
ServerAccepter::ServerAccepter(const std::string& port): skt(Socket(port.c_str())) {}

void ServerAccepter::run() {
try {
matches_manager.start();
while (online) {
while (_keep_running) {
Socket peer = skt.accept();
if (!online) {
if (!_keep_running) {
break;
}
matches_manager.add_new_client_to_manager(std::move(peer));
}
} catch (const std::exception& err) {
if (online) {
if (_keep_running) {
std::cerr << "An exception was caught in accepter: " << err.what() << "\n";
}
}
}

void ServerAccepter::stop() {
online = false;
skt.shutdown(2);
_keep_running = false;
skt.shutdown(SHUT_RDWR); // TODO revisar esto
skt.close();
matches_manager.stop();
matches_manager.join();
Expand Down
8 changes: 3 additions & 5 deletions server/protocol/accepter.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,23 @@
#include <utility>
#include <vector>

//#include "../common/common_queue.h"
//#include "../common/common_socket.h"
//#include "../common/common_thread.h"
//#include "game_logic/client_monitor.h"
#include "../game_logic/matches_manager.h"


class ServerAccepter: public Thread {
private:
Socket skt;
std::atomic<bool> online{true};
MatchesManager matches_manager;

public:
// Constructor of the ServerAccepter class
explicit ServerAccepter(const std::string& port);

void run() override;

// Kills the running thread
void stop() override;

// Destroyer
~ServerAccepter() override = default;
};
Expand Down
23 changes: 7 additions & 16 deletions server/server.cpp
Original file line number Diff line number Diff line change
@@ -1,34 +1,25 @@
#include "server.h"

#include <iostream>
#include <utility>

#include "game_logic/matches_manager.h"
#include "protocol/accepter.h"
Server::Server(const std::string& port): accepter(new ServerAccepter(port)) {}

void Server::run() {
try {
auto accepter = new ServerAccepter("8080");
accepter->start();

std::string serverInput;
while (true) {
std::cin >> serverInput;
if (serverInput == "q") {
if (serverInput == QUIT) {
break;
}
}

accepter->stop();
accepter->join();
delete accepter;
} catch (const std::exception& err) {
std::cerr << "An exception was caught in server_class: " << err.what() << "\n";
}
}

const std::string& Server::get_servname() { return portDir; }

Server::Server(std::string port): portDir(std::move(port)) {}

Server::~Server() = default;
Server::~Server() {
accepter->stop();
accepter->join();
delete accepter;
}
20 changes: 16 additions & 4 deletions server/server.h
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
#ifndef TP_FINAL_SERVER_H
#define TP_FINAL_SERVER_H

#include <iostream>
#include <string>
#include <utility>

#include "game_logic/matches_manager.h"
#include "protocol/accepter.h"

#define QUIT "q"

class Server {
private:
std::string portDir;
ServerAccepter* accepter;

public:
// Constructor of the Server class, initializes the protocol and receives the port as a
// parameter to create the socket
explicit Server(std::string port);
explicit Server(const std::string& port);

// cant copy or move
Server(const Server&) = delete;
Server& operator=(const Server&) = delete;
Server(Server&&) = delete;
Server& operator=(Server&&) = delete;

// Run the server, creates and throws two threads, the accepter and the gameloop (logic of the
// game), then waits for the user to close the server
void run();
// Returns the name of the port
const std::string& get_servname();

// Destroyer
~Server();
};
Expand Down

0 comments on commit 5ffc840

Please sign in to comment.