//+------------------------------------------------------------------+
//|                                                   PipeServer.mq4 |
//|                             Copyright © 2010, Stephen Ambatoding |
//|                                        sangmane@forexfactory.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, Stephen Ambatoding"
#property link      "sangmane@forexfactory.com"

#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 deinit()
{
   Comment("");
}

int start()
{
	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;
			string strTemp = "";
			
			for(int i=0; i<bytesRead[0]; i++)
				strTemp = strTemp + CharToStr( (inBuffer[i/4] >> ((i & 3)*8)) & 0xff);
			
			inString = fixMsg( strTemp );
			
			lastMessage = "Last message from client: "+inString;

			if(inString=="STOP") 
			{
				Comment("Pipe server stopped.");
				FlushFileBuffers(hPipe);
				DisconnectNamedPipe(hPipe);
				CloseHandle(hPipe);		
				return(0);
			}			
		}
		FlushFileBuffers(hPipe);
		DisconnectNamedPipe(hPipe);
		//Sleep(200);
	}	
	CloseHandle(hPipe);	
	Comment("");		
	return(0);
}

string fixMsg( string in )
{
   int len = StringLen( in );
   string strOut = in;
   
   if ( len > 0 )
   {         
      int intFind = StringFind( in, "\r\n" );
   
      if ( intFind >= 0 )
      {
         strOut = StringSubstr( in, 0, intFind );
      }
   }
   
   //Print( "[" + in + "]" + len + "  " + "[" + strOut + "]" + StringLen( strOut ) );
   return( strOut );
}
   
   

