Changed parameter name in mysock.au3, to reflet the name change onRecv -> onPacketRecv

#include-once
; #INDEX# =======================================================================================================================
; Title .........: MySock
; AutoIt Version : 3.3.8.1
; Description ...: WinSockets simple wrapper
; Author(s) .....: Matwachich
; Dll ...........: mysock.dll
; Remarks .......: Limitation of the AutoIt wrapper: you must use the same callbacks for all your servers/clients
; ===============================================================================================================================

; #DOC# =========================================================================================================================
; - You MUST call _MySock_Startup befor any other _MyXXX function, and _MySock_Shutdown at program termination
; - Every object created MUST be destroyed to avoir memory leak (_MySrv_Destroy, _MyCln_Destroy)
; - Callbacks definition:
;	mySrvOnConnectProc		=> _Func($pMySrv, $iPeerId)
;	mySrvOnDisconnectProc	=> _Func($pMySrv, $iPeerId)
;	mySrvOnPacketRecvProc	=> _Func($pMySrv, $iPeerId, $bData)
;	mySrvOnReceiving		=> _Func($pMySrv, $iPeerId, $iReceivedBytes, $iTotalBytes)
;	mySrvOnTimeOutProc		=> _Func($pMySrv, $iPeerId, $bPartialData, $iExceptedLen)
;	mySrvIterateProc		=> _Func($pMySrv, $iPeerId, $pUserData) -> Return 0 to stope iterating, any other value will continue
;	myClnOnDisconnectProc	=> _Func($pMyCln)
;	myClnOnPacketRecvProc	=> _Func($pMyCln, $bData)
;	myClnOnReceivingProc	=> _Func($pMyCln, $iReceivedBytes, $iTotalBytes)
;	myClnOnTimeOutProc		=> _Func($pMyCln, $bPartialData, $iExceptedLen)
; ===============================================================================================================================

; #CURRENT# =====================================================================================================================
;_MySock_Startup
;_MySock_Shutdown
;_MySock_Host2ip
;_MySock_MyHost
;_MySrv_Create
;_MySrv_Destroy
;_MySrv_Start
;_MySrv_Stop
;_MySrv_IsStarted
;_MySrv_SetProtocol
;_MySrv_GetProtocol
;_MySrv_GetAddr
;_MySrv_GetAddrStr
;_MySrv_SetDefKeepAlive
;_MySrv_GetDefKeepAlive
;_MySrv_GetSocket
;_MySrv_SetUserData
;_MySrv_GetUserData
;_MySrv_SetCallbacks
;_MySrv_Process
;_MySrv_PeerSend
;_MySrv_Broadcast
;_MySrv_Close
;_MySrv_CloseAll
;_MySrv_PeersCount
;_MySrv_PeersGetAll
;_MySrv_PeersIterate
;_MySrv_PeerGetAddr
;_MySrv_PeerGetAddrStr
;_MySrv_PeerSetKeepAlive
;_MySrv_PeerGetKeepAlive
;_MySrv_PeerGetSocket
;_MySrv_PeerSetUserData
;_MySrv_PeerGetUserData
;_MyCln_Create
;_MyCln_Destroy
;_MyCln_Connect
;_MyCln_Close
;_MyCln_IsConnected
;_MyCln_SetProtocol
;_MyCln_GetProtocol
;_MyCln_SetHost
;_MyCln_GetHost
;_MyCln_GetHostStr
;_MyCln_GetSrvIp
;_MyCln_SetCallbacks
;_MyCln_Process
;_MyCln_Send
;_MyCln_SetKeepAlive
;_MyCln_GetKeepAlive
;_MyCln_GetSocket
;_MyCln_SetUserData
;_MyCln_GetUserData
; ===============================================================================================================================

; #CONSTANTS# ===================================================================================================================
Enum _	; protocol_e
	$MYSOCK_PROT_AUTO, _
	$MYSOCK_PROT_IPV4, _
	$MYSOCK_PROT_IPV6
; ===============================================================================================================================

; ===============================================================================================================================
; Internals

; Global mySock dll handle
Global $__gMySock_hDll = -1

; Callbacks IDs
Enum _
	$MYSOCK_CB_SRV_ONCONNECT, _
	$MYSOCK_CB_SRV_ONDISCONNECT, _
	$MYSOCK_CB_SRV_ONPACKETRECV, _
	$MYSOCK_CB_SRV_ONRECEIVING, _
	$MYSOCK_CB_SRV_ONTIMEOUT, _
	$MYSOCK_CB_CLN_ONDISCONNECT, _
	$MYSOCK_CB_CLN_ONPACKETRECV, _
	$MYSOCK_CB_CLN_ONRECEIVING, _
	$MYSOCK_CB_CLN_ONTIMEOUT

Global $__gMySock_aCallbacks[9][3] = [ _
	[0, "none:cdecl", "ptr;int"], _					; mySrvOnConnectProc
	[0, "none:cdecl", "ptr;int"], _					; mySrvOnDisconnectProc
	[0, "none:cdecl", "ptr;int;ptr;uint"], _		; mySrvOnPacketRecvProc
	[0, "none:cdecl", "ptr;int;uint;uint"], _		; mySrvOnReceivingProc
	[0, "none:cdecl", "ptr;int;ptr;uint;uint"], _	; mySrvOnTimeOutProc
	[0, "none:cdecl", "ptr"], _						; myClnOnDisconnectProc
	[0, "none:cdecl", "ptr;ptr;uint"], _			; myClnOnPacketRecvProc
	[0, "none:cdecl", "ptr;uint;uint"], _			; myClnOnReceivingProc
	[0, "none:cdecl", "ptr;ptr;uint;uint"]]			; myClnOnTimeOutProc

; On recv special callbacks
Global $__gMySock_sSrvOnPacketRecv = "", $__gMySock_sSrvOnTimeOut = ""
Global $__gMySock_sClnOnPacketRecv = "", $__gMySock_sClnOnTimeOut = ""

; ===============================================================================================================================

;~ declare function MySock_Startup () as integer
Func _MySock_Startup($sDllPath = "mysock.dll")
	If $__gMySock_hDll <> -1 Then Return -1
	; ---
	$__gMySock_hDll = DllOpen($sDllPath)
	If $__gMySock_hDll = -1 Then Return SetError(1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MySock_Startup")
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySock_Startup)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return $ret[0]
EndFunc

;~ declare function MySock_Shutdown () as integer
Func _MySock_Shutdown()
	If $__gMySock_hDll = -1 Then Return -1
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MySock_Shutdown")
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySock_Shutdown)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	__MySock_FreeCallbacks()
	; ---
	DllClose($__gMySock_hDll)
	$__gMySock_hDll = -1
	; ---
	Return $ret[0]
EndFunc

;~ declare function 	MySock_Host2ip		(host as zstring, protocol as protocol_e, ip as zstring, ip_len as uinteger) as integer
Func _MySock_Host2ip($sHost, $iProtocol = $MYSOCK_PROT_AUTO)
	If $__gMySock_hDll = -1 Then Return -1
	; ---
	Local $struct = DllStructCreate("char[256]")
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MySock_Host2ip", "str", $sHost, "int", $iProtocol, "ptr", DllStructGetPtr($struct, 1), "uint", 256)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySock_Host2ip)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	If $ret[0] <> 1 Then Return ""
	Return DllStructGetData($struct, 1)
EndFunc

;~ declare sub 		MySock_MyHost		(host as zstring, host_len as uinteger)
Func _MySock_MyHost()
	If $__gMySock_hDll = -1 Then Return -1
	; ---
	Local $struct = DllStructCreate("char[256]")
	; ---
	DllCall($__gMySock_hDll, "none:cdecl", "MySock_MyHost", "ptr", DllStructGetPtr($struct, 1), "uint", 256)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySock_MyHost)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return DllStructGetData($struct, 1)
EndFunc

; ===============================================================================================================================

;~ declare function 	MySrv_Create	(max_peers as uinteger, protocol as protocol_e) as mySrv_t ptr
Func _MySrv_Create($iMaxPeers, $iProtocol = $MYSOCK_PROT_AUTO)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "ptr:cdecl", "MySrv_Create", "uint", $iMaxPeers, "int", $iProtocol)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_Create)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return $ret[0]
EndFunc

;~ declare function 	MySrv_Destroy	(mySrv as mySrv_t ptr) as integer
Func _MySrv_Destroy($pMySrv)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MySrv_Destroy", "ptr", $pMySrv)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_Destroy)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return $ret[0]
EndFunc

;~ declare function 	MySrv_Start		(mySrv as mySrv_t ptr, port as ushort) as integer
Func _MySrv_Start($pMySrv, $iPort)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MySrv_Start", "ptr", $pMySrv, "ushort", $iPort)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_Start)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return $ret[0]
EndFunc

;~ declare function 	MySrv_Stop		(mySrv as mySrv_t ptr) as integer
Func _MySrv_Stop($pMySrv)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MySrv_Stop", "ptr", $pMySrv)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_Stop)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return $ret[0]
EndFunc

;~ declare function 	MySrv_IsStarted	(mySrv as mySrv_t ptr) as integer
Func _MySrv_IsStarted($pMySrv)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MySrv_IsStarted", "ptr", $pMySrv)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_IsStarted)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return $ret[0]
EndFunc

;~ declare sub 			MySrv_SetProtocol		(mySrv as mySrv_t ptr, protocol as protocol_e)
Func _MySrv_SetProtocol($pMySrv, $iProtocol)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	DllCall($__gMySock_hDll, "none:cdecl", "MySrv_SetProtocol", "ptr", $pMySrv, "int", $iProtocol)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_SetProtocol)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return 1
EndFunc

;~ declare function		MySrv_GetProtocol		(mySrv as mySrv_t ptr) as integer
Func _MySrv_GetProtocol($pMySrv)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MySrv_GetProtocol", "ptr", $pMySrv)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_GetProtocol)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return $ret[0]
EndFunc

;~ declare function		MySrv_GetAddr			(mySrv as mySrv_t ptr, ip as zstring, ip_len as uinteger, port as ushort ptr) as integer
Func _MySrv_GetAddr($pMySrv)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $struct = DllStructCreate("char[512]")
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MySrv_GetAddr", "ptr", $pMySrv, "ptr", DllStructGetPtr($struct), "uint", 512, "ushort*", 0)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_GetAddr)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	If $ret[0] = 0 Then Return 0
	; ---
	Local $aRet[2] = [DllStructGetData($struct, 1), $ret[4]]
	Return $aRet
EndFunc

Func _MySrv_GetAddrStr($pMySrv, $iWithPort)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $struct = DllStructCreate("char[512]")
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MySrv_GetAddr", "ptr", $pMySrv, "ptr", DllStructGetPtr($struct), "uint", 512, "ushort*", 0)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_GetAddr)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	If $ret[0] = 0 Then Return ""
	; ---
	Local $sRet = DllStructGetData($struct, 1)
	If $iWithPort Then
		If StringInStr($sRet, ":") Then $sRet = "[" & $sRet & "]"
		$sRet &= ":" & $ret[4]
	EndIf
	Return $sRet
EndFunc

;~ declare sub 			MySrv_SetDefKeepAlive	(mySrv as mySrv_t ptr, timeout as uinteger, interval as uinteger)
Func _MySrv_SetDefKeepAlive($pMySrv, $iTimeOut, $iInterval)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	DllCall($__gMySock_hDll, "none:cdecl", "MySrv_SetDefKeepAlive", "ptr", $pMySrv, "uint", $iTimeOut, "uint", $iInterval)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_SetDefKeepAlive)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return 1
EndFunc

;~ declare sub 			MySrv_GetDefKeepAlive	(mySrv as mySrv_t ptr, timeout as uinteger ptr, interval as uinteger ptr)
Func _MySrv_GetDefKeepAlive($pMySrv)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "none:cdecl", "MySrv_GetDefKeepAlive", "ptr", $pMySrv, "uint*", 0, "uint*", 0)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_GetDefKeepAlive)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Local $aRet[2] = [$ret[2], $ret[3]]
	Return $aRet
