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

test

parent a16b070e
Loading
Loading
Loading
Loading
+78 −55
Original line number Diff line number Diff line
@@ -153,6 +153,36 @@ namespace netplus {
    };

    class EventWorker {
    private:
        static void start_read(client* ctx) {
            if (!ctx || !ctx->CurCon->csock) return;

            con& c = *ctx->CurCon;
            // WICHTIG: Erzeuge ein temporäres buffer-Objekt für die WSA-Methoden
            buffer buf(ctx->readCtx.buffer, BLOCKSIZE);

            if (c.csock->_Type == sockettype::SSL) {
                static_cast<ssl*>(c.csock.get())->recvDataWSA(buf, 0);
            }
            else {
                static_cast<tcp*>(c.csock.get())->recvDataWSA(buf, 0);
            }
        }

        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());
            buffer out(c.SendData.data(), toSend);

            if (c.csock->_Type == sockettype::SSL) {
                static_cast<ssl*>(c.csock.get())->sendDataWSA(out, 0);
            }
            else {
                static_cast<tcp*>(c.csock.get())->sendDataWSA(out, 0);
            }
        }
    public:
        EventWorker(int tid, ULONG_PTR args, EventWorkerArgs* eargs) {
            while (event::Running) {
@@ -178,59 +208,51 @@ namespace netplus {

                try {
                    std::lock_guard<std::mutex> guard(pClientContext->cltmtx);

                    if (pIoCtx->operation == OP_READ) {
                        if (c.csock->_Type == sockettype::SSL) {
							// CAST required because recvData in SSL class handles the decryption state
                            ssl* sslSocket = static_cast<ssl*>(c.csock.get());

							// 1. Prepare a buffer for the plaintext
                            buffer plain(BLOCKSIZE);

							// 2. This call takes the ciphertext (automatically filled by IOCP into _rx_netbuf)
							// and decrypts it into our 'plain' buffer.
                            size_t decrypted = sslSocket->recvData(plain, 0);

                            if (decrypted > 0) {
                                c.RecvData.append(plain.data.buf, decrypted);
                                eargs->event->RequestEvent(c, tid, args);
                            }

							// 3. Request more ciphertext from the wire
							sslSocket->recvDataWSA(c.RecvData, 0);
						} else {
							// Standard TCP: Direct append and re-read
                            // Prepare fresh buffer for next overlapped read
                            buffer readBuf(pClientContext->readCtx.buffer, BLOCKSIZE);
                            sslSocket->recvDataWSA(readBuf, 0);
                        }
                        else {
                            c.RecvData.append(pIoCtx->buffer, dwBytesTransfered);
                            eargs->event->RequestEvent(c, tid, args);
							static_cast<tcp*>(c.csock.get())->recvDataWSA(c.RecvData, 0);
                            buffer readBuf(pClientContext->readCtx.buffer, BLOCKSIZE);
                            static_cast<tcp*>(c.csock.get())->recvDataWSA(readBuf, 0);
                        }
                    }
                    } else if (pIoCtx->operation == OP_WRITE) {
						// 1. Remove the bytes that were successfully sent from the SendData buffer
						// For TCP/UDP, dwBytesTransfered represents the raw bytes on the wire.
						// For SSL, sendDataWSA handles the internal plaintext tracking.
						c.SendData.erase(0, dwBytesTransfered);
                    else if (pIoCtx->operation == OP_WRITE) {
                        // Correct iterator-based erasure
                        if (dwBytesTransfered > 0)
                            c.SendData.erase(c.SendData.begin(), c.SendData.begin() + dwBytesTransfered);

						// 2. Trigger ResponseEvent if the buffer is now empty to let the user add more data
                        if (c.SendData.empty()) {
							eargs->event->ResponseEvent(c, tid, (ULONG_PTR)eargs->args);
                            eargs->event->ResponseEvent(c, tid, args);
                        }
						// 3. Continue the write loop if there is remaining data

                        if (!c.SendData.empty()) {
							// Prepare a buffer object for the next chunk
							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);

							// 4. Use the specialized WSA methods based on socket type
                            if (c.csock->_Type == sockettype::TCP) {
                                static_cast<tcp*>(c.csock.get())->sendDataWSA(out, 0);
							} else if (c.csock->_Type == sockettype::UDP) {
                            }
                            else if (c.csock->_Type == sockettype::UDP) {
                                static_cast<udp*>(c.csock.get())->sendDataWSA(out, 0);
							} else if (c.csock->_Type == sockettype::SSL) {
								// This method encrypts the plaintext in c.SendData and initiates a WSASend
                            }
                            else if (c.csock->_Type == sockettype::SSL) {
                                // Now correctly calling sendDataWSA
                                static_cast<ssl*>(c.csock.get())->sendDataWSA(out, 0);
                            }
						} else {
							// If nothing left to write, switch back to listening for data
							EventWorker::start_read(pClientContext);
                        }
                    }
                } catch (NetException& e) {
@@ -240,6 +262,7 @@ namespace netplus {
                }
            }
        }
		friend class event;
    };

    event::event(socket* serversocket, int timeout) : _ServerSocket(serversocket) {
+3 −3
Original line number Diff line number Diff line
@@ -1383,7 +1383,7 @@ size_t netplus::ssl::sendData(buffer& data, int flags) {
    if (data.size == 0) return 0;

    static constexpr size_t TLS_MAX_PLAINTEXT = 16384;
    const size_t take = std::min<size_t>(data.size, TLS_MAX_PLAINTEXT);
    const size_t take = (std::min<size_t>(data.size, TLS_MAX_PLAINTEXT));

    const uint8_t recordType = 0x17; // ApplicationData

@@ -1800,7 +1800,7 @@ size_t netplus::ssl::sendDataWSA(buffer &data, int flags) {
    return tcp::sendDataWSA(wsa_out, flags);
}

size_t netplus::ssl::recvData(buffer &data, int flags) {
size_t netplus::ssl::recvDataWSA(buffer &data, int flags) {
    // 1. Process ciphertext that was placed in _rx_netbuf by the IOCP
    if (!_rx_netbuf.empty()) {
        // Append new network data to our decryption record buffer
@@ -1824,7 +1824,7 @@ size_t netplus::ssl::recvData(buffer &data, int flags) {
            std::vector<uint8_t> plaintext = _decryptRecordCBC(type, version, fragment);

            // 4. Copy decrypted plaintext to the user-provided buffer
            size_t toCopy = std::min(data.size, plaintext.size());
            size_t toCopy = (std::min)(data.size, plaintext.size());
            memcpy(data.data.buf, plaintext.data(), toCopy);

            // Remove the processed record from the internal buffer