Jump to content

Recommended Posts

IPC_IO.AU3

I am in the need for a simple synchronous Client/Server communication.  I found several examples and references to various kinds of Inter-Process Communications such as TCP, Named Pipes, Mail Slots, Shared Memory, Memory Mapped Files, and simple Files.  I wanted to see what the best solutions would be.  I began developing a library and slowly began adding each of the IPC methods and ended up with a library with a very simple synchronous “ASCII” API where the application can choose which method to use at startup.

For the Server side, a Server app must initialize communication by calling:

Func InitConnection($cType = $cDefaultType, $ResourceName = "", $bBlock = $cDefaultBlocking, $fSleepFunc = "", $iBufSize = $DEFAULT_BUFSIZE)

The optional arguments allow the app to specify the connection type (such as: $cNamedPipe, $cFile, $cTCP, $cSharedMem, $cMailSlot), a value for the resource name (such as the file, named pipe name, TCP port number, etc.), the communication buffer size, and a callback function for when the “read” is waiting for data.

A “File Descriptor” is returned and must be used in the future API calls.

The Server side must then call:

Func StartConnection($iFD)

This call waits for a Client to connect.  The Server then calls:

                Func ReadData($iFD, ByRef $sData)

To read a Request from the Client and then calls:

                Func WriteData($iFD, ByRef $sData)

To send the reply back to the Client.

When communication with the Client is done, the Server app will call:

Func StopConnection($iFD)

When the Server app is done with the communications it will call:

Func EndConnection($iFD)

 

For the Client side, a Client app must open the communication by calling:

Func OpenConnection($cType = $cDefaultType, $ResourceName = "", $bBlock = $cDefaultBlocking, $fSleepFunc = "", $iBufSize = $DEFAULT_BUFSIZE)

The optional arguments allow the app to specify the connection type (such as: $cNamedPipe, $cFile, $cTCP, $cSharedMem, $cMailSlot), a value for the resource name (such as the file, named pipe name, TCP port number, etc.), the communication buffer size, and a callback function for when the “read” is waiting for data.

A “File Descriptor” is returned and must be used in the future API calls.

The Client side then send a request to the Server app by calling:

                Func WriteData($iFD, ByRef $sData)

To read a Response from the Server by calling:

                Func ReadData($iFD, ByRef $sData)

To end the connection to the Server by calling:

Func CloseConnection($iFD)

 

Within the IPC_IO.AU3 library, each IPC method is ether:

·         “stream” based where data is read/written by calling _WinAPI_ReadFile/TCPRecv or _WinAPI_WriteFile/ TCPSend

·         “direct” based for Shared memory where the Client reads the data directly from the Server App’s memory and the Server directly reads the Client App’s memory

In processing a request, the “ReadData” process starts by checking if data is ready to be read by calling the routine: “ReadStart”, then it reads in the size of the request by calling “ReadSize”, it then reads in the Ascii Request by calling “ReadBuffer”, then the sequence is completed by calling “ReadEnd”. 

The Write Process follows the same sequence with “WriteData” calling “WriteStart”, “WriteSize”, “WriteBuffer”, “WriteEnd”.

 

Results

My testing showed that the performance of sending and receiving of a 10k file took:

·         "Shared Memory" was the fastest, at 0.007468 Sec

·         “Named Pipes” at 0.015954

·         “Mail Slots” at 0.016427

·         “File Based” at 0.270287

·         “TCP” at 0.994884

 

IPC_IO.au3

Client.au3

Server.au3

Link to comment
Share on other sites

7 hours ago, JohnWIlling said:

I am in the need for a simple synchronous Client/Server communication.

me too. Tried but the UDF gave me errors

>Running AU3Check (3.3.14.2)  from:C:\Program Files (x86)\AutoIt3  input:G:\au3s\ipc io\IPC_IO.au3
"G:\au3s\ipc io\IPC_IO.au3"(307,123) : error: _WinAPI_GetLastErrorMessage() called with wrong number of args.
    $sMessage &= " Err:" & _WinAPI_GetLastError($tError, $tExtended) & " (" & _WinAPI_GetLastErrorMessage($tError, $tExtended)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
"C:\Program Files (x86)\AutoIt3\Include\WinAPI.au3"(1526,35) : REF: definition of _WinAPI_GetLastErrorMessage().
Func _WinAPI_GetLastErrorMessage()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
"G:\au3s\ipc io\IPC_IO.au3"(1542,90) : error: _WinAPI_CreateFileEx(): undefined function.
    $CArray[$iFD][$C_InFD] = _WinAPI_CreateFileEx($InFile, $OPEN_EXISTING, $iAccess, $iShare)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
G:\au3s\ipc io\IPC_IO.au3 - 2 error(s), 0 warning(s)

 

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

  • 2 weeks later...

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

×
×
  • Create New...