EndFunc

;~ declare function		MySrv_GetSocket			(mySrv as mySrv_t ptr) as integer
Func _MySrv_GetSocket($pMySrv)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MySrv_GetSocket", "ptr", $pMySrv)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_GetSocket)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return $ret[0]
EndFunc

;~ declare sub 			MySrv_SetUserData		(mySrv as mySrv_t ptr, user_data as any ptr)
Func _MySrv_SetUserData($pMySrv, $iUserData)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	DllCall($__gMySock_hDll, "none:cdecl", "MySrv_SetUserData", "ptr", $pMySrv, "ptr", $iUserData)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_SetUserData)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return 1
EndFunc

;~ declare function		MySrv_GetUserData		(mySrv as mySrv_t ptr) as any ptr
Func _MySrv_GetUserData($pMySrv)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "ptr:cdecl", "MySrv_GetUserData", "ptr", $pMySrv)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_GetUserData)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return $ret[0]
EndFunc

;~ declare sub 			MySrv_SetCallbacks	(mySrv as mySrv_t ptr, onConnect as mySrvOnConnectProc, onDisconnect as mySrvOnDisconnectProc, onPacketRecv as mySrvOnRecvProc, onReceiving as mySrvOnReceivingProc, onTimeOut as mySrvOnTimeOutProc)
Func _MySrv_SetCallbacks($pMySrv, $sOnConnect, $sOnDisconnect, $sOnPacketRecv, $sOnReceiving, $sOnTimeOut)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "none:cdecl", "MySrv_SetCallbacks", "ptr", $pMySrv, _
		"ptr", __MySock_SetCallback($sOnConnect, $MYSOCK_CB_SRV_ONCONNECT), _
		"ptr", __MySock_SetCallback($sOnDisconnect, $MYSOCK_CB_SRV_ONDISCONNECT), _
		"ptr", __MySock_SetCallback($sOnPacketRecv, $MYSOCK_CB_SRV_ONPACKETRECV), _
		"ptr", __MySock_SetCallback($sOnReceiving, $MYSOCK_CB_SRV_ONRECEIVING), _
		"ptr", __MySock_SetCallback($sOnTimeOut, $MYSOCK_CB_SRV_ONTIMEOUT) _
	)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_SetCallbacks)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return 1
EndFunc

;~ declare sub 			MySrv_Process		(mySrv as mySrv_t ptr)
Func _MySrv_Process($pMySrv)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	DllCall($__gMySock_hDll, "none:cdecl", "MySrv_Process", "ptr", $pMySrv)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_Process)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return 1
EndFunc

;~ declare function 	MySrv_PeerSend	(mySrv as mySrv_t ptr, peer_id as integer, data_ as byte ptr, data_len as uinteger) as integer
Func _MySrv_PeerSend($pMySrv, $iPeerId, $bData)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $len = BinaryLen($bData)
	Local $struct = DllStructCreate("byte[" & $len & "]")
	DllStructSetData($struct, 1, $bData)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "none:cdecl", "MySrv_PeerSend", "ptr", $pMySrv, "int", $iPeerId, "ptr", DllStructGetPtr($struct, 1), "uint", $len)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_PeerSend)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return $ret[0]
EndFunc

;~ declare function 	MySrv_Broadcast	(mySrv as mySrv_t ptr, data_ as byte ptr, data_len as uinteger) as integer
Func _MySrv_Broadcast($pMySrv, $bData)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $len = BinaryLen($bData)
	Local $struct = DllStructCreate("byte[" & $len & "]")
	DllStructSetData($struct, 1, $bData)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MySrv_Broadcast", "ptr", $pMySrv, "ptr", DllStructGetPtr($struct, 1), "uint", $len)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_Broadcast)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return $ret[0]
EndFunc

;~ declare function 	MySrv_Close		(mySrv as mySrv_t ptr, peer_id as integer) as integer
Func _MySrv_Close($pMySrv, $iPeerId)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MySrv_Close", "ptr", $pMySrv, "int", $iPeerId)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_Close)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return $ret[0]
EndFunc

;~ declare function 	MySrv_CloseAll	(mySrv as mySrv_t ptr) as integer
Func _MySrv_CloseAll($pMySrv)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MySrv_CloseAll", "ptr", $pMySrv)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_CloseAll)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return $ret[0]
EndFunc

;~ declare function 	MySrv_PeersCount	(mySrv as mySrv_t ptr) as integer
Func _MySrv_PeersCount($pMySrv)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MySrv_PeersCount", "ptr", $pMySrv)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_PeersCount)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return $ret[0]
EndFunc

;~ declare function 	MySrv_PeersGetAll	(mySrv as mySrv_t ptr, peer_ids as integer ptr, peers_ids_size as uinteger) as integer
Func _MySrv_PeersGetAll($pMySrv, $iMax = -1)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	If $iMax = -1 Then $iMax = _MySrv_PeersCount($pMySrv)
	If $iMax = 0 Then Return 0
	; ---
	Local $struct = DllStructCreate("int[" & $iMax & "]")
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MySrv_PeersGetAll", "ptr", $pMySrv, "ptr", DllStructGetPtr($struct, 1), "uint", $iMax)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_PeersGetAll)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	If $ret[0] = 0 Then Return 0
	; ---
	Local $aRet[$ret[0] + 1]
	$aRet[0] = $ret[0]
	For $i = 1 To $aRet[0]
		$aRet[$i] = DllStructGetData($struct, 1, $i)
	Next
	; ---
	Return $aRet
EndFunc

;~ declare function 	MySrv_PeersIterate	(mySrv as mySrv_t ptr, callback as mySrvIterateProc, user_data as any ptr) as integer
Func _MySrv_PeersIterate($pMySrv, $sFunc, $iUserData)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $fnc = DllCallbackRegister($sFunc, "int:cdecl", "ptr;int;ptr")
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MySrv_PeersIterate", "ptr", $pMySrv, "ptr", DllCallbackGetPtr($fnc), "ptr", $iUserData)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_PeersIterate)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	DllCallbackFree($fnc)
	; ---
	Return $ret[0]
EndFunc

;~ declare function		MySrv_PeerGetAddr			(mySrv as mySrv_t ptr, peer_id as integer, ip as zstring, ip_len as integer, port as ushort ptr) as integer
Func _MySrv_PeerGetAddr($pMySrv, $iPeerId)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $struct = DllStructCreate("char[512]")
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MySrv_PeerGetAddr", "ptr", $pMySrv, "int", $iPeerId, "ptr", DllStructGetPtr($struct), "uint", 512, "ushort*", 0)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_PeerGetAddr)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	If $ret[0] = 0 Then Return 0
	; ---
	Local $aRet[2] = [DllStructGetData($struct, 1), $ret[5]]
	Return $aRet
EndFunc

Func _MySrv_PeerGetAddrStr($pMySrv, $iPeerId, $iWithPort)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $struct = DllStructCreate("char[512]")
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MySrv_PeerGetAddr", "ptr", $pMySrv, "int", $iPeerId, "ptr", DllStructGetPtr($struct), "uint", 512, "ushort*", 0)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_PeerGetAddr)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	If $ret[0] = 0 Then Return ""
	; ---
	Local $sRet = DllStructGetData($struct, 1)
	If $iWithPort Then
		If StringInStr($sRet, ":") Then $sRet = "[" & $sRet & "]"
		$sRet &= ":" & $ret[5]
	EndIf
	Return $sRet
EndFunc

;~ declare sub 			MySrv_PeerSetKeepAlive		(mySrv as mySrv_t ptr, peer_id as integer, timeout as uinteger, interval as uinteger)
Func _MySrv_PeerSetKeepAlive($pMySrv, $iPeerId, $iTimeOut, $iInterval)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	DllCall($__gMySock_hDll, "none:cdecl", "MySrv_PeerSetKeepAlive", "ptr", $pMySrv, "int", $iPeerId, "uint", $iTimeOut, "uint", $iInterval)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_PeerSetKeepAlive)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return 1
EndFunc

;~ declare sub 			MySrv_PeerGetKeepAlive		(mySrv as mySrv_t ptr, peer_id as integer, timeout as uinteger ptr, interval as uinteger ptr)
Func _MySrv_PeerGetKeepAlive($pMySrv, $iPeerId)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "none:cdecl", "MySrv_PeerGetKeepAlive", "ptr", $pMySrv, "int", $iPeerId, "uint*", 0, "uint*", 0)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_PeerGetKeepAlive)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Local $aRet[2] = [$ret[3], $ret[4]]
	Return $aRet
EndFunc

;~ declare function		MySrv_PeerGetSocket		(muSrv as mySrv_t ptr, peer_id as integer) as integer
Func _MySrv_PeerGetSocket($pMySrv, $iPeerId)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MySrv_PeerGetSocket", "ptr", $pMySrv, "int", $iPeerId)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_PeerGetSocket)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return $ret[0]
EndFunc

;~ declare function 	MySrv_PeerSetUserData		(mySrv as mySrv_t ptr, peer_id as integer, user_data as any ptr) as integer
Func _MySrv_PeerSetUserData($pMySrv, $iPeerId, $iUserData)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MySrv_PeerSetUserData", "ptr", $pMySrv, "int", $iPeerId, "ptr", $iUserData)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_PeerSetUserData)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return $ret[0]
EndFunc

;~ declare function 	MySrv_PeerGetUserData		(mySrv as mySrv_t ptr, peer_id as integer) as any ptr
Func _MySrv_PeerGetUserData($pMySrv, $iPeerId)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "ptr:cdecl", "MySrv_PeerGetUserData", "ptr", $pMySrv, "int", $iPeerId)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_PeerGetUserData)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return $ret[0]
EndFunc

; ===============================================================================================================================

;~ declare function 	MyCln_Create		(host as zstring, port as ushort, protocol as protocol_e) as myCln_t ptr
Func _MyCln_Create($sHost, $iPort, $iProtocol = $MYSOCK_PROT_AUTO)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "ptr:cdecl", "MyCln_Create", "str", $sHost, "ushort", $iPort, "int", $iProtocol)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MyCln_Create)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return $ret[0]
EndFunc

;~ declare function 	MyCln_Destroy		(myCln as myCln_t ptr) as integer
Func _MyCln_Destroy($pMyCln)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MyCln_Destroy", "ptr", $pMyCln)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MyCln_Destroy)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return $ret[0]
EndFunc

;~ declare function 	MyCln_Connect		(myCln as myCln_t ptr, timeout as uinteger) as integer
Func _MyCln_Connect($pMyCln, $iTimeOut)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MyCln_Connect", "ptr", $pMyCln, "uint", $iTimeOut)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MyCln_Connect)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return $ret[0]
EndFunc

;~ declare function 	MyCln_Close			(myCln as myCln_t ptr) as integer
Func _MyCln_Close($pMyCln)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MyCln_Close", "ptr", $pMyCln)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MyCln_Close)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return $ret[0]
EndFunc

;~ declare function 	MyCln_IsConnected	(myCln as myCln_t ptr) as integer
Func _MyCln_IsConnected($pMyCln)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MyCln_IsConnected", "ptr", $pMyCln)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MyCln_IsConnected)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return $ret[0]
EndFunc

;~ declare sub 			MyCln_SetProtocol		(myCln as myCln_t ptr, protocol as protocol_e)
Func _MyCln_SetProtocol($pMyCln, $iProtocol = $MYSOCK_PROT_AUTO)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	DllCall($__gMySock_hDll, "none:cdecl", "MyCln_SetProtocol", "ptr", $pMyCln, "int", $iProtocol)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MyCln_SetProtocol)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return 1
EndFunc

