如何使用命名管道在 C# 和 MQL5 之间读取和写入消息

时间:2021-05-06 02:01:58

标签: c# mql5

我正在尝试在 C# 命名管道服务器和 MQL5 命名管道客户端之间建立通信通道。 命名管道服务器和客户端正在成功创建,并且客户端连接到服务器。 从客户端向服务器发送消息,从服务器成功读取数据。 但问题是当从服务器向客户端发送消息时,客户端无法读取数据。 或者不确定服务器是否成功发送数据。

这是创建 C# 命名管道服务器的服务器源。

namespace PipeServer
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!!!");

            using (NamedPipeServerStream pipeServer = new NamedPipeServerStream("TestPipeServer", PipeDirection.InOut, 1, PipeTransmissionMode.Byte))
            {
                Console.WriteLine("NamedPipeServerStream object created.");

                // Wait for a client to connect
                Console.Write("Waiting for client connection...");
                pipeServer.WaitForConnection();
                Console.WriteLine("Client connected.");

                Console.WriteLine("Read client's response");
                const int BUFFERSIZE = 256;
                bool completed = false;
                while (!completed)
                {
                    byte[] buffer = new byte[BUFFERSIZE];
                    int nRead = pipeServer.Read(buffer, 0, BUFFERSIZE);
                    string line = Encoding.UTF8.GetString(buffer, 0, nRead);
                    Console.WriteLine(line);
                    if (line == "bye")
                        completed = true;
                }
                Console.WriteLine("Send response");
                byte[] msgAck = Encoding.UTF8.GetBytes("ACK from server");
                pipeServer.Write(msgAck, 0, msgAck.Length);
                pipeServer.Flush();
                pipeServer.WaitForPipeDrain();
            }

            Console.WriteLine("\npress any key to exit the process...");
            // basic use of "Console.ReadKey()" method
            Console.ReadKey();
        }
    }
}

这里是用 MQL5 编写的客户端源代码,用于 MT5 作为 EA

//+------------------------------------------------------------------+
//|                                                       TestEA.mq5 |
//|                                  Copyright 2021, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"

#include <Files\FilePipe.mqh>

CFilePipe  ExtPipe;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create timer
   EventSetTimer(10);

   while(!IsStopped())
     {
      if(ExtPipe.Open("\\\\.\\pipe\\TestPipeServer",FILE_READ|FILE_WRITE|FILE_BIN)!=INVALID_HANDLE)
         break;
      Sleep(250);
     }
   Print("Client: pipe opened");
//--- send welcome message
   if(!ExtPipe.WriteString(__FILE__+" on MQL5 build "+IntegerToString(__MQ5BUILD__)))
     {
      Print("Client: sending welcome message failed");
      return(INIT_FAILED);
     }
   else
     {
      ExtPipe.WriteString("bye");
     }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- destroy timer
   EventKillTimer();
   ExtPipe.Close();
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//printf(__FUNCTION__+": method called");
   MqlTick last_tick;
   string sendStr;
   if(SymbolInfoTick(Symbol(), last_tick))
     {
      sendStr = StringFormat("Symbol: %s, Bid: %f, Ask: %f", Symbol(), last_tick.bid, last_tick.ask);
      //printf(sendStr);
     }
   else
     {
      printf("SymbolInfoTick() failed, error = ",GetLastError());
     }
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//printf(__FUNCTION__ + ": method called");
//--- read data from server
   string str;
   if(!ExtPipe.ReadString(str))
     {
      Print("Client: reading string failed");
      return;
     }
   Print("Server: ",str," received");
  }
//+------------------------------------------------------------------+
//| Trade function                                                   |
//+------------------------------------------------------------------+
void OnTrade()
  {
//---

  }
//+------------------------------------------------------------------+

用谷歌搜索了一下,但没有找到任何合适的解决方案。 如果有任何类似的问题,请分享。 谢谢。

0 个答案:

没有答案
相关问题