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

new locking

parent 1267799c
Loading
Loading
Loading
Loading
+5 −0
Changes for src/connection.h: 5 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -25,6 +25,8 @@
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *******************************************************************************/

#include <mutex>

#pragma once

namespace netplus {
@@ -76,6 +78,9 @@ namespace netplus {

            /*set sending state*/
            void    sending(bool state);

            /*lock*/
            std::mutex conlock;
        protected:
            con();
            /*Incomming Data*/
+0 −3
Changes for src/eventapi.h: 0 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -25,8 +25,6 @@
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *******************************************************************************/

#include <mutex>

#include "socket.h"
#include "connection.h"

@@ -80,7 +78,6 @@ namespace netplus {
            int                  _pollFD;
            struct  poll_event  *_Events;
            socket              *_ServerSocket;
            std::mutex           _ELock;
        };
        
        class event : public poll {
+24 −15
Changes for src/linux/event.cpp: 24 added lines, 15 removed lines.
Original line number Diff line number Diff line
@@ -115,6 +115,7 @@ namespace netplus {
        try {
            if (!ccon) {
                ccon = new con(this);
                ccon->conlock.lock();
                ccon->csock = _ServerSocket->accept();
                ccon->csock->setnonblocking();

@@ -123,16 +124,12 @@ namespace netplus {
                setevent.data.ptr = ccon;

                if (epoll_ctl(_pollFD, EPOLL_CTL_ADD, ccon->csock->getSocket(), (struct epoll_event*)&setevent) < 0) {
                    delete ccon->csock;
                    delete ccon;
                    exception[NetException::Error] << "ConnectEventHandler: can't add socket to epoll";
                    throw exception;
                }

                ConnectEvent(ccon);

                std::cout << "I'am connecting" << std::endl;

                ConnectEvent(ccon);
                ccon->conlock.unlock();
                return EventHandlerStatus::EVIN;
            }
            else if (ccon->getSendData()) {
@@ -143,20 +140,25 @@ namespace netplus {
                std::cout << "I'am reading" << std::endl;
                return EventHandlerStatus::EVIN;
            }
        }
        catch (NetException& e) {
        } catch (NetException& e) {
            delete ccon->csock;
            delete ccon;
            throw e;
        }
    };

    void poll::ReadEventHandler(int pos) {
        try {
        con* rcon = (con*)_Events[pos].data.ptr;
        if (!rcon) {
                NetException exp;
                exp[NetException::Error] << "ReadEvent: No valied Connection at pos: " << pos;
                throw exp;
        }
        try {

            if(!rcon->conlock.try_lock())
                return;

            char buf[BLOCKSIZE];
            ssize_t rcvsize = _ServerSocket->recvData(rcon->csock, buf, BLOCKSIZE);
            if (rcvsize < 0) {
@@ -170,20 +172,25 @@ namespace netplus {

            rcon->addRecvQueue(buf, rcvsize);
            RequestEvent(rcon);
            rcon->conlock.unlock();
        }
        catch (NetException& e) {
            rcon->conlock.unlock();
            throw e;
        }
    };

    void poll::WriteEventHandler(int pos) {
        try {
        con* wcon = (con*)_Events[pos].data.ptr;
        if (!wcon) {
                NetException exp;
                exp[NetException::Error] << "WriteEvent: No valied Connection at pos: " << pos;
                throw exp;
        }
        try {
            if(!wcon->conlock.try_lock())
                return;

            ssize_t sended = _ServerSocket->sendData(wcon->csock,
                (void*)wcon->getSendData()->getData(),
                wcon->getSendData()->getDataLength(), 0);
@@ -199,21 +206,23 @@ namespace netplus {

            wcon->resizeSendQueue(sended);
            ResponseEvent(wcon);
            wcon->conlock.unlock();
        }
        catch (NetException& e) {
            wcon->conlock.unlock();
            throw e;
        }
    };

    void poll::CloseEventHandler(int pos) {
        NetException except;
        _ELock.lock();

        con* delcon = (con*)_Events[pos].data.ptr;

        delcon->conlock.lock();

        if (!delcon) {
            except[NetException::Error] << "CloseEvent connection empty cannot remove!";
            _ELock.unlock();
            delcon->conlock.unlock();
            throw except;
        }

@@ -222,7 +231,7 @@ namespace netplus {

        if (ect < 0) {
            except[NetException::Error] << "CloseEvent can't delete Connection from epoll";
            _ELock.unlock();
            delcon->conlock.unlock();
            throw except;
        }

@@ -230,7 +239,7 @@ namespace netplus {
        delete delcon->csock;
        delete delcon;

        _ELock.unlock();
        delcon->conlock.unlock();
    };

    /*Connection Ready to send Data*/