Quantcast
Channel: AutoIt v3 - General Help and Support
Viewing all articles
Browse latest Browse all 12506

Help improve this UDP server-less chat script

$
0
0

This script is going to be part of a bigger project that I'm working on (HINT: It involves UDP).

The following code may/may not have bugs in it, I'm posting it because I need suggestions on how to improve it.

AutoIt         
  1. #include <ButtonConstants.au3>
  2. #include <EditConstants.au3>
  3. #include <GUIConstantsEx.au3>
  4. #include <GuiIPAddress.au3>
  5. #include <StaticConstants.au3>
  6. #include <WindowsConstants.au3>
  7. #include <GuiListView.au3>
  8. #include <Array.au3>
  9.  
  10. DirRemove(@ScriptDir & "\BackUp", 1)
  11.  
  12. Local $ClientList[1][2]
  13. For $x = 0 To UBound($ClientList) - 1
  14.     For $y = 0 To 1
  15.         $ClientList[$x][$y] = -1
  16.     Next
  17.  
  18. $UserName = @ComputerName
  19. $Form1 = GUICreate($UserName & " - ChAT TeST", 445, 200, -1, -1)
  20.  
  21. $Edit1 = GUICtrlCreateEdit("", 5, 5, 282, 161)
  22. GUICtrlSetFont(-1, 7.5, Default, Default, "Lucida Console")
  23. GUICtrlSetData(-1, "Chat Log:", "append")
  24. GUICtrlSetState(-1, $GUI_DISABLE)
  25.  
  26. $Input1 = GUICtrlCreateInput("", 5, 169, 281, 21)
  27. GUICtrlSendMsg(-1, 0x1500 + 1, False, 'Type here and press ENTER to send')
  28. GUICtrlSetState(-1, $GUI_DISABLE)
  29.  
  30. $IPAddress3 = _GUICtrlIpAddress_Create($Form1, 290, 5, 130, 21)
  31. _GUICtrlIpAddress_Set($IPAddress3, "0.0.0.0")
  32. $Button1 = GUICtrlCreateButton("Add", 420, 5, 25, 21)
  33. $hListview = GUICtrlCreateListView("#|IP", 290, 27, 155, 150)
  34.  
  35. $YOUR_IP_HERE = GUICtrlCreateInput(@IPAddress1, 300, 180, 130, 20, BitOR($SS_CENTERIMAGE, $SS_CENTER))
  36. GUICtrlSetTip(-1, "Your IP")
  37.  
  38. Local $INsocket = UDPBind(@IPAddress1, 80), $SYSTEMsocket = UDPBind(@IPAddress1, 81), $IP, $OUTsocket, $fAllowSend = True
  39.  
  40.  
  41. $RecvTimer = TimerInit()
  42.     $nMsg = GUIGetMsg()
  43.     If $nMsg = $GUI_EVENT_CLOSE Then Exit
  44.     If $nMsg = $YOUR_IP_HERE Then GUICtrlSetData($YOUR_IP_HERE, @IPAddress1)
  45.  
  46.     If $nMsg = $Input1 Then
  47.         $Text = GUICtrlRead($Input1)
  48.         If StringStripWS($Text, 8) <> "" And $fAllowSend = True Then
  49.             For $x = 0 To UBound($ClientList) - 1
  50.                 UDPSend($ClientList[$x][0], @IPAddress1 & "::" & $UserName & ": " & $Text)
  51.             Next
  52.             GUICtrlSetData($Edit1, @CRLF & $UserName & ":   " & $Text, "append")
  53.             GUICtrlSetData($Input1, "")
  54.         EndIf
  55.     EndIf
  56.  
  57.     If $nMsg = $Button1 Then
  58.         If $IP <> _GUICtrlIpAddress_Get($IPAddress3) And _GUICtrlIpAddress_Get($IPAddress3) <> @IPAddress1 And _GUICtrlIpAddress_Get($IPAddress3) <> "0.0.0.0" And Not _IsIPInList(_GUICtrlIpAddress_Get($IPAddress3)) Then
  59.             $IP = _GUICtrlIpAddress_Get($IPAddress3)
  60.             $OUTsocket = UDPOpen($IP, 81)
  61.             UDPSend($OUTsocket, "CR:" & @IPAddress1)
  62.             UDPCloseSocket($OUTsocket)
  63.  
  64.             $iSearch = -1
  65.             For $x = 0 To UBound($ClientList) - 1
  66.                 If $ClientList[$x][0] <> -1 Then ContinueLoop
  67.                 If $ClientList[$x][0] = -1 Then
  68.                     $iSearch = $x
  69.                     ExitLoop
  70.                 EndIf
  71.             Next
  72.             If $iSearch = -1 Then
  73.                 $Bounds = UBound($ClientList)
  74.                 ReDim $ClientList[$Bounds + 1][2]
  75.                 $iSearch = $Bounds
  76.             EndIf
  77.             $ClientList[$iSearch][0] = UDPOpen($IP, 80)
  78.             $ClientList[$iSearch][1] = $IP
  79.             ConsoleWrite("Connected to (CR): " & $IP & "    @error = " & @error & @CRLF)
  80.  
  81.             $List = "CL"
  82.             For $x = 0 To UBound($ClientList) - 1
  83.                 If $x = $iSearch Or $ClientList[$x][1] = @IPAddress1 Then ContinueLoop
  84.                 $List &= ":" & $ClientList[$x][1]
  85.             Next
  86.             If $List <> "CL" Then
  87.                 $OUTsocket = UDPOpen($ClientList[$iSearch][1], 81)
  88.                 UDPSend($OUTsocket, $List)
  89.                 UDPCloseSocket($OUTsocket)
  90.             EndIf
  91.  
  92.             For $x = 0 To UBound($ClientList) - 1
  93.                 If $x = $iSearch Then ContinueLoop
  94.                 $OUTsocket = UDPOpen($ClientList[$x][1], 81)
  95.                 UDPSend($OUTsocket, "CRR:" & $IP)
  96.                 UDPCloseSocket($OUTsocket)
  97.             Next
  98.  
  99.             GUICtrlSetState($Edit1, $GUI_ENABLE)
  100.             GUICtrlSetState($Input1, $GUI_ENABLE)
  101.             _GUICtrlIpAddress_Set($IPAddress3, "0.0.0.0")
  102.             _UpdateListView()
  103.         EndIf
  104.     EndIf
  105.  
  106.     If TimerDiff($RecvTimer) >= 10 Then
  107.         $data = UDPRecv($INsocket, 1000000)
  108.         If $data <> "" Then
  109.             If IsBinary($data) Then $data = BinaryToString($data)
  110.             If StringInStr($data, "::") Then
  111.                 $Exp = StringRegExp($data, "([0-9.]{7,15})::(.*)", 1)
  112.                 If @error == 0 And UBound($Exp) == 2 And _IsIPInList($Exp[0]) Then
  113.                     $fAllowSend = False
  114.                     GUICtrlSetData($Edit1, @CRLF & $Exp[1], "append")
  115.                     $fAllowSend = True
  116.                 EndIf
  117.             EndIf
  118.         EndIf
  119.  
  120.         $data = UDPRecv($SYSTEMsocket, 100000)
  121.         If $data <> "" Then
  122.             ConsoleWrite("Recieved: " & $data & @CRLF)
  123.  
  124.             $Exp = StringRegExp($data, "CR:([0-9.]{7,15})", 1)
  125.             If @error == 0 Then
  126.                 $IP = $Exp[0]
  127.                 $iSearch = -1
  128.                 For $x = 0 To UBound($ClientList) - 1
  129.                     If $ClientList[$x][0] <> -1 Then ContinueLoop
  130.                     If $ClientList[$x][0] = -1 Then
  131.                         $iSearch = $x
  132.                         ExitLoop
  133.                     EndIf
  134.                 Next
  135.                 If $iSearch = -1 Then
  136.                     $Bounds = UBound($ClientList)
  137.                     ReDim $ClientList[$Bounds + 1][2]
  138.                     $iSearch = $Bounds
  139.                 EndIf
  140.                 $ClientList[$iSearch][0] = UDPOpen($IP, 80)
  141.                 $ClientList[$iSearch][1] = $IP
  142.                 ConsoleWrite("Connected to (CR): " & $IP & "    @error = " & @error & @CRLF)
  143.  
  144.                 $List = "CL"
  145.                 For $x = 0 To UBound($ClientList) - 1
  146.                     If $x = $iSearch Or $ClientList[$x][1] = @IPAddress1 Then ContinueLoop
  147.                     $List &= ":" & $ClientList[$x][1]
  148.                 Next
  149.                 If $List <> "CL" Then
  150.                     $OUTsocket = UDPOpen($ClientList[$iSearch][1], 81)
  151.                     UDPSend($OUTsocket, $List)
  152.                     UDPCloseSocket($OUTsocket)
  153.                 EndIf
  154.  
  155.                 For $x = 0 To UBound($ClientList) - 1
  156.                     If $x = $iSearch Then ContinueLoop
  157.                     $OUTsocket = UDPOpen($ClientList[$x][1], 81)
  158.                     UDPSend($OUTsocket, "CRR:" & $IP)
  159.                     UDPCloseSocket($OUTsocket)
  160.                 Next
  161.  
  162.                 GUICtrlSetState($Edit1, $GUI_ENABLE)
  163.                 GUICtrlSetState($Input1, $GUI_ENABLE)
  164.                 _UpdateListView()
  165.                 If GUICtrlRead($Edit1) <> "Chat Log:" And MsgBox(4, "ChAt TeST", "Send Chat History to " & $IP & "?") = 6 Then
  166.                     $OUTsocket = UDPOpen($ClientList[$iSearch][1], 81)
  167.                     UDPSend($OUTsocket, "CH:" & @IPAddress1 & ":" & StringReplace(StringReplace(GUICtrlRead($Edit1), "/n", "//n"), @CRLF, "/n"))
  168.                     UDPCloseSocket($OUTsocket)
  169.                 EndIf
  170.             EndIf
  171.  
  172.             $Exp = StringRegExp($data, "CL:(.*)", 1)
  173.             If @error == 0 Then
  174.                 $List = StringSplit($Exp[0], ":")
  175.                 For $y = 1 To $List[0]
  176.                     $IP = $List[$y]
  177.                     $iSearch = -1
  178.                     For $x = 0 To UBound($ClientList) - 1
  179.                         If $ClientList[$x][0] <> -1 Then ContinueLoop
  180.                         If $ClientList[$x][0] = -1 Then
  181.                             $iSearch = $x
  182.                             ExitLoop
  183.                         EndIf
  184.                     Next
  185.                     If $iSearch = -1 Then
  186.                         $Bounds = UBound($ClientList)
  187.                         ReDim $ClientList[$Bounds + 1][2]
  188.                         $iSearch = $Bounds
  189.                     EndIf
  190.                     $ClientList[$iSearch][0] = UDPOpen($IP, 80)
  191.                     $ClientList[$iSearch][1] = $IP
  192.                     ConsoleWrite("Connected to(CL): " & $IP & " @error = " & @error & @CRLF)
  193.                     _UpdateListView()
  194.                 Next
  195.             EndIf
  196.  
  197.             $Exp = StringRegExp($data, "CRR:([0-9.]{7,15})", 1)
  198.             If @error == 0 Then
  199.                 $IP = $Exp[0]
  200.                 $iSearch = -1
  201.                 For $x = 0 To UBound($ClientList) - 1
  202.                     If $ClientList[$x][0] <> -1 Then ContinueLoop
  203.                     If $ClientList[$x][0] = -1 Then
  204.                         $iSearch = $x
  205.                         ExitLoop
  206.                     EndIf
  207.                 Next
  208.                 If $iSearch = -1 Then
  209.                     $Bounds = UBound($ClientList)
  210.                     ReDim $ClientList[$Bounds + 1][2]
  211.                     $iSearch = $Bounds
  212.                 EndIf
  213.                 $ClientList[$iSearch][0] = UDPOpen($IP, 80)
  214.                 $ClientList[$iSearch][1] = $IP
  215.                 ConsoleWrite("Connected to(CRR): " & $IP & "    @error = " & @error & @CRLF)
  216.  
  217.                 GUICtrlSetState($Edit1, $GUI_ENABLE)
  218.                 GUICtrlSetState($Input1, $GUI_ENABLE)
  219.                 _UpdateListView()
  220.             EndIf
  221.  
  222.             $Exp = StringRegExp($data, "DEL:([0-9.]{7,15})", 1)
  223.             If @error == 0 Then
  224.                 $iSearch = _ArraySearch($ClientList, $Exp[0])
  225.                 If $iSearch <> -1 Then
  226.                     UDPCloseSocket($ClientList[$iSearch][0])
  227.                     _ArrayDelete($ClientList, $iSearch)
  228.                     If Not IsArray($ClientList) Then
  229.                         Local $ClientList[1][2]
  230.                         $ClientList[0][0] = -1
  231.                         $ClientList[0][1] = -1
  232.                         GUICtrlSetData($Edit1, @CRLF & "Everyone has left you." & @CRLF & "Are you feeling lonely yet?" & @CRLF, "append")
  233.                         GUICtrlSetState($Edit1, $GUI_DISABLE)
  234.                         GUICtrlSetState($Input1, $GUI_DISABLE)
  235.                     Else
  236.                         GUICtrlSetData($Edit1, @CRLF & $Exp[0] & " left chat.", "append")
  237.                     EndIf
  238.                     _UpdateListView()
  239.                 EndIf
  240.             EndIf
  241.  
  242.             $Exp = StringRegExp($data, "CH:([0-9.]{7,15}):(.*)", 1)
  243.             If @error == 0 Then
  244.                 ConsoleWrite("Recieved(CH): " & $data & @CRLF)
  245.                 $fAllowSend = False
  246.                 GUICtrlSetData($Edit1, @CRLF & @CRLF & "Chat History From " & $Exp[0] & ":" & @CRLF & StringReplace(StringRegExpReplace($Exp[1], "([^/]/n)", @CRLF), "//n", "/n") & @CRLF, "append")
  247.                 $fAllowSend = True
  248.             EndIf
  249.         EndIf
  250.         $RecvTimer = TimerInit()
  251.     EndIf
  252.  
  253. Func _UpdateListView()
  254.     GUICtrlDelete($hListview)
  255.     $hListview = GUICtrlCreateListView("#|IP", 290, 27, 155, 150)
  256.     _GUICtrlListView_SetColumnWidth($hListview, 0, 25)
  257.     _GUICtrlListView_SetColumnWidth($hListview, 1, 125)
  258.     $IPList = ""
  259.     For $x = 0 To UBound($ClientList) - 1
  260.         If $ClientList[$x][1] = -1 Then ContinueLoop
  261.         GUICtrlCreateListViewItem($x + 1 & "|" & $ClientList[$x][1], $hListview)
  262.         $IPList = "|" & $ClientList[$x][1]
  263.     Next
  264.     $IPList = StringTrimLeft($IPList, 1)
  265. EndFunc   ;==>_UpdateListView
  266.  
  267. Func _IsIPInList($sIP)
  268.     $IPList = ""
  269.     For $x = 0 To UBound($ClientList) - 1
  270.         $IPList &= "|" & $ClientList[$x][1]
  271.     Next
  272.     $IPList = StringTrimLeft($IPList, 1)
  273.     If StringInStr($IPList, $sIP) Then
  274.         Return 1
  275.     Else
  276.         Return 0
  277.     EndIf
  278. EndFunc   ;==>_IsIPInList
  279.  
  280. Func Cleanup()
  281.     GUIDelete()
  282.     UDPCloseSocket($INsocket)
  283.     UDPCloseSocket($SYSTEMsocket)
  284.     If $OUTsocket Then UDPCloseSocket($OUTsocket)
  285.  
  286.     For $x = 0 To UBound($ClientList) - 1
  287.         If $ClientList[$x][0] = -1 Then ContinueLoop
  288.  
  289.         $OUTsocket = UDPOpen($ClientList[$x][1], 81)
  290.         UDPSend($OUTsocket, "DEL:" & @IPAddress1)
  291.         UDPCloseSocket($OUTsocket)
  292.  
  293.         UDPCloseSocket($ClientList[$x][0])
  294.         ConsoleWrite("Closed: " & $ClientList[$x][1] & ":80" & @CRLF)
  295.     Next
  296.  
  297.     UDPShutdown()
  298. EndFunc   ;==>Cleanup

Viewing all articles
Browse latest Browse all 12506

Trending Articles