[Module] Named Pipe (Windows & MacOS)

Applications, Games, Tools, User libs and useful stuff coded in PureBasic
User avatar
Thorsten1867
Addict
Addict
Posts: 1366
Joined: Wed Aug 24, 2005 4:02 pm
Location: Germany

[Module] Named Pipe (Windows & MacOS)

Post by Thorsten1867 »

Named Pipe - Module (Windows & MacOS / 64Bit)

Exchange of messages between two programs using "named pipes

Code: Select all

; NamedPipe::GetFullName()     ; Path and name of the pipe

; ----- Server -----
; NamedPipe::EventPipe()       ; Returns the name of the pipe after #Event_Message
; NamedPipe::GetEventMessage() ; Querying the message after #Event_Message
; NamedPipe::Create()          ; Creates a pipe with the name and starts a thread to read the pipe
; NamedPipe::Use()             ; Use an existing pipe and starts a thread to read the pipe
; NamedPipe::Pause()           ; Pause the thread to read the pipe
; NamedPipe::Resume()          ; Resume the thread to read the pipe
; NamedPipe::Close()           ; Close the pipe and exit the thread
; NamedPipe::SetMessageReply() ; Set a reply to the receipt of a message.
	
; ----- Client -----
; NamedPipe::SendMessage()     ; Send a message (Return: Reply)
Download: NamedPipeModule.pbi
Last edited by Thorsten1867 on Wed Apr 08, 2020 10:42 am, edited 3 times in total.
Translated with http://www.DeepL.com/Translator

Download of PureBasic - Modules
Download of PureBasic - Programs

[Windows 11 x64] [PB V5.7x]
infratec
Always Here
Always Here
Posts: 6810
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: [Module] Named Pipe (Windows)

Post by infratec »

Instead of the fixed Delay(100) I use always something like this:

Code: Select all

Thread\Exit = #True

If WaitThread(NamedPipe\Thread, 100) = 0
  KillThread(NamedPipe\Thread)
EndIf
It has the possibity to be faster :wink:

But if you are in the Delay(1000) of the thread, you will always kill the thread.
Maybe the Delay(1000) is not a good idea.

And your code fails if I need more then one named pipe.
Until you implemented this, Create() should return a #False if there is already a pipe open.
Fred
Administrator
Administrator
Posts: 16581
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: [Module] Named Pipe (Windows)

Post by Fred »

KillThread should never be used unless you can't quit the thread properly.
User avatar
mk-soft
Always Here
Always Here
Posts: 5313
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: [Module] Named Pipe (Windows)

Post by mk-soft »

Fred wrote:KillThread should never be used unless you can't quit the thread properly.
Yes,
had only one case where KillThread needed to perform.
Was an external DCOM server that had deleted its resources without confirmation, and thus the interface call ran into nirvana.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
infratec
Always Here
Always Here
Posts: 6810
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: [Module] Named Pipe (Windows)

Post by infratec »

I also noticed that a blocking mode is used.
So if no client connect, you have to kill the thread, because it never reaches the end of the loop.
User avatar
mk-soft
Always Here
Always Here
Posts: 5313
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: [Module] Named Pipe (Windows)

Post by mk-soft »

At that time I had solved this problem by building a connect on the own handle and thus get out of the wait of "ConnectNamedPipe".
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Thorsten1867
Addict
Addict
Posts: 1366
Joined: Wed Aug 24, 2005 4:02 pm
Location: Germany

Re: [Module] Named Pipe (Windows)

Post by Thorsten1867 »

Update: Multiple pipe support
Translated with http://www.DeepL.com/Translator

Download of PureBasic - Modules
Download of PureBasic - Programs

[Windows 11 x64] [PB V5.7x]
User avatar
Thorsten1867
Addict
Addict
Posts: 1366
Joined: Wed Aug 24, 2005 4:02 pm
Location: Germany

Re: [Module] Named Pipe (Windows)

Post by Thorsten1867 »

Update: Support for MacOS
Translated with http://www.DeepL.com/Translator

Download of PureBasic - Modules
Download of PureBasic - Programs

[Windows 11 x64] [PB V5.7x]
tatanas
Enthusiast
Enthusiast
Posts: 186
Joined: Wed Nov 06, 2019 10:28 am
Location: France

Re: [Module] Named Pipe (Windows & MacOS)

Post by tatanas »

I have tried your client/server example. If you pause the server then try a send from the client, it hangs for ever.
Can a timeout be implementing ?
Windows 10 Pro x64
PureBasic 6.03 x64
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2050
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Re: [Module] Named Pipe (Windows & MacOS)

Post by Andre »

I used this module in my project. It compiled fine...

After a long break I tried it again with PB 6.03 LTS (Win x64, ASM) and the use of the module via XIncludeFile causes an error now.
And I can't understand this error.... :-/

Just try:
1) If you compile the NamedPipeModule.pbi standalone it compiles fine and displays the window of the included example.
2) Now try to include the module in another source like this one (saved as NamedPipeModule_Test.pb for example):

Code: Select all

XIncludeFile "NamedPipeModule.pbi"

;#Server = 1
Debug "Test"
This will cause an "Integer constant expression expected for compiler directives." compiler error online line 614 now with PB6.03, no matter if ";#Server = 1" is commented or not (anyway this was only a try to workaround the error...).

But I don't understand, what the compiler want to tell me here!?

#Server = 1 is of course an integer number (used in the example included in the module too).
And why is the code in the "CompilerIf #PB_Compiler_IsMainFile ..... CompilerEndIf" section compiled, if I've only included the module in my test file?

I don't see the point and it looks a bit like a false compiler error/check introduced in the new PB version... any thoughts?
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
User avatar
mk-soft
Always Here
Always Here
Posts: 5313
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: [Module] Named Pipe (Windows & MacOS)

Post by mk-soft »

Should not have run through at all, as the file is no longer a MainFile as an include. Must be a bug.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2050
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Re: [Module] Named Pipe (Windows & MacOS)

Post by Andre »

mk-soft wrote: Tue Oct 31, 2023 11:30 am Should not have run through at all, as the file is no longer a MainFile as an include. Must be a bug.
Thanks for confirmation, I've done a bug-report here: viewtopic.php?t=82778
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
Post Reply