emscripten-ports / SDL2_net

Other
6 stars 4 forks source link

SDLNet_UDP_Recv hangs #4

Open lukasschmit opened 7 years ago

lukasschmit commented 7 years ago

It seems that SDLNet_UDP_Recv is a blocking call. I have tested building the following example both natively and with Emscripten. The native version immediately returns zero (as it should if no packets are incoming) however the Emscripten version just hangs until I manually terminate the program. I have tested this in chrome, safari, and firefox (all running on macOS Sierra).

#include <iostream>

#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_net.h>
#else
#include <SDL2/SDL.h>
#include <SDL2_net/SDL_net.h>
#endif

int main()
{
    // Init SDL
    std::cout << "Initializing SDL" << std::endl;
    if (SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        std::cerr << "Failed to init SDL: " << SDL_GetError() << std::endl;
        return -1;
    }
    std::cout << "Initialized SDL successfully" << std::endl;

    // Init SDL_net
    if (SDLNet_Init() != 0)
    {
        std::cerr << "failed to init SDL_net: " << SDLNet_GetError() << std::endl;
        return -1;
    }
    std::cout << "Initialized SDL_net successfully" << std::endl;

    // create udp socket
    UDPsocket sock = SDLNet_UDP_Open(6532);
    if (!sock)
    {
        std::cerr << "Failed to create server UDP socket: " << SDLNet_GetError()
                << std::endl;
        return -1;
    }
    std::cout << "Successfully created server UDP socket on port " << 2822 << std::endl;

    // create udp packet to be filled
    UDPpacket *packet;
    packet = SDLNet_AllocPacket(1024);
    if (!packet)
    {
        std::cerr << "Failed to create SDL packet" << std::endl;
        return -1;
    }
    std::cout << "made packet" << std::endl;

    std::cout << "starting receive" << std::endl;
    int status = SDLNet_UDP_Recv(sock, packet);
    std::cout << "finished receive: " << status << std::endl;

    SDLNet_UDP_Close(sock);
    sock = nullptr;
    SDLNet_FreePacket(packet);
    packet = nullptr;

    SDL_Quit();
    SDLNet_Quit();

    return 0;
}
fallenoak commented 5 years ago

@lukasschmit I'm seeing this behavior, too. Not quite sure where the fault lies, but SDLNet_UDP_Recv definitely blocks until a packet is received.