Does mod_fsock.fsock_setblock work in non Windows platform?

Issue #60 resolved
Darío Cutillas Carrillo created an issue

Running in Linux, I could not find any way to make fsock_setblock make UDP receiving non-blocking.

Private
    int socket;
    string from_ip;
    byte bytes[256];
    int port;
    int received_bytes;
End 
Begin
    fsock_init ( 0 );
    socket = udpsock_open ();
    // I have tried with fsock_setblock ( socket, 1 ) and fsock_setblock ( socket, 0 ) and fsock_setblock ( socket, -1 )
    say ( "Result setblock: " + fsock_setblock ( socket, 1 ) );
    fsock_bind ( socket, 11000 );

    timer [0] = 0;

    Loop
        If ( timer[ 0 ] > 300 )

            received_bytes = udpsock_recv ( socket, 
                &bytes, 
                3, 
                &from_ip, 
                &port );

            say ( "Received: " + received_bytes );
            say(bytes[0]);
            say(bytes[1]);
            say(bytes[2]);

            If ( received_bytes > 0 )
                // multiplayer_process_receive_frame ( &bytes );
            End

            timer[ 0 ] = 0;
        End

        Frame;
    End

OnExit:
    fsock_close(socket);
    fsock_quit ();
End

Comments (5)

  1. Darío Cutillas Carrillo reporter

    Modifying libsocket.c would let me achieve desired behaviour:

    int libsocket_setblock(int fd, u_long ulVal) {
    #ifdef WIN32
        return (ioctlsocket(fd, FIONBIO, &ulVal));
    #else
        //Original code: return (fcntl(fd, O_NONBLOCK, ulVal));
    
        int flags = fcntl(fd, F_GETFL, 0);
    
        if (flags < 0) 
            return -1;
    
        flags = (ulVal == 0) ? (flags &~O_NONBLOCK) : (flags|O_NONBLOCK);
    
        return fcntl(fd, F_SETFL, flags|O_NONBLOCK);
    #endif
    }
    

    Is this a bug? Or am I doing something wrong?

  2. Darío Cutillas Carrillo reporter

    Desired behaviour is that the loop executes no matter whether data has actually been sent.

  3. Darío Cutillas Carrillo reporter

    Perhaps makes more sense with the name of the function to have flags = (ulVal != 0) ? (flags &~O_NONBLOCK) : (flags|O_NONBLOCK);

  4. Log in to comment