named pipes in System.IO.Pipes

Created at 17 Apr 2013, 01:24
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
VA

vajthorsz

Joined 17.04.2013

named pipes in System.IO.Pipes
17 Apr 2013, 01:24


Greetings,

Could you confirm that the System.IO.Pipes namespace is available inside cAlgo?

Could you please give a list of available/unavailable namespaces?

If System.IO.Pipes is available, could you show me a very basic use in a robot?

If System.IO.Pipes is available, which kind of interprocess communication would you suggest for cAlgo users. The other process is on the same machine as cAlgo.

 

Thanks!


@vajthorsz
Replies

vajthorsz
17 Apr 2013, 01:25

Edit: I meant

If System.IO.Pipes is *NOT* available, which kind of interprocess communication would you suggest for cAlgo users. The other process is on the same machine as cAlgo.


@vajthorsz

cAlgo_Fanatic
18 Apr 2013, 10:07

System.IO.Pipes is included in cAlgo. We will add a sample in this section: /forum/calgo-reference-samples.


@cAlgo_Fanatic

AimHigher
20 Mar 2014, 02:28

A working example for using named pipes would indeed be great.


@AimHigher

Emmanuel.evrard2
24 Oct 2020, 18:43

RE:

cAlgo_Fanatic said:

System.IO.Pipes is included in cAlgo. We will add a sample in this section: /forum/calgo-reference-samples.

 

Hello

 

I tried to used System.IO.Pipes in CTrader and it works !

 

I used this example : 

 

I had to add at the beginning in Calgo :

 

using System.IO.Pipes;
using System.Diagnostics;
using System.Text;

 

  [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]

 

for the server side : the form1 : 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;

namespace PipesServerTest
{
    public partial class Form1 : Form
    {
        public delegate void NewMessageDelegate(string NewMessage);
        private PipeServer _pipeServer;

        public Form1()
        {
            InitializeComponent();
            _pipeServer = new PipeServer();
            _pipeServer.PipeMessage += new DelegateMessage(PipesMessageHandler);
        }

        private void cmdListen_Click(object sender, EventArgs e)
        {
            try
            {
                _pipeServer.Listen("TestPipe");
                txtMessage.Text = "Listening - OK";
                cmdListen.Enabled = false;
            }
            catch (Exception)
            {
                txtMessage.Text = "Error Listening";
            }
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void PipesMessageHandler(string message)
        {
            try
            {
                if (this.InvokeRequired)
                {
                    this.Invoke(new NewMessageDelegate(PipesMessageHandler), message);
                }
                else
                {
                    txtMessage.Text = message;
                }
            }
            catch (Exception ex)
            {

                Debug.WriteLine(ex.Message);
            }

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            _pipeServer.PipeMessage -= new DelegateMessage(PipesMessageHandler);
            _pipeServer = null;

        }


    }
}

and a class  PipeServer

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Pipes;
using System.Diagnostics;

namespace PipesServerTest
{
    // Delegate for passing received message back to caller
    public delegate void DelegateMessage(string Reply);

    class PipeServer
    {
        public event DelegateMessage PipeMessage;
        string _pipeName;

        public void Listen(string PipeName)
        {
            try
            {
                // Set to class level var so we can re-use in the async callback method
                _pipeName = PipeName;
                // Create the new async pipe 
                NamedPipeServerStream pipeServer = new NamedPipeServerStream(PipeName, PipeDirection.In, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);

                // Wait for a connection
                pipeServer.BeginWaitForConnection(new AsyncCallback(WaitForConnectionCallBack), pipeServer);
            }
            catch (Exception oEX)
            {
                Debug.WriteLine(oEX.Message);
            }
        }

        private void WaitForConnectionCallBack(IAsyncResult iar)
        {
            try
            {
                // Get the pipe
                NamedPipeServerStream pipeServer = (NamedPipeServerStream)iar.AsyncState;
                // End waiting for the connection
                pipeServer.EndWaitForConnection(iar);

                byte[] buffer = new byte[255];

                // Read the incoming message
                pipeServer.Read(buffer, 0, 255);
                
                // Convert byte buffer to string
                string stringData = Encoding.UTF8.GetString(buffer, 0, buffer.Length);
                Debug.WriteLine(stringData + Environment.NewLine);

                // Pass message back to calling form
                PipeMessage.Invoke(stringData);

                // Kill original sever and create new wait server
                pipeServer.Close();
                pipeServer = null;
                pipeServer = new NamedPipeServerStream(_pipeName, PipeDirection.In, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);

                // Recursively wait for the connection again and again....
                pipeServer.BeginWaitForConnection(new AsyncCallback(WaitForConnectionCallBack), pipeServer);
            }
            catch
            {
                return;
            }
        }
    }
}
 

 

for the client side  in CAlgo :

        Send("test Ctrader ", "TestPipe", 1000);

        