;~ declare function		MyCln_GetProtocol		(myCln as myCln_t ptr) as integer
Func _MyCln_GetProtocol($pMyCln)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MyCln_GetProtocol", "ptr", $pMyCln)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MyCln_GetProtocol)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return $ret[0]
EndFunc

;~ declare sub 			MyCln_SetHost			(myCln as myCln_t ptr, host as zstring, port as ushort)
Func _MyCln_SetHost($pMyCln, $sHost, $iPort)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	DllCall($__gMySock_hDll, "none:cdecl", "MyCln_SetHost", "ptr", $pMyCln, "str", $sHost, "ushort", $iPort)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MyCln_SetHost)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return 1
EndFunc

;~ declare sub 			MyCln_GetHost			(myCln as myCln_t ptr, host as zstring, host_len as uinteger, port as ushort ptr)
Func _MyCln_GetHost($pMyCln)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $struct = DllStructCreate("char[512]")
	; ---
	Local $ret = DllCall($__gMySock_hDll, "none:cdecl", "MyCln_GetHost", "ptr", $pMyCln, "ptr", DllStructGetPtr($struct), "uint", 512, "ushort*", 0)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MyCln_GetHost)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Local $aRet[2] = [DllStructGetData($struct, 1), $ret[4]]
	Return $aRet
EndFunc

Func _MyCln_GetHostStr($pMyCln, $iWithPort)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $struct = DllStructCreate("char[512]")
	; ---
	Local $ret = DllCall($__gMySock_hDll, "none:cdecl", "MyCln_GetHost", "ptr", $pMyCln, "ptr", DllStructGetPtr($struct), "uint", 512, "ushort*", 0)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MyCln_GetHost)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Local $sRet = DllStructGetData($struct, 1)
	If $iWithPort Then
		If StringInStr($sRet, ":") Then $sRet = "[" & $sRet & "]"
		$sRet &= ":" & $ret[4]
	EndIf
	Return $sRet
EndFunc

;~ declare sub 			MyCln_GetSrvIp			(myCln as myCln_t ptr, ip as zstring, ip_len as uinteger)
Func _MyCln_GetSrvIp($pMyCln)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $struct = DllStructCreate("char[512]")
	; ---
	DllCall($__gMySock_hDll, "none:cdecl", "MyCln_GetSrvIp", "ptr", $pMyCln, "ptr", DllStructGetPtr($struct, 1), "uint", 512)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MyCln_GetSrvIp)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return DllStructGetData($struct, 1)
EndFunc

;~ declare sub 			MyCln_SetCallbacks	(myCln as myCln_t ptr, onDisconnect as myClnOnDisconnectProc, onPacketRecv as myClnOnRecvProc, onReceiving as myClnOnReceivingProc, onTimeOut as myClnOnTimeOutProc)
Func _MyCln_SetCallbacks($pMyCln, $sOnDisconnect, $sOnPacketRecv, $sOnReceiving, $sOnTimeOut)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	DllCall($__gMySock_hDll, "none:cdecl", "MyCln_SetCallbacks", "ptr", $pMyCln, _
		"ptr", __MySock_SetCallback($sOnDisconnect, $MYSOCK_CB_CLN_ONDISCONNECT), _
		"ptr", __MySock_SetCallback($sOnPacketRecv, $MYSOCK_CB_CLN_ONPACKETRECV), _
		"ptr", __MySock_SetCallback($sOnReceiving, $MYSOCK_CB_CLN_ONRECEIVING), _
		"ptr", __MySock_SetCallback($sOnTimeOut, $MYSOCK_CB_CLN_ONTIMEOUT) _
	)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MyCln_SetCallbacks)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return 1
EndFunc

;~ declare sub 			MyCln_Process		(myCln as myCln_t ptr)
Func _MyCln_Process($pMyCln)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	DllCall($__gMySock_hDll, "none:cdecl", "MyCln_Process", "ptr", $pMyCln)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MyCln_Process)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return 1
EndFunc

;~ declare function 	MyCln_Send	(myCln as myCln_t ptr, data_ as byte ptr, data_len as uinteger) as integer
Func _MyCln_Send($pMyCln, $bData)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $len = BinaryLen($bData)
	Local $struct = DllStructCreate("byte[" & $len & "]")
	DllStructSetData($struct, 1, $bData)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MyCln_Send", "ptr", $pMyCln, "ptr", DllStructGetPtr($struct), "uint", $len)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MyCln_Send)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return $ret[0]
EndFunc

;~ declare function 	MyCln_SetKeepAlive 	(myCln as myCln_t ptr, timeout as uinteger, interval as uinteger) as integer
Func _MyCln_SetKeepAlive($pMyCln, $iTimeOut, $iInterval)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MyCln_SetKeepAlive", "ptr", $pMyCln, "uint", $iTimeOut, "uint", $iInterval)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MyCln_SetKeepAlive)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return $ret[0]
EndFunc

;~ declare sub 			MyCln_GetKeepAlive 	(myCln as myCln_t ptr, timeout as uinteger ptr, interval as uinteger ptr)
Func _MyCln_GetKeepAlive($pMyCln)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "none:cdecl", "MyCln_GetKeepAlive", "ptr", $pMyCln, "uint*", 0, "uint*", 0)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MyCln_GetKeepAlive)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Local $aRet[2] = [$ret[2], $ret[3]]
	Return $aRet
EndFunc

;~ declare function		MyCln_GetSocket		(myCln as myCln_t ptr) as integer
Func _MyCln_GetSocket($pMyCln)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MyCln_GetSocket", "ptr", $pMyCln)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MyCln_GetSocket)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return $ret[0]
EndFunc

;~ declare sub 			MyCln_SetUserData 	(myCln as myCln_t ptr, user_data as any ptr)
Func _MyCln_SetUserData($pMyCln, $iUserData)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	DllCall($__gMySock_hDll, "none:cdecl", "MyCln_SetUserData", "ptr", $pMyCln, "ptr", $iUserData)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MyCln_SetUserData)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return 1
EndFunc

;~ declare function 	MyCln_GetUserData 	(myCln as myCln_t ptr) as any ptr
Func _MyCln_GetUserData($pMyCln)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "ptr:cdecl", "MyCln_GetUserData", "ptr", $pMyCln)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MyCln_GetUserData)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return $ret[0]
EndFunc

; #INTERNAL_USE_ONLY# ===========================================================================================================

Func __MySock_SetCallback($sFunc, $iCbId)
	DllCallbackFree($__gMySock_aCallbacks[$iCbId][0])
	$__gMySock_aCallbacks[$iCbId][0] = 0
	; ---
	If $sFunc Then
		Switch $iCbId
			Case $MYSOCK_CB_SRV_ONPACKETRECV
				$__gMySock_sSrvOnPacketRecv = $sFunc
				$sFunc = "__MySock_SpecCb_SrvOnRecv"
			; ---
			Case $MYSOCK_CB_SRV_ONTIMEOUT
				$__gMySock_sSrvOnTimeOut = $sFunc
				$sFunc = "__MySock_SpecCb_SrvOnTimeOut"
			; ---
			Case $MYSOCK_CB_CLN_ONPACKETRECV
				$__gMySock_sClnOnPacketRecv = $sFunc
				$sFunc = "__MySock_SpecCb_ClnOnRecv"
			; ---
			Case $MYSOCK_CB_CLN_ONTIMEOUT
				$__gMySock_sClnOnTimeOut = $sFunc
				$sFunc = "__MySock_SpecCb_ClnOnTimeOut"
		EndSwitch
		; ---
		$__gMySock_aCallbacks[$iCbId][0] = DllCallbackRegister($sFunc, $__gMySock_aCallbacks[$iCbId][1], $__gMySock_aCallbacks[$iCbId][2])
	Else
		Switch $iCbId
			Case $MYSOCK_CB_SRV_ONPACKETRECV
				$__gMySock_sSrvOnPacketRecv = ""
			Case $MYSOCK_CB_SRV_ONTIMEOUT
				$__gMySock_sSrvOnTimeOut = ""
			Case $MYSOCK_CB_CLN_ONPACKETRECV
				$__gMySock_sClnOnPacketRecv = ""
			Case $MYSOCK_CB_CLN_ONTIMEOUT
				$__gMySock_sClnOnTimeOut = ""
		EndSwitch
	EndIf
	; ---
	Return DllCallbackGetPtr($__gMySock_aCallbacks[$iCbId][0])
EndFunc

Func __MySock_FreeCallbacks()
	For $i = 0 To UBound($__gMySock_aCallbacks) - 1
		DllCallbackFree($__gMySock_aCallbacks[$i][0])
	Next
EndFunc

Func __MySock_SpecCb_SrvOnRecv($pMySrv, $iPeerId, $pData, $iDataLen)
	Local $struct = DllStructCreate("byte[" & $iDataLen & "]", $pData)
	Call($__gMySock_sSrvOnPacketRecv, $pMySrv, $iPeerId, DllStructGetData($struct, 1))
EndFunc

Func __MySock_SpecCb_SrvOnTimeOut($pMySrv, $iPeerId, $pPartialData, $iDataLen, $iExceptedLen)
	Local $struct = DllStructCreate("byte[" & $iDataLen & "]", $pPartialData)
	Call($__gMySock_sSrvOnTimeOut, $pMySrv, $iPeerId, DllStructGetData($struct, 1), $iExceptedLen)
EndFunc

Func __MySock_SpecCb_ClnOnRecv($pMyCln, $pData, $iDataLen)
	Local $struct = DllStructCreate("byte[" & $iDataLen & "]", $pData)
	Call($__gMySock_sClnOnPacketRecv, $pMyCln, DllStructGetData($struct, 1))
EndFunc

Func __MySock_SpecCb_ClnOnTimeOut($pMyCln, $pPartialData, $iDataLen, $iExceptedLen)
	Local $struct = DllStructCreate("byte[" & $iDataLen & "]", $pPartialData)
	Call($__gMySock_sClnOnTimeOut, $pMyCln, DllStructGetData($struct, 1), $iExceptedLen)
EndFunc

#include-once
; #INDEX# =======================================================================================================================
; Title .........: MySock
; AutoIt Version : 3.3.8.1
; Description ...: WinSockets simple wrapper
; Author(s) .....: Matwachich
; Dll ...........: mysock.dll
; Remarks .......: Limitation of the AutoIt wrapper: you must use the same callbacks for all your servers/clients
; ===============================================================================================================================

; #DOC# =========================================================================================================================
; - You MUST call _MySock_Startup befor any other _MyXXX function, and _MySock_Shutdown at program termination
; - Every object created MUST be destroyed to avoir memory leak (_MySrv_Destroy, _MyCln_Destroy)
; - Callbacks definition:
;	mySrvOnConnectProc		=> _Func($pMySrv, $iPeerId)
;	mySrvOnDisconnectProc	=> _Func($pMySrv, $iPeerId)
;	mySrvOnPacketRecvProc	=> _Func($pMySrv, $iPeerId, $bData)
;	mySrvOnReceiving		=> _Func($pMySrv, $iPeerId, $iReceivedBytes, $iTotalBytes)
;	mySrvOnTimeOutProc		=> _Func($pMySrv, $iPeerId, $bPartialData, $iExceptedLen)
;	mySrvIterateProc		=> _Func($pMySrv, $iPeerId, $pUserData) -> Return 0 to stope iterating, any other value will continue
;	myClnOnDisconnectProc	=> _Func($pMyCln)
;	myClnOnPacketRecvProc	=> _Func($pMyCln, $bData)
;	myClnOnReceivingProc	=> _Func($pMyCln, $iReceivedBytes, $iTotalBytes)
;	myClnOnTimeOutProc		=> _Func($pMyCln, $bPartialData, $iExceptedLen)
; ===============================================================================================================================

