Enet.c library not working correctly on mips system

140 Views Asked by At

I'm trying to create a function which uses enet to send some data, but first I wanted to make sure the example in here works correctly on the mips system.

I tested the example on Ubuntu and Mac and it works perfectly fine, but when I test it on the mips system it always fails to create the host, which is always created without any trouble on the other systems I tested. Do someone here knows what might be happening? Someone told me that it might be a problem with the libenet.so file, but since it works with the enet_initialize function, I think it might be something else.

This is my code if someone wants to check it out:

#define ENET_IMPLEMENTATION
#include <enet.h>
#include <stdio.h>

int main() {
    if (enet_initialize () != 0) {
        printf("An error occurred while initializing ENet.\n");
        return 1;
    } 
    else {
        printf("Welcome to enet! :D\n");
    }

    //Client side
    ENetHost* client = { 0 };
    client = enet_host_create(NULL /* create a client host */,
                              1 /* only allow 1 outgoing connection */,
                              2 /* allow up 2 channels to be used, 0 and 1 */,
                              0 /* assume any amount of incoming bandwidth */,
                              0 /* assume any amount of outgoing bandwidth */);
    if (client == NULL) {
        fprintf(stderr,
                "An error occurred while trying to create an ENet client host.\n");
        exit(EXIT_FAILURE);
    } 
    else {
        printf("Client created successfully! :D\n");
    }

    enet_host_destroy(server);
    enet_deinitialize();
    return 0; 
}

And my output is the messages: Welcome to enet! :D An error occurred while trying to create an ENet client host.

1

There are 1 best solutions below

0
Alex Riveron On

Try debugging it and stepping into the enet_host_create function to see where it's failing. It looks like the source code for that function is in the header file.

If you're not able to debug and step through it, you could temporarily modify the header file to add additional logging.

Some potential reasons it's failing:

  • peerCount > ENET_PROTOCOL_MAXIMUM_PEER_ID
  • enet_malloc malloc fails to allocate memory
  • net_socket_create(ENET_SOCKET_TYPE_DATAGRAM) == ENET_SOCKET_NULL

I'm thinking that it's probably not an issue with the peerCount because the default ENET_PROTOCOL_MAXIMUM_PEER_ID is 0xFFF and you're passing 1.

It may be an issue with enet_malloc or net_socket_create. By default enet_malloc is just the regular malloc from stdlib, which I'm guessing is what you're using since you're not initializing with enet_initialize_with_callbacks. And enet_socket_create just calls the socket system call:

ENetSocket enet_socket_create(ENetSocketType type) {
    return socket(PF_INET6, type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0);
}

I would make sure your platform's malloc and socket implementations are working as expected.