//+------------------------------------------------------------------+
//|                                          Disconnect_Detector.mq4 |
//|                                                    Kenny Hubbard |
//|                                       http://www.compu-forex.com |
//+------------------------------------------------------------------+
#property copyright "Kenny Hubbard"
#property link      "http://www.compu-forex.com"

#define OF_READ               0
#define OF_WRITE              1

#import "kernel32.dll"
   int _lopen  (string path, int of);
   int _lcreat (string path, int attrib);
   int _llseek (int handle, int offset, int origin);
   int _lread  (int handle, string buffer, int bytes);
   int _lwrite (int handle, string buffer, int bytes);
   int _lclose (int handle);
#import

//---- input parameters
extern bool    Master         = true;      
extern int     Warning_Time   = 60;
extern bool    Local_Alert    = true;
extern bool    Email_Alert    = true;
string         
   File_Path_1  = "C:\\MT4_Disconnect_Master.txt",
   File_Path_2  = "C:\\MT4_Disconnect_Slave.txt",
   Email_Text,
   Read_Path,
   Write_Path,
   Warning_Text;
bool
   Notified = false,
   Active_Alert = false;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
{
//----
   Email_Text  = StringConcatenate(AccountCompany() , " " , AccountNumber() , " " , AccountName());
   if(Master){
      Read_Path = File_Path_2;
      Write_Path = File_Path_1;
      Warning_Text = "Slave terminal lost connection";
   }
   else{
      Read_Path = File_Path_1; 
      Write_Path = File_Path_2;  
      Warning_Text = "Master terminal lost connection";
   }   
   WriteFile(Write_Path,TimeLocal());
//----
   return(0);
}
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{
//----
   string buffer=ReadFile(Read_Path);
   int timer = TimeLocal() - StrToInteger(buffer);
   Comment("Time since last read = " + timer);
   if(timer > Warning_Time){
      if(!Notified){
         if(Local_Alert)Alert(Warning_Text);
         if(Email_Alert)SendMail(Warning_Text,Email_Text);
         Notified = true;
         Active_Alert = true;
      }
   }
   if(timer < Warning_Time){
      if(Active_Alert){
         Active_Alert = false;
         Notified = false;
         if(Local_Alert)Alert("Connection restored");
         if(Email_Alert)SendMail("Connection restored",Email_Text);
      }
   }
   WriteFile(Write_Path,TimeLocal());
//----
   return(0);
  }
//+------------------------------------------------------------------+
string ReadFile (string path) 
{
   int handle=_lopen (path,OF_READ);           
   if(handle<0){
        Alert("No comparison file available....................sleeping "); 
        Sleep(60000);
        return ("");
    }
    int result=_llseek (handle,0,0);      
    if(result<0){
        Print("Error seeking" ); 
        return ("");
    }
    string buffer="";
    string char1="x";
    int count=0;
    result=_lread (handle,char1,1);
    while(result>0){
        buffer=buffer+char1;
        char1="x";
        count++;
        result=_lread (handle,char1,1);
    }
    result=_lclose (handle);              
    if(result<0)Print("Error closing ",path);
    return (buffer);
}
//+------------------------------------------------------------------+
void WriteFile (string path, string buffer) 
{
   int count=StringLen (buffer); 
   int result;
   int handle=_lopen (path,OF_WRITE);
   if(handle<0){
      handle=_lcreat (path,0);
      if(handle<0){
         Print ("Error creating ",path);
         return;
      }
      result=_lclose (handle);
   }
   handle=_lopen (path,OF_WRITE);               
   if(handle<0){
      Print("Error opening ",path); 
      return;
   }
   result=_llseek (handle,0,0);          
   if(result<0){
      Print("Seek error - filewrite "); 
      return;
   }
   result=_lwrite (handle,buffer,count); 
   if(result<0)Print("Writing ",path," ",count," char");
   result=_lclose (handle);              
   if(result<0)Print("Error closing - filewrite function ",path);
}
//+------------------------------------------------------------------+