I´m trying to integrate MT5 with an external Named Pipe Server application written in C# through named pipes. The problem is the app requires a Asyncronous named pipe, and I can´t find a way to connect to it from MT5.
I´m using the example from https://www.mql5.com/en/articles/503:
void OnStart()
{
//--- wait for pipe server
while(!IsStopped())
{
if(ExtPipe.Open("\\\\RemoteServerName\\pipe\\MQL5.Pipe.Server",FILE_READ|FILE_WRITE|FILE_BIN)!=INVALID_HANDLE) break;
if(ExtPipe.Open("\\\\.\\pipe\\MQL5.Pipe.Server",FILE_READ|FILE_WRITE|FILE_BIN)!=INVALID_HANDLE) break;
Sleep(250);
}
Print("Client: pipe opened");
C# server source code:
// 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; } } }
MT5 can connect and send a message once, but then hangs at next connection. I searched through documentatin but found no way to open an async named pipe. Ideas?