Code : Tout sélectionner
#include <array.au3>
#include <String.au3>
Global $Global_Connexion_Listen = -1
Global $Global_Connexion_ListenForList[1][2]
Global $Global_Connexion_ListenForCount = 1
Global $Global_Connexion_ListenForCall
$Global_Connexion_ListenForList[0][0] = "ping"
$Global_Connexion_ListenForList[0][1] = "return OK"
; #FUNCTION# ====================================================================================================================
; Name...........: RemoteServ
; Description ...: Se met en attente de connexion de la part d'une IP et sur un port spécifié
; Syntax.........: RemoteServ($ip, $port, $async)
; Parameters ....: $ip - l'IP d'attente de client
; $port - le port à utiliser
; $async - si true, l'application est en mode asynchrone, elle ne peut se connecter qu'à un client à la fois mais toute les tâches d'écoute sont en parallèle
; Return values .: Success - Socket connecté
; Failure - Rien
; Author ........: Alexandre THOUVENIN (poke123)
; Remarks .......:
; Related .......: Remote, TestClient, ListenStart, ListenEnd
; Example .......: No
; ===============================================================================================================================
func RemoteServ($ip, $port, $async = true)
TCPStartup()
$socket = TCPListen($ip, $port)
while true
$adr = TCPAccept($socket)
If $adr >= 0 Then
_Debug("Connexion réussi")
ListenStart($adr, $async)
return $adr
EndIf
WEnd
EndFunc
; #FUNCTION# ====================================================================================================================
; Name...........: Remote
; Description ...: Se connecte à une application en attente
; Syntax.........: Remote($ip, $port, $async)
; Parameters ....: $ip - l'IP du client en attente
; $port - le port à utiliser
; $async - si true, l'application est en mode asynchrone, elle ne peut se connecter qu'à un client à la fois mais toute les tâches d'écoute sont en parallèle
; Return values .: Success - Socket connecté
; Failure - Rien
; Author ........: Alexandre THOUVENIN (poke123)
; Remarks .......:
; Related .......: RemoteServ, TestClient, ListenStart, ListenEnd
; Example .......: No
; ===============================================================================================================================
func Remote($ip, $port, $async = true)
TCPStartup()
$adr = TCPConnect($ip, $port)
ListenStart($adr, $async)
return $adr
EndFunc
; #FUNCTION# ====================================================================================================================
; Name...........: TestClient
; Description ...: Vérifie si un client distant est en train d'écouter
; Syntax.........: TestClient($adr)
; Parameters ....: $adr - le socket retourné par Remote ou RemoteServ
; Return values .: Success - true si le cient est en écoute, false sinon
; Failure - Rien
; Author ........: Alexandre THOUVENIN (poke123)
; Remarks .......:
; Related .......: Remote, RemoteServ
; Example .......: No
; ===============================================================================================================================
func TestClient($adr)
SendData("ping", $adr)
return ListenFor("OK", $adr, 3000) <> $adr
EndFunc
; #FUNCTION# ====================================================================================================================
; Name...........: ListenStart
; Description ...: Enregistre les fonctions de base et commence l'écoute asynchrone (en mode async)
; Syntax.........: ListenStart($adr, $async)
; Parameters ....: $adr - le socket retourné par Remote ou RemoteServ
; $async - si true, la fonction Listen est enregistré en Adlib, ceci limite à un seul client pour le script
; Return values .: Success - rien
; Failure - Rien
; Author ........: Alexandre THOUVENIN (poke123)
; Remarks .......:
; Related .......: ListenEnd, Listen, ListenFor
; Example .......: No
; ===============================================================================================================================
func ListenStart($adr, $async)
ListenEnd()
if $async then $Global_Connexion_Listen = $adr
ListenForRegister("call (.+)", "_call")
ListenForRegister("get (.+)", "_get")
ListenForRegister("set (.+)", "_set")
if $async then AdlibRegister("Listen", 200)
EndFunc
; #FUNCTION# ====================================================================================================================
; Name...........: ListenEnd
; Description ...: Ferme l'écoute asynchrone et retire les fonction d'écoute de base
; Syntax.........: ListenEnd()
; Parameters ....: aucun
; Return values .: Success - rien
; Failure - Rien
; Author ........: Alexandre THOUVENIN (poke123)
; Remarks .......:
; Related .......: ListenStart, Listen, ListenFor
; Example .......: No
; ===============================================================================================================================
func ListenEnd() ;ferme l'écoute
AdlibUnRegister("Listen")
$Global_Connexion_Listen = -1
$Global_Connexion_ListenForCount = 1
dim $Global_Connexion_ListenForList[1][2]
$Global_Connexion_ListenForList[0][0] = "ping"
$Global_Connexion_ListenForList[0][1] = "return OK"
EndFunc
; #FUNCTION# ====================================================================================================================
; Name...........: Listen
; Description ...: Ecoute le client distant spécifié, décode le format de donnée utilisé et effectue les actions enregistré
; Syntax.........: Listen($adr)
; Parameters ....: $adr - le socket retourné par Remote ou RemoteServ
; Return values .: Success - rien
; Failure - Rien
; Author ........: Alexandre THOUVENIN (poke123)
; Remarks .......:
; Related .......: SendData, ListenFor, ListenForRegister, ListenForUnregister
; Example .......: No
; ===============================================================================================================================
func Listen($adr = default)
if IsDeclared("adr") <> -1 or $adr = Default then $adr = $Global_Connexion_Listen
$d = TCPRecv($adr, 1000)
if $d = "" then return 0
_Debug("Reception de: " & $d)
$data = _StringBetween($d , "<data>", "</data>")
for $i = 0 to UBound($data) - 1
_Debug("Instruction: " & $data[$i])
_do($data[$i], $adr)
Next
EndFunc
; #FUNCTION# ====================================================================================================================
; Name...........: SendData
; Description ...: Formate et envoi des données au client distant spécifié
; Syntax.........: SendData($data, $adr)
; Parameters ....: $data - Le texte à envoyer
; $adr - le socket retourné par Remote ou RemoteServ
; Return values .: Success - rien
; Failure - Rien
; Author ........: Alexandre THOUVENIN (poke123)
; Remarks .......:
; Related .......: Listen, ListenFor, ListenForRegister, ListenForUnregister
; Example .......: No
; ===============================================================================================================================
func SendData($data, $adr = default)
if IsDeclared("adr") <> -1 or $adr = Default then $adr = $Global_Connexion_Listen
_Debug("Envoi de: " & $data & " à " & $adr)
TCPSend($adr, "<data>" & $data & "</data>")
EndFunc
; #FUNCTION# ====================================================================================================================
; Name...........: ListenFor
; Description ...: Attend la réception d'un certain Pattern de donnée
; Syntax.........: ListenFor($text, $dest, $timeout)
; Parameters ....: $text - Le pattern à attendre (voir expression régulière)
; $dest - le socket retourné par Remote ou RemoteServ
; $timeout - le temps avant d'abandonner l'écoute
; Return values .: Success - Le premier résultat renvoyé par l'expression régulière
; Failure - $dest
; Author ........: Alexandre THOUVENIN (poke123)
; Remarks .......:
; Related .......: Listen, ListenForRegister, ListenForUnregister
; Example .......: No
; ===============================================================================================================================
func ListenFor($text, $dest = Default, $timeout = 10000)
if $dest = Default then $dest = $Global_Connexion_Listen
$Global_Connexion_ListenForCall = $dest
ListenForRegister($text, "_ListenForCallback")
$i = 0
while $Global_Connexion_ListenForCall = $dest And $i < $timeout
if $dest <> $Global_Connexion_Listen then Listen($dest)
Sleep(100)
$i += 100
WEnd
ListenForUnregister($text)
return $Global_Connexion_ListenForCall
EndFunc
;fonction utilisé en interne
func _ListenForCallback($data, $r, $sender)
if $Global_Connexion_ListenForCall = $sender then $Global_Connexion_ListenForCall = $r[0]
EndFunc
; #FUNCTION# ====================================================================================================================
; Name...........: ListenForRegister
; Description ...: Associe un pattern de donnée à une fonction
; Syntax.........: ListenForRegister($text, $CallBack)
; Parameters ....: $text - Le pattern à attendre (voir expression régulière)
; $CallBack - la fonction de type function($data_reçu_en_entier, $resultat_du_regexp, $adr_de_l'envoyeur)
; Return values .: Success - rien
; Failure - rien
; Author ........: Alexandre THOUVENIN (poke123)
; Remarks .......:
; Related .......: Listen, ListenFor, ListenForUnregister
; Example .......: No
; ===============================================================================================================================
func ListenForRegister($text, $callback) ;ListenFor assynchrone (qui s'execute en parallèle, sans valeur de retour)
$Global_Connexion_ListenForCount += 1
ReDim $Global_Connexion_ListenForList[$Global_Connexion_ListenForCount][2]
$Global_Connexion_ListenForList[$Global_Connexion_ListenForCount - 1][0] = $text
$Global_Connexion_ListenForList[$Global_Connexion_ListenForCount - 1][1] = $callback
EndFunc
; #FUNCTION# ====================================================================================================================
; Name...........: ListenForUnRegister
; Description ...: Arrête d'écouter un pattern spécifique
; Syntax.........: ListenForUnRegister($text)
; Parameters ....: $text - Le pattern qu'il faut cesser d'écouter
; Return values .: Success - rien
; Failure - rien
; Author ........: Alexandre THOUVENIN (poke123)
; Remarks .......:
; Related .......: Listen, ListenFor, ListenForRegister, ListenForUnregisterNbr
; Example .......: No
; ===============================================================================================================================
func ListenForUnregister($text) ;arrête une écoute
for $i = 0 to $Global_Connexion_ListenForCount - 1
if $Global_Connexion_ListenForList[$i][0] = $text then ListenForUnregisterNbr($i)
Next
EndFunc
; #FUNCTION# ====================================================================================================================
; Name...........: ListenForUnRegisterNbr
; Description ...: Retire de la liste d'écoute un pattern avec l'index spécifié (interne mais pouvant servir quand même)
; Syntax.........: ListenForUnRegister($nbr)
; Parameters ....: $nbr - L'index du pattern qu'il faut cesser d'écouter
; Return values .: Success - rien
; Failure - rien
; Author ........: Alexandre THOUVENIN (poke123)
; Remarks .......:
; Related .......: Listen, ListenFor, ListenForRegister, ListenForUnregister
; Example .......: No
; ===============================================================================================================================
func ListenForUnregisterNbr($nbr)
_ArrayDelete($Global_Connexion_ListenForList, $nbr)
$Global_Connexion_ListenForCount -= 1
EndFunc
; #FUNCTION# ====================================================================================================================
; Name...........: CallDistant
; Description ...: Appelle une fonction du client avec les arguments spécifié
; Syntax.........: CallDistant($function, $params, $dest)
; Parameters ....: $function - le nom de la fonction (pas de fonction build-in !!!)
; $params - Les arguments de la fonction contenu dans un array
; $dest - le socket retourné par Remote ou RemoteServ
; Return values .: Success - la valeur de retour de la fonction
; Failure - $dest
; Author ........: Alexandre THOUVENIN (poke123)
; Remarks .......:
; Related .......: GetDistant, SetDistant
; Example .......: No
; ===============================================================================================================================
func CallDistant($function, $params, $dest = Default) ;appele une fonction de l'autre client
if $dest = Default then $dest = $Global_Connexion_Listen
$a = "call " & $function & "|" & _ArrayToString($params, "|")
SendData($a, $dest)
return ListenFor("result (.+)", $dest)
EndFunc
; #FUNCTION# ====================================================================================================================
; Name...........: GetDistant
; Description ...: Obtient la valeur d'une variable du client
; Syntax.........: GetDistant($var, $dest)
; Parameters ....: $var - le nom de la variable (sans le "$" !!!!)
; $dest - le socket retourné par Remote ou RemoteServ
; Return values .: Success - la valeur de la variable
; Failure - $dest
; Author ........: Alexandre THOUVENIN (poke123)
; Remarks .......:
; Related .......: CallDistant, SetDistant
; Example .......: No
; ===============================================================================================================================
func GetDistant($var, $dest = Default) ;obtient une variable (sans le "$") de l'autre client
if $dest = Default then $dest = $Global_Connexion_Listen
SendData("get " & $var, $dest)
return ListenFor("result (.+)", $dest)
EndFunc
; #FUNCTION# ====================================================================================================================
; Name...........: SetDistant
; Description ...: Change la valeur d'une variable du client
; Syntax.........: SetDistant($var, $value, $dest)
; Parameters ....: $var - Le nom de la variable (sans le "$" !!!!)
; $value - La valeur à assigner
; $dest - le socket retourné par Remote ou RemoteServ
; Return values .: Success - la valeur de retour de la fonction
; Failure - $dest
; Author ........: Alexandre THOUVENIN (poke123)
; Remarks .......:
; Related .......: CallDistant, GetDistant
; Example .......: No
; ===============================================================================================================================
func SetDistant($var, $value, $dest = Default) ;change la valeur d'une variable de l'autre client
if $dest = Default then $dest = $Global_Connexion_Listen
SendData("set " & $var & " " & $value, $dest)
ListenFor("OK", $dest)
EndFunc
;Fonctions utilisées en interne
func _Debug($msg)
ConsoleWrite($msg & @CRLf)
EndFunc
func FatalError($msg)
Msgbox(0, "Erreur Fatale", $msg)
Exit
EndFunc
func _call($data, $m, $sender)
$m = StringSplit($m[0], "|")
_ArrayDelete($m, 0)
switch UBound($m)
case 1
$a = Call($m[0])
case 2
$a = Call($m[0], $m[1])
case 3
$a = Call($m[0], $m[1], $m[2])
case 4
$a = Call($m[0], $m[1], $m[2], $m[3])
case 5
$a = Call($m[0], $m[1], $m[2], $m[3], $m[4])
case 6
$a = Call($m[0], $m[1], $m[2], $m[3], $m[4], $m[5])
case 7
$a = Call($m[0], $m[1], $m[2], $m[3], $m[4], $m[5], $m[6])
case 8
$a = Call($m[0], $m[1], $m[2], $m[3], $m[4], $m[5], $m[6], $m[7])
case 9
$a = Call($m[0], $m[1], $m[2], $m[3], $m[4], $m[5], $m[6], $m[7], $m[8])
EndSwitch
SendData("result " & $a, $sender)
EndFunc
func _get($data, $m, $sender)
SendData("result " & Eval($m[0]), $sender)
EndFunc
func _set($data, $m, $sender)
$m = StringSplit($m[0], " ")
_ArrayDelete($m, 0)
Assign($m[0], $m[1])
SendData("OK", $sender)
EndFunc
func _Do($data, $sender = Default)
if $sender = Default then $sender = $Global_Connexion_Listen
for $i = 0 to $Global_Connexion_ListenForCount - 1
$match = StringRegExp($data, $Global_Connexion_ListenForList[$i][0], 0)
$matches = StringRegExp($data, $Global_Connexion_ListenForList[$i][0], 3)
if $match and StringLeft($Global_Connexion_ListenForList[$i][1], 7) = "return " Then
SendData(StringTrimLeft($Global_Connexion_ListenForList[$i][1], 7), $sender)
ElseIf $match Then
_Debug("correspondance à: " & $Global_Connexion_ListenForList[$i][0])
Call($Global_Connexion_ListenForList[$i][1], $data, $matches, $sender)
EndIf
next
EndFunc