Skip to content

Commit

Permalink
faltou a msg
Browse files Browse the repository at this point in the history
  • Loading branch information
Luiza Medeiros bezerra committed Jul 6, 2024
1 parent dca00dd commit d36b046
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 42 deletions.
56 changes: 56 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"files.associations": {
"stdexcept": "cpp",
"iostream": "cpp",
"array": "cpp",
"atomic": "cpp",
"bit": "cpp",
"*.tcc": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"compare": "cpp",
"concepts": "cpp",
"csignal": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"map": "cpp",
"unordered_map": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"optional": "cpp",
"random": "cpp",
"string": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"fstream": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"iosfwd": "cpp",
"istream": "cpp",
"limits": "cpp",
"new": "cpp",
"ostream": "cpp",
"ranges": "cpp",
"sstream": "cpp",
"streambuf": "cpp",
"typeinfo": "cpp"
}
}
14 changes: 11 additions & 3 deletions include/Client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* Client.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bmoretti <bmoretti@student.42sp.org.br> +#+ +:+ +#+ */
/* By: lumedeir < lumedeir@student.42sp.org.br +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/27 14:40:02 by bmoretti #+# #+# */
/* Updated: 2024/07/01 13:47:27 by bmoretti ### ########.fr */
/* Updated: 2024/07/06 16:09:27 by lumedeir ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -21,14 +21,22 @@ class Client
Client();
~Client();
void connectToServer(const int serverSocket);
void _setNonBlock(void);
int getClientSocket(void) const;
int getPortClient(void) const;
bool _isConnected(void) const;
void _setConnected(bool status);
void _setClientSocket(int socket);

protected:
bool _connected;

private:
int _clientSocket;
struct sockaddr_in _address;

void _createClientSocket(void);
void _setNonBlock(void);


};

Expand Down
9 changes: 6 additions & 3 deletions include/Server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* Server.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bmoretti <bmoretti@student.42sp.org.br> +#+ +:+ +#+ */
/* By: lumedeir < lumedeir@student.42sp.org.br +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/27 10:40:41 by bmoretti #+# #+# */
/* Updated: 2024/07/02 11:49:50 by bmoretti ### ########.fr */
/* Updated: 2024/07/06 18:40:23 by lumedeir ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -23,6 +23,7 @@ class Server {

private:
int _serverSocket;
bool _isSigInt;
struct sockaddr_in _address;
std::vector<Client *> _clients;

Expand All @@ -31,9 +32,11 @@ class Server {
void _bindServer(void);
void _listen(void);
void _polling(void);
void _verifyClients(void);
static void _handleSignal(int c);

// Client management
void _acceptClient(void);
void _acceptClient(int socket);

};

Expand Down
10 changes: 7 additions & 3 deletions include/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* common.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bmoretti <bmoretti@student.42sp.org.br> +#+ +:+ +#+ */
/* By: lumedeir < lumedeir@student.42sp.org.br +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/27 11:24:48 by bmoretti #+# #+# */
/* Updated: 2024/07/03 12:48:13 by vde-frei ### ########.fr */
/* Updated: 2024/07/06 17:15:43 by lumedeir ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -26,11 +26,15 @@
# include <string.h>
# include <sys/epoll.h>
# include <unistd.h>
# include <fstream>
# include <stdexcept>
# include <sstream>
# include <csignal>

// C++ LIBS
# include <cstring>
# include <csignal>
# include <iostream>
# include <iomanip>
# include <stdexcept>
# include <string>
# include <vector>
Expand Down
22 changes: 19 additions & 3 deletions src/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* Client.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bmoretti <bmoretti@student.42sp.org.br> +#+ +:+ +#+ */
/* By: lumedeir < lumedeir@student.42sp.org.br +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/27 14:46:10 by bmoretti #+# #+# */
/* Updated: 2024/07/01 14:10:31 by bmoretti ### ########.fr */
/* Updated: 2024/07/06 17:26:24 by lumedeir ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -22,7 +22,7 @@ Client::~Client() {
void Client::connectToServer(const int serverSocket) {
socklen_t addressSize = sizeof(this->_address);

this->_clientSocket = accept(serverSocket,
this->_clientSocket = accept(serverSocket,
(struct sockaddr *)&this->_address, &addressSize);
if (this->_clientSocket == -1) {
throw std::runtime_error("Error connecting to the server");
Expand All @@ -34,6 +34,18 @@ int Client::getClientSocket(void) const {
return this->_clientSocket;
}

void Client::_setClientSocket(int socket) {
this->_clientSocket = socket;
}

void Client::_setConnected(bool status) {
this->_connected = status;
}

bool Client::_isConnected(void) const {
return this->_connected;
}

void Client::_createClientSocket(void) {
this->_clientSocket = socket(AF_INET, SOCK_STREAM, 0);
if (this->_clientSocket == -1) {
Expand All @@ -53,3 +65,7 @@ void Client::_setNonBlock(void) {
throw std::runtime_error("Error setting client to nonblock");
}
}

int Client::getPortClient(void) const{
return this->_address.sin_port;
}
95 changes: 78 additions & 17 deletions src/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@
/* ::: :::::::: */
/* Server.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bmoretti <bmoretti@student.42sp.org.br> +#+ +:+ +#+ */
/* By: lumedeir < lumedeir@student.42sp.org.br +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/27 10:40:35 by bmoretti #+# #+# */
/* Updated: 2024/07/02 11:52:58 by bmoretti ### ########.fr */
/* Updated: 2024/07/06 18:50:23 by lumedeir ### ########.fr */
/* */
/* ************************************************************************** */

