Jump to content

Multi thread, Picasso's "CoProc".


jokke
 Share

Recommended Posts

I loved his example on the tcp server and i want to make it even abit more functional where clients are able to chat to each other, but i cant find out how

to create a message "function" between the threads when they are looped. So i had to "cheat" with a ini file where each section have the name of the PID the process, then the process itself had to check ini for updates. Iwe not stress tested this inn any way, so i have no idea if it would work under huge load.

Anyways heres what iwe written so far, or more correct rewritten and added some stuff ;)

TCPStartup() ;For all Processes
#include "CoProc.au3"
#include <guiconstants.au3>


If FileExists("connections.ini") Then
    FileDelete("connections.ini")
EndIf
IniWriteSection("connections.ini","Index","Help","not added")

$GUI = GUICreate("Chat server :: 0 connections.",300,400)
$liste = GUICtrlCreateListView("Pid | Name | Last Activity",5,5,290,365)
$kill = GUICtrlCreateButton("Kill selected PID", 5, 375, 290, 20)
GUISetState(@SW_SHOW)

Dim $connected[1][4]
Dim $newcon = 0
Dim $connections[2] = [0,0]

; Main Process
TraySetToolTip("Main Process " & @AutoItPID)
$hSocket = TCPListen("", 1024)
While 1
    $hAccept = TCPAccept($hSocket)
    If $hAccept > 0 Then
        $newcon +=1
        ReDim $connected[$newcon][4]
        $connected[$newcon-1][0] = _CoProc ("_NewConnection", $hAccept)
        $connected[$newcon-1][1] = GUICtrlCreateListViewItem($connected[$newcon-1][0]&"|"&"|"&@YEAR&"-"&@MON&"-"&@MDAY&" "&@HOUR&":"&@MIN&":"&@SEC,$liste)
        ; No need to Close the socket, the new
        ; Process will do that
    EndIf
    $var = IniReadSectionNames("connections.ini")
    If @error Then 
        MsgBox(4096, "", "Error occurred, probably no INI file.")
    Else
        $connections[0] = 0
        For $i = 1 To $var[0]
            For $x = 0 To UBound($connected) -1
                If IniRead("connections.ini",$var[$i],"pid","") = $connected[$x][0] Then
                    If IniRead("connections.ini",$var[$i],"Name","") <> $connected[$x][3] Then
                        $connected[$x][3] = IniRead("connections.ini",$var[$i],"Name","")
                        GUICtrlSetData($connected[$x][1],$connected[$x][0]&"|"&$connected[$x][3]&"|"&@YEAR&"-"&@MON&"-"&@MDAY&" "&@HOUR&":"&@MIN&":"&@SEC)
                    EndIf
                EndIf
            Next
            
            $connections[0] += IniRead("connections.ini",$var[$i],"connected",0)
            
            If IniRead("connections.ini",$var[$i],"connected",0) = 0 Then
                For $x = 0 To UBound($connected) -1
                    If $connected[$x][0] = $var[$i] Then
                        GUICtrlDelete($connected[$x][1])
                        IniDelete("connections.ini",$var[$i])
                    EndIf
                Next
            EndIf
        Next
    EndIf

    If $connections[0] <> $connections[1] Then
        $title = WinGetTitle("Chat server ::")
        WinSetTitle($title,"","Chat server :: "&$connections[0]&" connected.")
        $connections[1] = $connections[0]
    EndIf
    
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $kill
            If StringInStr(GUICtrlRead(GUICtrlRead($liste)),"|") Then
                $tmp = StringSplit(GUICtrlRead(GUICtrlRead($liste)),"|")
                ProcessClose($tmp[1])
                IniWrite("connections.ini",$tmp[1],"connected",0)
            EndIf
    EndSwitch
    
    Sleep(10)
WEnd