; #CURRENT# =====================================================================================================================
;_MySock_Startup
;_MySock_Shutdown
;_MySock_Host2ip
;_MySock_MyHost
;_MySrv_Create
;_MySrv_Destroy
;_MySrv_Start
;_MySrv_Stop
;_MySrv_IsStarted
;_MySrv_SetProtocol
;_MySrv_GetProtocol
;_MySrv_GetAddr
;_MySrv_GetAddrStr
;_MySrv_SetDefKeepAlive
;_MySrv_GetDefKeepAlive
;_MySrv_GetSocket
;_MySrv_SetUserData
;_MySrv_GetUserData
;_MySrv_SetCallbacks
;_MySrv_Process
;_MySrv_PeerSend
;_MySrv_Broadcast
;_MySrv_Close
;_MySrv_CloseAll
;_MySrv_PeersCount
;_MySrv_PeersGetAll
;_MySrv_PeersIterate
;_MySrv_PeerGetAddr
;_MySrv_PeerGetAddrStr
;_MySrv_PeerSetKeepAlive
;_MySrv_PeerGetKeepAlive
;_MySrv_PeerGetSocket
;_MySrv_PeerSetUserData
;_MySrv_PeerGetUserData
;_MyCln_Create
;_MyCln_Destroy
;_MyCln_Connect
;_MyCln_Close
;_MyCln_IsConnected
;_MyCln_SetProtocol
;_MyCln_GetProtocol
;_MyCln_SetHost
;_MyCln_GetHost
;_MyCln_GetHostStr
;_MyCln_GetSrvIp
;_MyCln_SetCallbacks
;_MyCln_Process
;_MyCln_Send
;_MyCln_SetKeepAlive
;_MyCln_GetKeepAlive
;_MyCln_GetSocket
;_MyCln_SetUserData
;_MyCln_GetUserData
; ===============================================================================================================================

; #CONSTANTS# ===================================================================================================================
Enum _	; protocol_e
	$MYSOCK_PROT_AUTO, _
	$MYSOCK_PROT_IPV4, _
	$MYSOCK_PROT_IPV6
; ===============================================================================================================================

; ===============================================================================================================================
; Internals

; Global mySock dll handle
Global $__gMySock_hDll = -1

; Callbacks IDs
Enum _
	$MYSOCK_CB_SRV_ONCONNECT, _
	$MYSOCK_CB_SRV_ONDISCONNECT, _
	$MYSOCK_CB_SRV_ONPACKETRECV, _
	$MYSOCK_CB_SRV_ONRECEIVING, _
	$MYSOCK_CB_SRV_ONTIMEOUT, _
	$MYSOCK_CB_CLN_ONDISCONNECT, _
	$MYSOCK_CB_CLN_ONPACKETRECV, _
	$MYSOCK_CB_CLN_ONRECEIVING, _
	$MYSOCK_CB_CLN_ONTIMEOUT

Global $__gMySock_aCallbacks[9][3] = [ _
	[0, "none:cdecl", "ptr;int"], _					; mySrvOnConnectProc
	[0, "none:cdecl", "ptr;int"], _					; mySrvOnDisconnectProc
	[0, "none:cdecl", "ptr;int;ptr;uint"], _		; mySrvOnPacketRecvProc
	[0, "none:cdecl", "ptr;int;uint;uint"], _		; mySrvOnReceivingProc
	[0, "none:cdecl", "ptr;int;ptr;uint;uint"], _	; mySrvOnTimeOutProc
	[0, "none:cdecl", "ptr"], _						; myClnOnDisconnectProc
	[0, "none:cdecl", "ptr;ptr;uint"], _			; myClnOnPacketRecvProc
	[0, "none:cdecl", "ptr;uint;uint"], _			; myClnOnReceivingProc
	[0, "none:cdecl", "ptr;ptr;uint;uint"]]			; myClnOnTimeOutProc

; On recv special callbacks
Global $__gMySock_sSrvOnPacketRecv = "", $__gMySock_sSrvOnTimeOut = ""
Global $__gMySock_sClnOnPacketRecv = "", $__gMySock_sClnOnTimeOut = ""

; ===============================================================================================================================

;~ declare function MySock_Startup () as integer
Func _MySock_Startup($sDllPath = "mysock.dll")
	If $__gMySock_hDll <> -1 Then Return -1
	; ---
	$__gMySock_hDll = DllOpen($sDllPath)
	If $__gMySock_hDll = -1 Then Return SetError(1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MySock_Startup")
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySock_Startup)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return $ret[0]
EndFunc

;~ declare function MySock_Shutdown () as integer
Func _MySock_Shutdown()
	If $__gMySock_hDll = -1 Then Return -1
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MySock_Shutdown")
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySock_Shutdown)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	__MySock_FreeCallbacks()
	; ---
	DllClose($__gMySock_hDll)
	$__gMySock_hDll = -1
	; ---
	Return $ret[0]
EndFunc

;~ declare function 	MySock_Host2ip		(host as zstring, protocol as protocol_e, ip as zstring, ip_len as uinteger) as integer
Func _MySock_Host2ip($sHost, $iProtocol = $MYSOCK_PROT_AUTO)
	If $__gMySock_hDll = -1 Then Return -1
	; ---
	Local $struct = DllStructCreate("char[256]")
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MySock_Host2ip", "str", $sHost, "int", $iProtocol, "ptr", DllStructGetPtr($struct, 1), "uint", 256)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySock_Host2ip)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	If $ret[0] <> 1 Then Return ""
	Return DllStructGetData($struct, 1)
EndFunc

;~ declare sub 		MySock_MyHost		(host as zstring, host_len as uinteger)
Func _MySock_MyHost()
	If $__gMySock_hDll = -1 Then Return -1
	; ---
	Local $struct = DllStructCreate("char[256]")
	; ---
	DllCall($__gMySock_hDll, "none:cdecl", "MySock_MyHost", "ptr", DllStructGetPtr($struct, 1), "uint", 256)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySock_MyHost)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return DllStructGetData($struct, 1)
EndFunc

; ===============================================================================================================================

;~ declare function 	MySrv_Create	(max_peers as uinteger, protocol as protocol_e) as mySrv_t ptr
Func _MySrv_Create($iMaxPeers, $iProtocol = $MYSOCK_PROT_AUTO)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "ptr:cdecl", "MySrv_Create", "uint", $iMaxPeers, "int", $iProtocol)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_Create)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return $ret[0]
EndFunc

;~ declare function 	MySrv_Destroy	(mySrv as mySrv_t ptr) as integer
Func _MySrv_Destroy($pMySrv)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MySrv_Destroy", "ptr", $pMySrv)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_Destroy)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return $ret[0]
EndFunc

;~ declare function 	MySrv_Start		(mySrv as mySrv_t ptr, port as ushort) as integer
Func _MySrv_Start($pMySrv, $iPort)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MySrv_Start", "ptr", $pMySrv, "ushort", $iPort)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_Start)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return $ret[0]
EndFunc

;~ declare function 	MySrv_Stop		(mySrv as mySrv_t ptr) as integer
Func _MySrv_Stop($pMySrv)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MySrv_Stop", "ptr", $pMySrv)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_Stop)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return $ret[0]
EndFunc

;~ declare function 	MySrv_IsStarted	(mySrv as mySrv_t ptr) as integer
Func _MySrv_IsStarted($pMySrv)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MySrv_IsStarted", "ptr", $pMySrv)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_IsStarted)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return $ret[0]
EndFunc

;~ declare sub 			MySrv_SetProtocol		(mySrv as mySrv_t ptr, protocol as protocol_e)
Func _MySrv_SetProtocol($pMySrv, $iProtocol)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	DllCall($__gMySock_hDll, "none:cdecl", "MySrv_SetProtocol", "ptr", $pMySrv, "int", $iProtocol)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_SetProtocol)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return 1
EndFunc

;~ declare function		MySrv_GetProtocol		(mySrv as mySrv_t ptr) as integer
Func _MySrv_GetProtocol($pMySrv)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MySrv_GetProtocol", "ptr", $pMySrv)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_GetProtocol)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return $ret[0]
EndFunc

;~ declare function		MySrv_GetAddr			(mySrv as mySrv_t ptr, ip as zstring, ip_len as uinteger, port as ushort ptr) as integer
Func _MySrv_GetAddr($pMySrv)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $struct = DllStructCreate("char[512]")
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MySrv_GetAddr", "ptr", $pMySrv, "ptr", DllStructGetPtr($struct), "uint", 512, "ushort*", 0)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_GetAddr)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	If $ret[0] = 0 Then Return 0
	; ---
	Local $aRet[2] = [DllStructGetData($struct, 1), $ret[4]]
	Return $aRet
EndFunc

Func _MySrv_GetAddrStr($pMySrv, $iWithPort)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $struct = DllStructCreate("char[512]")
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MySrv_GetAddr", "ptr", $pMySrv, "ptr", DllStructGetPtr($struct), "uint", 512, "ushort*", 0)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_GetAddr)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	If $ret[0] = 0 Then Return ""
	; ---
	Local $sRet = DllStructGetData($struct, 1)
	If $iWithPort Then
		If StringInStr($sRet, ":") Then $sRet = "[" & $sRet & "]"
		$sRet &= ":" & $ret[4]
	EndIf
	Return $sRet
EndFunc

;~ declare sub 			MySrv_SetDefKeepAlive	(mySrv as mySrv_t ptr, timeout as uinteger, interval as uinteger)
Func _MySrv_SetDefKeepAlive($pMySrv, $iTimeOut, $iInterval)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	DllCall($__gMySock_hDll, "none:cdecl", "MySrv_SetDefKeepAlive", "ptr", $pMySrv, "uint", $iTimeOut, "uint", $iInterval)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_SetDefKeepAlive)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return 1
EndFunc

;~ declare sub 			MySrv_GetDefKeepAlive	(mySrv as mySrv_t ptr, timeout as uinteger ptr, interval as uinteger ptr)
Func _MySrv_GetDefKeepAlive($pMySrv)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "none:cdecl", "MySrv_GetDefKeepAlive", "ptr", $pMySrv, "uint*", 0, "uint*", 0)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_GetDefKeepAlive)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Local $aRet[2] = [$ret[2], $ret[3]]
	Return $aRet
EndFunc

;~ declare function		MySrv_GetSocket			(mySrv as mySrv_t ptr) as integer
Func _MySrv_GetSocket($pMySrv)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MySrv_GetSocket", "ptr", $pMySrv)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_GetSocket)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return $ret[0]
EndFunc

;~ declare sub 			MySrv_SetUserData		(mySrv as mySrv_t ptr, user_data as any ptr)
Func _MySrv_SetUserData($pMySrv, $iUserData)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	DllCall($__gMySock_hDll, "none:cdecl", "MySrv_SetUserData", "ptr", $pMySrv, "ptr", $iUserData)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_SetUserData)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return 1
EndFunc

;~ declare function		MySrv_GetUserData		(mySrv as mySrv_t ptr) as any ptr
Func _MySrv_GetUserData($pMySrv)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "ptr:cdecl", "MySrv_GetUserData", "ptr", $pMySrv)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_GetUserData)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return $ret[0]
EndFunc

;~ declare sub 			MySrv_SetCallbacks	(mySrv as mySrv_t ptr, onConnect as mySrvOnConnectProc, onDisconnect as mySrvOnDisconnectProc, onRecv as mySrvOnRecvProc, onReceiving as mySrvOnReceivingProc, onTimeOut as mySrvOnTimeOutProc)
Func _MySrv_SetCallbacks($pMySrv, $sOnConnect, $sOnDisconnect, $sOnRecv, $sOnReceiving, $sOnTimeOut)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "none:cdecl", "MySrv_SetCallbacks", "ptr", $pMySrv, _
		"ptr", __MySock_SetCallback($sOnConnect, $MYSOCK_CB_SRV_ONCONNECT), _
		"ptr", __MySock_SetCallback($sOnDisconnect, $MYSOCK_CB_SRV_ONDISCONNECT), _
		"ptr", __MySock_SetCallback($sOnRecv, $MYSOCK_CB_SRV_ONPACKETRECV), _
		"ptr", __MySock_SetCallback($sOnReceiving, $MYSOCK_CB_SRV_ONRECEIVING), _
		"ptr", __MySock_SetCallback($sOnTimeOut, $MYSOCK_CB_SRV_ONTIMEOUT) _
	)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_SetCallbacks)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return 1
