Commit 6aebaa54 authored by jan.koester's avatar jan.koester
Browse files

inital commit

parent daaa5c63
Loading
Loading
Loading
Loading

.gitignore

0 → 100644
+1 −0
Changes for .gitignore: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
build/

CMakeLists.txt

0 → 100644
+27 −0
Changes for CMakeLists.txt: 27 added lines, 0 removed lines.
Original line number Diff line number Diff line
project(systempp C CXX ASM )
cmake_minimum_required(VERSION 3.0)

if(${CMAKE_HOST_SYSTEM_NAME} STREQUAL "Linux")
    set(CMAKE_C_COMPILER "clang")
    set(CMAKE_CXX_COMPILER "clang++")
    set(CMAKE_SHARED_LINKER_FLAGS_INIT "-fuse-ld=lld")
    set (VISIBILTY_HIDDEN "__attribute__ ((visibility (\"hidden\")))")

    set(CMAKE_CXX_FLAGS 
        "${CMAKE_CXX_FLAGS} \
        -fPIC \
        -Wall \
        -fsanitize=address "
    )

    set(CMAKE_C_FLAGS 
        "${CMAKE_C_FLAGS} \
        -fPIC \
        -fsanitize=address \
        -Wall "
    )
endif()

find_package(libcryptplus REQUIRED)

add_subdirectory(src)

src/CMakeLists.txt

0 → 100644
+17 −0
Changes for src/CMakeLists.txt: 17 added lines, 0 removed lines.
Original line number Diff line number Diff line
list(APPEND netplussrc
    exception.cpp
    connection.cpp
)

if(${CMAKE_HOST_SYSTEM_NAME} STREQUAL "Linux")
    list(APPEND netplussrc
        linux/socket.cpp
        linux/event.cpp
    )
endif()

include_directories(
    ${CMAKE_SOURCE_DIR}/src
)

add_library(netplus SHARED ${netplussrc})

src/connection.cpp

0 → 100644
+276 −0
Changes for src/connection.cpp: 276 added lines, 0 removed lines.
Original line number Diff line number Diff line
/*******************************************************************************
 * Copyright (c) 2022, 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
 *      notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 * 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
 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * 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 <cstring>

#include "socket.h"
#include "connection.h"
#include "eventapi.h"
#include "exception.h"

const char* netplus::con::condata::getData(){
  return _Data.c_str();
}

unsigned long netplus::con::condata::getDataSize(){
  return _Data.length();
}

netplus::con::condata *netplus::con::condata::nextcondata(){
  return _nextConnectionData;
}

netplus::con::condata::condata(const char*data,unsigned long datasize)  {
    _Data.append(data,datasize);
    _nextConnectionData=nullptr;
}

netplus::con::condata::~condata() {
    delete _nextConnectionData;
}

/** \brief a method to add Data to Sendqueue
  * \param data an const char* to add to sendqueue
  * \param datasize an size_t to set datasize
  * \return the last ConnectionData Block from Sendqueue
  * 
  * This method does unbelievably useful things.  
  * And returns exceptionally the new connection data block.
  * Use it everyday with good health.
  */

netplus::con::condata *netplus::con::addSendQueue(const char*data,unsigned long datasize){
    if(datasize<=0){
        NetException excp;
        excp[NetException::Error] << "addSendQueue wrong datasize";
        throw excp;
    }
    if(!_SendDataFirst){
        _SendDataFirst= new con::condata(data,datasize);
        _SendDataLast=_SendDataFirst;
    }else{
        _SendDataLast->_nextConnectionData=new con::condata(data,datasize);
        _SendDataLast=_SendDataLast->_nextConnectionData;
    }
    _SendDataSize+=datasize;
    _eventapi->sendReady(this,true);
    return _SendDataLast;
}

void netplus::con::cleanSendData(){
   delete _SendDataFirst;
   _SendDataFirst=nullptr;
   _SendDataLast=nullptr;
   _SendDataSize=0;
}

