Commit 2d994929 authored by jan.koester's avatar jan.koester
Browse files

much changes

parent 20b31f58
Loading
Loading
Loading
Loading
+21 −18
Original line number Diff line number Diff line
@@ -55,17 +55,13 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#define BLOCKSIZE 16384

namespace netplus {
    
    struct client {
    class client {
    public:
        client(eventapi* eapi) {
            api = eapi;
            api->CreateConnetion(&CurCon);
            OpCode = 0;
            m_pol.Internal = 0;
            m_pol.InternalHigh = 0;
            m_pol.Offset = 0;
            m_pol.OffsetHigh = 0;
            m_pol.hEvent = nullptr;
            memset(&m_pol, 0, sizeof(m_pol));
        };

        ~client() {
@@ -137,6 +133,12 @@ namespace netplus {
            if (g_serversocket->_Type == TCP)
                pClientContext->CurCon->csock = new tcp();
        ACCEPTCON:
            //int size = 0;
            //pClientContext->CurCon->csock->_Socket=WSAAccept(g_serversocket->fd(), (sockaddr*)pClientContext->CurCon->csock->_SocketInfo,
            //                                                 &size,nullptr,0);

            //if (WSA_IO_PENDING == WSAGetLastError() || WSAEWOULDBLOCK == WSAGetLastError())
            //    goto ACCEPTCON;
            try {
                g_serversocket->accept(pClientContext->CurCon->csock);
            } catch (NetException &e) {
@@ -146,7 +148,7 @@ namespace netplus {
                delete pClientContext;
                throw e;
            }
            pClientContext->CurCon->csock->setnonblocking();
            

            //Store this object
            AddToClientList(pClientContext);
@@ -160,9 +162,11 @@ namespace netplus {
                //Get data.
                DWORD dwFlags = 0;
                DWORD dwBytes = 0;

                WSABUF wbuf;
                wbuf.buf = new char[BLOCKSIZE];
                wbuf.len = BLOCKSIZE;

RECONNECT:
                int nBytesRecv = WSARecv(pClientContext->CurCon->csock->fd(), &wbuf, 1,
                                         &dwBytes, &dwFlags, &pClientContext->m_pol,nullptr);
@@ -193,7 +197,7 @@ RECONNECT:
        socket              *g_serversocket;
        eventapi            *g_eventapi;
        std::vector<client*> g_ClientContext;
        friend struct        client;
        friend class         client;
        friend class         EventWorker;
    };

@@ -274,7 +278,7 @@ RECONNECT:
            DWORD       dwBytesTransfered = 0;
            DWORD       dwBytes = 0, dwFlags = 0;
            DWORD       ret = 0;
            size_t          ssize = 0;
            ULONG       ssize = 0;

            for (;;){
                int bReturn = GetQueuedCompletionStatus(
@@ -308,7 +312,7 @@ RECONNECT:
                        std::cerr << "oh no3" << std::endl;
                        dwFlags = 0;

                        ssize = BLOCKSIZE < pClientContext->CurCon->SendData.size() ? BLOCKSIZE : pClientContext->CurCon->SendData.size();
                        ssize = BLOCKSIZE < pClientContext->CurCon->SendData.size() ? BLOCKSIZE : (ULONG) pClientContext->CurCon->SendData.size();

                        WSABUF wbuf;
                        wbuf.buf = pClientContext->CurCon->SendData.data();
@@ -332,7 +336,7 @@ RECONNECT:

                        dwFlags = 0;

                        ssize = BLOCKSIZE < pClientContext->CurCon->SendData.size() ? BLOCKSIZE : pClientContext->CurCon->SendData.size();
                        ssize = BLOCKSIZE < pClientContext->CurCon->SendData.size() ? BLOCKSIZE : (ULONG) pClientContext->CurCon->SendData.size();

                        WSABUF wbuf;
                        wbuf.buf = new char[BLOCKSIZE];
@@ -362,7 +366,7 @@ RECONNECT:
                    //Send the message back to the client.                    
                    dwFlags = 0;

                    ssize = BLOCKSIZE < pClientContext->CurCon->SendData.size() ? BLOCKSIZE : pClientContext->CurCon->SendData.size();
                    ssize = BLOCKSIZE < pClientContext->CurCon->SendData.size() ? BLOCKSIZE : (ULONG) pClientContext->CurCon->SendData.size();

                    WSABUF wbuf;
                    wbuf.buf = pClientContext->CurCon->SendData.data();
@@ -486,7 +490,6 @@ RECONNECT:
        for (auto i = threadpool.begin(); i != threadpool.end(); ++i) {
            i->join();
        }

    }
};

+2 −1
Original line number Diff line number Diff line
@@ -54,7 +54,8 @@ std::atomic<int> netplus::socket::_InitCount(0);

netplus::socket::socket(){
    _Socket=-1;
    _SocketPtr=nullptr;
    _SocketInfo=nullptr;
    _SocketInfoLen = 0;
    _Type=-1;
    ++_InitCount;
}
+30 −30
Original line number Diff line number Diff line
@@ -52,21 +52,21 @@ netplus::tcp::tcp(const char* uxsocket,int maxconnections,int sockopts) : socket
    if(sockopts == -1)
        sockopts=SO_REUSEADDR;
    _Maxconnections=maxconnections;
    _SocketPtr = new struct sockaddr_un;
    memset(_SocketPtr,0,sizeof(struct sockaddr_un));
    ((struct sockaddr_un *)_SocketPtr)->sun_family = AF_UNIX;
    _SocketInfo = new struct sockaddr_un;
    memset(_SocketInfo,0,sizeof(struct sockaddr_un));
    ((struct sockaddr_un *)_SocketInfo)->sun_family = AF_UNIX;
    if(!uxsocket){
        exception[NetException::Critical] << "Can't copy Server UnixSocket";
        throw exception;
    }
    _UxPath=uxsocket;
    memcpy(((struct sockaddr_un *)_SocketPtr)->sun_path,uxsocket,strlen(uxsocket)+1);
    memcpy(((struct sockaddr_un *)_SocketInfo)->sun_path,uxsocket,strlen(uxsocket)+1);

    if ((_Socket=::socket(AF_UNIX,SOCK_STREAM, 0)) < 0){
        exception[NetException::Critical] << "Can't create TCP UnixSocket";
        throw exception;
    }
    _SocketPtrSize=sizeof(sockaddr_un);
    _SocketInfoSize=sizeof(sockaddr_un);
    setsockopt(_Socket,SOL_SOCKET,sockopts,&optval, sizeof(optval));
    _Type=sockettype::TCP;
}
@@ -100,17 +100,17 @@ netplus::tcp::tcp(const char* addr, int port,int maxconnections,int sockopts) :
        throw exception;
    }

    _SocketPtr=nullptr;
    _SocketInfo=nullptr;

    for (rp = result; rp != nullptr; rp = rp->ai_next) {
        _Socket = ::socket(rp->ai_family, rp->ai_socktype,
                           rp->ai_protocol);
        if (_Socket == -1)
            continue;
        _SocketPtr = ::malloc(rp->ai_addrlen);
        memset(_SocketPtr, 0, rp->ai_addrlen);
        memcpy(_SocketPtr,rp->ai_addr,rp->ai_addrlen);
        _SocketPtrSize=rp->ai_addrlen;
        _SocketInfo = ::malloc(rp->ai_addrlen);
        memset(_SocketInfo, 0, rp->ai_addrlen);
        memcpy(_SocketInfo,rp->ai_addr,rp->ai_addrlen);
        _SocketInfoSize=rp->ai_addrlen;

        break;
    }
@@ -125,20 +125,20 @@ netplus::tcp::tcp(const char* addr, int port,int maxconnections,int sockopts) :
netplus::tcp::~tcp(){
    if(_Socket!=-1)
        ::close(_Socket);
    ::free(_SocketPtr);
    ::free(_SocketInfo);
}

netplus::tcp::tcp() : socket() {
    _SocketPtr=::malloc(sizeof(struct sockaddr));
    _SocketPtrSize=sizeof(struct sockaddr);
    _SocketInfo=::malloc(sizeof(struct sockaddr));
    _SocketInfoSize=sizeof(struct sockaddr);
    _Socket=-1;
    _Type=sockettype::TCP;
}

netplus::tcp::tcp(SOCKET sock) : socket(){
    _SocketPtr=::malloc(sizeof(struct sockaddr));
    _SocketPtrSize=sizeof(struct sockaddr);
     ((struct sockaddr*)_SocketPtr)->sa_family=AF_UNSPEC;
    _SocketInfo=::malloc(sizeof(struct sockaddr));
    _SocketInfoSize=sizeof(struct sockaddr);
     ((struct sockaddr*)_SocketInfo)->sa_family=AF_UNSPEC;
    _Socket=sock;
    _Type=sockettype::TCP;
}
@@ -176,14 +176,14 @@ void netplus::tcp::accept(tcp *csock){
        exception[etype] << "Can't accept on Socket: " << errstr;
        throw exception;
    }
    csock->_SocketPtrSize = myaddrlen;
    csock->_SocketPtr = malloc(myaddrlen);
    memcpy(csock->_SocketPtr,&myaddr,myaddrlen);
    csock->_SocketInfoSize = myaddrlen;
    csock->_SocketInfo = malloc(myaddrlen);
    memcpy(csock->_SocketInfo,&myaddr,myaddrlen);
}

void netplus::tcp::bind(){
    NetException exception;
    if (::bind(_Socket,((const struct sockaddr *)_SocketPtr), _SocketPtrSize) < 0){
    if (::bind(_Socket,((const struct sockaddr *)_SocketInfo), _SocketInfoSize) < 0){
        exception[NetException::Error] << "Can't bind Server Socket";
        throw exception;
    }
@@ -202,7 +202,7 @@ size_t netplus::tcp::sendData(socket *csock, void* data, unsigned long size,int
                        data,
                        size,
                        flags,
                        (struct sockaddr *)csock->_SocketPtr,
                        (struct sockaddr *)csock->_SocketInfo,
                        sizeof(struct sockaddr)
                     );
    if(rval<0){
@@ -235,8 +235,8 @@ size_t netplus::tcp::recvData(socket *csock, void* data, unsigned long size,int
                            data,
                            size,
                            flags,
                            (struct sockaddr *)csock->_SocketPtr,
                            (socklen_t*)&csock->_SocketPtrSize
                            (struct sockaddr *)csock->_SocketInfo,
                            (socklen_t*)&csock->_SocketInfoSize
                         );
    if(recvsize<0){
        int etype=NetException::Error;
@@ -258,9 +258,9 @@ size_t netplus::tcp::recvData(socket *csock, void* data, unsigned long size,int
void netplus::tcp::connect(socket *ssock){
    NetException exception;

    ((struct sockaddr*)_SocketPtr)->sa_family=((struct sockaddr*)ssock->_SocketPtr)->sa_family;
    ((struct sockaddr*)_SocketInfo)->sa_family=((struct sockaddr*)ssock->_SocketInfo)->sa_family;

    if( (_Socket=::socket(((struct sockaddr*)_SocketPtr)->sa_family,SOCK_STREAM,0) ) < 0 ){
    if( (_Socket=::socket(((struct sockaddr*)_SocketInfo)->sa_family,SOCK_STREAM,0) ) < 0 ){
        NetException exception;
        char errstr[512];
        strerror_r_netplus(errno,errstr,512);
@@ -268,7 +268,7 @@ void netplus::tcp::connect(socket *ssock){
        throw exception;
    }

    if ( ::connect(_Socket,(struct sockaddr*)ssock->_SocketPtr,ssock->_SocketPtrSize) < 0) {
    if ( ::connect(_Socket,(struct sockaddr*)ssock->_SocketInfo,ssock->_SocketInfoSize) < 0) {

        char errstr[512];
        strerror_r_netplus(errno,errstr,512);
@@ -280,12 +280,12 @@ void netplus::tcp::connect(socket *ssock){


void netplus::tcp::getAddress(std::string &addr){
    if(!_SocketPtr)
    if(!_SocketInfo)
        return;
    char ipaddr[INET6_ADDRSTRLEN];
    if(((struct sockaddr*)_SocketPtr)->sa_family==AF_INET6)
        inet_ntop(AF_INET6, &(((struct sockaddr_in6*)_SocketPtr)->sin6_addr), ipaddr, INET6_ADDRSTRLEN);
    if(((struct sockaddr*)_SocketInfo)->sa_family==AF_INET6)
        inet_ntop(AF_INET6, &(((struct sockaddr_in6*)_SocketInfo)->sin6_addr), ipaddr, INET6_ADDRSTRLEN);
    else
        inet_ntop(AF_INET, &((struct sockaddr_in*)_SocketPtr)->sin_addr, ipaddr, INET_ADDRSTRLEN);
        inet_ntop(AF_INET, &((struct sockaddr_in*)_SocketInfo)->sin_addr, ipaddr, INET_ADDRSTRLEN);
    addr=ipaddr;
}
+27 −27
Original line number Diff line number Diff line
@@ -47,9 +47,9 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "error.h"

netplus::udp::udp() : socket() {
    _SocketPtr=::malloc(sizeof(sockaddr));
    _SocketPtrSize=sizeof(sockaddr);
    ((struct sockaddr*)_SocketPtr)->sa_family=AF_UNSPEC;
    _SocketInfo=::malloc(sizeof(sockaddr));
    _SocketInfoSize=sizeof(sockaddr);
    ((struct sockaddr*)_SocketInfo)->sa_family=AF_UNSPEC;
    _Socket=-1;
    _Type=sockettype::UDP;
}
@@ -61,21 +61,21 @@ netplus::udp::udp(const char* uxsocket,int maxconnections,int sockopts) : socket
        sockopts=SO_REUSEADDR;
    _Maxconnections=maxconnections;

    _SocketPtr = new struct sockaddr_un;
    memset(_SocketPtr,0,sizeof(struct sockaddr_un));
    ((struct sockaddr_un *)_SocketPtr)->sun_family = AF_UNIX;
    _SocketInfo = new struct sockaddr_un;
    memset(_SocketInfo,0,sizeof(struct sockaddr_un));
    ((struct sockaddr_un *)_SocketInfo)->sun_family = AF_UNIX;
    if(!uxsocket){
        exception[NetException::Critical] << "Can't copy Server UnixSocket";
        throw exception;
    }
    _UxPath=uxsocket;
    memcpy(((struct sockaddr_un *)_SocketPtr)->sun_path,uxsocket,strlen(uxsocket)+1);
    memcpy(((struct sockaddr_un *)_SocketInfo)->sun_path,uxsocket,strlen(uxsocket)+1);

    if ((_Socket=::socket(AF_UNIX,SOCK_DGRAM, IPPROTO_UDP)) < 0){
        exception[NetException::Critical] << "Can't create UDP UnixSocket";
        throw exception;
    }
    _SocketPtrSize=sizeof(sockaddr_un);
    _SocketInfoSize=sizeof(sockaddr_un);
    setsockopt(_Socket,SOL_SOCKET,sockopts,&optval, sizeof(optval));
    _Type=sockettype::UDP;
}
@@ -112,10 +112,10 @@ netplus::udp::udp(const char* addr, int port,int maxconnections,int sockopts) :
                            rp->ai_protocol);
        if (_Socket == -1)
            continue;
        _SocketPtr = malloc(rp->ai_addrlen);
        memset(_SocketPtr, 0, rp->ai_addrlen);
        _SocketPtrSize=rp->ai_addrlen;
        memcpy(_SocketPtr,rp->ai_addr,rp->ai_addrlen);
        _SocketInfo = malloc(rp->ai_addrlen);
        memset(_SocketInfo, 0, rp->ai_addrlen);
        _SocketInfoSize=rp->ai_addrlen;
        memcpy(_SocketInfo,rp->ai_addr,rp->ai_addrlen);
        break;
    }

@@ -129,13 +129,13 @@ netplus::udp::udp(const char* addr, int port,int maxconnections,int sockopts) :
netplus::udp::~udp(){
    if(_Socket>=0)
        ::close(_Socket);
    ::free(_SocketPtr);
    ::free(_SocketInfo);
}

netplus::udp::udp(SOCKET sock) : socket() {
    _SocketPtr=::malloc(sizeof(sockaddr));
    _SocketPtrSize=sizeof(sockaddr);
    ((struct sockaddr*)_SocketPtr)->sa_family=AF_UNSPEC;
    _SocketInfo=::malloc(sizeof(sockaddr));
    _SocketInfoSize=sizeof(sockaddr);
    ((struct sockaddr*)_SocketInfo)->sa_family=AF_UNSPEC;
    _Socket=sock;
    _Type=sockettype::UDP;
}
@@ -167,14 +167,14 @@ void netplus::udp::accept(socket *csock){
        exception[NetException::Error] << "Can't accept on Socket";
        throw exception;
    }
    csock->_SocketPtrSize = myaddrlen;
    csock->_SocketPtr = malloc(myaddrlen);
    memcpy(csock->_SocketPtr,&myaddr,myaddrlen);
    csock->_SocketInfoSize = myaddrlen;
    csock->_SocketInfo = malloc(myaddrlen);
    memcpy(csock->_SocketInfo,&myaddr,myaddrlen);
}

void netplus::udp::bind(){
    NetException exception;
    if (::bind(_Socket,((const struct sockaddr *)_SocketPtr), _SocketPtrSize) < 0){
    if (::bind(_Socket,((const struct sockaddr *)_SocketInfo), _SocketInfoSize) < 0){
        exception[NetException::Error] << "Can't bind Server Socket";
        throw exception;
    }
@@ -239,9 +239,9 @@ size_t netplus::udp::recvData(socket *csock, void* data, unsigned long size,int
void netplus::udp::connect(socket *ssock){
    NetException exception;

    ((struct sockaddr*)_SocketPtr)->sa_family=((struct sockaddr*)ssock->_SocketPtr)->sa_family;
    ((struct sockaddr*)_SocketInfo)->sa_family=((struct sockaddr*)ssock->_SocketInfo)->sa_family;

    if( (_Socket=::socket(((struct sockaddr*)_SocketPtr)->sa_family,SOCK_DGRAM,0) ) < 0 ){
    if( (_Socket=::socket(((struct sockaddr*)_SocketInfo)->sa_family,SOCK_DGRAM,0) ) < 0 ){
        NetException exception;
        char errstr[512];
        strerror_r_netplus(errno,errstr,512);
@@ -249,7 +249,7 @@ void netplus::udp::connect(socket *ssock){
        throw exception;
    }

    if ( ::connect(_Socket,(struct sockaddr*)ssock->_SocketPtr,ssock->_SocketPtrSize) < 0) {
    if ( ::connect(_Socket,(struct sockaddr*)ssock->_SocketInfo,ssock->_SocketInfoSize) < 0) {
        char errstr[512];
        strerror_r_netplus(errno,errstr,512);

@@ -259,13 +259,13 @@ void netplus::udp::connect(socket *ssock){
}

void netplus::udp::getAddress(std::string &addr){
    if(!_SocketPtr)
    if(!_SocketInfo)
        return;
    char ipaddr[INET6_ADDRSTRLEN];
    if(((struct sockaddr*)_SocketPtr)->sa_family==AF_INET6)
        inet_ntop(AF_INET6, &(((struct sockaddr_in6*)_SocketPtr)->sin6_addr), ipaddr, INET6_ADDRSTRLEN);
    if(((struct sockaddr*)_SocketInfo)->sa_family==AF_INET6)
        inet_ntop(AF_INET6, &(((struct sockaddr_in6*)_SocketInfo)->sin6_addr), ipaddr, INET6_ADDRSTRLEN);
    else
        inet_ntop(AF_INET, &((struct sockaddr_in*)_SocketPtr)->sin_addr, ipaddr, INET_ADDRSTRLEN);
        inet_ntop(AF_INET, &((struct sockaddr_in*)_SocketInfo)->sin_addr, ipaddr, INET_ADDRSTRLEN);
    addr=ipaddr;
}

+4 −3
Original line number Diff line number Diff line
@@ -37,9 +37,10 @@
typedef int SOCKET;
typedef unsigned long long ULONG_PTR;
#else
#include <winsock.h>
#include <WinSock2.h>
#endif


namespace netplus {
        enum sockettype {TCP=0,UDP=1,SSL=2};

@@ -68,8 +69,8 @@ namespace netplus {

            virtual socket&          operator=(SOCKET sock)=0;

            void                    *_SocketPtr;
            size_t                   _SocketPtrSize;
            struct addrinfo          _SocketInfo;
            int                      _SocketInfoLen;
            SOCKET                   _Socket;
            int                      _Type;
            ULONG_PTR                _Extension;
Loading