EndFunc

;~ declare sub 			MySrv_Process		(mySrv as mySrv_t ptr)
Func _MySrv_Process($pMySrv)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	DllCall($__gMySock_hDll, "none:cdecl", "MySrv_Process", "ptr", $pMySrv)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_Process)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return 1
EndFunc

;~ declare function 	MySrv_PeerSend	(mySrv as mySrv_t ptr, peer_id as integer, data_ as byte ptr, data_len as uinteger) as integer
Func _MySrv_PeerSend($pMySrv, $iPeerId, $bData)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $len = BinaryLen($bData)
	Local $struct = DllStructCreate("byte[" & $len & "]")
	DllStructSetData($struct, 1, $bData)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "none:cdecl", "MySrv_PeerSend", "ptr", $pMySrv, "int", $iPeerId, "ptr", DllStructGetPtr($struct, 1), "uint", $len)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_PeerSend)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return $ret[0]
EndFunc

;~ declare function 	MySrv_Broadcast	(mySrv as mySrv_t ptr, data_ as byte ptr, data_len as uinteger) as integer
Func _MySrv_Broadcast($pMySrv, $bData)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $len = BinaryLen($bData)
	Local $struct = DllStructCreate("byte[" & $len & "]")
	DllStructSetData($struct, 1, $bData)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MySrv_Broadcast", "ptr", $pMySrv, "ptr", DllStructGetPtr($struct, 1), "uint", $len)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_Broadcast)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return $ret[0]
EndFunc

;~ declare function 	MySrv_Close		(mySrv as mySrv_t ptr, peer_id as integer) as integer
Func _MySrv_Close($pMySrv, $iPeerId)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MySrv_Close", "ptr", $pMySrv, "int", $iPeerId)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_Close)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return $ret[0]
EndFunc

;~ declare function 	MySrv_CloseAll	(mySrv as mySrv_t ptr) as integer
Func _MySrv_CloseAll($pMySrv)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MySrv_CloseAll", "ptr", $pMySrv)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_CloseAll)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return $ret[0]
EndFunc

;~ declare function 	MySrv_PeersCount	(mySrv as mySrv_t ptr) as integer
Func _MySrv_PeersCount($pMySrv)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MySrv_PeersCount", "ptr", $pMySrv)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_PeersCount)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return $ret[0]
EndFunc

;~ declare function 	MySrv_PeersGetAll	(mySrv as mySrv_t ptr, peer_ids as integer ptr, peers_ids_size as uinteger) as integer
Func _MySrv_PeersGetAll($pMySrv, $iMax = -1)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	If $iMax = -1 Then $iMax = _MySrv_PeersCount($pMySrv)
	If $iMax = 0 Then Return 0
	; ---
	Local $struct = DllStructCreate("int[" & $iMax & "]")
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MySrv_PeersGetAll", "ptr", $pMySrv, "ptr", DllStructGetPtr($struct, 1), "uint", $iMax)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_PeersGetAll)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	If $ret[0] = 0 Then Return 0
	; ---
	Local $aRet[$ret[0] + 1]
	$aRet[0] = $ret[0]
	For $i = 1 To $aRet[0]
		$aRet[$i] = DllStructGetData($struct, 1, $i)
	Next
	; ---
	Return $aRet
EndFunc

;~ declare function 	MySrv_PeersIterate	(mySrv as mySrv_t ptr, callback as mySrvIterateProc, user_data as any ptr) as integer
Func _MySrv_PeersIterate($pMySrv, $sFunc, $iUserData)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $fnc = DllCallbackRegister($sFunc, "int:cdecl", "ptr;int;ptr")
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MySrv_PeersIterate", "ptr", $pMySrv, "ptr", DllCallbackGetPtr($fnc), "ptr", $iUserData)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_PeersIterate)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	DllCallbackFree($fnc)
	; ---
	Return $ret[0]
EndFunc

;~ declare function		MySrv_PeerGetAddr			(mySrv as mySrv_t ptr, peer_id as integer, ip as zstring, ip_len as integer, port as ushort ptr) as integer
Func _MySrv_PeerGetAddr($pMySrv, $iPeerId)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $struct = DllStructCreate("char[512]")
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MySrv_PeerGetAddr", "ptr", $pMySrv, "int", $iPeerId, "ptr", DllStructGetPtr($struct), "uint", 512, "ushort*", 0)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_PeerGetAddr)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	If $ret[0] = 0 Then Return 0
	; ---
	Local $aRet[2] = [DllStructGetData($struct, 1), $ret[5]]
	Return $aRet
EndFunc

Func _MySrv_PeerGetAddrStr($pMySrv, $iPeerId, $iWithPort)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $struct = DllStructCreate("char[512]")
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MySrv_PeerGetAddr", "ptr", $pMySrv, "int", $iPeerId, "ptr", DllStructGetPtr($struct), "uint", 512, "ushort*", 0)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_PeerGetAddr)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	If $ret[0] = 0 Then Return ""
	; ---
	Local $sRet = DllStructGetData($struct, 1)
	If $iWithPort Then
		If StringInStr($sRet, ":") Then $sRet = "[" & $sRet & "]"
		$sRet &= ":" & $ret[5]
	EndIf
	Return $sRet
EndFunc

;~ declare sub 			MySrv_PeerSetKeepAlive		(mySrv as mySrv_t ptr, peer_id as integer, timeout as uinteger, interval as uinteger)
Func _MySrv_PeerSetKeepAlive($pMySrv, $iPeerId, $iTimeOut, $iInterval)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	DllCall($__gMySock_hDll, "none:cdecl", "MySrv_PeerSetKeepAlive", "ptr", $pMySrv, "int", $iPeerId, "uint", $iTimeOut, "uint", $iInterval)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_PeerSetKeepAlive)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return 1
EndFunc

;~ declare sub 			MySrv_PeerGetKeepAlive		(mySrv as mySrv_t ptr, peer_id as integer, timeout as uinteger ptr, interval as uinteger ptr)
Func _MySrv_PeerGetKeepAlive($pMySrv, $iPeerId)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "none:cdecl", "MySrv_PeerGetKeepAlive", "ptr", $pMySrv, "int", $iPeerId, "uint*", 0, "uint*", 0)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_PeerGetKeepAlive)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Local $aRet[2] = [$ret[3], $ret[4]]
	Return $aRet
EndFunc

;~ declare function		MySrv_PeerGetSocket		(muSrv as mySrv_t ptr, peer_id as integer) as integer
Func _MySrv_PeerGetSocket($pMySrv, $iPeerId)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MySrv_PeerGetSocket", "ptr", $pMySrv, "int", $iPeerId)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_PeerGetSocket)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return $ret[0]
EndFunc

;~ declare function 	MySrv_PeerSetUserData		(mySrv as mySrv_t ptr, peer_id as integer, user_data as any ptr) as integer
Func _MySrv_PeerSetUserData($pMySrv, $iPeerId, $iUserData)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MySrv_PeerSetUserData", "ptr", $pMySrv, "int", $iPeerId, "ptr", $iUserData)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_PeerSetUserData)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return $ret[0]
EndFunc

;~ declare function 	MySrv_PeerGetUserData		(mySrv as mySrv_t ptr, peer_id as integer) as any ptr
Func _MySrv_PeerGetUserData($pMySrv, $iPeerId)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "ptr:cdecl", "MySrv_PeerGetUserData", "ptr", $pMySrv, "int", $iPeerId)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MySrv_PeerGetUserData)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return $ret[0]
EndFunc

; ===============================================================================================================================

;~ declare function 	MyCln_Create		(host as zstring, port as ushort, protocol as protocol_e) as myCln_t ptr
Func _MyCln_Create($sHost, $iPort, $iProtocol = $MYSOCK_PROT_AUTO)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "ptr:cdecl", "MyCln_Create", "str", $sHost, "ushort", $iPort, "int", $iProtocol)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MyCln_Create)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return $ret[0]
EndFunc

;~ declare function 	MyCln_Destroy		(myCln as myCln_t ptr) as integer
Func _MyCln_Destroy($pMyCln)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MyCln_Destroy", "ptr", $pMyCln)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MyCln_Destroy)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return $ret[0]
EndFunc

;~ declare function 	MyCln_Connect		(myCln as myCln_t ptr, timeout as uinteger) as integer
Func _MyCln_Connect($pMyCln, $iTimeOut)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MyCln_Connect", "ptr", $pMyCln, "uint", $iTimeOut)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MyCln_Connect)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return $ret[0]
EndFunc

;~ declare function 	MyCln_Close			(myCln as myCln_t ptr) as integer
Func _MyCln_Close($pMyCln)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MyCln_Close", "ptr", $pMyCln)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MyCln_Close)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return $ret[0]
EndFunc

;~ declare function 	MyCln_IsConnected	(myCln as myCln_t ptr) as integer
Func _MyCln_IsConnected($pMyCln)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MyCln_IsConnected", "ptr", $pMyCln)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MyCln_IsConnected)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return $ret[0]
EndFunc

;~ declare sub 			MyCln_SetProtocol		(myCln as myCln_t ptr, protocol as protocol_e)
Func _MyCln_SetProtocol($pMyCln, $iProtocol = $MYSOCK_PROT_AUTO)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	DllCall($__gMySock_hDll, "none:cdecl", "MyCln_SetProtocol", "ptr", $pMyCln, "int", $iProtocol)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MyCln_SetProtocol)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return 1
EndFunc

;~ declare function		MyCln_GetProtocol		(myCln as myCln_t ptr) as integer
Func _MyCln_GetProtocol($pMyCln)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MyCln_GetProtocol", "ptr", $pMyCln)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MyCln_GetProtocol)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return $ret[0]
EndFunc

;~ declare sub 			MyCln_SetHost			(myCln as myCln_t ptr, host as zstring, port as ushort)
Func _MyCln_SetHost($pMyCln, $sHost, $iPort)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	DllCall($__gMySock_hDll, "none:cdecl", "MyCln_SetHost", "ptr", $pMyCln, "str", $sHost, "ushort", $iPort)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MyCln_SetHost)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return 1
EndFunc

;~ declare sub 			MyCln_GetHost			(myCln as myCln_t ptr, host as zstring, host_len as uinteger, port as ushort ptr)
Func _MyCln_GetHost($pMyCln)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $struct = DllStructCreate("char[512]")
	; ---
	Local $ret = DllCall($__gMySock_hDll, "none:cdecl", "MyCln_GetHost", "ptr", $pMyCln, "ptr", DllStructGetPtr($struct), "uint", 512, "ushort*", 0)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MyCln_GetHost)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Local $aRet[2] = [DllStructGetData($struct, 1), $ret[4]]
	Return $aRet
EndFunc

Func _MyCln_GetHostStr($pMyCln, $iWithPort)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $struct = DllStructCreate("char[512]")
	; ---
	Local $ret = DllCall($__gMySock_hDll, "none:cdecl", "MyCln_GetHost", "ptr", $pMyCln, "ptr", DllStructGetPtr($struct), "uint", 512, "ushort*", 0)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MyCln_GetHost)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Local $sRet = DllStructGetData($struct, 1)
	If $iWithPort Then
		If StringInStr($sRet, ":") Then $sRet = "[" & $sRet & "]"
		$sRet &= ":" & $ret[4]
	EndIf
	Return $sRet
EndFunc

