Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client not connecting when it's a dll #54

Open
SrMilton opened this issue Jul 2, 2024 · 2 comments
Open

Client not connecting when it's a dll #54

SrMilton opened this issue Jul 2, 2024 · 2 comments

Comments

@SrMilton
Copy link

SrMilton commented Jul 2, 2024

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
Copy link
Member

inlife commented Jul 4, 2024

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

@SrMilton
Copy link
Author

SrMilton commented Jul 4, 2024

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants