zpl-c / enet

⚡️ ENet reliable UDP networking library
https://blog.zpl.pw
MIT License
703 stars 60 forks source link

Client not connecting when it's a dll #54

Open SrMilton opened 5 months ago

SrMilton commented 5 months ago

The same Client code converted to a .dll instead of an .exe not connect to the server. I'm using Enet header only. Am I forgeting something? (It's connecting fine when it's an .exe)

#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <iostream>
#define ENET_IMPLEMENTATION
#include "../enet/enet.h"

#include <fstream>
#include <windows.h>

std::ofstream logFile("dll_log.txt");

extern "C" __declspec(dllexport) void RunENetClient() {

    if (enet_initialize() != 0) {
        logFile << "An error occurred while initializing ENet." << std::endl;
        return;
    }

    ENetHost* client = enet_host_create(NULL, 1, 2, 0, 0);
    if (client == NULL) {
        logFile << "An error occurred while trying to create an ENet client host." << std::endl;
        enet_deinitialize();
        return;
    }

    ENetAddress address;
    ENetEvent event;
    ENetPeer* peer;

    enet_address_set_host(&address, "127.0.0.1");
    address.port = 7777;

    peer = enet_host_connect(client, &address, 2, 0);
    if (peer == NULL) {
        logFile << "No available peers for initiating an ENet connection." << std::endl;
        enet_host_destroy(client);
        enet_deinitialize();
        return;
    }

    if (enet_host_service(client, &event, 5000) > 0 && event.type == ENET_EVENT_TYPE_CONNECT) {
        logFile << "Connection to some.server.net:1234 succeeded." << std::endl;
        std::cout << "Success";
    }
    else {
        enet_peer_reset(peer);
        logFile << "Connection to some.server.net:1234 failed." << std::endl;
        std::cout << "Fail"; //<<---------- Aways Fail
    }
    enet_host_service(client, &event, 5000);
    enet_peer_disconnect(peer, 0);

    uint8_t disconnected = false;
    while (enet_host_service(client, &event, 3000) > 0) {
        switch (event.type) {
        case ENET_EVENT_TYPE_RECEIVE:
            enet_packet_destroy(event.packet);
            break;
        case ENET_EVENT_TYPE_DISCONNECT:
            logFile << "Disconnection succeeded." << std::endl;
            disconnected = true;
            break;
        }
    }

    if (!disconnected) {
        enet_peer_reset(peer);
    }

    enet_host_destroy(client);
    enet_deinitialize();
}

BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
    switch (ul_reason_for_call) {
    case DLL_PROCESS_ATTACH:
        logFile.open("dll_log.txt", std::ios::out | std::ios::app);
        break;
    case DLL_PROCESS_DETACH:
        logFile.close();
        break;
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
        break;
    }
    return TRUE;
}
inlife commented 4 months ago

Most likely something is wrong with your codebase. Typically there are no problems with DLL/shared libs.

SrMilton commented 4 months ago

@inlife Enet Header Only is not working when client it's a dll, you can try the code by yourself. Legacy Enet from vcpkg is working fine.