1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
Procedure TMySocket.loop;
Begin
inherited loop;
case step of
S_DISABLED : ; // do nothing
S_CONNECTION : // Connexion requested
Begin
StartLongTMO := GetTickCount;
StartOnErrorTMO := GetTickCount;
LastErrorMsg := 'Connecting'; // used by the inherited class to display hint message
LastErrorTime := now;
ClientSocket1.Port := Port;
ClientSocket1.Host := IPAdr;
SocketErrCode := 0;
Step := S_WAIT_CONNECTION;
if not(ClientSocket1.Active) and not(RegErrStatus) then MyLog.LogEvent('',ChamberId,li_SOCKET_CONNECTING,LOG_COM,EL_NONE,'['+Name+']'+' Trying to connect to IP='+IPAdr+' and port='+IntToStr(Port));
ClientSocket1.Active:=true;
End;
S_WAIT_CONNECTION: // Waiting for connection
Begin
if (ClientSocket1.Active) THEN //and (SocketErrCode=0) then // Connected !
Begin
CallAllStartModules;
inc(CounterConnect);
DateConnected := now;
step := S_LOOP_COM;
LastErrorMsg := 'Connected'; // used by the inherited class to display hint message
LastErrorTime := now;
SocketErrCode := 0;
SetErrStatus(false); // This will dispatch the information to all module which are connected to this socket with the SetErrStatus function
if RegSecEnabled then MyLog.logEvent('',ChamberId,li_SOCKET_ERROR,LOG_COM,EL_RESTORED,'['+Name+']'+' Connected to IP='+IPAdr+' and port='+IntToStr(Port));
End else // not connected !
Begin
//---------------!!
if MyScheduler.UsesThread then Application.ProcessMessages;
//---------------!!
if (GetLapsTickCount(StartLongTMO)>TMO_Connect) then // TMO_Connect=30s
Begin
step:=S_CONNECTION; // if the timeout is reached then try to reconnect
if not(RegErrStatus) then
Begin
MyLog.logEvent('',ChamberId,li_SOCKET_TMO,LOG_COM,EL_CRITICAL,'['+Name+']'+' Time out occured when trying to connect to IP='+IPAdr+' and port='+IntToStr(Port));
SetErrStatus(true); // This will dispatch the information to all module which are connected to this socket with the SetErrStatus function
End;
End else
if ((SocketErrCode>0) and (GetLapsTickCount(StartOnErrorTMO)>TMO_RECONNECT_ONERROR)) then // if error occured (OnError) then after TMO_RECONNECT_ONERROR = 9s
Begin
SocketErrCode := 0;
StartOnErrorTMO := GetTickCount;
ClientSocket1.Active:=true;
End;
End;
End;
S_LOOP_COM: // idle step
Begin
// Check connection and if error, log error and reconnect
if (not ClientSocket1.Active) then // or (SocketErrCode>0) then // if loss of connection then reconnect
Begin
MyLog.LogEvent('',ChamberId,li_SOCKET_LOST_CONNECT,LOG_COM,EL_WARNING,'['+Name+']'+' Loss of connection to IP='+IPAdr+' and port='+IntToStr(Port));
StartLongTMO := GetTickCount;
StartOnErrorTMO := GetTickCount;
SocketErrCode := 1; // to force to reconnect
step:=S_WAIT_CONNECTION;
End else
Begin
SocketErrCode:=0;
CallLoopModule;
End;
End;
else step:=S_CONNECTION;
end; // end case
end; |
Partager