;~ declare sub 			MyCln_GetSrvIp			(myCln as myCln_t ptr, ip as zstring, ip_len as uinteger)
Func _MyCln_GetSrvIp($pMyCln)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $struct = DllStructCreate("char[512]")
	; ---
	DllCall($__gMySock_hDll, "none:cdecl", "MyCln_GetSrvIp", "ptr", $pMyCln, "ptr", DllStructGetPtr($struct, 1), "uint", 512)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MyCln_GetSrvIp)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return DllStructGetData($struct, 1)
EndFunc

;~ declare sub 			MyCln_SetCallbacks	(myCln as myCln_t ptr, onDisconnect as myClnOnDisconnectProc, onRecv as myClnOnRecvProc, onReceiving as myClnOnReceivingProc, onTimeOut as myClnOnTimeOutProc)
Func _MyCln_SetCallbacks($pMyCln, $sOnDisconnect, $sOnRecv, $sOnReceiving, $sOnTimeOut)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	DllCall($__gMySock_hDll, "none:cdecl", "MyCln_SetCallbacks", "ptr", $pMyCln, _
		"ptr", __MySock_SetCallback($sOnDisconnect, $MYSOCK_CB_CLN_ONDISCONNECT), _
		"ptr", __MySock_SetCallback($sOnRecv, $MYSOCK_CB_CLN_ONPACKETRECV), _
		"ptr", __MySock_SetCallback($sOnReceiving, $MYSOCK_CB_CLN_ONRECEIVING), _
		"ptr", __MySock_SetCallback($sOnTimeOut, $MYSOCK_CB_CLN_ONTIMEOUT) _
	)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MyCln_SetCallbacks)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return 1
EndFunc

;~ declare sub 			MyCln_Process		(myCln as myCln_t ptr)
Func _MyCln_Process($pMyCln)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	DllCall($__gMySock_hDll, "none:cdecl", "MyCln_Process", "ptr", $pMyCln)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MyCln_Process)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return 1
EndFunc

;~ declare function 	MyCln_Send	(myCln as myCln_t ptr, data_ as byte ptr, data_len as uinteger) as integer
Func _MyCln_Send($pMyCln, $bData)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $len = BinaryLen($bData)
	Local $struct = DllStructCreate("byte[" & $len & "]")
	DllStructSetData($struct, 1, $bData)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MyCln_Send", "ptr", $pMyCln, "ptr", DllStructGetPtr($struct), "uint", $len)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MyCln_Send)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return $ret[0]
EndFunc

;~ declare function 	MyCln_SetKeepAlive 	(myCln as myCln_t ptr, timeout as uinteger, interval as uinteger) as integer
Func _MyCln_SetKeepAlive($pMyCln, $iTimeOut, $iInterval)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MyCln_SetKeepAlive", "ptr", $pMyCln, "uint", $iTimeOut, "uint", $iInterval)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MyCln_SetKeepAlive)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return $ret[0]
EndFunc

;~ declare sub 			MyCln_GetKeepAlive 	(myCln as myCln_t ptr, timeout as uinteger ptr, interval as uinteger ptr)
Func _MyCln_GetKeepAlive($pMyCln)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "none:cdecl", "MyCln_GetKeepAlive", "ptr", $pMyCln, "uint*", 0, "uint*", 0)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MyCln_GetKeepAlive)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Local $aRet[2] = [$ret[2], $ret[3]]
	Return $aRet
EndFunc

;~ declare function		MyCln_GetSocket		(myCln as myCln_t ptr) as integer
Func _MyCln_GetSocket($pMyCln)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "int:cdecl", "MyCln_GetSocket", "ptr", $pMyCln)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MyCln_GetSocket)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return $ret[0]
EndFunc

;~ declare sub 			MyCln_SetUserData 	(myCln as myCln_t ptr, user_data as any ptr)
Func _MyCln_SetUserData($pMyCln, $iUserData)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	DllCall($__gMySock_hDll, "none:cdecl", "MyCln_SetUserData", "ptr", $pMyCln, "ptr", $iUserData)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MyCln_SetUserData)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return 1
EndFunc

;~ declare function 	MyCln_GetUserData 	(myCln as myCln_t ptr) as any ptr
Func _MyCln_GetUserData($pMyCln)
	If $__gMySock_hDll = -1 Then Return SetError(-1, 0, 0)
	; ---
	Local $ret = DllCall($__gMySock_hDll, "ptr:cdecl", "MyCln_GetUserData", "ptr", $pMyCln)
	If @error Then
		Local $err = @error
		If Not @Compiled Then ConsoleWrite("! DllCall error " & $err & " (MyCln_GetUserData)" & @CRLF)
		Return SetError($err, 0, 0)
	EndIf
	; ---
	Return $ret[0]
EndFunc

; #INTERNAL_USE_ONLY# ===========================================================================================================

Func __MySock_SetCallback($sFunc, $iCbId)
	DllCallbackFree($__gMySock_aCallbacks[$iCbId][0])
	$__gMySock_aCallbacks[$iCbId][0] = 0
	; ---
	If $sFunc Then
		Switch $iCbId
			Case $MYSOCK_CB_SRV_ONPACKETRECV
				$__gMySock_sSrvOnPacketRecv = $sFunc
				$sFunc = "__MySock_SpecCb_SrvOnRecv"
			; ---
			Case $MYSOCK_CB_SRV_ONTIMEOUT
				$__gMySock_sSrvOnTimeOut = $sFunc
				$sFunc = "__MySock_SpecCb_SrvOnTimeOut"
			; ---
			Case $MYSOCK_CB_CLN_ONPACKETRECV
				$__gMySock_sClnOnPacketRecv = $sFunc
				$sFunc = "__MySock_SpecCb_ClnOnRecv"
			; ---
			Case $MYSOCK_CB_CLN_ONTIMEOUT
				$__gMySock_sClnOnTimeOut = $sFunc
				$sFunc = "__MySock_SpecCb_ClnOnTimeOut"
		EndSwitch
		; ---
		$__gMySock_aCallbacks[$iCbId][0] = DllCallbackRegister($sFunc, $__gMySock_aCallbacks[$iCbId][1], $__gMySock_aCallbacks[$iCbId][2])
	Else
		Switch $iCbId
			Case $MYSOCK_CB_SRV_ONPACKETRECV
				$__gMySock_sSrvOnPacketRecv = ""
			Case $MYSOCK_CB_SRV_ONTIMEOUT
				$__gMySock_sSrvOnTimeOut = ""
			Case $MYSOCK_CB_CLN_ONPACKETRECV
				$__gMySock_sClnOnPacketRecv = ""
			Case $MYSOCK_CB_CLN_ONTIMEOUT
				$__gMySock_sClnOnTimeOut = ""
		EndSwitch
	EndIf
	; ---
	Return DllCallbackGetPtr($__gMySock_aCallbacks[$iCbId][0])
EndFunc

Func __MySock_FreeCallbacks()
	For $i = 0 To UBound($__gMySock_aCallbacks) - 1
		DllCallbackFree($__gMySock_aCallbacks[$i][0])
	Next
EndFunc

Func __MySock_SpecCb_SrvOnRecv($pMySrv, $iPeerId, $pData, $iDataLen)
	Local $struct = DllStructCreate("byte[" & $iDataLen & "]", $pData)
	Call($__gMySock_sSrvOnPacketRecv, $pMySrv, $iPeerId, DllStructGetData($struct, 1))
EndFunc

Func __MySock_SpecCb_SrvOnTimeOut($pMySrv, $iPeerId, $pPartialData, $iDataLen, $iExceptedLen)
	Local $struct = DllStructCreate("byte[" & $iDataLen & "]", $pPartialData)
	Call($__gMySock_sSrvOnTimeOut, $pMySrv, $iPeerId, DllStructGetData($struct, 1), $iExceptedLen)
EndFunc

Func __MySock_SpecCb_ClnOnRecv($pMyCln, $pData, $iDataLen)
	Local $struct = DllStructCreate("byte[" & $iDataLen & "]", $pData)
	Call($__gMySock_sClnOnPacketRecv, $pMyCln, DllStructGetData($struct, 1))
EndFunc

Func __MySock_SpecCb_ClnOnTimeOut($pMyCln, $pPartialData, $iDataLen, $iExceptedLen)
	Local $struct = DllStructCreate("byte[" & $iDataLen & "]", $pPartialData)
	Call($__gMySock_sClnOnTimeOut, $pMyCln, DllStructGetData($struct, 1), $iExceptedLen)
EndFunc


remove FindAndSelectInListView

remove FindAndSelectInListView

; This is free and unencumbered software released into the public domain.
;
; Anyone is free to copy, modify, publish, use, compile, sell, or
; distribute this software, either in source code form or as a compiled
; binary, for any purpose, commercial or non-commercial, and by any
; means.
;
; In jurisdictions that recognize copyright laws, the author or authors
; of this software dedicate any and all copyright interest in the
; software to the public domain. We make this dedication for the benefit
; of the public at large and to the detriment of our heirs and
; successors. We intend this dedication to be an overt act of
; relinquishment in perpetuity of all present and future rights to this
; software under copyright law.
;
; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
; IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
; OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
; ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
; OTHER DEALINGS IN THE SOFTWARE.
;
; For more information, please refer to <http://unlicense.org/>

#include <Constants.au3>
#include <Misc.au3>
#include <Date.au3>

; Налоговый период.
Global Const $g_iYear = 2016
; Исполняемый файл программы Декларация.
Global Const $g_sDeclBinary = "Decl" & String($g_iYear) & ".exe"
; Полный путь к программе.
Global Const $g_sPath = StringFormat("%s\Декларация %d\%s", @ProgramFilesDir, $g_iYear, $g_sDeclBinary)
; Заголовок основного окна программы.
Global Const $g_sTitleMain = "Без имени - Декларация " & String($g_iYear)
; Координаты кнопки "Доходы за пределами РФ".
Global Const $g_iXxRus = 98, $g_iYxRus = 414
; Координаты кнопки "Добавить источник выплат".
Global Const $g_ixAddSrc = 206, $g_iYaddSrc = 113
; Источник выплаты дохода по умолчанию.
Global Const $g_sDefaultIncomeSource = "Брокер ""XXX"" (США)"
; Код страны по умолчанию (США).
Global Const $g_iDefaultCountryCode = 840
; Код валюты по умолчанию (доллар США).
Global Const $g_iDefaultCurrencyCode = 840
; Код дохода "Дивиденды".
Global Const $g_iIncomeTypeDividends = 1010

; Подготовка к вводу данных:
; запускаю программу,
; отмечаю "Имеются доходы - В иностранной валюте",
; перехожу в раздел "Доходы за пределами РФ",
; закрываю окно с сообщением "Не введен номер инспекции".
OpenDecl()

; Ввод данных о дивидендах:
; дата, поступившая сумма, удержанная брокером сумма, описание бумаги.
; Дата в формате YYYY-MM-DD т.е. ГГГГ-ММ-ДД т.е. ГОД-МЕСЯЦ-ДЕНЬ .
$sDesc = "ISHARES CORE US AGGREGATE BOND ETF"
FillDiv("2016-02-01", 22.8, 2.28, $sDesc)
; ...
FillDiv("2016-12-22", 24.86, 2.48, $sDesc)

$sDesc = "VANGUARD TOTAL BOND MARKET ETF"
FillDiv("2016-02-05", 17.44, 1.74, $sDesc)
; ...
FillDiv("2016-12-07", 16.02, 1.60, $sDesc)
FillDiv("2016-12-29", 1.14, 0, $sDesc)    ; LT Cap Gain
FillDiv("2016-12-29", 2.43, 0, $sDesc)    ; ST Cap Gain

$sDesc = "RDR UNITED COMPANY RUSAL PLC"
FillDiv("2016-11-08", 1230, 0, $sDesc, "Брокер ""YYY"" (Россия)", 832, 643)

