Commit 7f249944 authored by jan.koester's avatar jan.koester
Browse files

test

parent 7ce9326a
Loading
Loading
Loading
Loading
+24 −18
Changes for src/event/epoll.cpp: 24 added lines, 18 removed lines.
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <stdlib.h>
#include <stdint.h>
#include <mutex>
#include <map>

#include <cstring>
#include <errno.h>
@@ -57,10 +58,12 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#define BLOCKSIZE 16384

namespace netplus {
    class poll;
    bool RUNNING=true;
    std::vector<poll*> POLLS;
    std::mutex CLOSE_HANDLER_MUTEX;

    std::vector<socket*> SSOCKETS;
    std::map<int,con*>    CONNECTIONS;

    std::mutex POLL_HANDLER_MUTEX;

    class pollapi {
    public:
@@ -100,23 +103,16 @@ namespace netplus {
            _Timeout(timeout)
        {

            SSOCKETS.emplace_back(_ServerSocket);

            for (int i = 0; i <_ServerSocket->getMaxconnections();  ++i){
                _Events[i].events=-1;
                _Events[i].data.fd=-1;
                _Events[i].data.ptr=nullptr;
            }

            POLLS.push_back(this);
        };

        virtual ~poll() {
            for (int i = 0; i <_ServerSocket->getMaxconnections();  ++i){
                CloseEventHandler(i,0,0);
            }
            for(auto poll = POLLS.begin(); poll!=POLLS.end(); ++poll){
                if(*poll==this)
                    POLLS.erase(poll);
            };
        };

        /*basic functions*/
@@ -206,8 +202,12 @@ namespace netplus {
            }

             evconnection ->ConnectEvent(*ccon,tid,args);

            {
                std::lock_guard<std::mutex> clock(ccon->event_mutex),global_lock(POLL_HANDLER_MUTEX);
                CONNECTIONS.emplace(ccon->csock->fd(),ccon.get());
                ccon.release();
            }

        };

        void ReadEventHandler(int pos,const int tid,ULONG_PTR args) {
@@ -296,7 +296,7 @@ namespace netplus {
        };

        void CloseEventHandler(int pos,const int tid,ULONG_PTR args) {
            std::lock_guard<std::mutex> global_lock(CLOSE_HANDLER_MUTEX);
            std::lock_guard<std::mutex> global_lock(POLL_HANDLER_MUTEX);

            if (!_Events[pos].data.ptr) {
                return;
@@ -324,6 +324,8 @@ namespace netplus {
                    }
                }
                _Events[pos].data.ptr = nullptr;

                CONNECTIONS.erase(ccon->csock->fd());
            }

            delete ccon;
@@ -459,12 +461,16 @@ namespace netplus {
    }

    void signalHandler(int signum) {
        std::lock_guard<std::mutex> lock(POLL_HANDLER_MUTEX);
        switch(signum){
            case SIGINT:
                for(poll *polli : POLLS){
                    polli->_ServerSocket->close();
                    for(int i = 0; i< polli->_ServerSocket->getMaxconnections(); ++i){
                            polli->setpollEvents(i,EPOLLHUP | EPOLLONESHOT);
                for(auto ssock : SSOCKETS){
                    ssock->close();
                }
                for(auto ccon : CONNECTIONS){
                    {
                        std::lock_guard<std::mutex> clock(ccon.second->event_mutex);
                        ccon.second->csock->close();
                    }
                }
                RUNNING=false;