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
|
#include "server.hpp"
#include "node.hpp"
#include "socket.hpp"
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdexcept>
#include <iostream>
#define MAX_CONN 20
Server::Server(){}
Server::Server(const portNr& p)
:Node(p)
{
sock->sbind(*this);
sock->slisten(MAX_CONN);
}
//LA METHODE POSANT PROBLEME
cmdQueue Server::update()
{
using namespace std;
cmdQueue ret;
//initializing an fd_set
fd_set set;
FD_ZERO(&set);
FD_SET(sock->getFd(), &set);
struct timeval timeout;
timeout.tv_sec=0;
timeout.tv_usec = 50000;
for(unsigned i=0; i<clients.size(); ++i)
{
FD_SET(clients[i].getSocketFd(), &set);
// clients étant un attribut de la classe Serveur étant un vecteur de RemoteClient, héritant aussi de Node mais qui représente une connexion entrante.
}
cout << flush;
//select
int s=select(clients.size()+2, &set, NULL, NULL, &timeout);
cout << s << " ";
if(s==-1)
{
throw std::runtime_error("runtime_error: Server: update: select");
}
//check for new pending connexions
if(FD_ISSET(sock->getFd(), &set))
{
cout << "nouvelle connexion" << endl ;
clients.push_back(sock->saccept());
}
//check for new incoming data
for(unsigned i=0; i<clients.size(); ++i)
{
cout << "incoming data" << endl;
if(FD_ISSET(clients[i].getSocketFd(), &set))
{
cmdQueue Q=clients[i].update();
ret.append(Q);
}
}
return ret;
}
RemoteClient& Server::getClient(unsigned& i)
{
return clients[i];
}
Server& Server::removeClient(unsigned& i)
{
clientList::iterator it(clients.begin());
it+=i;
clients.erase(it);
return *this;
} |
Partager