themax90 Posted November 5, 2005 Share Posted November 5, 2005 (edited) Ok, I am running this Chat server script, the problem is it takes 50 - 90% CPU Usage and I cannot figure out WHY in the world it uses so much of my computer. I have only 128 memory and this is built for higher computer stats, but is there any way I can reduce how much it uses? Serverexpandcollapse popup; ---------------------------------------------------------------------------- ; AutoIt Version: 3.1.1++(BETA) ; Author: Max Gardner(AutoIt Smith;king.of.all@comcast.net) ; Script Function: ; Chat Server to talk amongst friends and peers. ; ---------------------------------------------------------------------------- #include <Inet.au3> TCPStartup() Global $Port = 8000; Port Global $MyIp = _GetIP(); Hosts Ip Address Global Const $MaxConnection = 2500; Maximum Users at a Time Dim $ConnectedSocket[ ($MaxConnection + 1) ] Dim $Recv, $MaxText = 512, $NewSocket = 0 Global $MainSocket = TCPListen($MyIp, $Port, $MaxConnection) If $MainSocket = -1 Then Error(0) ElseIf $MainSocket <> - 1 Then $RogueSocket = -1 EndIf ClearSockets() MsgBox(0, "Ready", "Your server on " & $MyIp & " is ready!") While 1 $Track = 0 For $Track = 0 To $MaxConnection Step 1 If $ConnectedSocket[$Track] <> -1 Then ; There Is A User Connected $Recv = TCPRecv($ConnectedSocket[$Track], $MaxText) If $Recv <> "" Then ; Message Received SendMessage($Recv) Else ; No Message Received EndIf Else ; There Is Not A User Connected EndIf Next If $NewSocket <> 1 And $NewSocket = 0 Then $CurrentUserOpen = FindOpenSocket() $NewSocket = 1 EndIf $ConnectedSocket[$CurrentUserOpen] = TCPAccept($MainSocket) If $ConnectedSocket[$CurrentUserOpen] <> - 1 Then ; User Connected To Socket $NewSocket = 0 IniWrite( "Master.ini", "Socket", $CurrentUserOpen, "In Use") Else ; User Did Not Connect EndIf WEnd Func ClearSockets() $Track = 0 For $Track = 0 To $MaxConnection Step 1 IniWrite( "Master.ini", "Socket", $Track, "0") Next EndFunc ;==>ClearSockets Func Error($ErrorNumber = -1) Select Case $ErrorNumber = 0 MsgBox(0, "Error", "Unknown Internal Error") Case $ErrorNumber = 1 MsgBox(0, "Error", "Corrupt Ini File") Case Else MsgBox(0, "Error", "Unknown Error") EndSelect EndFunc ;==>Error Func FindOpenSocket() $Track = 0 For $Track = 0 To $MaxConnection Step 1 $OpenSocket = IniRead( "Master.ini", "Socket", $Track, -1) Select Case $OpenSocket = -1 Error(1) Case $OpenSocket <> -1 And $OpenSocket = 0 And $OpenSocket <> "In Use" ; Socket Open ExitLoop Case Else ; Socket Not Open EndSelect Next Return $Track EndFunc ;==>FindOpenSocket Func SendMessage($Text, $User = 0) If $User = 0 Then ; User Not Defined $Track = 0 For $Track = 0 To $MaxConnection Step 1 If $ConnectedSocket[$Track] <> -1 Then ; User Is Connected TCPSend($ConnectedSocket[$Track], $Text) Else ; User Is Not Connected EndIf Next Else ; User Defined EndIf EndFunc ;==>SendMessage Func OnAutoItExit() $Track = 0 For $Track = 0 To $MaxConnection Step 1 If $ConnectedSocket[$Track] <> - 1 And $ConnectedSocket[$Track] > 0 Then ; A User Is Connected TCPSend($ConnectedSocket[$Track], "~~bye-Server is Shutting down, please try again later.") TCPCloseSocket($ConnectedSocket[$Track]) Else ; A User Is Not Connected TCPCloseSocket($ConnectedSocket[$Track]) EndIf Next TCPShutdown() EndFunc ;==>OnAutoItExit Here is the client if anyone wants to try and use it, I am working on the advanced version right now. It will have alot more functionality. I was testing the new advanced and saw the usage, then tested the basic one and was amazed. I think it might be the TCPRecv loop. Just a guess Client expandcollapse popup; ---------------------------------------------------------------------------- ; AutoIt Version: 3.1.1++(BETA) ; Author: Max Gardner(AutoIt Smith;king.of.all@comcast.net) ; Script Function: ; Chat Client to talk amongst friends and peers using the Chat Server. ; ---------------------------------------------------------------------------- #include <Inet.au3> TCPStartup() Global $Port = 8000; Port Global $MyIp = _GetIP(); Hosts Ip Address Dim $MainSocket, $ConnectedSocket, $Recv, $MaxText = 512 GUICreate("Client", 290, 320, (@DesktopWidth - 290) / 2, (@DesktopHeight - 320) / 2, 0x00CF0000 + 0x10000000 + 0x04000000) $Message = GUICtrlCreateEdit("", 10, 210, 270, 60, 0x00200000 + 0x0040) $Send = GUICtrlCreateButton("Send Message", 150, 280, 130, 30) $Connect = GUICtrlCreateButton("Connect To Server", 10, 280, 130, 30) $History = GUICtrlCreateEdit("", 10, 10, 270, 190, 0x00200000 + 0x0040 + 0x0800) GUICtrlSetState($Send, 128) GUISetState() While 1 $Msg = GUIGetMsg() Select Case $Msg = -3 ExitLoop Case $Msg = $Connect $ConnectIp = InputBox("Connect To Server....", "Please input the Ip Address of the server you wish to connect to:") If $ConnectIp = "" Or @Error = 1 Then ; User Canceled ElseIf $ConnectIp <> "" Or @Error <> 1 Then ; User Typed Ip $ConnectIp = IpTest($ConnectIp) If $ConnectIp = -1 Then Error(2) ElseIf $ConnectIp <> - 1 Then ; IpAddress Is Valid Global $MainSocket = TCPConnect($ConnectIp, $Port) If $MainSocket = -1 Then Error(0) ElseIf $MainSocket <> - 1 Then $RogueSocket = -1 GUICtrlSetState($Connect, 128) GUICtrlSetState($Send, 64) EndIf EndIf EndIf Case $Msg = $Send $SM = GUICtrlRead($Message) GUICtrlSetData($Message, "") TCPSend($MainSocket, $SM) Case Else ;;; EndSelect $Recv = TCPRecv($MainSocket, $MaxText) If $Recv <> "" Then ; Message Received GUICtrlSetData($History, GUICtrlRead($History) & @CRLF & $Recv) Else ; No Message Received EndIf WEnd Exit Func Error($ErrorNumber = -1) Select Case $ErrorNumber = 0 MsgBox(0, "Error", "Unknown Internal Error") Case $ErrorNumber = 1 MsgBox(0, "Error", "Corrupt Ini File") Case $ErrorNumber = 2 MsgBox(0, "Error", "Invalid Server IpAddress") Case Else MsgBox(0, "Error", "Unknown Error") EndSelect EndFunc ;==>Error Func IpTest($IpAddress) $Ips = StringSplit($IpAddress, ".") If $IpAddress = "" Or StringIsDigit($Ips[1]) = 0 Or StringIsDigit($Ips[2]) = 0 Or StringIsDigit($Ips[3]) = 0 Or StringIsDigit($Ips[4]) = 0 Then Return -1 Else Return $IpAddress EndIf EndFunc ;==>IpTest Func OnAutoItExit() TCPCloseSocket($MainSocket) TCPShutdown() EndFunc ;==>OnAutoItExit Thanks ahead of time. AutoIt Smith Edited November 5, 2005 by AutoIt Smith Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted November 5, 2005 Moderators Share Posted November 5, 2005 How bad would a Sleep(10) hurt your speed in your While/For loops? Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
themax90 Posted November 5, 2005 Author Share Posted November 5, 2005 @ronrules I've tried Sleep(10) in the while loops, but in the For loops I dont think it would hurt, let me try, hopefully it wont stop the flow to bad. If I add it into the recv loop it cuts off possible connections. My clients could not connect. I guess this is a full server. I will research MDSN and other sources for loop retrainment as to not block the flow but hold onto the data without analyzing it. @Larry Yah i thought that would be the case. I know UDP but not as much as I have experimented with TCP. I think I will take this server shell and make it into UDP experimentally for a private version of this server or maybe to grade. I did think the TCP stuff was a pain to check all the sockets, but it's reliable. If you could take a look into these loops and find a spot or something where I could possibly alter it without disrupting the flow of Input and Output signals. That would be great. Maybe it's a autoit issue? It only checks on the currently connected hosts so it shouldnt be much, its just like getting a gui msg contiguously, only in a variable form with a delimiting approach. They way I see it $msg= GUIGetMsg would work the same as If $SuchandSuch <> -1 Then ; Do Something ElseIf $SuchandSuch = -1 Then ; Dont Do Anything EndIf If something happens, record it, if nothing does, dont record it. Maybe a upgrade or look over to autoit might be in need now. But as to being a bug, should I post this? Link to comment Share on other sites More sharing options...
LxP Posted November 5, 2005 Share Posted November 5, 2005 This won't really have any effect on the function of your script, but in case you're interested you can write lines such as this:Case $OpenSocket <> -1 And $OpenSocket = 0 And $OpenSocket <> "In Use"like this:Case $OpenSocket = 0since if $OpenSocket is zero then it can't possibly be any of the other cases. Also, this:If $Var = 1 Then ; ... ElseIf $Var <> 1 Then ; ... EndIfcan be simplified to:If $Var = 1 Then ; ... Else ; ... EndIfsince if $Var doesn't equal 1, then it automatically has to not equal 1! One other thing (you might like to ignore this -- maybe you do it as a placeholder for future code):If Condition Then ; Do something Code() Else ; Do nothing EndIfcan be simplified to:If Condition Then ; Do something Code() EndIfGood luck in getting the script working the way you want it. Link to comment Share on other sites More sharing options...
themax90 Posted November 5, 2005 Author Share Posted November 5, 2005 Thanks for your input. Yah actually it's a placeholder. I code quite type and keep almost all scenarios written so it's universal, if anyone wants to edit it, it wont be hard to figure out how Thanks for your comment LxP Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now