Func FillDiv($sDate, $fGross, $fWithheld, $sSecDesc, _
              $sSource = $g_sDefaultIncomeSource, _
              $iCountry = $g_iDefaultCountryCode, _
              $iCurrency = $g_iDefaultCurrencyCode _
)
  ; Проверяю, что строка с описанием источника не пустая.
  If $sSource = "" Then
    MsgBox($MB_SYSTEMMODAL, "Ошибка", "Не задано описание источника выплаты")
    Exit
  EndIf
  ; Проверяю, что доход - положительный.
  If $fGross <= 0 Then
    MsgBox($MB_SYSTEMMODAL, "Ошибка", "Отрицательный или нулевой доход: " & String($fGross))
    Exit
  EndIf
  ; Проверяю, что удержанная сумма - неотрицательная.
  If $fWithheld < 0 Then
    MsgBox($MB_SYSTEMMODAL, "Ошибка", "Отрицательная удержанная сумма: " & String($fWithheld))
    Exit
  EndIf
  ; Проверяю, что удержано меньше, чем поступило.
  If $fWithheld >= $fGross Then
    MsgBox($MB_SYSTEMMODAL, "Ошибка", "Удержано больше(равно), чем поступило: " & String($fWithheld))
    Exit
  EndIf
  ; Проверяю, что дата - существующая.
  If Not _DateIsValid($sDate) Then
    MsgBox($MB_SYSTEMMODAL, "Ошибка", "Несуществующая дата: " & $sDate)
    Exit
  EndIf
  ; Перевожу строку даты в числа.
  Local $aDate, $aTime
  _DateTimeSplit($sDate, $aDate, $aTime)
  Local $iYear = $aDate[1], $iMonth = $aDate[2], $iDay = $aDate[3]
  ; Проверяю, что год введённой даты равен году налогового периода.
  If $iYear <> $g_iYear Then
    MsgBox($MB_SYSTEMMODAL, "Ошибка", "Неверный год в дате: " & $sDate)
    Exit
  Endif
  ; Проверяю, что дата - рабочий день.
  if _DateToDayOfWeekISO($iYear, $iMonth, $iDay) > 5 Then
    MsgBox($MB_SYSTEMMODAL, "Ошибка", "Дата - выходной: " & $sDate)
    Exit
  EndIf
  ; Нажимаю "Добавить источник выплат".
  MouseClick($MOUSE_CLICK_LEFT, $g_ixAddSrc, $g_iYaddSrc)
  ; Жду появления окна "Доход".
  Local $sTitleIncome = "Доход"
  WinWaitActive($sTitleIncome)
  ; Заполняю "Наименование источника выплаты"
  Local $sIncomeSrc = StringFormat("%s Дивиденд от %02d.%02d.%d %s", $sSource, $iDay, $iMonth, $iYear, $sSecDesc)
  ControlSetText($sTitleIncome, "", "[CLASS:TMaskedEdit; INSTANCE:2]", $sIncomeSrc)
  ; Заполняю "Код страны".
  OpenListViewThenFindAndSelectNumber($sTitleIncome, "[CLASS:TButton; INSTANCE:1]", "Справочник стран", $iCountry)
  If @error Then
    MsgBox($MB_SYSTEMMODAL, "Ошибка", "Неизвестный код страны: " & String($iCountry))
    Exit
  EndIf
  ; Нажимаю "Да".
  ControlClick($sTitleIncome, "", "[CLASS:TButton; INSTANCE:2]")
  ; Жду появления основного окна.
  WinWaitActive($g_sTitleMain);
  ; Заполняю "Дата получения дохода".
  SetDateTimePicker($g_sTitleMain, "[CLASS:TDateTimePicker; INSTANCE:2]", $iDay, $iMonth, $iYear)
  ; Заполняю "Дата уплаты налога".
  SetDateTimePicker($g_sTitleMain, "[CLASS:TDateTimePicker; INSTANCE:1]", $iDay, $iMonth, $iYear)
  ; Заполняю "Код и наименование валюты".
  OpenListViewThenFindAndSelectNumber($g_sTitleMain, "[CLASS:TButton; INSTANCE:3]", "Справочник валют", $iCurrency)
  If @error Then
    MsgBox($MB_SYSTEMMODAL, "Ошибка", "Неизвестный код валюты: " & String($iCurrency))
    Exit
  EndIf
  ; Отмечаю "Автоматическое определение курса валюты".
  ControlClick($g_sTitleMain, "", "[CLASS:TCheckBox; INSTANCE:1]")
  ; Заполняю "Код дохода".
  OpenListViewThenFindAndSelectNumber($g_sTitleMain, "[CLASS:TButton; INSTANCE:2]", "Справочник видов доходов", $g_iIncomeTypeDividends)
  If @error Then
    MsgBox($MB_SYSTEMMODAL, "Ошибка", "Неизвестный код дохода: " & String($g_iIncomeTypeDividends))
    Exit
  EndIf
  ; Заполняю "Полученный доход" - "В иностранной валюте"
  ControlSetText($g_sTitleMain, "", "[CLASS:TMaskedEdit; INSTANCE:3]", FloatToStringRus($fGross))
  ; Заполняю "Налог, уплаченный в иностранном государстве" - "В иностранной валюте".
  ControlSetText($g_sTitleMain, "", "[CLASS:TMaskedEdit; INSTANCE:2]", FloatToStringRus($fWithheld))
EndFunc

Func OpenListViewThenFindAndSelectNumber($sTitleParent, $sCtrlButtonOpen, $sTitleLv, $iNumberToSelect)
  ; Нажимаю кнопку вызова списка.
  ControlClick($sTitleParent, "", $sCtrlButtonOpen)
  ; Жду появления окна списка.
  WinWaitActive($sTitleLv)
  ; Ищу строку в списке.
  Local $sCtrlLv = "[CLASS:TListView; INSTANCE:1]"
  Local $iIndex = ControlListView($sTitleLv, "", $sCtrlLv, "FindItem" , String($iNumberToSelect))
  if $iIndex = -1 Then
    SetError(1)
    Return
  Endif
  ; Выбираю строку в списке.
  ControlListView($sTitleLv, "", $sCtrlLv, "Select" , $iIndex)
  ; Нажимаю кнопку подтвержения выбора.
  Local $sCtrlButtonConfirm = "[CLASS:TButton; INSTANCE:2]"
  ControlClick($sTitleLv, "", $sCtrlButtonConfirm)
  ; Жду появления родительского окна.
  WinWaitActive($sTitleParent)
EndFunc

Func SetDateTimePicker($sTitle, $sCtrl, $iDay, $iMonth, $iYear)
  ; Устанавливаю фокус на элемент выбора даты.
  ControlFocus($sTitle, "", $sCtrl)
  Local $iDelayMs = 100
  ; Если дата в элементе вводится не первый раз, то фокус будет на годе.
  ; Нажимаю два раза стрелку влево, чтобы установить фокус на день.
  Local Static $iCalls = 0
  $iCalls += 1
  If $iCalls > 2 Then
    ControlSend($sTitle, "", $sCtrl, "{LEFT}")
    Sleep($iDelayMs)
    ControlSend($sTitle, "", $sCtrl, "{LEFT}")
    Sleep($iDelayMs)
  Endif
  ; Ввожу компонент даты, нажимаю стрелку вправо.
  ControlSend($sTitle, "", $sCtrl, String($iDay))
  Sleep($iDelayMs)
  ControlSend($sTitle, "", $sCtrl, "{RIGHT}")
  Sleep($iDelayMs)
  ControlSend($sTitle, "", $sCtrl, String($iMonth))
  Sleep($iDelayMs)
  ControlSend($sTitle, "", $sCtrl, "{RIGHT}")
  Sleep($iDelayMs)
  ControlSend($sTitle, "", $sCtrl, String($iYear))
  Sleep($iDelayMs)
EndFunc

Func OpenDecl()
  ; Проверяю, что разрешение экрана, не менее 1024 в ширину
  If @DeskTopWidth < 1024 Then
    MsgBox($MB_SYSTEMMODAL, "Ошибка", "Разрешение экрана не поддерживается: " & @DeskTopWidth & "x" & @DeskTopHeight)
    Exit
  EndIf
  ; Проверяю, что запущен только один экземляр этого скрипта.
  If _Singleton(@ScriptName, 1) = 0 Then
    MsgBox($MB_SYSTEMMODAL, "Ошибка", "Скрипт " & @ScriptName & " уже запущен")
    Exit
  EndIf
  ; Проверяю, что Декларация не запущена.
  If ProcessExists($g_sDeclBinary) Then
    MsgBox($MB_SYSTEMMODAL, "Ошибка", "Программа Декларация уже запущена")
    Exit
  Endif
  ; Запускаю программу.
  Run($g_sPath)
  ; Жду появления основного окна.
  WinWaitActive($g_sTitleMain)
  ; Разворачиваю окно на весь экран.
  WinSetState($g_sTitleMain, "", @SW_MAXIMIZE)
  ; Отмечаю "Имеются доходы - В иностранной валюте".
  ControlClick($g_sTitleMain, "", "[TEXT:В иностранной валюте]")
  ; Перехожу в раздел "Доходы за пределами РФ".
  MouseClick($MOUSE_CLICK_LEFT, $g_iXxRus, $g_iYxRus)
  ; Жду появления окна с сообщением "Не введен номер инспекции".
  Local $sTitleErr = "Проверка: Задание условий"
  WinWaitActive($sTitleErr)
  ; Нажимаю "Пропустить".
  ControlClick($sTitleErr, "", "[TEXT:Пропустить]")
  ; Жду появления основного окна.
  WinWaitActive($g_sTitleMain)
EndFunc

Func FloatToStringRus($fVal)
  ; Преобразую число в строку, заменяю точку на запятую.
  Return StringReplace(String($fVal), ".", ",")
EndFunc

; This is free and unencumbered software released into the public domain.
;
; Anyone is free to copy, modify, publish, use, compile, sell, or
; distribute this software, either in source code form or as a compiled
; binary, for any purpose, commercial or non-commercial, and by any
; means.
;
; In jurisdictions that recognize copyright laws, the author or authors
; of this software dedicate any and all copyright interest in the
; software to the public domain. We make this dedication for the benefit
; of the public at large and to the detriment of our heirs and
; successors. We intend this dedication to be an overt act of
; relinquishment in perpetuity of all present and future rights to this
; software under copyright law.
;
; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
; IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
; OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
; ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
; OTHER DEALINGS IN THE SOFTWARE.
;
; For more information, please refer to <http://unlicense.org/>

#include <Constants.au3>
#include <Misc.au3>
#include <Date.au3>

; Налоговый период.
Global Const $g_iYear = 2016
; Исполняемый файл программы Декларация.
Global Const $g_sDeclBinary = "Decl" & String($g_iYear) & ".exe"
; Полный путь к программе.
Global Const $g_sPath = StringFormat("%s\Декларация %d\%s", @ProgramFilesDir, $g_iYear, $g_sDeclBinary)
; Заголовок основного окна программы.
Global Const $g_sTitleMain = "Без имени - Декларация " & String($g_iYear)
; Координаты кнопки "Доходы за пределами РФ".
Global Const $g_iXxRus = 98, $g_iYxRus = 414
; Координаты кнопки "Добавить источник выплат".
Global Const $g_ixAddSrc = 206, $g_iYaddSrc = 113
; Источник выплаты дохода по умолчанию.
Global Const $g_sDefaultIncomeSource = "Брокер ""XXX"" (США)"
; Код страны по умолчанию (США).
Global Const $g_iDefaultCountryCode = 840
; Код валюты по умолчанию (доллар США).
Global Const $g_iDefaultCurrencyCode = 840
; Код дохода "Дивиденды".
Global Const $g_iIncomeTypeDividends = 1010

; Подготовка к вводу данных:
; запускаю программу,
; отмечаю "Имеются доходы - В иностранной валюте",
; перехожу в раздел "Доходы за пределами РФ",
; закрываю окно с сообщением "Не введен номер инспекции".
OpenDecl()

