Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emman/connect managing #23

Merged
merged 3 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 39 additions & 24 deletions Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,71 +94,87 @@ void Server::socketListening() {
void Server::serverRoutine(){
initPollfd();
while(1){
if(poll(this->_fds, this->_nfds, 100) == 1){
for(this->_client_index = 0; this->_client_index < this->_nfds; this->_client_index++){
if(poll(this->_fds, MAXFDS, 100) == 1){
for(this->_client_index = 0; this->_client_index < MAXFDS; this->_client_index++){
if(this->_client_index == 0 && this->_fds[this->_client_index].revents & POLLIN){
acceptConnection();
}else if(_fds[this->_client_index].revents & POLLIN){
}else if(_fds[this->_client_index].revents & POLLIN)
receiver();
}
}
}
// only for visualition of the fd
/* for(int i = 0; i < MAXCLIENT + 1; i++)
cout << "i : "<< i << " -> " <<_fds[i].fd << endl;
sleep(1); */
// for(int i = 0; i < MAXFDS; i++)
// cout << "i : "<< i << " -> " <<_fds[i].fd << endl;
// sleep(1);
}
}

void Server::initPollfd(){
this->_fds[0].fd = this->_socket_fd;
this->_fds[0].events = POLLIN;
this->_nfds++;
for(int i = 1; i < MAXCLIENT + 1; i++)
for(int i = 1; i < MAXFDS; i++)
this->_fds[i].fd = -1;
}

void Server::acceptConnection() {
int status = accept(this->_socket_fd, 0, 0);
if(status != -1){
// besoin d'un recv pour save les info NICK/USER
if(this->_nfds < MAXCLIENT + 1){
addNewClient(status);
}else{
//marche pas full bien
send(status, "Server is full comeback later \r\n", 50, 0);
close(status);
}
addNewClient(status);
}else
acceptFailureException(); // peut etre pas d'exception si on veux pas que le server ferme
}

void Server::addNewClient(int status){
_fds[_nfds].fd = status;
_fds[_nfds].events = POLLIN;
cout << "New connect #" << _fds[_nfds].fd << endl;
_nfds++;
for(uint32_t i = 0; i <= _nfds; i++){
if(_fds[i].fd == -1){
_fds[i].fd = status;
_fds[i].events = POLLIN;
cout << "New connect #" << _fds[i].fd << endl;
_nfds++;
return;
}
}
//besoin d'un send pour pas de place au server pour un nouveau client
}

void Server::receiver(){
getBuffer();
if(getBuffer() == -1)
return;
processRequests();
}

void Server::getBuffer(){
int Server::getBuffer(){
int bytes = 0;
while(1){
bzero(_buf, BUFFERSIZE);
bytes = recv(_fds[this->_client_index].fd, _buf, BUFFERSIZE, 0);
if(bytes != -1)
if(bytes > 0)
_buffer.append(_buf, BUFFERSIZE);
else if(bytes == 0)
return closeConnection();
else
break;
return 0;
}
}

int Server::closeConnection(){
cout << "Closing connection #" << _fds[_client_index].fd << endl;
close(_fds[_client_index].fd);
_fds[_client_index].fd = -1;
// if(_userDB.find(_fds[_client_index].fd) != _userDB.end()) //TODO utiliser avec frank merge
// _userDB.erase(_userDB.find(_fds[_client_index].fd));
if(_userDB.find(_client_index) != _userDB.end()) //TODO delete apres merge frank
_userDB.erase(_client_index);
return -1;
//TODO verifier les structure client savoir quoi detruire a la deconnection
}

void Server::processRequests(){
// _buffer.assign("NICK salut\r\nNICK\r\nNICK\r\n");
if(_buf[0] != 0)
return;
while(_buffer.empty() == false){
splitBuffer();
messageHandler();
Expand All @@ -174,7 +190,6 @@ void Server::splitBuffer(){
}

void Server::buildCommandReceived(size_t pos){

if(pos != std::string::npos)
_command_received.assign(_buffer.substr(0, pos));
if(_command_received.find("\r") != string::npos)
Expand Down
11 changes: 6 additions & 5 deletions Server.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


#ifndef SERVER_HPP
#define SERVER_HPP

Expand Down Expand Up @@ -29,6 +27,7 @@
#define PORT 6667
#define BACKLOG 20
#define MAXCLIENT 10
#define MAXFDS (MAXCLIENT + 1) // +1 for the socket_fd
#define BUFFERSIZE 512

#define WELCOME "001 user Welcome !\r\n"
Expand Down Expand Up @@ -75,7 +74,8 @@ class Server {
void parseCommand();

void receiver();
void getBuffer();
int getBuffer();
int closeConnection();
void processRequests();
void splitBuffer();
void buildCommandReceived(size_t pos);
Expand Down Expand Up @@ -110,12 +110,13 @@ class Server {
string _hostname;

map<int, clientInfo> _userDB;
struct pollfd _fds[MAXCLIENT + 1]; // +1 for the socket_fd
struct pollfd _fds[MAXFDS];
nfds_t _nfds;


};

#include "CommandHandler.hpp"

#endif
#endif