#include "Server.hpp"
#include "common.hpp"

// PUBLIC FUNCTIONS
static bool isSigInt = false;

// PUBLIC FUNCTIONS
Server::Server() {
this->_address.sin_family = AF_INET;
this->_address.sin_addr.s_addr = INADDR_ANY;
Expand All @@ -25,6 +27,7 @@ Server::Server() {
}

Server::~Server() {
std::cout << "Client disconnected" << std::endl;
for (std::vector<Client *>::iterator it = this->_clients.begin();
it != this->_clients.end(); it++) {
delete *it;
Expand Down Expand Up @@ -54,13 +57,32 @@ void Server::_listen(void) {
}
}

void Server::_acceptClient(void) {
void Server::_acceptClient(int socket) {
Client *client = new Client();

client->connectToServer(this->_serverSocket);
// client->connectToServer(this->_serverSocket);
client->_setClientSocket(socket);
if (client->_isConnected() == true)
return;
client->_setNonBlock();
// client->_setConnected(true);
this->_clients.push_back(client);
}

// void Server::_verifyClients(void) {
// for (std::vector<Client *>::iterator it = this->_clients.begin();
// it != this->_clients.end(); it++) {
// it
// //do_use_fd(*it);
// }
// }

void Server::_handleSignal(int)
{
isSigInt = true;
OUTNL(RED("\r[HALT]: STOPPING THE SERVER... \n"));
}

void Server::_polling(void) {
struct epoll_event ev, events[MAX_EVENTS];
int conn_sock, nfds, epollfd;
Expand All @@ -81,18 +103,57 @@ void Server::_polling(void) {
}
for (int n = 0; n < nfds; ++n) {
if (events[n].data.fd == this->_serverSocket) {
this->_acceptClient();
conn_sock = this->_clients.back()->getClientSocket();
}
ev.events = EPOLLIN | EPOLLET;
ev.data.fd = conn_sock;
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, conn_sock, &ev) == -1) {
throw std::runtime_error("Error adding client to server poll");
}
else {
//RESOLVER OS EVENTOS DO NOSSO SERVER
std::cout << "teste" << std::endl;
//do_use_fd(events[n].data.fd);
conn_sock = accept(this->_serverSocket, (struct sockaddr *) &this->_address, (socklen_t*)&this->_address);
if (conn_sock < 0) {
throw std::runtime_error("Error accepting client");
}
this->_acceptClient(conn_sock);

if(send(conn_sock, "Hello, world!\n", 14, 0) != -1)
std::cout << "Mensagem enviada" << std::endl;
else
std::cout << "Erro ao enviar mensagem" << std::endl;
}
ev.events = EPOLLIN | EPOLLET;
ev.data.fd = conn_sock;
if (!this->_clients.back()->_isConnected() && epoll_ctl(epollfd, EPOLL_CTL_ADD, conn_sock, &ev) == -1) {
throw std::runtime_error("Error adding client server poll");
}
else if (!this->_clients.back()->_isConnected()){
this->_clients.back()->_setConnected(true);
}
else if (events[n].events & EPOLLIN) {
char buffer[1024];
ssize_t bytesRead = read(events[n].data.fd, buffer, sizeof(buffer) - 1);
if (bytesRead > 0) {
buffer[bytesRead] = '\0'; // Null-terminate the buffer
std::string data(buffer);
std::istringstream ss(data);
std::string line;
while (std::getline(ss, line)) {
std::cout << "Received from client " << events[n].data.fd << " " << line << std::endl;
if (line == "exit\r"){
close(events[n].data.fd);
for (std::vector<Client *>::iterator it = this->_clients.begin();
it != this->_clients.end(); it++) {
if ((*it)->getClientSocket() == events[n].data.fd){
delete *it;
this->_clients.erase(it);
break;
}
}
}
else {
std::string response = "You said: " + line + "\n";
send(events[n].data.fd, response.c_str(), response.size(), 0);
}
}
} else if (bytesRead == 0) {
// Conexão fechada pelo cliente
close(events[n].data.fd);
} else {
std::cerr << "Error reading from client" << std::endl;
}
}
}
}
Expand Down
16 changes: 3 additions & 13 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,15 @@
# include "Server.hpp"

bool g_sigint = false;

void handle_signal(int c)
{
(void)c;
g_sigint = true;
OUTNL(RED("\r[HALT]: STOPPING THE SERVER... \n"));
}
# include <csignal>

int main(void)
{
signal(SIGPIPE, SIG_IGN);
signal(SIGINT, handle_signal);
signal(SIGINT, Server::_handleSignal);

try {
Server serv;
}

catch (std::exception &e)
{
catch (std::exception &e){
std::cerr << e.what() << std::endl;
}
return 0;
Expand Down

0 comments on commit d36b046

Please sign in to comment.