Jump to content

UDPRecv timeout of 100ms


Recommended Posts

Hi,

If no UDP packet is received on the udpsocket, UDPRecv will hangs for 100ms.

Thereis a way for down the UDPRecv timeout ?

UDPStartup()
$socket = UDPBind("0.0.0.0",1234)
While 1
$startime = TimerInit()
$udprecv = UdpRecv($socket, 10)
ConsoleWrite("UDP Recv Time: " & _Timer_Diff($startime) & @CRLF)
Wend
UdpCloseSocket($socket)
UdpShutdown()

Thanks you

Link to comment
Share on other sites

Hi,

If no UDP packet is received on the udpsocket, UDPRecv will hangs for 100ms.

Thereis a way for down the UDPRecv timeout ?

Thanks you

You can check the winsock receiving functions on MSDN,

but I don't see any timeout options.

MSDN winsock reference: http://msdn.microsoft.com/en-us/library/ms740506%28v=VS.85%29.aspx

Use asynchronous sockets, or run the receiving loop in a separate process.

Asynchronous sockets UDF:

TCP UDF, Event driven! - based on Zatorg's asynchronous UDF:

A modified UDP only version of Zatorg's asynchronous UDF.

#include <WindowsConstants.au3>
#include <WinAPI.au3>
Opt("MustDeclareVars", 1)
Opt("TrayAutoPause", 0)
Global Const $FD_READ = 1, $AF_INET = 2, $SOCK_DGRAM = 2, $IPPROTO_UDP = 17
Global $UDPaddr = @IPAddress1, $UDPport = 514
Global $aUDPSocket[4] = [45454545, -1, $UDPaddr, $UDPport] ;Global array required for UDPRecv()
Global $sDataBuff, $hNotifyGUI = 0
Global $hWs2_32 = DllOpen("Ws2_32.dll")
OnAutoItExitRegister("_Exit")
; Start The UDP Services
If UDPStartup() = 0 Then
ConsoleWrite("UDPStartup() - Error starting UDP services - WSAStartup Code: " & @error & @CRLF)
Exit
EndIf
; Bind to an Asynchronous Socket
If Not _ASyncUDP($aUDPSocket, $hNotifyGUI) Then ;optionally use existing gui in 2nd param for registering messages
ConsoleWrite("_ASyncUDP() - Error binding to an Asynchronous Socket - WSAGetLastError Code: " & @extended & " Error: " & @error & @CRLF)
Exit
EndIf
 
Do
Until MsgBox(4096, "the cows come home...     ", "")
Exit
 
;/////////////////////////////////////
; Asynchronous UDP Socket Open
;/////////////////////////////////////
Func _ASyncUDP(ByRef $aSocket, ByRef $hGUIn)
Local $UDPMsg = $WM_USER + 5000, $Ret
$aSocket[1] = _ASocket() ; UDP only - returns socket
If @error Or $aSocket[1] = -1 Then Return SetError(@error, @extended, False)
If _WinAPI_GetClassName($hGUIn) <> "AutoIt v3 GUI" Then
  $hGUIn = GUICreate("UDP Notify");create a hidden gui for messages if valid handle not provided
  If IsHWnd($hGUIn) = 0 Then Return SetError(@error, @extended, False)
EndIf
$Ret = _ASockSelect($aSocket[1], $hGUIn, $UDPMsg, $FD_READ) ; returns 'True or False'
If @error Or $Ret = False Then Return SetError(@error, @extended, False)
If Not GUIRegisterMsg($UDPMsg, "_UDPNotify") Then Return SetError(@error, @extended, False)
$Ret = _ASockListen($aSocket[1], $aSocket[2], $aSocket[3]) ; returns 'True or False'
If @error Or @extended Or $Ret = False Then Return SetError(@error, @extended, False)
Return $Ret
EndFunc   ;==>_ASyncUDP
;/////////////////////////////////////
;/////////////////////////////////////
; Asynchronous UDP Socket Event Handler
;/////////////////////////////////////
Func _UDPNotify($hWnd, $Msg, $wParam, $lParam)
#forceref $hWnd, $Msg
If BitAND($lParam, 0xFFFF) = $FD_READ Then ; The event: $FD_READ = 1 data received
  Switch BitShift($lParam, 16); Error: If = 0 then event indicates a success
   Case 0
    $sDataBuff = UDPRecv($aUDPSocket, 1024)
    If @error Then ConsoleWrite("!Error: " & @error & " - WSAGetLastError" & _WSAGetLastError() & @CRLF)
    ConsoleWrite('+Syslog: ' & $sDataBuff & @CRLF)
   Case Else
    ConsoleWrite("!Error while attempting to receive data - Code: " & BitShift($lParam, 16) & @CRLF)
  EndSwitch
