Commit 9c70e09b authored by jan.koester's avatar jan.koester
Browse files

test

parent b557381c
Loading
Loading
Loading
Loading
+26 −6
Changes for src/event/kqueue.cpp: 26 added lines, 6 removed lines.
Original line number Diff line number Diff line
/*******************************************************************************
Copyright (c) 2014, Jan Koester jan.koester@gmx.net
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
@@ -11,6 +12,7 @@ modification, are permitted provided that the following conditions are met:
    * Neither the name of the <organization> nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
@@ -22,10 +24,12 @@ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*******************************************************************************/

#include <iostream>
#include <algorithm>
#include <chrono>
#include <memory>

#include <unistd.h>
#include <signal.h>
#include <sys/event.h>
@@ -34,23 +38,31 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <stdint.h>
#include <mutex>
#include <map>

#include <cstring>
#include <errno.h>

#include "socket.h"
#include "exception.h"
#include "eventapi.h"
#include "connection.h"
#include "../posix/error.h"
#include <assert.h>

#define READEVENT 0
#define SENDEVENT 1

#define BLOCKSIZE 16384

namespace netplus {
    std::atomic<bool> event::Running(true);
    std::atomic<bool> event::Restart(false);

    std::mutex POLL_HANDLER_MUTEX;

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

    class pollapi {
    public:
        pollapi(eventapi *eapi,int timeout){
@@ -58,21 +70,27 @@ namespace netplus {
        };

        virtual ~pollapi(){

        };

        enum EventHandlerStatus{EVWAIT=0,EVIN=1,EVOUT=2,EVUP=3,EVERR=4,EVCON=5};

        virtual void initEventHandler()=0;
        virtual const char *getpolltype()=0;
        /*pollstate*/
        virtual int pollState(int pos)=0;

        /*EventHandler*/
        virtual  int waitEventHandler(int timeout)=0;
        virtual void ConnectEventHandler(int pos,const int tid,ULONG_PTR args)=0;
        virtual void ReadEventHandler(int pos,const int tid,ULONG_PTR args)=0;
        virtual void WriteEventHandler(int pos,const int tid,ULONG_PTR args)=0;
        virtual void CloseEventHandler(int pos,const int tid,ULONG_PTR args)=0;

    protected:
        eventapi *evconnection;
    };

    class poll : public pollapi{
    public:
        poll(socket* serversocket,eventapi *eapi,int pollfd,int timeout) : pollapi(eapi,timeout){
@@ -91,25 +109,33 @@ namespace netplus {
                _Events[i].udata=nullptr;
            }
        };

        ~poll() {
            delete[] _Events;
        };

        /*basic functions*/
        const char* getpolltype() {
            return "KQUEUE";
        }

        /*event handler function*/
        void initEventHandler() {

        };

        int getEventFlags(int pos) {
            return _Events[pos].filter;
        }

        void setpollEvents(con* curcon,int filter,int events){
            NetException except;
            struct kevent setevent = { 0 };
            if(!curcon)
                return;

            if(!curcon->csock) return;

            EV_SET(&setevent,curcon->csock->fd(),filter,events,0,0,curcon);

            if (kevent(_pollFD, &setevent, 1, nullptr, 0, nullptr) < 0) {
@@ -182,7 +208,6 @@ void ConnectEventHandler(int pos, const int tid, ULONG_PTR args) {
                    // 3. Attempt to accept the new connection
                    _ServerSocket->accept(ccon->csock);
                }catch(NetException &e){

                    // Check for the non-fatal error (EAGAIN/EWOULDBLOCK) wrapped in NetException::Note.
                    if (e.getErrorType()==NetException::Note) {
                        // This means the listen queue is empty. Break the accept loop.
@@ -254,7 +279,6 @@ void ConnectEventHandler(int pos, const int tid, ULONG_PTR args) {

            try{


                size_t rcvsize = rcon->csock->recvData(buf, BLOCKSIZE);

                rcon->lasteventime = time(nullptr);
@@ -325,7 +349,6 @@ void ConnectEventHandler(int pos, const int tid, ULONG_PTR args) {
                wcon->SendData.erase(wcon->SendData.begin(),
                                        wcon->SendData.begin() + sended);


                wcon->lasteventime = time(nullptr);

                setpollEvents(wcon,EVFILT_WRITE,EV_ADD | EV_ONESHOT);
@@ -395,7 +418,6 @@ void ConnectEventHandler(int pos, const int tid, ULONG_PTR args) {

    class EventWorkerArgs{
    public:

        EventWorkerArgs(){
        }

@@ -466,7 +488,6 @@ void ConnectEventHandler(int pos, const int tid, ULONG_PTR args) {
                            }
                        }
                    }

                } catch (NetException& e) {
                    // Catches critical exceptions re-thrown from inner block or waitEventHandler failure.
                    std::cerr << "CRITICAL WORKER ERROR: " << e.what() << std::endl;
@@ -537,7 +558,6 @@ void ConnectEventHandler(int pos, const int tid, ULONG_PTR args) {
    void signalHandler(int signum) {
        std::lock_guard<std::mutex> lock(POLL_HANDLER_MUTEX);
        switch(signum){

            case SIGINT:
                for(auto ssock : SSOCKETS){
                    ssock->close();