From 6dbe2c26d833fd2946a4e73aaaf2166834cd7ef1 Mon Sep 17 00:00:00 2001 From: Carl Klemm Date: Mon, 20 May 2019 13:28:51 +0200 Subject: [PATCH] basic epoll implementation --- Socket.cpp | 5 +++++ Socket.h | 2 ++ main.cpp | 29 +++++++++++++++++++++++------ 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/Socket.cpp b/Socket.cpp index 429f861..c17c901 100644 --- a/Socket.cpp +++ b/Socket.cpp @@ -141,6 +141,11 @@ void Socket::cleanUp() { } +int Socket::getFD() +{ + return sockDesc; +} + unsigned short Socket::resolveService(const string &service, const string &protocol) { struct servent *serv; /* Structure containing service information */ diff --git a/Socket.h b/Socket.h index cc943d3..4023a7e 100644 --- a/Socket.h +++ b/Socket.h @@ -103,6 +103,8 @@ public: void setKeepalive(); void setBlocking(bool flag); + + int getFD(); private: // Prevent the user from trying to use value semantics on this object diff --git a/main.cpp b/main.cpp index 08c7946..de71444 100644 --- a/main.cpp +++ b/main.cpp @@ -3,20 +3,23 @@ #include #include #include +#include #include #include #include #include +#include #include #include #include "serial_io.h" #include "Socket.h" -#define VERSION "v0.4" +#define VERSION "v0.5" volatile bool stop = false; volatile bool resettSerialPort = false; +struct epoll_event ev; void intHandler(int sig) { @@ -100,18 +103,21 @@ static int parseCmdArgs(int argc, char** argv, Config *config) return 0; } -void acceptThreadFunction( TCPServerSocket* servSock, std::vector* clientSockets ) +void acceptThreadFunction( TCPServerSocket* servSock, std::vector* clientSockets, std::mutex* clientSocketsMutex, int pollQue ) { while(!stop) { TCPSocket* newSock = servSock->accept(); if(newSock != nullptr) { + clientSocketsMutex->lock(); clientSockets->push_back(newSock); // Wait for a client to connect + clientSocketsMutex->unlock(); + epoll_ctl(pollQue, EPOLL_CTL_ADD, newSock->getFD(), &ev); clientSockets->back()->send("UVOS serial multiplexer " VERSION "\n", sizeof("UVOS serial multiplexer " VERSION "\n")-1); std::cout<<"got client\n"; } - std::this_thread::sleep_for(std::chrono::milliseconds(200)); + std::this_thread::sleep_for(std::chrono::seconds(1)); } } @@ -141,16 +147,25 @@ int main(int argc, char* argv[]) std::cout<<"UVOS serial mulitplexer "< clientSockets; std::thread* acceptThread; @@ -161,7 +176,7 @@ int main(int argc, char* argv[]) { servSock = new TCPServerSocket(config.port, 5, true); // Server Socket object servSock->setBlocking(false); - acceptThread = new std::thread(acceptThreadFunction, servSock, &clientSockets); + acceptThread = new std::thread(acceptThreadFunction, servSock, &clientSockets, &clientSocketsMutex, pollQue); } catch(SocketException &e) { @@ -177,16 +192,18 @@ int main(int argc, char* argv[]) if(config.reinit) alarm(600); + std::cout<<"starting loop\n"; while(!stop) { + epoll_wait(pollQue, &ev, 1, 2000); int readlen = sRead(serial, buffer, 4096); for(unsigned int i = 0; i < clientSockets.size(); i++) { + clientSocketsMutex.lock(); try { - if(readlen > 0) { if(config.verbose) @@ -262,6 +279,7 @@ int main(int argc, char* argv[]) i--; if(i < 0) i=0; } + clientSocketsMutex.unlock(); } if(resettSerialPort) { @@ -269,7 +287,6 @@ int main(int argc, char* argv[]) serial = openSerialPort(config); resettSerialPort = false; } - std::this_thread::sleep_for(std::chrono::milliseconds(10)); } acceptThread->join();