; Worker Process
Func _NewConnection($hAccept)
    $hAccept = _DuplicateHandle ($gi_CoProcParent, _ ; Provided by CoProc
            $hAccept, _ ; The Handle to duplicate
            @AutoItPID, _ ; current pid
            True) ; Close the socket in the parent process
    If @error Then Exit
    TraySetToolTip("Worker Process " & @AutoItPID)
    Local $sBuff
    TCPSend($hAccept, "Your connected to pid: " & @AutoItPID & @CRLF)
    TCPSend($hAccept, "Hint: type help" & @CRLF)
    IniWrite("connections.ini",@AutoItPID,"pid",@AutoItPID)
    IniWrite("connections.ini",@AutoItPID,"connected","1")
    IniWrite("connections.ini",@AutoItPID,"msg","")
    
    While 1
        $sBuff &= TCPRecv($hAccept, 256)
        If @error Then 
            IniWrite("connections.ini",@AutoItPID,"connected",0)
            Exit
        EndIf
        
        $message = IniRead("connections.ini",@AutoItPID,"msg","")
        If $message <> "" Then
            TCPSend($hAccept, $message & @CRLF)
            IniWrite("connections.ini",@AutoItPID,"msg","")
        EndIf
        
        If StringLen($sBuff) > 0 And StringInStr($sBuff, @CR) Then
            If StringInStr($sBuff, "exit") = 1 Then Exit
            If StringInStr($sBuff, "MsgBox ") = 1 Then
                MsgBox(0, @AutoItPID, StringMid($sBuff, 7))
            EndIf
            
            If StringInStr($sBuff, "name") = 1 Then
                IniWrite("connections.ini",@AutoItPID,"Name",StringReplace(StringMid($sBuff,6),@CRLF,""))
            EndIf
            If StringInStr($sBuff, "sendall") = 1 Then
                $var = IniReadSectionNames("connections.ini")
                If @error Then 
                    MsgBox(4096, "", "Error occurred, probably no INI file.")
                Else
                    For $i = 1 To $var[0]
                        If IniRead("connections.ini",$var[$i],"connected",0) = 1 Then
                            IniWrite("connections.ini",$var[$i],"msg", StringReplace(StringMid($sBuff,9),@CRLF,""))
                        EndIf
                    Next
                EndIf
            EndIf
            
            If StringInStr($sBuff, "help") = 1 Then
                TCPSend($hAccept, "Commands:" & @CRLF)
                TCPSend($hAccept, "Help - This thing" & @CRLF)
                TCPSend($hAccept, "MsgBox <Msg> - Try it :)" & @CRLF)
                TCPSend($hAccept, "Name <Name> - Your name" & @CRLF)
                TCPSend($hAccept, "Exit - Quit" & @CRLF)
            EndIf
            $sBuff = ""
        EndIf
        Sleep(15)
    WEnd
EndFunc   ;==>_NewConnection

A screeny: post-16522-1188135551_thumb.jpg

If anyone have any idea on how to transfer "data" between the threads when they are allready looped please hallow :)

Edited by jokke
UDF:Crypter a file encrypt / decrypt tool with no need to remember a password again. Based on Caesar cipher using entire ASCII Table.Script's: PixelSearch Helper, quick and simple way to create a PixelSeach.Chatserver - simplified, not so complicated multi-socket server.AutoIT - Firewall, simple example on howto create a firewall with AutoIt.
Link to comment
Share on other sites

Well yeah, iwe allready made a singe process chat server.

What i wanted was experience with more then one process, and how to comunicate between them, i thought about redo my file cryptation where i had 3 processes one for "reading" , "translating" and one for "writing". Then see if im able to get a litle boost from doing so.

UDF:Crypter a file encrypt / decrypt tool with no need to remember a password again. Based on Caesar cipher using entire ASCII Table.Script's: PixelSearch Helper, quick and simple way to create a PixelSeach.Chatserver - simplified, not so complicated multi-socket server.AutoIT - Firewall, simple example on howto create a firewall with AutoIt.
Link to comment
Share on other sites

you could use sqllite to grap all lines and if changed get all idis from low to high that are not in the history of this process....

just an example.

$a=StringSplit("547275737420796F757220546563686E6F6C75737421","")
For $b=1 To UBound($a)+(-1*-1*-1)step(2^4/8);&$b+=1*2/40*µ&Asc(4)
Assign("c",Eval("c")&Chr(Dec($a[$b]&$a[$b+1])));''Chr("a")&"HI"
Next ;time_U&r34d,ths,U-may=get$the&c.l.u.e;b3st-regards,JRSmile;
MsgBox(0x000000,"",Eval("c"));PiEs:d0nt+*b3.s4d.4ft3r.1st-try:-)
Link to comment
Share on other sites

Create an account or sign in to comment

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

Create an account

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

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

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