Commit c789ccb3 authored by jan.koester's avatar jan.koester
Browse files

test

parent 0ea7ca43
Loading
Loading
Loading
Loading
+32 −27
Changes for src/event/epoll.cpp: 32 added lines, 27 removed lines.
Original line number Diff line number Diff line
@@ -57,20 +57,11 @@ 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;

    void signalHandler(int signum) {
        switch(signum){
            case SIGINT:
                RUNNING=false;
                break;
            default:
                break;
        }
    }

    class pollapi {
    public:
        pollapi(eventapi *evcon,int timeout) : evconnection(evcon){
@@ -114,9 +105,18 @@ namespace netplus {
                _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*/
@@ -170,9 +170,9 @@ namespace netplus {
        void ConnectEventHandler(int pos,const int tid,ULONG_PTR args)  {
            NetException exception;

            con *ccon;
            std::unique_ptr<con> ccon;

            evconnection->CreateConnection(&ccon);
            evconnection->CreateConnection(ccon);

            if(_ServerSocket->_Type==sockettype::TCP){
                ccon->csock=std::make_unique<tcp>(-1);
@@ -194,7 +194,7 @@ namespace netplus {

            struct epoll_event setevent { 0 };
            setevent.events =  EPOLLIN | EPOLLONESHOT | EPOLLET;
            setevent.data.ptr = ccon;
            setevent.data.ptr = ccon.get();

            int estate = epoll_ctl(_pollFD, EPOLL_CTL_ADD,ccon->csock->fd(), &setevent);

@@ -206,6 +206,8 @@ namespace netplus {
            }

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

             ccon.release();
        };

        void ReadEventHandler(int pos,const int tid,ULONG_PTR args) {
@@ -293,11 +295,10 @@ namespace netplus {
            }
        };


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

    if (!_Events || !_Events[pos].data.ptr) {
            if (!_Events[pos].data.ptr) {
                return;
            }

@@ -325,7 +326,7 @@ void CloseEventHandler(int pos,const int tid,ULONG_PTR args) {
                _Events[pos].data.ptr = nullptr;
            }

     evconnection->DeleteConnection(ccon);
            delete ccon;
        }

    private:
@@ -333,6 +334,7 @@ void CloseEventHandler(int pos,const int tid,ULONG_PTR args) {
        std::unique_ptr<struct epoll_event[]> _Events;
        socket*                                                   _ServerSocket;
        int                                                           _Timeout;
        friend  void signalHandler(int signum);
    };

    bool event::_Run = true;
@@ -418,11 +420,6 @@ void CloseEventHandler(int pos,const int tid,ULONG_PTR args) {
                    std::cerr << e.what() << std::endl;
                }
            }

            for(int i =0; i<eargs->ssocket->getMaxconnections(); ++i){
                 pollptr.CloseEventHandler(i,tid,0);
            }

        }
    };

@@ -461,12 +458,20 @@ void CloseEventHandler(int pos,const int tid,ULONG_PTR args) {
    event::~event() {
    }

    void sighandler(int signal){
        raise(SIGINT);
    void signalHandler(int signum) {
        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);
                    }
                }
                RUNNING=false;
                break;
            default:
                break;
        }

    void event::CreateConnection(std::unique_ptr<con> &res){
        res=std::unique_ptr<con>(new con(this));
    }

    void event::runEventloop(ULONG_PTR args) {
+1 −3
Changes for src/eventapi.h: 1 added line, 3 removed lines.
Original line number Diff line number Diff line
@@ -46,8 +46,7 @@ namespace netplus {
            virtual void ConnectEvent(con &curcon,const int tid, ULONG_PTR args)=0;
            virtual void DisconnectEvent(con &curcon,const int tid, ULONG_PTR args)=0;
            /*memory allocation*/
            virtual void CreateConnection(con **res)=0;
            virtual void DeleteConnection(con *res)=0;
            virtual void CreateConnection(std::unique_ptr<con> &res)=0;
        protected:
            eventapi(){};
        };
@@ -65,7 +64,6 @@ namespace netplus {

            int                      threads;
        protected:
            void CreateConnection(std::unique_ptr<con> &res);
            event(const event &src);
        private:
            socket   *_ServerSocket;
+5
Changes for src/posix/socket.cpp: 5 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -65,6 +65,11 @@ netplus::socket::~socket(){
     }
}

void netplus::socket::close(){
    ::close(_Socket);
}


void netplus::socket::setFlag(int flag,int value){
    int sockopts=fcntl(_Socket, F_GETFL, 0);
    if (fcntl(_Socket, F_SETFL, sockopts | flag) < 0) {
−1
Changes for src/posix/tcp.cpp: 0 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -116,7 +116,6 @@ netplus::tcp::tcp(const std::string &addr, int port,int maxconnections,int socko
}

netplus::tcp::~tcp(){
    int zero = 0;
    if (_Socket!=-1)
        ::close(_Socket);
}
+2
Changes for src/socket.h: 2 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -110,6 +110,8 @@ namespace netplus {

            virtual socket&        operator=(SOCKET sock)=0;

            void                          close();

            ULONG_PTR                _SocketInfo;
            size_t                   _SocketInfoLen;
            SOCKET                   _Socket;
Loading