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 78 79 80 81 82 83 84 85 86 87 88 89 90
|
Procedure TMySocket.loop;
Var
okselect,bReadReady, bWriteReady, bExceptFlag : boolean;
Begin
Try
case step of
S_DISABLED : ; // do nothing
S_DELAY_BEFORE_CX:
Begin
if getlapsTickCount(TMO)> TimeBeforeCx then Step:=S_CONNECTION;
End;
S_CONNECTION : // Connexion requested
Begin
TMO := GetTickCount;
LastErrorMsg := 'Connecting'; // used by the inherited class to diplay hint message
LastErrorTime := now;
SocketErrCode := 0;
if not(RegErrStatus) and (NbTry=0) then MyLog.LogEvent('',ChamberId,li_SOCKET_CONNECTING,LOG_COM,EL_NONE,'['+Name+']'+' Attempt to connect to IP='+IPAdr+' and port='+IntToStr(Port));
LastOpenTime := now;
try
TcpClient1.Close;
TcpClient1.RemoteHost:= IPAdr;
TcpClient1.RemotePort:= IntToStr(Port);
TcpClient1.Open;
except
end;
Step := S_WAIT_CONNECTION;
End;
S_WAIT_CONNECTION: // Waiting for connection
Begin
try
okSelect:=TCpClient1.Select(@bReadReady, @bWriteReady, @bExceptFlag);
except
okselect:=false;
end;
if okselect and bWriteReady and (TcpClient1.Active) then
Begin
NbTry := 0;
inc(CounterConnect);
DateConnected := now;
step := S_DELAY_AFTER_CONNECTION;
LastErrorMsg := 'Connected'; // used by the inherited class to diplay hint message
LastErrorTime := now;
SocketErrCode := 0;
TMO := GetTickCount;
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_CONNECTED,LOG_COM,EL_RESTORED,'['+Name+']'+' Connected to IP='+IPAdr+' and port='+IntToStr(Port));
End else // not connected !
Begin
if (GetLapsTickCount(TMO)>TMO_RECONNECT_ONERROR) then // if error occured (OnError) then after TMO_RECONNECT_ONERROR = 9s
Begin
inc(NbTry);
if (NbTry>4) and not(RegErrStatus) then
Begin
MyLog.logEvent('',ChamberId,li_SOCKET_TMO,LOG_COM,EL_CRITICAL,'['+Name+']'+' Time out occured while 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;
step:=S_CONNECTION; // if the timeout is reached then try to reconnect
End;
End;
End;
S_DELAY_AFTER_CONNECTION:
Begin
if getLapsTickCount(TMO)>TimeAfterCx then
Begin
TMO := GetTickCount;
step:= S_LOOP_COM;
End;
End;
S_LOOP_COM: // idle step
Begin
// Check connection and if error, log error and reconnect
if SocketErrCode<>0 then
Begin
MyLog.LogEvent('',ChamberId,li_SOCKET_LOST_CONNECT,LOG_COM,EL_WARNING,'['+Name+']'+' Loss of connection to IP='+IPAdr+' and port='+IntToStr(Port)+' with error #'+IntToStr(SocketErrCode));
TMO := GetTickCount;
step:=S_TMO_BEFORE_RECONNECT;
End;
End;
S_TMO_BEFORE_RECONNECT:
Begin
if GetLapsTickCount(TMO)>TMO_RECONNECT_ONERROR then Step:=S_CONNECTION;
End
else step:=S_CONNECTION;
end; // end case
except
LogExceptionInConsole('Exception in Socket_module function loop');
End;
end; |
Partager