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

fixed windows

parent eda39d4d
Loading
Loading
Loading
Loading
+76 −154
Original line number Diff line number Diff line
@@ -25,61 +25,6 @@ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*******************************************************************************/

#include <iostream>
#include <algorithm>
#include <chrono>
#include <mutex>
#include <cstring>

#include <signal.h>
#include <stdlib.h>
#include <stdint.h>

#include <winsock2.h>
#include <ws2ipdef.h>
#include <mswsock.h>
#include <strsafe.h>

#include <errno.h>

#include "socket.h"
#include "exception.h"
#include "eventapi.h"
#include "connection.h"
#include <assert.h>

#define READEVENT 0
#define SENDEVENT 1

#define BLOCKSIZE 16384

/*******************************************************************************
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.
*******************************************************************************/

#include <iostream>
#include <algorithm>
#include <chrono>
@@ -105,7 +50,6 @@ namespace netplus {
    std::atomic<bool> event::Running(true);
    std::atomic<bool> event::Restart(false);

    // Structure to track specific Overlapped I/O operations
    enum IO_OPERATION { OP_READ = 0, OP_WRITE = 1 };

    struct IO_CONTEXT {
@@ -120,25 +64,20 @@ namespace netplus {
        client(eventapi *eapi) : api(eapi) {
            api->CreateConnection(CurCon);

            // Initialize Read Context
            memset(&readCtx, 0, sizeof(IO_CONTEXT));
            readCtx.operation = OP_READ;
            readCtx.wsaBuf.buf = readCtx.buffer;
            readCtx.wsaBuf.len = BLOCKSIZE;

            // Initialize Write Context
            memset(&writeCtx, 0, sizeof(IO_CONTEXT));
            writeCtx.operation = OP_WRITE;
            writeCtx.wsaBuf.buf = writeCtx.buffer;
            writeCtx.wsaBuf.len = 0;
        }

        ~client() {}

        std::shared_ptr<con> CurCon;
        eventapi* api;
        std::mutex           cltmtx;

        IO_CONTEXT           readCtx;
        IO_CONTEXT           writeCtx;
    };
@@ -154,15 +93,8 @@ namespace netplus {

    class EventWorker {
    public:
class EventWorker {
public:
    /**
     * @brief Initiates an asynchronous read.
     * Uses the specialized WSA methods to interact with the IOCP.
     */
        static void start_read(client* ctx) {
            con& c = *ctx->CurCon;
        // Temporary buffer for the overlapped operation
            buffer buf(ctx->readCtx.buffer, BLOCKSIZE);

            if (c.csock->_Type == sockettype::TCP) {
@@ -170,20 +102,15 @@ public:
            } else if (c.csock->_Type == sockettype::UDP) {
                static_cast<udp*>(c.csock.get())->recvDataWSA(buf, 0);
            } else if (c.csock->_Type == sockettype::SSL) {
            // SSL recvDataWSA prepares the internal _rx_netbuf for ciphertext
                static_cast<ssl*>(c.csock.get())->recvDataWSA(buf, 0);
            }
        }

    /**
     * @brief Initiates an asynchronous write.
     * For SSL, this triggers encryption before the WSASend call.
     */
        static void start_write(client* ctx) {
            con& c = *ctx->CurCon;
            if (c.SendData.empty()) return;

        size_t toSend = std::min<size_t>(BLOCKSIZE, c.SendData.size());
            size_t toSend = (std::min)((size_t)BLOCKSIZE, c.SendData.size());
            buffer out(c.SendData.data(), toSend);

            if (c.csock->_Type == sockettype::TCP) {
@@ -201,7 +128,6 @@ public:
                ULONG_PTR lpContext = 0;
                OVERLAPPED* pOverlapped = nullptr;

            // Wait for completion from the port
                BOOL bReturn = GetQueuedCompletionStatus(eargs->eviocp, &dwBytesTransfered,
                                                        &lpContext, &pOverlapped, eargs->timeout);

@@ -211,7 +137,6 @@ public:
                IO_CONTEXT* pIoCtx = CONTAINING_RECORD(pOverlapped, IO_CONTEXT, overlapped);
                con& c = *pClientContext->CurCon;

            // Handle Disconnection or Reset (Fixes PR_CONNECT_RESET_ERROR logic)
                if (!bReturn || (bReturn && dwBytesTransfered == 0)) {
                    eargs->event->DisconnectEvent(c, tid, (ULONG_PTR)eargs->args);
                    delete pClientContext;
@@ -224,8 +149,6 @@ public:
                    if (pIoCtx->operation == OP_READ) {
                        if (c.csock->_Type == sockettype::SSL) {
                            ssl* sslSocket = static_cast<ssl*>(c.csock.get());

                        // Bridge: Feed ciphertext from network to SSL engine for decryption
                            buffer plain(BLOCKSIZE);
                            size_t decrypted = sslSocket->recvData(plain, 0);

@@ -234,18 +157,18 @@ public:
                                eargs->event->RequestEvent(c, tid, (ULONG_PTR)eargs->args);
                            }
                        } else {
                        // Plaintext TCP/UDP
                            c.RecvData.append(pIoCtx->buffer, dwBytesTransfered);
                            eargs->event->RequestEvent(c, tid, (ULONG_PTR)eargs->args);
                        }

                    // Continue Read/Write cycle
                        if (!c.SendData.empty()) start_write(pClientContext);
                        else start_read(pClientContext);

                    } else if (pIoCtx->operation == OP_WRITE) {
                    // Update buffer: for SSL, dwBytesTransfered is the encrypted wire size
                    c.SendData.erase(0, dwBytesTransfered);
                        // FIXED: Use iterators for erasure to prevent compiler error
                        if (dwBytesTransfered > 0 && dwBytesTransfered <= c.SendData.size()) {
                            c.SendData.erase(c.SendData.begin(), c.SendData.begin() + dwBytesTransfered);
                        }

                        if (c.SendData.empty()) {
                            eargs->event->ResponseEvent(c, tid, (ULONG_PTR)eargs->args);
@@ -255,7 +178,6 @@ public:
                        else start_read(pClientContext);
                    }
                } catch (NetException& e) {
                // Ignore non-critical notes, disconnect on actual errors
                    if (e.getErrorType() != NetException::Note) {
                        eargs->event->DisconnectEvent(c, tid, (ULONG_PTR)eargs->args);
                        delete pClientContext;