EndIf
EndFunc   ;==>_UDPNotify
Func _Exit()
UDPCloseSocket($aUDPSocket)
UDPShutdown()
DllClose($hWs2_32)
EndFunc   ;==>_Exit
 
 
;AsockUDP.au3
;UDP only edited version of Zatorgs Asynchronous sockets UDF
;XP/Vista x86 tested only
;Asynchronous sockets UDF
;http://www.autoitscript.com/forum/topic/45189-asynchronous-sockets-udf/
;//////////////////////////////////////////////////////////////
Func _ASocket() ; UDP only
If $hWs2_32 = -1 Then $hWs2_32 = DllOpen("Ws2_32.dll")
Local $hSocket = DllCall($hWs2_32, "uint", "socket", "int", $AF_INET, "int", $SOCK_DGRAM, "int", $IPPROTO_UDP)
If @error Then Return SetError(1, @error, -1)
If $hSocket[0] = -1 Then Return SetError(2, _WSAGetLastError(), -1)
Return $hSocket[0]
EndFunc   ;==>_ASocket
;//////////////////////////////////////////////////////////////
;//////////////////////////////////////////////////////////////
Func _ASockSelect($hSocket, $hWnd, $uiMsg, $iEvent)
If $hWs2_32 = -1 Then $hWs2_32 = DllOpen("Ws2_32.dll")
Local $iRet = DllCall($hWs2_32, "int", "WSAAsyncSelect", "uint", $hSocket, "hwnd", $hWnd, "uint", $uiMsg, "int", $iEvent)
If @error Then Return SetError(1, @error, False)
If $iRet[0] <> 0 Then Return SetError(2, _WSAGetLastError(), False)
Return True
EndFunc   ;==>_ASockSelect
;//////////////////////////////////////////////////////////////
;//////////////////////////////////////////////////////////////
Func __SockAddr($sIP, $iPort)
Local $iRet, $tAddress
If $hWs2_32 = -1 Then $hWs2_32 = DllOpen("Ws2_32.dll")
$tAddress = DllStructCreate("short;ushort;uint;char[8]")
If @error Then Return SetError(1, @error, False)
$iRet = DllCall($hWs2_32, "ushort", "htons", "ushort", $iPort) ; ntohs
If @error Or $iRet[0] <= 0 Then Return SetError(2, @error, False)
DllStructSetData($tAddress, 1, $AF_INET)
DllStructSetData($tAddress, 2, $iRet[0])
$iRet = DllCall($hWs2_32, "uint", "inet_addr", "str", $sIP)
If @error Or $iRet[0] = 0xFFFFFFFF Then Return SetError(3, _WSAGetLastError(), False); INADDR_NONE
DllStructSetData($tAddress, 3, $iRet[0])
Return $tAddress
EndFunc   ;==>__SockAddr
;//////////////////////////////////////////////////////////////
;//////////////////////////////////////////////////////////////
Func _ASockListen($hSocket, $sIP, $uiPort)
Local $iRet, $tAddress
If $hWs2_32 = -1 Then $hWs2_32 = DllOpen("Ws2_32.dll")
Local $tAddress = __SockAddr($sIP, $uiPort)
If @error Then SetError(@error, @extended, False)
$iRet = DllCall($hWs2_32, "int", "bind", "uint", $hSocket, "ptr", DllStructGetPtr($tAddress), _
   "int", DllStructGetSize($tAddress))
If @error Then Return SetError(3, @error, False)
If $iRet[0] <> 0 Then Return SetError(4, _WSAGetLastError(), False)
Return True
EndFunc   ;==>_ASockListen
;//////////////////////////////////////////////////////////////
;//////////////////////////////////////////////////////////////
Func _WSAGetLastError()
;MSDN - Windows Sockets Error Codes
;http://msdn.microsoft.com/en-us/library/ms740668.aspx
If $hWs2_32 = -1 Then $hWs2_32 = DllOpen("Ws2_32.dll")
Local $iRet = DllCall($hWs2_32, "int", "WSAGetLastError")
If @error Then
  ;ConsoleWrite("+> _WSAGetLastError(): WSAGetLastError() failed. Script line number: " & @ScriptLineNumber & @CRLF)
  SetExtended(1)
  Return 0
EndIf
Return $iRet[0]
EndFunc   ;==>_WSAGetLastError
;//////////////////////////////////////////////////////////////

Edited by rover

I see fascists...

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...