Named pipe assistance

Started by tgall, December 28, 2020, 09:11:07 PM

Previous topic - Next topic

tgall

Hi all,

I'm attempting to have a .NET process kick off a .WBT file, then have the WBT file be able to communicate back to the C# process.
I couldn't find any info on having winbatch write stdout/stderror, but I did see it has named pipe support.

I'm using my C# process as a named pipe server with bidirectional communication. Here's the C# code; in short, it creates a named pipe server, writes out a message, waits for a message in, then sends another message back out.
using (var pipeServer = new NamedPipeServerStream("agent-pipe", PipeDirection.InOut))
using (var streamReader = new StreamReader(pipeServer))
using (var streamWriter = new StreamWriter(pipeServer))
{
    Console.Write("Waiting for client connection...");
    await pipeServer.WaitForConnectionAsync();
    Console.WriteLine("Client connected.");
    streamWriter.AutoFlush = true;

    streamWriter.WriteLine("connected!");
    pipeServer.WaitForPipeDrain();
    Console.WriteLine("Pipe drained; waiting for input!");
    string line;
    while ((line = streamReader.ReadLine()) != null)
    {
        Console.WriteLine($"Read: {line}");
    }

    streamWriter.WriteLine("ack");
    pipeServer.WaitForPipeDrain();
}


Here's my winbatch code

SERVER = "." ; Or "." for the local machine
pipename = "agent-pipe"
pipepath = StrCat("\\",SERVER,"\pipe\",pipename)
timeout = -1
BoxOpen("Time Client","Initializing connection to server...")
While @TRUE
   pipehandle = PipeClientOpen(pipepath,timeout)
   if pipehandle=="*ERROR*" || pipehandle=="*TIMER*"
      BoxText("Retrying")
      Continue ; try again
   endif
   BoxText("Client connected")
   data=PipeClientSendRecvData(pipehandle, "",timeout)
   BoxText(StrCat("Data sent from server = ", data))
   data=PipeClientSendRecvData(pipehandle, "hello world",5000)
   BoxText(StrCat("Data sent from server = ", data))
   PipeClientClose(pipehandle)
EndWhile


I see that the client is successfully connecting, and it also receives back the "connected!" message from the server. The second call to PipeClientSendRecvData however never sends to the server. If I kill the process, then the server immediately sees the message. It seems like winbatch isn't finishing the send operation until I kill the process... any advice?

Windows 10 pro running in hyper-v, winbatch 2018
--
open to other suggestions on communicating progress/errors back up the c# process as well, I'm not particularly tied to named pipes

ChuckC

Something may be a bit "off" with how you are creating an instance of NamedPipeServerStream.  Take a look at the other constructor overloads for the class that can be used.  When using named pipe servers in my code, I have always used an overload of the c'tor that gives me explicit control over all aspects of the named pipe server creation.  The c'torl that you are using specifies that the pipe server performs bi-directional communications, but you haven't specified whether the pipe server instance is in stream mode or message mode, nor have you indicated whether the pipe is using synchronous or asynchronous communications.

Typically, when working with new implementation of code to make use of pipes in any given language, I first write test code where I use the same programming language to create both the pipe server code and the pipe client code in separate programs/scripts and establish that they will communicate properly.  Then, I change things around to have the pipe server in a program written in one language and the client in a different program written in another language.

td

The .Net Framework/Core server was my first guess at the source of the issue. The WIL language's pipe implementation gets used regularly as part of the WinBatch+Compiler build process so we know it functions as advertised.

Also, WinBatch is a GUI application so it cannot directly write to standard in/out. That is why you don't see functions like the C#'s WriteLine in WinBatch or WIL.  That said, here is a Tech Database example of a way to attach to an existing console window and write text to it:

https://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/nftechsupt.web+WinBatch/UDF~-~UDS~Library/DOS~Console~UDFs+Write~Data~to~An~Existing~Console~Window~STDOUT.txt

There are multiple ways to implement inter-process communication using WinBatch.  You can find examples of some of the various techniques in the Tech Database.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade