Commit 3ba89bf2 authored by jan.koester's avatar jan.koester
Browse files

sycronisation changed

parent 3234cbd7
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -58,6 +58,10 @@ namespace netplus {
            virtual void ConnectEvent(con *curcon)=0;
            virtual void DisconnectEvent(con *curcon)=0;

            /*locking for competing threads*/
            virtual void unlockCon(int pos)=0;
            virtual bool trylockCon(int pos)=0;

            /*Connection Ready to send Data
             * DANGEROUS to burnout your cpu
             *only use this if know what you do!*/
@@ -77,6 +81,8 @@ namespace netplus {
            void WriteEventHandler(int pos);
            void CloseEventHandler(int pos);
            void sendReady(con *curcon,bool ready);
            void unlockCon(int pos);
            bool trylockCon(int pos);
        private:

            void                 _setpollEvents(con *curcon,int events);
+28 −9
Original line number Diff line number Diff line
@@ -127,6 +127,7 @@ namespace netplus {
        con *ccon;
        try {
            ccon = new con(this);
            ccon->conlock.lock();
            ccon->csock = _ServerSocket->accept();
            ccon->csock->setnonblocking();

@@ -143,6 +144,7 @@ namespace netplus {
            std::cout << "Connected: " << ip  << std::endl;
            ConnectEvent(ccon);
        } catch (NetException& e) {
            ccon->conlock.unlock();
            delete ccon->csock;
            delete ccon;
        }
@@ -251,6 +253,20 @@ namespace netplus {
        }
    };

    void poll::unlockCon(int pos){
        con *curcon=(con*)_Events[pos].data.ptr;
        if(curcon)
            curcon->conlock.unlock();
    }

    bool poll::trylockCon(int pos){
        const std::lock_guard<std::mutex> lock(_StateLock);
        con *curcon=(con*)_Events[pos].data.ptr;
        if(curcon)
            return curcon->conlock.try_lock();
        return false;
    }

    bool event::_Run = true;
    bool event::_Restart = false;

@@ -267,6 +283,7 @@ namespace netplus {
                            int state=eventptr->pollState(i);
                            if(state==poll::EVCON)
                                eventptr->ConnectEventHandler(i);
                            if(eventptr->trylockCon(i)){
                                switch (state) {
                                    case poll::EVIN:
                                        eventptr->ReadEventHandler(i);
@@ -277,6 +294,8 @@ namespace netplus {
                                    default:
                                        break;
                                }
                                eventptr->unlockCon(i);
                            }
                        } catch (NetException& e) {
                            eventptr->CloseEventHandler(i);
                            if (e.getErrorType() == NetException::Critical) {