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

. #7

Merged
merged 1 commit into from
Jul 3, 2024
Merged

. #7

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
33 changes: 33 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/bin/webserv",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
}

]
}
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CXX := c++
CXXFLAGS := -std=c++98 -Wall -Wextra -Werror -Iinclude
CXXFLAGS := -std=c++98 -Wall -Wextra -Werror -Iinclude -g3
CXXFLAGS_TEST := -Wall -Wextra -Werror -Iinclude -isystem $(GTEST_DIR)/include

SRC_DIR := src
Expand Down
7 changes: 2 additions & 5 deletions include/Server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: bmoretti <bmoretti@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/27 10:40:41 by bmoretti #+# #+# */
/* Updated: 2024/07/01 13:13:04 by bmoretti ### ########.fr */
/* Updated: 2024/07/02 11:49:50 by bmoretti ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -25,19 +25,16 @@ class Server {
int _serverSocket;
struct sockaddr_in _address;
std::vector<Client *> _clients;
std::vector<struct pollfd> _fds;

// Server initialization
void _createServerSocket(void);
void _bindServer(void);
void _listen(void);
void _polling(void);

// Client management
void _acceptClient(void);

// Polling
void _polling(void);

};

#endif
12 changes: 7 additions & 5 deletions src/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: bmoretti <bmoretti@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/27 10:40:35 by bmoretti #+# #+# */
/* Updated: 2024/07/01 14:20:04 by bmoretti ### ########.fr */
/* Updated: 2024/07/02 11:52:58 by bmoretti ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -21,6 +21,7 @@ Server::Server() {
this->_createServerSocket();
this->_bindServer();
this->_listen();
this->_polling();
}

Server::~Server() {
Expand All @@ -36,7 +37,7 @@ Server::~Server() {
void Server::_createServerSocket(void) {
this->_serverSocket = socket(AF_INET, SOCK_STREAM, 0);
if (this->_serverSocket == -1) {
throw std::runtime_error("Error creating the server socket");
throw std::runtime_error("Error creating the server socket");
}
}

Expand All @@ -49,7 +50,7 @@ void Server::_bindServer(void) {

void Server::_listen(void) {
if (listen(this->_serverSocket, MAX_CONNECTIONS) == -1) {
throw std::runtime_error("Error listening the server");
throw std::runtime_error("Error listening the server");
}
}

Expand All @@ -62,7 +63,7 @@ void Server::_acceptClient(void) {

void Server::_polling(void) {
struct epoll_event ev, events[MAX_EVENTS];
int listen_sock, conn_sock, nfds, epollfd;
int conn_sock, nfds, epollfd;

epollfd = epoll_create1(0);
if (epollfd == -1) {
Expand All @@ -79,7 +80,7 @@ void Server::_polling(void) {
throw std::runtime_error("Error polling events");
}
for (int n = 0; n < nfds; ++n) {
if (events[n].data.fd == listen_sock) {
if (events[n].data.fd == this->_serverSocket) {
this->_acceptClient();
conn_sock = this->_clients.back()->getClientSocket();
}
Expand All @@ -90,6 +91,7 @@ void Server::_polling(void) {
}
else {
//RESOLVER OS EVENTOS DO NOSSO SERVER
std::cout << "teste" << std::endl;
//do_use_fd(events[n].data.fd);
}
}
Expand Down
121 changes: 69 additions & 52 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,54 +1,71 @@
#include <cstring>
#include <cstdlib>
#include <errno.h>
#include <fcntl.h>
#include <iostream>
#include <poll.h> // to use poll functions and macros
#include <unistd.h>

int main() {
// Open files using open function
int fd1 = open("file1.txt", O_RDONLY | O_NONBLOCK | O_CREAT, 0666);
int fd2 = open("file2.txt", O_RDONLY | O_NONBLOCK | O_CREAT, 0666);

if (fd1 == -1 || fd2 == -1) {
std::cerr << "Error opening file(s): " << strerror(errno) << std::endl;
return 1;
}

// Initialize pollfd structures
struct pollfd fds[2];
fds[0].fd = fd1;
fds[0].events = POLLIN; // Monitor fd1 for input
fds[1].fd = fd2;
fds[1].events = POLLIN; // Monitor fd2 for input

int timeout = 5000; // Timeout in milliseconds (5 seconds)

// Call poll
int ret = poll(fds, 2, timeout);

if (ret == -1) {
std::cerr << "poll error: " << strerror(errno) << std::endl;
return 1;
} else if (ret == 0) {
std::cout << "Timeout occurred! No data available." << std::endl;
} else {
for (int i = 0; i < 2; ++i) {
if (fds[i].revents & POLLIN) {
std::cout << "File descriptor " << fds[i].fd << " is ready to read."
<< std::endl;
// Read data or perform necessary operations
}
}
}

// Close file descriptors
close(fd1);
close(fd2);

system("rm file1.txt");
system("rm file2.txt");

return 0;
// #include <cstdlib>
// #include <errno.h>
// #include <fcntl.h>
// #include <iostream>
// #include <poll.h> // to use poll functions and macros
// #include <unistd.h>

// int main() {
// // Open files using open function
// int fd1 = open("file1.txt", O_RDONLY | O_NONBLOCK | O_CREAT, 0666);
// int fd2 = open("file2.txt", O_RDONLY | O_NONBLOCK | O_CREAT, 0666);

// if (fd1 == -1 || fd2 == -1) {
// std::cerr << "Error opening file(s): " << strerror(errno) << std::endl;
// return 1;
// }

// // Initialize pollfd structures
// struct pollfd fds[2];
// fds[0].fd = fd1;
// fds[0].events = POLLIN; // Monitor fd1 for input
// fds[1].fd = fd2;
// fds[1].events = POLLIN; // Monitor fd2 for input

// int timeout = 5000; // Timeout in milliseconds (5 seconds)

// // Call poll
// int ret = poll(fds, 2, timeout);

// if (ret == -1) {
// std::cerr << "poll error: " << strerror(errno) << std::endl;
// return 1;
// } else if (ret == 0) {
// std::cout << "Timeout occurred! No data available." << std::endl;
// } else {
// for (int i = 0; i < 2; ++i) {
// if (fds[i].revents & POLLIN) {
// std::cout << "File descriptor " << fds[i].fd << " is ready to read."
// << std::endl;
// // Read data or perform necessary operations
// }
// }
// }

// // Close file descriptors
// close(fd1);
// close(fd2);

// system("rm file1.txt");
// system("rm file2.txt");

// return 0;
// }

# include "Server.hpp"

int main(void)
{

try {
Server serv;
}

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

return 0;
}