Blitz3D+ Command Reference

UDPTimeouts recv_timeout

Parameters

recv_timeout - milliseconds RecvUDPMsg waits for a packet; 0 means don't wait at all (default)

Description

Sets how long RecvUDPMsg waits for a packet before giving up.

Out of the box the timeout is 0: RecvUDPMsg glances at the stream and returns immediately, packet or not. That is the right mode for a running game - poll once per loop and carry on. Set a timeout and every RecvUDPMsg becomes patient instead, waiting up to that many milliseconds for something to arrive before returning 0. If a packet is already waiting it is returned at once; the timeout is a maximum, not a delay.

A short timeout suits moments where the game has nothing better to do than listen - waiting for the host's go signal in a lobby, or a turn-based exchange - because it saves you writing the poll-and-Delay loop yourself. The example times the same call under both settings so you can see the difference in milliseconds.

Two things to keep in mind: the setting is global (it applies to every UDP stream, not just one), and it stays in force until you change it - so a generous lobby timeout left switched on will stall your main loop for that long on every quiet frame. Set it back to 0 when the game proper starts. The TCP streams have their own separate command, TCPTimeouts.

See also: RecvUDPMsg, CreateUDPStream, SendUDPMsg, TCPTimeouts.

Example

; UDPTimeouts Example
; -------------------

; This program times RecvUDPMsg on a quiet stream to show what
; UDPTimeouts changes, then delivers a real packet.

; The loopback address 127.0.0.1 as a packed integer
loopback=(127 Shl 24) Or 1

host=CreateUDPStream(5409)
If host=0 Then
    Print "Could not bind port 5409 (it may be in use)"
Else
    ; Default timeout is 0: RecvUDPMsg never waits, it just polls
    started=MilliSecs()
    r=RecvUDPMsg(host)
    Print "Timeout 0ms: RecvUDPMsg = "+r+" after "+(MilliSecs()-started)+"ms (instant)"

    ; UDPTimeouts makes every RecvUDPMsg wait up to that many
    ; milliseconds for a packet before giving up
    UDPTimeouts 400
    started=MilliSecs()
    r=RecvUDPMsg(host)
    Print "Timeout 400ms: RecvUDPMsg = "+r+" after "+(MilliSecs()-started)+"ms (waited)"

    ; With a packet already waiting, the timeout does not delay it
    player=CreateUDPStream()
    If player=0 Then
        Print "Player: could not create a UDP stream"
    Else
        WriteLine player,"No waiting for this one"
        SendUDPMsg player,loopback,5409

        started=MilliSecs()
        r=RecvUDPMsg(host)
        If r<>0 Then
            Print "With data: RecvUDPMsg = sender "+DottedIP$(r)+" after "+(MilliSecs()-started)+"ms"
            Print "Message: '"+ReadLine$(host)+"'"
        EndIf
        CloseUDPStream player
    EndIf

    ; Back to non-blocking polling for a typical game loop
    UDPTimeouts 0

    CloseUDPStream host
EndIf

Print ""
Print "Press any key to close the example"
WaitKey

End

Index