netplus::con::condata *netplus::con::resizeSendQueue(unsigned long size){
    try{
        return _resizeQueue(&_SendDataFirst,&_SendDataLast,_SendDataSize,size);
    }catch(char *e){
        throw e; 
    }
}

netplus::con::condata* netplus::con::getSendData(){
  return _SendDataFirst;
}

size_t netplus::con::getSendSize(){
  return _SendDataSize;
}

netplus::con::condata *netplus::con::addRecvQueue(const char *data,unsigned long datasize){
    if(datasize<=0){
        NetException excp;
        excp[NetException::Error] << "addRecvQueue wrong datasize";
        throw excp;
    }
    if(!_ReadDataFirst){
        _ReadDataFirst= new con::condata(data,datasize);
        _ReadDataLast=_ReadDataFirst;
    }else{
        _ReadDataLast->_nextConnectionData=new con::condata(data,datasize);
        _ReadDataLast=_ReadDataLast->_nextConnectionData;
    }
    _ReadDataSize+=datasize;
    return _ReadDataLast;
}

void netplus::con::cleanRecvData(){
   delete _ReadDataFirst;
  _ReadDataFirst=nullptr;
  _ReadDataLast=nullptr;
  _ReadDataSize=0;
}


netplus::con::condata *netplus::con::resizeRecvQueue(unsigned long size){
    try{
        return _resizeQueue(&_ReadDataFirst,&_ReadDataLast,_ReadDataSize,size);
    }catch(char *e){
        throw e; 
    }
}

netplus::con::condata *netplus::con::getRecvData(){
  return _ReadDataFirst;
}

unsigned long netplus::con::getRecvSize(){
  return _ReadDataSize;
}

netplus::con::condata* netplus::con::_resizeQueue(condata** firstdata, condata** lastdata,
    unsigned long& qsize, unsigned long size) {
    if (!*firstdata || size > qsize) {
        NetException excp;
        excp[NetException::Error] << "_resizeQueue wrong datasize or ConnectionData";
        throw excp;
    }
#ifdef DEBUG
    unsigned long delsize = 0, presize = qsize;
#endif
    qsize -= size;
HAVEDATA:
    if ((*firstdata)->getDataSize() >= size) {
#ifdef DEBUG
        delsize += (*firstdata)->getDataSize();;
#endif
        size -= (*firstdata)->getDataSize();
        condata* newdat = (*firstdata)->_nextConnectionData;
        (*firstdata)->_nextConnectionData = nullptr;
        if (*firstdata == *lastdata)
            (*lastdata) = nullptr;
        delete* firstdata;
        *firstdata = newdat;
        if (*firstdata)
            goto HAVEDATA;
    }
    if (size > 0) {
#ifdef DEBUG
        delsize += size;
#endif
        std::string buffer;
        buffer = (*firstdata)->_Data.substr(size, (*firstdata)->_Data.length() - size);
        (*firstdata)->_Data = buffer;
        *firstdata = (*firstdata);
    }
#ifdef DEBUG
    sys::cout << " delsize: " << delsize
        << " Calculated Blocksize: " << (presize - delsize)
        << " Planned size: " << qsize
        << sys::endl;
    if ((presize - delsize) != qsize) {
        exception[SystemException::Critical] << "_resizeQueue: Calculated wrong size";
        throw exception;
    }
#endif

    return *firstdata;
}

                                                               
int netplus::con::copyValue(con::condata* startblock, size_t startpos,
    con::condata* endblock, size_t endpos, std::string& buffer) {

    con::condata* curdat = startblock;

    do {
        if (curdat == startblock && curdat == endblock) {
            buffer += curdat->_Data.substr(startpos, endpos - startpos);
            break;
        }
        else if (curdat == startblock) {
            size_t len = curdat->_Data.length();
            buffer += curdat->_Data.substr(startpos, len - startpos);
        }
        else if (curdat == endblock) {
            buffer += curdat->_Data.substr(0, endpos);
        }
        else {
            buffer += curdat->_Data;
        }
        curdat = curdat->nextcondata();
    } while (curdat != endblock);

    return buffer.length(); //not include termination
}