        public void Send(string SendStr, string PipeName, int TimeOut = 1000)
        {
            try
            {
                NamedPipeClientStream pipeStream = new NamedPipeClientStream(".", PipeName, PipeDirection.Out, PipeOptions.Asynchronous);

                // The connect function will indefinitely wait for the pipe to become available
                // If that is not acceptable specify a maximum waiting time (in ms)
                pipeStream.Connect(TimeOut);
                //  Debug.WriteLine("[Client] Pipe connection established");

                byte[] _buffer = Encoding.UTF8.GetBytes(SendStr);
                pipeStream.BeginWrite(_buffer, 0, _buffer.Length, AsyncSend, pipeStream);
            } catch (TimeoutException oEX)
            {
                // Debug.WriteLine(oEX.Message);
            }
        }
        private void AsyncSend(IAsyncResult iar)
        {
            try
            {
                // Get the pipe
                NamedPipeClientStream pipeStream = (NamedPipeClientStream)iar.AsyncState;

                // End the write
                pipeStream.EndWrite(iar);
                pipeStream.Flush();
                pipeStream.Close();
                pipeStream.Dispose();
            } catch (Exception oEX)
            {
                // Debug.WriteLine(oEX.Message);
            }
        }


@Emmanuel.evrard2

Emmanuel.evrard2
25 Oct 2020, 22:35

RE: RE:

I tried again this solution , but it doesn't work anymore.

 

Ctrader is the same. it works the first day but not the next day.

 

If someone have an idea ?

 

Emmanuel.evrard2 said:

cAlgo_Fanatic said:

System.IO.Pipes is included in cAlgo. We will add a sample in this section: /forum/calgo-reference-samples.

 

Hello

 

I tried to used System.IO.Pipes in CTrader and it works !

 

I used this example : 

 

I had to add at the beginning in Calgo :

 

using System.IO.Pipes;
using System.Diagnostics;
using System.Text;

 

  [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]

 

for the server side : the form1 : 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;

namespace PipesServerTest
{
    public partial class Form1 : Form
    {
        public delegate void NewMessageDelegate(string NewMessage);
        private PipeServer _pipeServer;

        public Form1()
        {
            InitializeComponent();
            _pipeServer = new PipeServer();
            _pipeServer.PipeMessage += new DelegateMessage(PipesMessageHandler);
        }

        private void cmdListen_Click(object sender, EventArgs e)
        {
            try
            {
                _pipeServer.Listen("TestPipe");
                txtMessage.Text = "Listening - OK";
                cmdListen.Enabled = false;
            }
            catch (Exception)
            {
                txtMessage.Text = "Error Listening";
            }
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void PipesMessageHandler(string message)
        {
            try
            {
                if (this.InvokeRequired)
                {
                    this.Invoke(new NewMessageDelegate(PipesMessageHandler), message);
                }
                else
                {
                    txtMessage.Text = message;
                }
            }
            catch (Exception ex)
            {

                Debug.WriteLine(ex.Message);
            }

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            _pipeServer.PipeMessage -= new DelegateMessage(PipesMessageHandler);
            _pipeServer = null;

        }


    }
}

and a class  PipeServer

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Pipes;
using System.Diagnostics;

namespace PipesServerTest
{
    // Delegate for passing received message back to caller
    public delegate void DelegateMessage(string Reply);

    class PipeServer
    {
        public event DelegateMessage PipeMessage;
        string _pipeName;

        public void Listen(string PipeName)
        {
            try
            {
                // Set to class level var so we can re-use in the async callback method
                _pipeName = PipeName;
                // Create the new async pipe 
                NamedPipeServerStream pipeServer = new NamedPipeServerStream(PipeName, PipeDirection.In, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);

                // Wait for a connection
                pipeServer.BeginWaitForConnection(new AsyncCallback(WaitForConnectionCallBack), pipeServer);
            }
            catch (Exception oEX)
            {
                Debug.WriteLine(oEX.Message);
            }
        }

        private void WaitForConnectionCallBack(IAsyncResult iar)
        {
            try
            {
                // Get the pipe
                NamedPipeServerStream pipeServer = (NamedPipeServerStream)iar.AsyncState;
                // End waiting for the connection
                pipeServer.EndWaitForConnection(iar);

                byte[] buffer = new byte[255];

                // Read the incoming message
                pipeServer.Read(buffer, 0, 255);
                
                // Convert byte buffer to string
                string stringData = Encoding.UTF8.GetString(buffer, 0, buffer.Length);
                Debug.WriteLine(stringData + Environment.NewLine);

                // Pass message back to calling form
                PipeMessage.Invoke(stringData);

                // Kill original sever and create new wait server
                pipeServer.Close();
                pipeServer = null;
                pipeServer = new NamedPipeServerStream(_pipeName, PipeDirection.In, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);

                // Recursively wait for the connection again and again....
                pipeServer.BeginWaitForConnection(new AsyncCallback(WaitForConnectionCallBack), pipeServer);
            }
            catch
            {
                return;
            }
        }
    }
}
 

 

for the client side  in CAlgo :

        Send("test Ctrader ", "TestPipe", 1000);

        

        public void Send(string SendStr, string PipeName, int TimeOut = 1000)
        {
            try
            {
                NamedPipeClientStream pipeStream = new NamedPipeClientStream(".", PipeName, PipeDirection.Out, PipeOptions.Asynchronous);

                // The connect function will indefinitely wait for the pipe to become available
                // If that is not acceptable specify a maximum waiting time (in ms)
                pipeStream.Connect(TimeOut);
                //  Debug.WriteLine("[Client] Pipe connection established");

                byte[] _buffer = Encoding.UTF8.GetBytes(SendStr);
                pipeStream.BeginWrite(_buffer, 0, _buffer.Length, AsyncSend, pipeStream);
            } catch (TimeoutException oEX)
            {
                // Debug.WriteLine(oEX.Message);
            }
        }
        private void AsyncSend(IAsyncResult iar)
        {
            try
            {
                // Get the pipe
                NamedPipeClientStream pipeStream = (NamedPipeClientStream)iar.AsyncState;

                // End the write
                pipeStream.EndWrite(iar);
                pipeStream.Flush();
                pipeStream.Close();
                pipeStream.Dispose();
            } catch (Exception oEX)
            {
                // Debug.WriteLine(oEX.Message);
            }
        }

 


@Emmanuel.evrard2