; Ввод данных о дивидендах:
; дата, поступившая сумма, удержанная брокером сумма, описание бумаги.
; Дата в формате YYYY-MM-DD т.е. ГГГГ-ММ-ДД т.е. ГОД-МЕСЯЦ-ДЕНЬ .
$sDesc = "ISHARES CORE US AGGREGATE BOND ETF"
FillDiv("2016-02-01", 22.8, 2.28, $sDesc)
; ...
FillDiv("2016-12-22", 24.86, 2.48, $sDesc)

$sDesc = "VANGUARD TOTAL BOND MARKET ETF"
FillDiv("2016-02-05", 17.44, 1.74, $sDesc)
; ...
FillDiv("2016-12-07", 16.02, 1.60, $sDesc)
FillDiv("2016-12-29", 1.14, 0, $sDesc)    ; LT Cap Gain
FillDiv("2016-12-29", 2.43, 0, $sDesc)    ; ST Cap Gain

$sDesc = "RDR UNITED COMPANY RUSAL PLC"
FillDiv("2016-11-08", 1230, 0, $sDesc, "Брокер ""YYY"" (Россия)", 832, 643)

Func FillDiv($sDate, $fGross, $fWithheld, $sSecDesc, _
              $sSource = $g_sDefaultIncomeSource, _
              $iCountry = $g_iDefaultCountryCode, _
              $iCurrency = $g_iDefaultCurrencyCode _
)
  ; Проверяю, что строка с описанием источника не пустая.
  If $sSource = "" Then
    MsgBox($MB_SYSTEMMODAL, "Ошибка", "Не задано описание источника выплаты")
    Exit
  EndIf
  ; Проверяю, что доход - положительный.
  If $fGross <= 0 Then
    MsgBox($MB_SYSTEMMODAL, "Ошибка", "Отрицательный или нулевой доход: " & String($fGross))
    Exit
  EndIf
  ; Проверяю, что удержанная сумма - неотрицательная.
  If $fWithheld < 0 Then
    MsgBox($MB_SYSTEMMODAL, "Ошибка", "Отрицательная удержанная сумма: " & String($fWithheld))
    Exit
  EndIf
  ; Проверяю, что удержано меньше, чем поступило.
  If $fWithheld >= $fGross Then
    MsgBox($MB_SYSTEMMODAL, "Ошибка", "Удержано больше(равно), чем поступило: " & String($fWithheld))
    Exit
  EndIf
  ; Проверяю, что дата - существующая.
  If Not _DateIsValid($sDate) Then
    MsgBox($MB_SYSTEMMODAL, "Ошибка", "Несуществующая дата: " & $sDate)
    Exit
  EndIf
  ; Перевожу строку даты в числа.
  Local $aDate, $aTime
  _DateTimeSplit($sDate, $aDate, $aTime)
  Local $iYear = $aDate[1], $iMonth = $aDate[2], $iDay = $aDate[3]
  ; Проверяю, что год введённой даты равен году налогового периода.
  If $iYear <> $g_iYear Then
    MsgBox($MB_SYSTEMMODAL, "Ошибка", "Неверный год в дате: " & $sDate)
    Exit
  Endif
  ; Проверяю, что дата - рабочий день.
  if _DateToDayOfWeekISO($iYear, $iMonth, $iDay) > 5 Then
    MsgBox($MB_SYSTEMMODAL, "Ошибка", "Дата - выходной: " & $sDate)
    Exit
  EndIf
  ; Нажимаю "Добавить источник выплат".
  MouseClick($MOUSE_CLICK_LEFT, $g_ixAddSrc, $g_iYaddSrc)
  ; Жду появления окна "Доход".
  Local $sTitleIncome = "Доход"
  WinWaitActive($sTitleIncome)
  ; Заполняю "Наименование источника выплаты"
  Local $sIncomeSrc = StringFormat("%s Дивиденд от %02d.%02d.%d %s", $sSource, $iDay, $iMonth, $iYear, $sSecDesc)
  ControlSetText($sTitleIncome, "", "[CLASS:TMaskedEdit; INSTANCE:2]", $sIncomeSrc)
  ; Заполняю "Код страны".
  OpenListViewThenFindAndSelectNumber($sTitleIncome, "[CLASS:TButton; INSTANCE:1]", "Справочник стран", $iCountry)
  If @error Then
    MsgBox($MB_SYSTEMMODAL, "Ошибка", "Неизвестный код страны: " & String($iCountry))
    Exit
  EndIf
  ; Нажимаю "Да".
  ControlClick($sTitleIncome, "", "[CLASS:TButton; INSTANCE:2]")
  ; Жду появления основного окна.
  WinWaitActive($g_sTitleMain);
  ; Заполняю "Дата получения дохода".
  SetDateTimePicker($g_sTitleMain, "[CLASS:TDateTimePicker; INSTANCE:2]", $iDay, $iMonth, $iYear)
  ; Заполняю "Дата уплаты налога".
  SetDateTimePicker($g_sTitleMain, "[CLASS:TDateTimePicker; INSTANCE:1]", $iDay, $iMonth, $iYear)
  ; Заполняю "Код и наименование валюты".
  OpenListViewThenFindAndSelectNumber($g_sTitleMain, "[CLASS:TButton; INSTANCE:3]", "Справочник валют", $iCurrency)
  If @error Then
    MsgBox($MB_SYSTEMMODAL, "Ошибка", "Неизвестный код валюты: " & String($iCurrency))
    Exit
  EndIf
  ; Отмечаю "Автоматическое определение курса валюты".
  ControlClick($g_sTitleMain, "", "[CLASS:TCheckBox; INSTANCE:1]")
  ; Заполняю "Код дохода".
  OpenListViewThenFindAndSelectNumber($g_sTitleMain, "[CLASS:TButton; INSTANCE:2]", "Справочник видов доходов", $g_iIncomeTypeDividends)
  If @error Then
    MsgBox($MB_SYSTEMMODAL, "Ошибка", "Неизвестный код дохода: " & String($g_iIncomeTypeDividends))
    Exit
  EndIf
  ; Заполняю "Полученный доход" - "В иностранной валюте"
  ControlSetText($g_sTitleMain, "", "[CLASS:TMaskedEdit; INSTANCE:3]", FloatToStringRus($fGross))
  ; Заполняю "Налог, уплаченный в иностранном государстве" - "В иностранной валюте".
  ControlSetText($g_sTitleMain, "", "[CLASS:TMaskedEdit; INSTANCE:2]", FloatToStringRus($fWithheld))
EndFunc

Func FindAndSelectInListView($sTitle, $sCtrl, $sStringToSelect)
  Local $iIndex = ControlListView($sTitle, "", $sCtrl, "FindItem" , $sStringToSelect)
  if $iIndex = -1 Then
    SetError(1)
    Return
  Endif
  ControlListView($sTitle, "", $sCtrl, "Select" , $iIndex)
EndFunc

Func OpenListViewThenFindAndSelectNumber($sTitleParent, $sCtrlButtonOpen, $sTitleLv, $iNumberToSelect)
  ; Нажимаю кнопку вызова списка.
  ControlClick($sTitleParent, "", $sCtrlButtonOpen)
  ; Жду появления окна списка.
  WinWaitActive($sTitleLv)
  ; Ищу строку в списке.
  Local $sCtrlLv = "[CLASS:TListView; INSTANCE:1]"
  Local $iIndex = ControlListView($sTitleLv, "", $sCtrlLv, "FindItem" , String($iNumberToSelect))
  if $iIndex = -1 Then
    SetError(1)
    Return
  Endif
  ; Выбираю строку в списке.
  ControlListView($sTitleLv, "", $sCtrlLv, "Select" , $iIndex)
  ; Нажимаю кнопку подтвержения выбора.
  Local $sCtrlButtonConfirm = "[CLASS:TButton; INSTANCE:2]"
  ControlClick($sTitleLv, "", $sCtrlButtonConfirm)
  ; Жду появления родительского окна.
  WinWaitActive($sTitleParent)
EndFunc

Func SetDateTimePicker($sTitle, $sCtrl, $iDay, $iMonth, $iYear)
  ; Устанавливаю фокус на элемент выбора даты.
  ControlFocus($sTitle, "", $sCtrl)
  Local $iDelayMs = 100
  ; Если дата в элементе вводится не первый раз, то фокус будет на годе.
  ; Нажимаю два раза стрелку влево, чтобы установить фокус на день.
  Local Static $iCalls = 0
  $iCalls += 1
  If $iCalls > 2 Then
    ControlSend($sTitle, "", $sCtrl, "{LEFT}")
    Sleep($iDelayMs)
    ControlSend($sTitle, "", $sCtrl, "{LEFT}")
    Sleep($iDelayMs)
  Endif
  ; Ввожу компонент даты, нажимаю стрелку вправо.
  ControlSend($sTitle, "", $sCtrl, String($iDay))
  Sleep($iDelayMs)
  ControlSend($sTitle, "", $sCtrl, "{RIGHT}")
  Sleep($iDelayMs)
  ControlSend($sTitle, "", $sCtrl, String($iMonth))
  Sleep($iDelayMs)
  ControlSend($sTitle, "", $sCtrl, "{RIGHT}")
  Sleep($iDelayMs)
  ControlSend($sTitle, "", $sCtrl, String($iYear))
  Sleep($iDelayMs)
EndFunc

Func OpenDecl()
  ; Проверяю, что разрешение экрана, не менее 1024 в ширину
  If @DeskTopWidth < 1024 Then
    MsgBox($MB_SYSTEMMODAL, "Ошибка", "Разрешение экрана не поддерживается: " & @DeskTopWidth & "x" & @DeskTopHeight)
    Exit
  EndIf
  ; Проверяю, что запущен только один экземляр этого скрипта.
  If _Singleton(@ScriptName, 1) = 0 Then
    MsgBox($MB_SYSTEMMODAL, "Ошибка", "Скрипт " & @ScriptName & " уже запущен")
    Exit
  EndIf
  ; Проверяю, что Декларация не запущена.
  If ProcessExists($g_sDeclBinary) Then
    MsgBox($MB_SYSTEMMODAL, "Ошибка", "Программа Декларация уже запущена")
    Exit
  Endif
  ; Запускаю программу.
  Run($g_sPath)
  ; Жду появления основного окна.
  WinWaitActive($g_sTitleMain)
  ; Разворачиваю окно на весь экран.
  WinSetState($g_sTitleMain, "", @SW_MAXIMIZE)
  ; Отмечаю "Имеются доходы - В иностранной валюте".
  ControlClick($g_sTitleMain, "", "[TEXT:В иностранной валюте]")
  ; Перехожу в раздел "Доходы за пределами РФ".
  MouseClick($MOUSE_CLICK_LEFT, $g_iXxRus, $g_iYxRus)
  ; Жду появления окна с сообщением "Не введен номер инспекции".
  Local $sTitleErr = "Проверка: Задание условий"
  WinWaitActive($sTitleErr)
  ; Нажимаю "Пропустить".
  ControlClick($sTitleErr, "", "[TEXT:Пропустить]")
  ; Жду появления основного окна.
  WinWaitActive($g_sTitleMain)
EndFunc

Func FloatToStringRus($fVal)
  ; Преобразую число в строку, заменяю точку на запятую.
  Return StringReplace(String($fVal), ".", ",")
EndFunc


Update mancala.au3

Update mancala.au3

; #FUNCTION# ====================================================================================================================
; Name ..........: MyRiverRaid
; Author ........: Luigi (Luismar Chechelaky)
; Description ...: um simples exemplo utilizando IrrLitch para renderizar um avião movendo-se por um mapa finito em loop infinito.
;                  O mapa foi construído com o Tiled Map Editor e o código traduz imagens 2d em seu equivalente em 3D.
; Link ..........: https://github.com/chechelaky/MyRiverRaid/
;
; Q muda a camera (3 ângulos)
; T sobe
; G desce
; setas (cima, baixo, esquerda, direita) movimento
; ===============================================================================================================================