From f3110e9007ab0636ee8872fba262b009c759c152 Mon Sep 17 00:00:00 2001 From: Wiebe Cazemier Date: Sat, 5 Dec 2020 11:31:27 +0100 Subject: [PATCH] Construct clients --- CMakeLists.txt | 2 +- client.cpp | 15 +++++++++++++++ client.h | 29 +++++++++++++++++++++++++++++ main.cpp | 30 +++++++++++++++++++++++++++--- threaddata.cpp | 15 ++++++++++++++- threaddata.h | 11 ++++++++++- 6 files changed, 96 insertions(+), 6 deletions(-) create mode 100644 client.cpp create mode 100644 client.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 9b61a2d..3d8484c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,6 @@ set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED ON) -add_executable(FlashMQ main.cpp utils.cpp threaddata.cpp) +add_executable(FlashMQ main.cpp utils.cpp threaddata.cpp client.cpp) target_link_libraries(FlashMQ pthread) diff --git a/client.cpp b/client.cpp new file mode 100644 index 0000000..100a64f --- /dev/null +++ b/client.cpp @@ -0,0 +1,15 @@ +#include "client.h" + +Client::Client(int fd, ThreadData &threadData) : + fd(fd), + threadData(threadData) +{ + int flags = fcntl(fd, F_GETFL); + fcntl(fd, F_SETFL, flags | O_NONBLOCK); +} + +Client::~Client() +{ + epoll_ctl(threadData.epollfd, EPOLL_CTL_DEL, fd, NULL); // NOTE: the last NULL can cause crash on old kernels + close(fd); +} diff --git a/client.h b/client.h new file mode 100644 index 0000000..132bfa7 --- /dev/null +++ b/client.h @@ -0,0 +1,29 @@ +#ifndef CLIENT_H +#define CLIENT_H + +#include +#include + +#include "threaddata.h" + +class ThreadData; + +class Client +{ + int fd; + + // maybe read buffer? + + ThreadData &threadData; + +public: + Client(int fd, ThreadData &threadData); + ~Client(); + + int getFd() { return fd;} + +}; + +typedef std::shared_ptr Client_p; + +#endif // CLIENT_H diff --git a/main.cpp b/main.cpp index b611ec7..cb32107 100644 --- a/main.cpp +++ b/main.cpp @@ -9,6 +9,7 @@ #include "utils.h" #include "threaddata.h" +#include "client.h" #define MAX_EVENTS 1024 #define NR_OF_THREADS 4 @@ -17,7 +18,31 @@ void do_thread_work(ThreadData &threadData) { + int epoll_fd = threadData.epollfd; + struct epoll_event events[MAX_EVENTS]; + memset(&events, 0, sizeof (struct epoll_event)*MAX_EVENTS); + + while (1) + { + int fdcount = epoll_wait(epoll_fd, events, MAX_EVENTS, 100); + + if (fdcount > 0) + { + for (int i = 0; i < fdcount; i++) + { + struct epoll_event cur_ev = events[i]; + int fd = cur_ev.data.fd; + + Client_p client = threadData.getClient(fd); + + if (client) // TODO: is this check necessary? + { + // TODO left here + } + } + } + } } int main() @@ -81,9 +106,8 @@ int main() socklen_t len = sizeof(struct sockaddr); int fd = check(accept(cur_fd, &addr, &len)); - // TODO: make client - - thread_data.giveFdToEpoll(fd); + Client_p client(new Client(fd, thread_data)); + thread_data.giveClient(client); } else { diff --git a/threaddata.cpp b/threaddata.cpp index 9e1ce51..db9738b 100644 --- a/threaddata.cpp +++ b/threaddata.cpp @@ -8,11 +8,24 @@ ThreadData::ThreadData(int threadnr) : event_fd = eventfd(0, EFD_NONBLOCK); } -void ThreadData::giveFdToEpoll(int fd) +void ThreadData::giveClient(Client_p client) { + int fd = client->getFd(); struct epoll_event ev; memset(&ev, 0, sizeof (struct epoll_event)); ev.data.fd = fd; ev.events = EPOLLIN; check(epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev)); + + clients_by_fd[fd] = client; +} + +Client_p ThreadData::getClient(int fd) +{ + return this->clients_by_fd[fd]; +} + +void ThreadData::removeClient(Client_p client) +{ + clients_by_fd.erase(client->getFd()); } diff --git a/threaddata.h b/threaddata.h index bc210f3..90149c6 100644 --- a/threaddata.h +++ b/threaddata.h @@ -5,9 +5,16 @@ #include "utils.h" #include #include +#include "client.h" +#include + +class Client; +typedef std::shared_ptr Client_p; class ThreadData { + std::map clients_by_fd; + public: std::thread thread; int threadnr = 0; @@ -16,7 +23,9 @@ public: ThreadData(int threadnr); - void giveFdToEpoll(int fd); + void giveClient(Client_p client); + Client_p getClient(int fd); + void removeClient(Client_p client); }; #endif // THREADDATA_H -- libgit2 0.21.4