int netplus::con::searchValue(con::condata* startblock, con::condata** findblock,
                                       const char* keyword){
    return searchValue(startblock, findblock, keyword,strlen(keyword));
}
                                       
int netplus::con::searchValue(con::condata* startblock, con::condata** findblock,
                                       const char* keyword,unsigned long keylen){
   unsigned long fpos=0,fcurpos=0;
    for(con::condata *curdat=startblock; curdat; curdat=curdat->nextcondata()){
        for(unsigned long pos=0; pos<curdat->getDataSize(); ++pos){
            if(keyword[fcurpos]==curdat->_Data[pos]){
                if(fcurpos==0){
                    fpos=pos;
                    *findblock=curdat;
                }
                fcurpos++;
            }else{
                fcurpos=0;
                fpos=0;
                *findblock=nullptr;
            }
            if(fcurpos==keylen)
                return fpos;
        }
    }
    return -1;
}

netplus::con::con(eventapi *event){
    _ReadDataFirst=nullptr;
    _ReadDataLast=nullptr;
    _ReadDataSize=0;
    _SendDataFirst=nullptr;
    _SendDataLast=nullptr;
    _SendDataSize=0;
    _eventapi=event;
}

netplus::con::con(){
    _ReadDataFirst=nullptr;
    _ReadDataLast=nullptr;
    _ReadDataSize=0;
    _SendDataFirst=nullptr;
    _SendDataLast=nullptr;
    _SendDataSize=0;
    _eventapi=nullptr;
}

netplus::con::~con(){
    delete _ReadDataFirst;
    delete _SendDataFirst;
}

src/connection.h

0 → 100644
+97 −0
Changes for src/connection.h: 97 added lines, 0 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
 *      notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 * 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
 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * 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.
 *******************************************************************************/

#pragma once

namespace netplus {
        class eventapi;
        
        class con {
        public:
            class condata {
            public:
                const char*      getData();
                unsigned long    getDataSize();
                condata         *nextcondata();
            protected:
                condata(const char*data, unsigned long datasize);
                ~condata();
            private:
                std::string      _Data;
                condata         *_nextConnectionData;
                friend class    con;
            };
            
            con(eventapi *event);
            ~con();
            
            /*Cache helper functions*/
            
            int             copyValue(condata* startblock, size_t startpos,
                                      condata* endblock, size_t endpos, std::string &buffer);
            int             searchValue(condata* startblock, condata** findblock,
                                        const char* keyword);
            int             searchValue(condata* startblock, condata** findblock,
                                        const char* keyword,unsigned long keylen);
            
            condata *addSendQueue(const char *data,unsigned long datasize);
            condata *resizeSendQueue(unsigned long size);
            void     cleanSendData();
            condata *getSendData();
            size_t   getSendSize();
            
            /*Get Data funtions Recv Queue*/
            condata *addRecvQueue(const char *data,unsigned long datasize);
            condata *resizeRecvQueue(unsigned long size);
            void     cleanRecvData();
            condata *getRecvData();
            size_t   getRecvSize();
            
            /*clientsocket*/
            socket  *csock;
        protected:
            con();
            /*Incomming Data*/
            size_t   _ReadDataSize;
            /*Outgoing Data*/
            size_t   _SendDataSize;
        private:
            condata *_resizeQueue(condata **firstdata, condata **lastdata,
                                  unsigned long &qsize,unsigned long size);
            
            /*Incomming Data*/
            condata *_ReadDataFirst;
            condata *_ReadDataLast;
            
            /*Outgoing Data*/
            condata  *_SendDataFirst;
            condata  *_SendDataLast;
            eventapi *_eventapi;
            
            friend class event;
        };
};
Loading