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

wstartup included

parent cfcbf8cb
Loading
Loading
Loading
Loading
+1 −2
Original line number Diff line number Diff line
@@ -73,9 +73,8 @@ namespace netplus {
            int                 _Type;
            void               *_Extension;
#ifdef Windows
            WSADATA             _WSAData;
            static WSADATA     *_WSAData;
#endif // Windows

        };
        
        class tcp : public socket{
+23 −1
Original line number Diff line number Diff line
@@ -26,7 +26,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*******************************************************************************/

#include <chrono>
#include <thread>
#include <atomic>
#include <cstring>

#include <vector>
@@ -40,15 +40,37 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "socket.h"
#include "error.h"

#include "config.h"

#define HIDDEN __attribute__ ((visibility ("hidden")))

WSADATA* netplus::socket::_WSAData = nullptr;

namespace netplus {
    std::atomic<int> _Instance;
};

netplus::socket::socket(){
    _Socket=-1;
    _SocketPtr=nullptr;
    _Type=-1;
    ++_Instance;
    if (!_WSAData) {
        if (WSAStartup(MAKEWORD(2, 2), _WSAData) != 0) {
            NetException exception;
            exception[NetException::Critical] << "udp: WSAStartup failed: ";
        }
    }
}

netplus::socket::~socket(){
    --_Instance;
    int zero = 0;
    if (_Instance.compare_exchange_strong(zero, std::memory_order_release)) {
        WSACleanup();
        delete _WSAData;
        _WSAData = nullptr;
    }
}

void netplus::socket::setnonblocking(){
+6 −8
Original line number Diff line number Diff line
@@ -42,13 +42,11 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "socket.h"
#include "error.h"

#include "config.h"

netplus::udp::udp() {
    _SocketPtr=::malloc(sizeof(addrinfo));
    _SocketPtrSize=sizeof(addrinfo);
    if (WSAStartup(MAKEWORD(2, 2),&_WSAData) != 0) {
        NetException exception;
        exception[NetException::Critical] << "udp: WSAStartup failed: ";
    }
    _Socket = INVALID_SOCKET;
    _Type=sockettype::UDP;
}
@@ -67,7 +65,6 @@ netplus::udp::udp(const char* addr, int port,int maxconnections,int sockopts) {

    struct addrinfo hints,*result,*rp;


    memset(&hints, 0, sizeof(hints));
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_DGRAM;
@@ -102,8 +99,9 @@ netplus::udp::udp(const char* addr, int port,int maxconnections,int sockopts) {
}

netplus::udp::~udp(){

    if(_Socket>=0)
        ::close(_Socket);
        ::closesocket(_Socket);
    ::free(_SocketPtr);
}

@@ -167,7 +165,7 @@ unsigned int netplus::udp::sendData(socket *csock, void* data, unsigned long siz
unsigned int netplus::udp::sendData(socket *csock, void* data, unsigned long size,int flags){
    NetException exception;
    int rval=::send(csock->_Socket,
                        data,
                 (char*)data,
                        size,
                        flags
                     );
@@ -195,7 +193,7 @@ unsigned int netplus::udp::recvData(socket *csock, void* data, unsigned long siz
unsigned int netplus::udp::recvData(socket *csock, void* data, unsigned long size,int flags){
    NetException exception;
    int recvsize=::recv(csock->_Socket,
                            data,
                            (char*)data,
                            size,
                            flags
                         );