implemented primary event loop for server operations
This commit is contained in:
20
Server.cpp
20
Server.cpp
@@ -39,6 +39,25 @@ void Server::init() {
|
|||||||
std::cout << "Waiting for connections..." << std::endl;
|
std::cout << "Waiting for connections..." << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Server::run() {
|
||||||
|
while (!_signal) {
|
||||||
|
int ret = poll(&_fds[0], _fds.size(), -1);
|
||||||
|
if (ret == -1)
|
||||||
|
throw std::runtime_error("Error: poll() failed");
|
||||||
|
|
||||||
|
for (size_t i = 0; i < _fds.size(); ++i) {
|
||||||
|
if (_fds[i].revents & POLLIN) {
|
||||||
|
if (_fds[i].fd == _serverSocketFd)
|
||||||
|
// handle new client connection here
|
||||||
|
continue;
|
||||||
|
else
|
||||||
|
// handle existing client incoming data here
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void Server::createServerSocket() {
|
void Server::createServerSocket() {
|
||||||
_serverSocketFd = socket(AF_INET, SOCK_STREAM, 0);
|
_serverSocketFd = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
if (_serverSocketFd == -1)
|
if (_serverSocketFd == -1)
|
||||||
@@ -57,7 +76,6 @@ void Server::createServerSocket() {
|
|||||||
throw std::runtime_error("Error: listen() failed");
|
throw std::runtime_error("Error: listen() failed");
|
||||||
|
|
||||||
addPollfd(_serverSocketFd, POLLIN, 0);
|
addPollfd(_serverSocketFd, POLLIN, 0);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Server::bindServerSocket() {
|
void Server::bindServerSocket() {
|
||||||
|
@@ -23,11 +23,11 @@ class Server {
|
|||||||
void parseArgs(int ac, char **av);
|
void parseArgs(int ac, char **av);
|
||||||
static void receiveSignal(int signum);
|
static void receiveSignal(int signum);
|
||||||
void init();
|
void init();
|
||||||
|
void run();
|
||||||
|
|
||||||
void createServerSocket();
|
void createServerSocket();
|
||||||
void bindServerSocket();
|
void bindServerSocket();
|
||||||
void addPollfd(int fd, short events, short revents);
|
void addPollfd(int fd, short events, short revents);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
1
main.cpp
1
main.cpp
@@ -7,6 +7,7 @@ int main(int ac, char **av)
|
|||||||
try {
|
try {
|
||||||
server.parseArgs(ac, av);
|
server.parseArgs(ac, av);
|
||||||
server.init();
|
server.init();
|
||||||
|
server.run();
|
||||||
} catch (std::exception& e) {
|
} catch (std::exception& e) {
|
||||||
std::cerr << e.what() << std::endl;
|
std::cerr << e.what() << std::endl;
|
||||||
return 1;
|
return 1;
|
||||||
|
Reference in New Issue
Block a user