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

SDLNet_UDP_Recv hangs #4

Open
lukasschmit opened this issue Jul 3, 2017 · 1 comment
Open

SDLNet_UDP_Recv hangs #4

lukasschmit opened this issue Jul 3, 2017 · 1 comment

Comments

@lukasschmit
Copy link

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

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

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