Commit 8c79b2d8 authored by jan.koester's avatar jan.koester
Browse files

test

parent be0d887f
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -94,6 +94,8 @@ namespace netplus {
            virtual void             accept(std::unique_ptr<socket> &csock)=0;
#ifdef Windows 
            virtual void             accept(LPFN_ACCEPTEX lpfnAcceptEx, std::unique_ptr<socket> &csock)=0;
            virtual size_t           sendDataWSA(buffer &data, int flags = 0)=0;
            virtual size_t           recvDataWSA(buffer &data, int flags = 0)=0;
#endif
            virtual void             bind()=0;
            virtual void             listen()=0;
@@ -145,6 +147,8 @@ namespace netplus {
            ~tcp();
#ifdef Windows
            void          accept(LPFN_ACCEPTEX lpfnAcceptEx, std::unique_ptr<socket> &csock);
            size_t        sendDataWSA(buffer &data, int flags = 0); // Renamed from sendData
            size_t        recvDataWSA(buffer &data, int flags = 0); // Renamed from recvData
#endif
            void          accept(std::unique_ptr <socket> &csock);
            void          bind();
@@ -179,6 +183,8 @@ namespace netplus {
            void          accept(std::unique_ptr<socket> &csock);
#ifdef Windows
            void          accept(LPFN_ACCEPTEX lpfnAcceptEx, std::unique_ptr<socket> &csock);
            size_t        sendDataWSA(buffer &data, int flags = 0); // Renamed from sendData
            size_t        recvDataWSA(buffer &data, int flags = 0); // Renamed from recvData
#endif
            void          bind();
            void          listen();
+33 −0
Original line number Diff line number Diff line
@@ -364,3 +364,36 @@ void netplus::tcp::getAddress(std::string& addr) {
        addr = "Invalid address"; // Handle errors from inet_ntop
    }
}

// tcp.cpp
size_t netplus::tcp::sendDataWSA(buffer& data, int flags) {
    NetException exception;
    _wsaBuf.buf = data.ptr ? (char*)data.data.ptr : data.data.buf;
    _wsaBuf.len = static_cast<ULONG>(data.size);

    DWORD dwBytes = 0;
    // Note: We do not reset _Overlapped here because it is managed by the IOCP context
    int rval = ::WSASend(_Socket, &_wsaBuf, 1, &dwBytes, flags, &_Overlapped, nullptr);

    if (rval == SOCKET_ERROR && WSAGetLastError() != WSA_IO_PENDING) {
        exception[NetException::Error] << "WSASend failed: " << GetLastError();
        throw exception;
    }
    return (size_t)dwBytes;
}

size_t netplus::tcp::recvDataWSA(buffer& data, int flags) {
    NetException exception;
    _wsaBuf.buf = data.data.buf;
    _wsaBuf.len = static_cast<ULONG>(data.size);

    DWORD dwBytes = 0;
    DWORD dwFlags = (DWORD)flags;
    int rval = ::WSARecv(_Socket, &_wsaBuf, 1, &dwBytes, &dwFlags, &_Overlapped, nullptr);

    if (rval == SOCKET_ERROR && WSAGetLastError() != WSA_IO_PENDING) {
        exception[NetException::Error] << "WSARecv failed: " << GetLastError();
        throw exception;
    }
    return (size_t)dwBytes;
}
+43 −0
Original line number Diff line number Diff line
@@ -362,3 +362,46 @@ void netplus::udp::getAddress(std::string& addr) {
		addr = "Invalid address"; // Handle errors from inet_ntop
	}
}

size_t netplus::udp::sendDataWSA(buffer& data, int flags) {
    NetException exception;

    // Assign buffer pointers to the Windows-specific WSABUF
    _wsaBuf.buf = data.ptr ? (char*)data.data.ptr : data.data.buf;
    _wsaBuf.len = static_cast<ULONG>(data.size);

    DWORD dwBytes = 0;
    // Initiate the overlapped send operation
    int rval = ::WSASend(_Socket, &_wsaBuf, 1, &dwBytes, flags, &_Overlapped, nullptr);

    if (rval == SOCKET_ERROR) {
        int lastError = WSAGetLastError();
        if (lastError != WSA_IO_PENDING) {
            exception[NetException::Error] << "UDP WSASend failed: " << lastError;
            throw exception;
        }
    }
    return (size_t)dwBytes;
}

size_t netplus::udp::recvDataWSA(buffer& data, int flags) {
    NetException exception;

    _wsaBuf.buf = data.data.buf;
    _wsaBuf.len = static_cast<ULONG>(data.size);

    DWORD dwBytes = 0;
    DWORD dwFlags = (DWORD)flags;

    // Initiate the overlapped receive operation
    int rval = ::WSARecv(_Socket, &_wsaBuf, 1, &dwBytes, &dwFlags, &_Overlapped, nullptr);

    if (rval == SOCKET_ERROR) {
        int lastError = WSAGetLastError();
        if (lastError != WSA_IO_PENDING) {
            exception[NetException::Error] << "UDP WSARecv failed: " << lastError;
            throw exception;
        }
    }
    return (size_t)dwBytes;
}