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

tons of fixes

parent adc36112
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -140,7 +140,7 @@ void netplus::udp::listen(){
    }
}

netplus::udp& netplus::udp::operator=(int sock){
netplus::udp& netplus::udp::operator=(SOCKET sock){
     _Socket=sock;
     return *this;
};
+1 −1
Original line number Diff line number Diff line
@@ -123,7 +123,7 @@ namespace netplus {
            void          bind();
            void          listen();

            udp&          operator=(int socket);
            udp&          operator=(SOCKET socket);

            int           getMaxconnections();

+16 −17
Original line number Diff line number Diff line
@@ -81,8 +81,8 @@ netplus::tcp::tcp(const char* addr, int port,int maxconnections,int sockopts) :
                           rp->ai_protocol,nullptr, 0, WSA_FLAG_OVERLAPPED);
        if (_Socket == -1)
            continue;
        memcpy(&_SocketInfo,rp->ai_addr,rp->ai_addrlen);
        _SocketInfoLen=rp->ai_addrlen;
        memcpy(&_SocketInfo,rp,sizeof(*rp));
        _SocketInfoLen = sizeof(rp);

        break;
    }
@@ -101,14 +101,14 @@ netplus::tcp::~tcp(){

netplus::tcp::tcp() : socket() {
    _SocketInfoLen=0;
    _SocketInfo.ai_family=AF_UNSPEC;
    ((struct addrinfo*)_SocketInfo)->ai_family=AF_UNSPEC;
    _Socket=-1;
    _Type=sockettype::TCP;
}

netplus::tcp::tcp(SOCKET sock) : socket() {
    _SocketInfoLen = 0;
    _SocketInfo.ai_family=AF_UNSPEC;
    ((struct addrinfo*)_SocketInfo)->ai_family=AF_UNSPEC;
    _Socket=sock;
    _Type=sockettype::TCP;
}
@@ -133,7 +133,8 @@ int netplus::tcp::getMaxconnections(){

void netplus::tcp::accept(socket *csock){
    NetException exception;
    csock->_Socket=::WSAAccept(_Socket,(struct sockaddr*)&csock->_SocketInfo,&csock->_SocketInfoLen, nullptr, 0);
    csock->_Socket=::WSAAccept(_Socket, ((struct addrinfo*)csock->_SocketInfo)->ai_addr, 
        (LPINT)((struct addrinfo*)csock->_SocketInfo)->ai_addrlen, nullptr, 0);
    if(csock->_Socket==SOCKET_ERROR){
        int etype=NetException::Error;
        if(GetLastError() ==WSA_IO_PENDING)
@@ -146,7 +147,7 @@ void netplus::tcp::accept(socket *csock){

void netplus::tcp::bind(){
    NetException exception;
    if (::bind(_Socket,(struct sockaddr*) &_SocketInfo, (int)_SocketInfoLen) < 0) {
    if (::bind(_Socket, ((struct addrinfo*)_SocketInfo)->ai_addr, (int)((struct addrinfo*)_SocketInfo)->ai_addrlen) < 0) {
        exception[NetException::Error] << "Can't bind Server Socket";
        throw exception;
    }
@@ -165,8 +166,8 @@ size_t netplus::tcp::sendData(socket *csock, void* data, unsigned long size,int
                 (char*)data,
                        size,
                        flags,
                        (struct sockaddr*)&csock->_SocketInfo,
                        csock->_SocketInfoLen
                        ((struct addrinfo*)csock->_SocketInfo)->ai_addr ,
                        ((struct addrinfo*)csock->_SocketInfo)->ai_addrlen
                     );
    if(rval == SOCKET_ERROR){
        int etype=NetException::Error;
@@ -193,8 +194,8 @@ size_t netplus::tcp::recvData(socket *csock, void* data, unsigned long size,int
                     (char*)data,
                            size,
                            flags,
                            (struct sockaddr*)&csock->_SocketInfo,
                            (int*)csock->_SocketInfoLen
                            ((struct addrinfo*)csock->_SocketInfo)->ai_addr,
                            (int*)&((struct addrinfo*)csock->_SocketInfo)->ai_addrlen
                         );
    if(recvsize<0){
        int etype=NetException::Error;
@@ -211,15 +212,16 @@ size_t netplus::tcp::recvData(socket *csock, void* data, unsigned long size,int
void netplus::tcp::connect(socket *ssock){
    NetException exception;

    _SocketInfo.ai_family=ssock->_SocketInfo.ai_family;
    ((struct addrinfo*)_SocketInfo)->ai_family = ((struct addrinfo*)ssock->_SocketInfo)->ai_family;

    if( (_Socket=::socket(_SocketInfo.ai_family,SOCK_STREAM,0) ) < 0 ){
    if( (_Socket=::socket(((struct addrinfo*)_SocketInfo)->ai_family,SOCK_STREAM,0) ) < 0 ){
        NetException exception;
        exception[NetException::Error] << "Create Socket " << (int) _Socket << " failed : " <<  GetLastError();
        throw exception;
    }

    if ( ::connect(_Socket,(struct sockaddr*)&ssock->_SocketInfo, (int)ssock->_SocketInfoLen) < 0) {
    if ( ::connect(_Socket, ((struct addrinfo*)ssock->_SocketInfo)->ai_addr, 
        (int)((struct addrinfo*)ssock->_SocketInfo)->ai_addrlen) < 0) {
        exception[NetException::Error] << "Socket connect: can't connect to server aborting " << " ErrorMsg:" << GetLastError();
        throw exception;
    }
@@ -228,9 +230,6 @@ void netplus::tcp::connect(socket *ssock){

void netplus::tcp::getAddress(std::string &addr){
    char ipaddr[INET6_ADDRSTRLEN];
    if(_SocketInfo.ai_family==AF_INET6)
       inet_ntop(AF_INET6,_SocketInfo.ai_addr, ipaddr, INET6_ADDRSTRLEN);
    else
       inet_ntop(AF_INET,_SocketInfo.ai_addr, ipaddr, INET_ADDRSTRLEN);
    inet_ntop(AF_INET6, ((struct addrinfo*)_SocketInfo)->ai_addr, ipaddr, ((struct addrinfo*)_SocketInfo)->ai_addrlen);
    addr=ipaddr;
}
 No newline at end of file
+105 −95
Original line number Diff line number Diff line
@@ -28,8 +28,9 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <chrono>
#include <thread>
#include <vector>
#include <cstring>

#include <cstdio>
#include <cstring>

extern "C" {
#include <winsock2.h>
@@ -40,18 +41,9 @@ extern "C" {
#include "exception.h"
#include "socket.h"

#include "config.h"

netplus::udp::udp() : socket() {
    _SocketInfoLen=0;
    _Socket = INVALID_SOCKET;
    _Type=sockettype::UDP;
    _Maxconnections = 1024;
}

netplus::udp::udp(const char* uxsocket, int maxconnections, int sockopts) : socket() {
    NetException exception;
    exception[NetException::Critical] << "udp: Windows doesn't support UnixSockets !";
    exception[NetException::Critical] << "tcp: Windows doesn't support UnixSockets !";
    throw exception;
}

@@ -61,6 +53,8 @@ netplus::udp::udp(const char* addr, int port,int maxconnections,int sockopts) :
    if (sockopts == -1)
        sockopts = SO_REUSEADDR;



    struct addrinfo hints, * result, * rp;

    memset(&hints, 0, sizeof(hints));
@@ -72,41 +66,52 @@ netplus::udp::udp(const char* addr, int port,int maxconnections,int sockopts) :
    hints.ai_addr = NULL;
    hints.ai_next = NULL;

    SOCKET tsock;
    int tsock;

    char serv[512];
    snprintf(serv, 512, "%d", port);

    if ((tsock=getaddrinfo(addr, serv,&hints,&result)) < 0) {
    if ((tsock = ::getaddrinfo(addr, serv, &hints, &result)) < 0) {
        exception[NetException::Critical] << "Socket Invalid address/ Address not supported";
        throw exception;
    }

    for (rp = result; rp != NULL; rp = rp->ai_next) {
        _Socket = ::socket(rp->ai_family, rp->ai_socktype,
                            rp->ai_protocol);
    for (rp = result; rp != nullptr; rp = rp->ai_next) {
        _Socket = ::WSASocketW(rp->ai_family, rp->ai_socktype,
            rp->ai_protocol, nullptr, 0, WSA_FLAG_OVERLAPPED);
        if (_Socket == -1)
            continue;
        _SocketInfoLen=rp->ai_addrlen;
        memcpy(&_SocketInfo,rp->ai_addr,rp->ai_addrlen);
        memcpy(&_SocketInfo, rp, sizeof(*rp));
        _SocketInfoLen = sizeof(rp);

        break;
    }

    freeaddrinfo(result);
    ::freeaddrinfo(result);

    char* optval = nullptr;
    setsockopt(_Socket, SOL_SOCKET, sockopts, optval, sizeof(optval));
    _Type = sockettype::UDP;
}

netplus::udp::~udp() {

    if (_Socket >= 0)
        ::closesocket(_Socket);
}

netplus::udp::udp(SOCKET sock) : socket() {
netplus::udp::udp() : socket() {
    _SocketInfoLen = 0;
    _SocketInfo.ai_family=AF_UNSPEC;
    _Socket=sock;
    ((struct addrinfo*)_SocketInfo)->ai_family = AF_UNSPEC;
    _Socket = -1;
    _Type = sockettype::UDP;
}

netplus::udp::udp(SOCKET sock) : socket() {
    _SocketInfoLen = 0;
    ((struct addrinfo*)_SocketInfo)->ai_family = AF_UNSPEC;
    _Socket = sock;
    _Type = sockettype::TCP;
}

void netplus::udp::listen() {
    NetException exception;
@@ -116,27 +121,33 @@ void netplus::udp::listen(){
    }
}

netplus::udp& netplus::udp::operator=(int sock){
netplus::udp& netplus::udp::operator=(SOCKET sock) {
    _Socket = sock;
    return *this;
};


int netplus::udp::getMaxconnections() {
    return _Maxconnections;
}

void netplus::udp::accept(socket* csock) {
    NetException exception;
    *csock = ::accept(_Socket,(struct sockaddr*) &csock->_SocketInfo, &csock->_SocketInfoLen);
    if(csock->_Socket<0){
        exception[NetException::Error] << "Can't accept on Socket";
    csock->_Socket = ::WSAAccept(_Socket, ((struct addrinfo*)csock->_SocketInfo)->ai_addr,
        (LPINT)((struct addrinfo*)csock->_SocketInfo)->ai_addrlen, nullptr, 0);
    if (csock->_Socket == SOCKET_ERROR) {
        int etype = NetException::Error;
        if (GetLastError() == WSA_IO_PENDING)
            etype = NetException::Note;

        exception[etype] << "Can't accept on Socket: " << GetLastError();
        throw exception;
    }
}

void netplus::udp::bind() {
    NetException exception;
    if (::bind(_Socket,(struct sockaddr*)&_SocketInfo,_SocketInfoLen) < 0){
    if (::bind(_Socket, ((struct addrinfo*)_SocketInfo)->ai_addr, (int)((struct addrinfo*)_SocketInfo)->ai_addrlen) < 0) {
        exception[NetException::Error] << "Can't bind Server Socket";
        throw exception;
    }
@@ -148,20 +159,21 @@ size_t netplus::udp::sendData(socket *csock, void* data, unsigned long size){
}

size_t netplus::udp::sendData(socket* csock, void* data, unsigned long size, int flags) {

    NetException exception;

    SOCKET rval = ::send(csock->_Socket,
        (char*)data,
        size,
        flags
    );
    if(rval<0){
    if (rval == SOCKET_ERROR) {
        int etype = NetException::Error;
        if (GetLastError() == WSA_IO_PENDING)
            etype = NetException::Note;

        exception[etype] << "Socket senddata failed on Socket: " << (int)csock->_Socket
            << " ErrorMsg: " << GetLastError();

        throw exception;
    }
    return (size_t)rval;
@@ -173,8 +185,10 @@ size_t netplus::udp::recvData(socket *csock, void* data, unsigned long size){
}

size_t netplus::udp::recvData(socket* csock, void* data, unsigned long size, int flags) {

    NetException exception;
    SOCKET recvsize=::recv(csock->_Socket,

    int recvsize = ::recv(csock->_Socket,
        (char*)data,
        size,
        flags
@@ -182,40 +196,36 @@ size_t netplus::udp::recvData(socket *csock, void* data, unsigned long size,int
    if (recvsize < 0) {
        int etype = NetException::Error;

        if(GetLastError() ==WSA_IO_PENDING)
        if (GetLastError() == EAGAIN)
            etype = NetException::Note;

        exception[etype] << "Socket recvData failed on Socket: " << (int)csock->_Socket
        exception[etype] << "Socket recvdata failed on Socket: " << (int)csock->_Socket
            << " ErrorMsg: " << GetLastError();
        throw exception;
    }
    return (size_t)recvsize;
}

void netplus::udp::connect(socket* ssock) {
    NetException exception;

    _SocketInfo.ai_family=ssock->_SocketInfo.ai_family;
    ((struct addrinfo*)_SocketInfo)->ai_family = ((struct addrinfo*)ssock->_SocketInfo)->ai_family;

    if( (_Socket=::socket(_SocketInfo.ai_family,SOCK_DGRAM,0) ) < 0 ){
    if ((_Socket = ::socket(((struct addrinfo*)_SocketInfo)->ai_family, SOCK_STREAM, 0)) < 0) {
        NetException exception;
        exception[NetException::Error] << "Create Socket " << (int)_Socket << " failed : " << GetLastError();
        throw exception;
    }

    if ( ::connect(_Socket,(struct sockaddr*) & ssock->_SocketInfo, ssock->_SocketInfoLen) < 0) {
    if (::connect(_Socket, ((struct addrinfo*)ssock->_SocketInfo)->ai_addr,
        (int)((struct addrinfo*)ssock->_SocketInfo)->ai_addrlen) < 0) {
        exception[NetException::Error] << "Socket connect: can't connect to server aborting " << " ErrorMsg:" << GetLastError();
        throw exception;
    }
}


void netplus::udp::getAddress(std::string& addr) {
    char ipaddr[INET6_ADDRSTRLEN];
    if(_SocketInfo.ai_family==AF_INET6)
        inet_ntop(AF_INET6, &_SocketInfo.ai_addr, ipaddr, INET6_ADDRSTRLEN);
    else
        inet_ntop(AF_INET, &_SocketInfo.ai_addr, ipaddr, INET_ADDRSTRLEN);
    inet_ntop(AF_INET6, ((struct addrinfo*)_SocketInfo)->ai_addr, ipaddr, ((struct addrinfo*)_SocketInfo)->ai_addrlen);
    addr = ipaddr;
}
 No newline at end of file