Jump to content

Named Pipes Sample


Recommended Posts

I'm tearing through the _NamedPipes_Server.au3 and _NamedPipes_Client.au3 scripts (%programfiles%\AutoIt3\Examples\GUI\Advanced\)

I have the scripts working so that the client will send message that I want to the server app, the server app does a few things and generates a value to send back. I can see in the _NamedPipes_Server app that the function RelayOutput() should be doing the sending of data back to the client, but I can't quite figure out how to send my data back to the Client.

Here's the RelayOutput function from the example:

Func RelayOutput()
  Local $pBuffer, $tBuffer, $sLine, $iRead, $bSuccess, $iWritten

  $tBuffer = DllStructCreate("char Text[" & $BUFSIZE & "]")
  $pBuffer = DllStructGetPtr($tBuffer)
  ; Read data from console pipe
  _WinAPI_ReadFile($hReadPipe, $pBuffer, $BUFSIZE, $iRead)
  if $iRead = 0 then
    MsgBox(0, "server", "RelayOutput .......: Write done")
    _WinAPI_CloseHandle($hReadPipe)
    _WinAPI_FlushFileBuffers($hPipe)
    ReconnectClient()
    Return
  endif

  ; Get the data and strip out the extra carriage returns
  $sLine    = StringLeft(DllStructGetData($tBuffer, "Text"), $iRead)
  $sLine    = StringReplace($sLine, @CR & @CR, @CR)
  $iToWrite = StringLen($sLine)
  DllStructSetData($tBuffer, "Text", $sLine)
  ; Relay the data back to the client
  $bSuccess = _WinAPI_WriteFile($hPipe, $pBuffer, $iToWrite, $iWritten, $pOverlap)
  if $bSuccess and ($iWritten = $iToWrite) then
    MsgBox(0, "server", "RelayOutput .......: Write success")
  else
    if not $bSuccess and (@Error = $ERROR_IO_PENDING) then
      MsgBox(0, "server", "RelayOutput .......: Write pending")
      $iState = 2
    else
      ; An error occurred, disconnect from the client
      MsgBox(0, "server", "RelayOutput .......: Write failed")
      ReconnectClient()
    endif
  endif
EndFunc

The data I want to send back is less than 10 characters and contained in the global variable $sMessage.

How can I edit the above function to send my $sMessage to the client?

My second question is how can I verify the data got to the client? I'd like to throw a message box from the client application with the received data in it.

The client application reads data with the ReadMsg function, but I'm not sure which variable contains the data (I've tried most of them in the ReadMsg() function.)

Func ReadMsg()
    Local $bSuccess, $iRead, $pBuffer, $tBuffer

    $tBuffer = DllStructCreate("char Text[4096]")
    $pBuffer = DllStructGetPtr($tBuffer)
    While 1
        $bSuccess = _WinAPI_ReadFile($hPipe, $pBuffer, $BUFSIZE, $iRead, 0)
        If $iRead = 0 Then ExitLoop
        If Not $bSuccess Or (@error = $ERROR_MORE_DATA) Then ExitLoop
    WEnd
    MsgBox(0, "Client", "info: " & $hPipe & $pBuffer & $BUFSIZE & $iRead & $tBuffer)
EndFunc   ;==>ReadMsg

I don't think this should be too difficult, but I'm not familiar enough with the different commands and structures used in the code.

Any thoughts?

Thanks

Edit:

After starting from the beginning again I figured out how everything is supposed to work. Here's what I got working on those two functions:

Server RelayOutput:

($sMessage is a globally defined variable, and the data you want to send to the client.

Local $pBuffer, $tBuffer, $sLine, $iRead, $bSuccess, $iWritten
;Create the Structure for carrying data
    $tBuffer = DllStructCreate("char Text[" & $BUFSIZE & "]")
;Get the memory pointer (location) of the structure
    $pBuffer = DllStructGetPtr($tBuffer)
;Set the number of number of characters of data to be sent
    $iToWrite = StringLen($sMessage) + 1
;Write the $sMessage (data to send back) to the $tBuffer variable
    DllStructSetData($tBuffer, "Text", $sMessage)
;MsgBox(0, "Server", StringLeft(DllStructGetData($tBuffer, "Text"), $iToWrite)) ;Messagebox to show what is being sent
;Send Data back to client
    $bSuccess = _WinAPI_WriteFile($hPipe, $pBuffer, $iToWrite, $iWritten, $pOverlap)
    if $bSuccess and ($iWritten = $iToWrite) then
        ;MsgBox(0, "NamedPipes_Server", "RelayOutput .......: Write success")
        _WinAPI_CloseHandle($hPipe)
        $iState = 0
    else
        if not $bSuccess and (@Error = $ERROR_IO_PENDING) then
            ;MsgBox(0, "NamedPipes_Server", "RelayOutput .......: Write pending")
            $iState = 2
        else
            ; An error occurred, disconnect from the client
            ;MsgBox(0, "NamedPipes_Server", "RelayOutput .......: Write failed")
            ReconnectClient()
        endif
    endif
    InitPipe()
EndFunc  ;==>RelayOutput

Client ReadMsg:

Func ReadMsg()
    Local $bSuccess, $iRead, $pBuffer, $tBuffer

    $tBuffer = DllStructCreate("char Text[4096]")
    $pBuffer = DllStructGetPtr($tBuffer)
    While 1
        $bSuccess = _WinAPI_ReadFile($hPipe, $pBuffer, $BUFSIZE, $iRead, 0)
        If $iRead = 0 Then ExitLoop
        If Not $bSuccess Or (@error = $ERROR_MORE_DATA) Then ExitLoop
    WEnd
    MsgBox(0, "Client", DllStructGetData($tBuffer, "Text")) ;;This displays what is relayed back to the client application.
EndFunc   ;==>ReadMsg
Edited by cvocvo
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...