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

fixed

parent 01e3d0fa
Loading
Loading
Loading
Loading
+1 −1
Changes for src/connection.cpp: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -114,7 +114,7 @@ netplus::con::con() {
    std::shared_ptr<condata<char>> SendData(new condata<char>, std::default_delete<condata<char>>());
}

netplus::con::con(std::shared_ptr<eventapi> event) :_eventapi(event) {
netplus::con::con(std::shared_ptr<event> event) : _event(event) {
    state = 0;
	lasteventime = time(nullptr);
}
+5 −5
Changes for src/connection.h: 5 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -36,7 +36,7 @@
#pragma once

namespace netplus {
        class eventapi;
        class event;
        class pollapi;

        template <class T>
@@ -137,7 +137,7 @@ namespace netplus {
        class con {
        public:
            
            con(std::shared_ptr<eventapi> event);
            con(std::shared_ptr<event> event);
            virtual ~con();

            /*clientsocket*/
@@ -147,14 +147,14 @@ namespace netplus {
            int state;

            /*connection Data*/
            std::shared_ptr<condata<char>> RecvData;
            std::shared_ptr<condata<char>> SendData;
            condata<char> RecvData;
            condata<char> SendData;

        protected:
            con();
            int lasteventime;
        private:
            std::shared_ptr<eventapi> _eventapi;
            std::shared_ptr<event>      _event;
            std::shared_ptr<pollapi>    _pollapi;
            friend class poll;
        };
+58 −34
Changes for src/event/epoll.cpp: 58 added lines, 34 removed lines.
Original line number Diff line number Diff line
@@ -57,9 +57,25 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#define BLOCKSIZE 16384

namespace netplus {
    class client{
    public:
        client(std::shared_ptr<event> eapi){
            eapi->CreateConnetion(connection);
        }

        client(const client &src){
            connection=src.connection;
        }

        ~client(){

        }
        std::shared_ptr<con> connection;
    };

    class pollapi {
    public:
        pollapi(std::shared_ptr<eventapi> &eapi,int timeout) : _evtapi(eapi){
        pollapi(std::shared_ptr<event> &eapi,int timeout) : _evtapi(eapi){
        };

        virtual ~pollapi(){
@@ -81,12 +97,12 @@ namespace netplus {
        virtual void CloseEventHandler(int pos,const int tid,ULONG_PTR args)=0;

    protected:
        std::shared_ptr<eventapi> &_evtapi;
        std::shared_ptr<event> &_evtapi;
    };

    class poll : public pollapi{
    public:
        poll(std::shared_ptr<socket> &serversocket,std::shared_ptr<eventapi> &eapi,int pollfd,int timeout) :
        poll(std::shared_ptr<socket> &serversocket,std::shared_ptr<event> &eapi,int pollfd,int timeout) :
            pollapi(eapi,timeout), _ServerSocket (serversocket){

            NetException exception;
@@ -104,6 +120,9 @@ namespace netplus {
        };

        ~poll() {
            for (int i = 0; i <_ServerSocket->getMaxconnections();  ++i){
                delete reinterpret_cast<client*>(_Events[i].data.ptr);
            }
        };

        /*basic functions*/
@@ -116,11 +135,11 @@ namespace netplus {

        };

        void setpollEvents(std::shared_ptr<con> curcon,int events){
        void setpollEvents(int pos,std::shared_ptr<con> curcon,int events){
            NetException except;
            struct epoll_event setevent = { 0 };
            setevent.events = events;
            setevent.data.ptr = &curcon;
            setevent.data.ptr = _Events[pos].data.ptr;

            if (epoll_ctl(_pollFD, EPOLL_CTL_MOD,curcon->csock->fd()
                ,&setevent) < 0) {
@@ -133,7 +152,8 @@ namespace netplus {
            if(!_Events[pos].data.ptr)
                return EventHandlerStatus::EVCON;

            std::shared_ptr<con> pcon(*reinterpret_cast<std::shared_ptr<con>*>(_Events[pos].data.ptr));
            std::shared_ptr<con> pcon=reinterpret_cast<client*>(_Events[pos].data.ptr)->connection;
            // std::cout << pcon->state << std::endl;
            return pcon->state;
        }

@@ -153,11 +173,9 @@ namespace netplus {
        void ConnectEventHandler(int pos,const int tid,ULONG_PTR args)  {
            NetException exception;

            client *clt = new client(_evtapi);


            std::shared_ptr<con> ccon;

            _evtapi->CreateConnetion(ccon,_Events[pos].data.ptr);
            std::shared_ptr<con> ccon=clt->connection;

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

            struct epoll_event setevent { 0 };
            setevent.events =  EPOLLIN | EPOLLONESHOT;
            setevent.data.ptr = &ccon;
            setevent.data.ptr = clt;

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

@@ -188,39 +206,43 @@ namespace netplus {
                char errstr[255];
                strerror_r_netplus(errno,errstr,255);
                exception[NetException::Error] << "ConnectEventHandler: can't add socket to epoll: " << errstr;
                delete clt;
                throw exception;
            }
            _evtapi->ConnectEvent(ccon,tid,args);

        };

        void ReadEventHandler(int pos,const int tid,ULONG_PTR args) {
            std::shared_ptr<con> rcon(*reinterpret_cast<std::shared_ptr<con>*>(_Events[pos].data.ptr));

            std::shared_ptr<con> rcon=reinterpret_cast<client*>(_Events[pos].data.ptr)->connection;

            try{
                std::shared_ptr<char[]> buf( new char[BLOCKSIZE], []( char *p ){ delete [] p; });
                size_t rcvsize = rcon->csock->recvData(buf, BLOCKSIZE);

                rcon->lasteventime = time(nullptr);

                std::cerr.write(buf.get(),rcvsize)<<std::endl;

                if(rcvsize!=0){
                    rcon->RecvData->append(buf.get(),rcvsize);
                    rcon->RecvData.append(buf.get(),rcvsize);
                    rcon->state=EVIN;
                }

                _evtapi->RequestEvent(rcon,tid,args);

                if(!rcon->SendData->empty()){
                if(!rcon->SendData.empty()){
                    rcon->state=EVOUT;
                    setpollEvents(rcon,EPOLLOUT | EPOLLONESHOT);
                    setpollEvents(pos,rcon,EPOLLOUT | EPOLLONESHOT);
                    return;
                }

                setpollEvents(rcon,EPOLLIN | EPOLLONESHOT);
                setpollEvents(pos,rcon,EPOLLIN | EPOLLONESHOT);

            }catch(NetException &e){
                if(e.getErrorType()== NetException::Note){
                     rcon->state=EVIN;
                     setpollEvents(rcon,EPOLLIN | EPOLLONESHOT);
                     setpollEvents(pos,rcon,EPOLLIN | EPOLLONESHOT);
                     return;
                }
                throw e;
@@ -229,35 +251,35 @@ namespace netplus {

        void WriteEventHandler(int pos,const int tid,ULONG_PTR args) {

            std::shared_ptr<con> wcon(*reinterpret_cast<std::shared_ptr<con>*>(_Events[pos].data.ptr));
            std::shared_ptr<con> wcon=reinterpret_cast<client*>(_Events[pos].data.ptr)->connection;

            try{
                _evtapi->ResponseEvent(wcon,tid,args);

                if(wcon->SendData->empty()){
                if(wcon->SendData.empty()){
                    wcon->state=EVIN;
                    setpollEvents(wcon,EPOLLIN | EPOLLONESHOT);
                    setpollEvents(pos,wcon,EPOLLIN | EPOLLONESHOT);
                    return;
                }

                size_t ssize = BLOCKSIZE < wcon->SendData->size() ? BLOCKSIZE : wcon->SendData->size();
                size_t ssize = BLOCKSIZE < wcon->SendData.size() ? BLOCKSIZE : wcon->SendData.size();


                size_t sended;

                wcon->state=EVOUT;

                std::shared_ptr<const char[]> buf(wcon->SendData->data());
                std::shared_ptr<const char[]> buf(wcon->SendData.data());
                sended = wcon->csock->sendData(buf,ssize);
                wcon->SendData->resize(sended);
                wcon->SendData.resize(sended);

                wcon->lasteventime = time(nullptr);

                setpollEvents(wcon,EPOLLOUT | EPOLLONESHOT);
                setpollEvents(pos,wcon,EPOLLOUT | EPOLLONESHOT);
            }catch(NetException &e){
                if(e.getErrorType()== NetException::Note){
                    wcon->state=EVOUT;
                    setpollEvents(wcon,EPOLLOUT | EPOLLONESHOT);
                    setpollEvents(pos,wcon,EPOLLOUT | EPOLLONESHOT);
                    return;
                }else{
                    throw e;
@@ -267,7 +289,7 @@ namespace netplus {


        void CloseEventHandler(int pos,const int tid,ULONG_PTR args) {
            std::shared_ptr<con> ccon(*reinterpret_cast<std::shared_ptr<con>*>(_Events[pos].data.ptr));
            std::shared_ptr<con> ccon =std::make_shared<con>(*reinterpret_cast<con*>(_Events[pos].data.ptr));
            try{
                if(_pollFD!=pos){
                    if(epoll_ctl(_pollFD, EPOLL_CTL_DEL,ccon->csock->fd(), nullptr)<0){
@@ -279,7 +301,10 @@ namespace netplus {
                    }
                    _evtapi->DisconnectEvent(ccon,tid,args);

                    delete reinterpret_cast<client*>(_Events[pos].data.ptr);

                    _Events[pos].data.ptr=nullptr;

                }
            }catch(NetException &e){
                throw e;
@@ -303,7 +328,7 @@ namespace netplus {
        }

        EventWorkerArgs(const EventWorkerArgs &eargs){
            event=eargs.event;
            eventapi=eargs.eventapi;
            pollfd=eargs.pollfd;
            ssocket=eargs.ssocket;
            timeout=eargs.timeout;
@@ -313,7 +338,7 @@ namespace netplus {
        int                 pollfd;
        int                 timeout;

        std::shared_ptr<eventapi> event;
        std::shared_ptr<event>   eventapi;
        std::shared_ptr<socket>  ssocket;
        void               *args;
    };
@@ -322,7 +347,7 @@ namespace netplus {
    public:
        EventWorker(int tid,ULONG_PTR args,EventWorkerArgs* eargs) {

            poll pollptr(eargs->ssocket, eargs->event, eargs->pollfd, eargs->timeout);
            poll pollptr(eargs->ssocket, eargs->eventapi, eargs->pollfd, eargs->timeout);

            int wait=0;
            for(;;){
@@ -377,9 +402,8 @@ namespace netplus {
        //dummy
    };

    void event::CreateConnetion(std::shared_ptr<con> &curcon,void *res){
        res=new con(shared_from_this());
        curcon=std::shared_ptr<con>(reinterpret_cast<con*>(res));
    void event::CreateConnetion(std::shared_ptr <con> &curcon){
        curcon=std::make_shared<con>(shared_from_this());
    };

    event::event(std::shared_ptr<socket> &serversocket,int timeout) : _ServerSocket(serversocket) {
@@ -430,7 +454,7 @@ MAINWORKERLOOP:

        EventWorkerArgs eargs;
        eargs.ssocket=_ServerSocket;
        eargs.event=std::shared_ptr<event>(this);
        eargs.eventapi=std::shared_ptr<event>(this);
        eargs.pollfd=_pollFD;
        eargs.timeout=_Timeout;

+4 −19
Changes for src/eventapi.h: 4 added lines, 19 removed lines.
Original line number Diff line number Diff line
@@ -38,8 +38,10 @@ namespace netplus {
        class EventWorker;
        class pollapi;

        class eventapi  {
        class event : public std::enable_shared_from_this<event>{
        public:
            event(std::shared_ptr<socket>& serversocket, int timeout=-1);

            /*HTTP API Events*/
            virtual void RequestEvent(std::shared_ptr <con> curcon,const int tid,ULONG_PTR args)=0;
            virtual void ResponseEvent(std::shared_ptr <con> curcon,const int tid, ULONG_PTR args)=0;
@@ -47,24 +49,7 @@ namespace netplus {
            virtual void DisconnectEvent(std::shared_ptr <con> curcon,const int tid, ULONG_PTR args)=0;

            /*memory allocation*/
            virtual void CreateConnetion(std::shared_ptr<con> &curcon, void *res)=0;

            /*run main eventloop*/
            virtual void runEventloop(ULONG_PTR args = 0)=0;
        };

        class event : public eventapi , std::enable_shared_from_this<event>{
        public:
            event(std::shared_ptr<socket>& serversocket, int timeout=-1);

            /*HTTP API Events*/
            virtual void RequestEvent(std::shared_ptr <con> curcon,const int tid,ULONG_PTR args);
            virtual void ResponseEvent(std::shared_ptr <con> curcon,const int tid, ULONG_PTR args);
            virtual void ConnectEvent(std::shared_ptr <con> curcon,const int tid, ULONG_PTR args);
            virtual void DisconnectEvent(std::shared_ptr <con> curcon,const int tid, ULONG_PTR args);

            /*memory allocation*/
            virtual void CreateConnetion(std::shared_ptr<con> &curcon,void *res);
            virtual void CreateConnetion(std::shared_ptr <con> &curcon)=0;

            void runEventloop(ULONG_PTR args=0);