Commit 59e531b0 authored by jan.koester's avatar jan.koester
Browse files

Merge branch 'main' of git.tuxist.de:jan.koester/libnetplus

parents e3aaa71f 9660ab46
Loading
Loading
Loading
Loading
+1 −1
Changes for src/connection.h: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -150,7 +150,7 @@ namespace netplus {
            virtual ~con();

            /*clientsocket*/
            std::shared_ptr<socket> csock;
            std::unique_ptr<socket> csock;

            /*event stauts*/
            int state;
+33 −19
Changes for src/event/epoll.cpp: 33 added lines, 19 removed lines.
Original line number Diff line number Diff line
@@ -57,6 +57,19 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#define BLOCKSIZE 16384

namespace netplus {

    bool RUNNING=true;

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

    class pollapi {
    public:
        pollapi(eventapi *evcon,int timeout) : evconnection(evcon){
@@ -86,25 +99,23 @@ namespace netplus {
    class poll : public pollapi{
    public:
        poll(socket *serversocket,eventapi *evcon,int pollfd,int timeout) :
            pollapi(evcon,timeout), _ServerSocket (serversocket){

            NetException exception;
            pollapi(evcon,timeout),
            _pollFD(pollfd),
            _Events(
                new epoll_event[serversocket->getMaxconnections()]
            ),
            _ServerSocket (serversocket),
            _Timeout(timeout)
        {

            _Timeout=timeout;
            _pollFD=pollfd;
            _Events = std::shared_ptr<struct epoll_event[]>(new struct epoll_event[_ServerSocket->getMaxconnections()]);
            int maxcon=_ServerSocket->getMaxconnections();
            for (int i = 0; i <maxcon;  ++i){
            for (int i = 0; i <_ServerSocket->getMaxconnections();  ++i){
                _Events[i].events=-1;
                _Events[i].data.fd=-1;
                _Events[i].data.ptr=nullptr;
            }
        };

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

        /*basic functions*/
@@ -162,11 +173,11 @@ namespace netplus {
            evconnection->CreateConnection(ccon);

            if(_ServerSocket->_Type==sockettype::TCP){
                ccon->csock=std::make_shared<tcp>(-1);
                ccon->csock=std::make_unique<tcp>(-1);
            }else if(_ServerSocket->_Type==sockettype::UDP){
                ccon->csock=std::make_shared<udp>(-1);
                ccon->csock=std::make_unique<udp>(-1);
            }else if(_ServerSocket->_Type==sockettype::SSL){
                // clt->connection->csock=std::make_shared<ssl>( -1);
                // clt->connection->csock=std::make_unique<ssl>( -1);
            }

            _ServerSocket->accept(ccon->csock);
@@ -281,7 +292,6 @@ namespace netplus {
                    evconnection->DisconnectEvent(*ccon,tid,args);
                    delete ccon;
                    _Events[pos].data.ptr=nullptr;

                }
            }catch(NetException &e){
                throw e;
@@ -290,9 +300,10 @@ namespace netplus {
        };

    private:

        int                                    _pollFD;
        std::shared_ptr<struct epoll_event[]>  _Events;
        std::shared_ptr<socket>                _ServerSocket;
        std::unique_ptr<struct epoll_event[]> _Events;
        socket*                                                   _ServerSocket;
        int                                                           _Timeout;
    };

@@ -327,7 +338,8 @@ namespace netplus {
            poll pollptr(eargs->ssocket,eargs->event,eargs->pollfd, eargs->timeout);

            int wait=0;
            for(;;){

            while(RUNNING){
                try {
                    wait=pollptr.waitEventHandler(eargs->timeout);
                    for(int i =0; i<wait; ++i){
@@ -409,6 +421,8 @@ namespace netplus {
    void event::runEventloop(ULONG_PTR args) {
        NetException exception;

        signal(SIGINT, signalHandler);

        signal(SIGPIPE, SIG_IGN);

MAINWORKERLOOP:
+1 −1
Changes for src/eventapi.h: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -53,7 +53,7 @@ namespace netplus {

        class event :public eventapi {
        public:
            event(socket *serversocket, int timeout=-1);
            event(socket *serversocket, int timeout=1000);

            void runEventloop(ULONG_PTR args=0);

+5 −2
Changes for src/posix/socket.cpp: 5 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -57,14 +57,17 @@ netplus::socket::socket(){
    _SocketInfoLen=sizeof(struct addrinfo);
    memset((void*)_SocketInfo,0,sizeof(struct addrinfo));
    _Type=-1;
    ++_InitCount;
}

netplus::socket::~socket(){
    --_InitCount;
    int zero = 0;
    if (_InitCount.compare_exchange_strong(zero, std::memory_order_release)) {
        if(_SocketInfo){
            free(((struct addrinfo*)_SocketInfo)->ai_addr);
            free((void*)_SocketInfo);
        }
    }
}

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

netplus::tcp::~tcp(){
    if(_Socket!=-1)
    int zero = 0;
    --_InitCount;
    if (_Socket!=-1 && _InitCount.compare_exchange_strong(zero, std::memory_order_release))
        ::close(_Socket);
}

netplus::tcp::tcp() : socket() {
    _Socket=-1;
    _Type=sockettype::TCP;
    ++_InitCount;
}

netplus::tcp::tcp(SOCKET sock) : socket (){
     ((struct addrinfo*)_SocketInfo)->ai_family=AF_UNSPEC;
    _Socket=sock;
    _Type=sockettype::TCP;
    ++_InitCount;
}

void netplus::tcp::listen(){
@@ -153,7 +157,7 @@ int netplus::tcp::getMaxconnections(){
    return _Maxconnections;
}

void netplus::tcp::accept(std::shared_ptr<socket> &csock){
void netplus::tcp::accept(std::unique_ptr<socket> &csock){
    NetException exception;

    copyAddrInfo(&csock->_SocketInfo,_SocketInfo,_SocketInfoLen);
@@ -241,7 +245,7 @@ size_t netplus::tcp::recvData(buffer &data,int flags){
    return (size_t)recvsize != 0 ? recvsize : data.size;
}

void netplus::tcp::connect(std::shared_ptr<socket> &csock){
void netplus::tcp::connect(std::unique_ptr<socket> &csock){
    NetException exception;

    copyAddrInfo(&_SocketInfo,csock->_SocketInfo, csock->_SocketInfoLen);
Loading