MQL4 - reading named pipes - help needed

r_a_u_l

Интересующийся
hello

having the following peace of code:

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

namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
NamedPipeClientStream pipeClient =
new NamedPipeClientStream(".", "EURUSD", PipeDirection.In);

Console.WriteLine("Connecting to server...\n");
pipeClient.Connect();
StreamReader ss = new StreamReader(pipeClient);
while (true)
{
Console.WriteLine("From pipe: " + ss.ReadLine());
Thread.Sleep(1200);
}
pipeClient.Close();
}
}
}

please help me translate this in mq4 code / a reading function that returns bid and ask

best regards

raul
 

Вложения

  • NamedPipesreader.png
    NamedPipesreader.png
    12,3 КБ · Просмотры: 72
Последнее редактирование модератором:

testopal

Местный житель
C помощью StreamReader ss = new StreamReader я не смог создать pipe server и clent. Но нашел пример, как передавать строки через bytes. Этот пример рабочий, я его использовал в своей работе.

PipeServer

PHP:
//+------------------------------------------------------------------+
//|                                                   PipeServer.mq4 |
//|                             Copyright © 2010, Stephen Ambatoding |
//|                                        [email protected] |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, Stephen Ambatoding"
#property link      "[email protected]"

#define PIPE_TYPE_MESSAGE			4
#define PIPE_READMODE_MESSAGE		2
#define PIPE_WAIT					0
#define PIPE_ACCESS_DUPLEX      	3
#define PIPE_UNLIMITED_INSTANCES 	255
#define NMPWAIT_USE_DEFAULT_WAIT	0
#define INVALID_HANDLE_VALUE 		-1
#define ERROR_PIPE_CONNECTED 		535

#import "kernel32.dll"
	int CreateNamedPipeA(string PipeName,int dwOpenMode,int dwPipeMode,int nMaxInstances,int nOutBufferSize,int nInBufferSize,int nDefaultTimeOut,int lpSecurityAttributes);
	int ConnectNamedPipe(int hPipe,int lpOverlapped);
	int ReadFile(int hPipe, int& inBuffer[],int NumberOfBytesToRead, int& bytesRead[], int lpOverlapped);
	int WriteFile(int hPipe, string sBuffer, int NumberOfBytesToWrite, int& bytesWritten[], int lpOverlapped);
	int FlushFileBuffers(int hPipe);
	int DisconnectNamedPipe(int hPipe);
	int CloseHandle(int hPipe);
#import

string lastMessage="";
extern string Pipe = "Pipe1";

int start()
{
Print ("PipeServer");
	string PipeName = "\\\\.\\pipe\\"+Pipe;
	int PipeMode = PIPE_TYPE_MESSAGE|PIPE_READMODE_MESSAGE|PIPE_WAIT;
	int hPipe = CreateNamedPipeA(PipeName,PIPE_ACCESS_DUPLEX,PipeMode,PIPE_UNLIMITED_INSTANCES,1024,1024,NMPWAIT_USE_DEFAULT_WAIT,NULL);
	if (hPipe == INVALID_HANDLE_VALUE) {
		Alert("CreateNamedPipe failed");
       	return (-1);
	}
	while(!IsStopped())	{

		Comment("Pipe server ready ...\nSend \"STOP\" to stop server\n",lastMessage);
		bool fConnected = ConnectNamedPipe(hPipe, NULL) != 0;
		if(!fConnected)
			fConnected = GetLastError() == ERROR_PIPE_CONNECTED;
			
		if (fConnected) {
			int inBuffer[256];
			int bytesRead[1];
			bool fSuccess = ReadFile(hPipe,inBuffer,4*ArraySize(inBuffer),bytesRead,NULL) !=0;
			if (!fSuccess || (bytesRead[0] == 0)) break;
			string inString="";
			for(int i=0; i<bytesRead[0]; i++)
				inString = inString + CharToStr( (inBuffer[i/4] >> ((i & 3)*8)) & 0xff);
			lastMessage = "Last message from client: "+inString;
			string outString = "Received ["+inString+"]";
			int bytesWritten[1];
			fSuccess = WriteFile(hPipe,outString,StringLen(outString)+1,bytesWritten,NULL) != 0;
			if (! fSuccess || bytesWritten[0] != StringLen(outString)+1) break;
			if(inString=="STOP") {
				Comment("Pipe server stopped.");
				FlushFileBuffers(hPipe);
				DisconnectNamedPipe(hPipe);
				CloseHandle(hPipe);				
				return(0);
			}			
		}
		FlushFileBuffers(hPipe);
		DisconnectNamedPipe(hPipe);
	}	
	CloseHandle(hPipe);			
	return(0);
}

pipe client

PHP:
//+------------------------------------------------------------------+
//|                                                   PipeClient.mq4 |
//|                             Copyright © 2010, Stephen Ambatoding |
//|                                        [email protected] |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, Stephen Ambatoding"
#property link      "[email protected]"
#property show_inputs

#define ERROR_MORE_DATA 234

#import "kernel32.dll"
	int CallNamedPipeA(string PipeName, string outBuffer, int outBufferSz, int& inBuffer[], int inBufferSz, int& bytesRead[], int timeOut);
#import

extern string extMessage = "Where do you want to go today?";
extern string Pipe		 = "Pipe1";

int start()
{
Print ("PipeClient");
	string PipeName = "\\\\.\\pipe\\"+Pipe;
	int inBuffer[256];
	int bytesRead[1];
	string sComment="";
	Comment("Connecting to pipe server..."); 
	bool fSuccess = CallNamedPipeA(PipeName,extMessage,StringLen(extMessage)+1,inBuffer,4*ArraySize(inBuffer),bytesRead,0) != 0;
	int lastError = GetLastError();
	if (fSuccess || lastError == ERROR_MORE_DATA) { 
		string inString = "";
		for(int i=0; i<bytesRead[0]; i++)
			inString = inString + CharToStr( (inBuffer[i/4] >> ((i & 3)*8)) & 0xff);		
		sComment = "Reply from server: "+inString;
		if(!fSuccess) sComment = sComment+ "\nSome data was lost. Increase receiving buffer size.";		
		Comment(sComment);			
	} else {
		Print("Last Error: ",lastError);
		Comment("CallNamedPipe Failed!");
	}
	return(0);
}

переписать на c# этот код легко. я его портировал удачно и на c# и на delphi.
 

cyls

Прохожий
переписать на c# этот код легко. я его портировал удачно и на c# и на delphi.

Не могли бы Вы привести код портированный на с#. Все примеры реализации namedpipes на c# почему то только с использованием StreamReader. Пример как прикрутить функцию CreateNamedPipeA к с# так и не смог найти.
 
Верх