My Server Terminate Unexpectedly

Hi everybody, I have been working on my server for about 6 months and I have a problem that I have never be able to repair. I really don't known what do do.. it can take 10 minutes or 16 hours to my server to shutdown unexpectedly. I think this happen because an exception is unhandled but I have checked ALL my source to protect everything using Try Except, Finally.. I alse checker all code in EXCEPT section to be sure that no error occure. My server work like that:

My main component is TServerSocket in multithread Mode (stThreadBlocking)
My application is a Non-Interactive Service
I have 3 Thread:
[b]1[/b]: Main tread containing Server Component
[b]2[/b]: Thread checking all connected client and deconnect if there is no activity for a fixed time
[b]3[/b]: Thread Writing the Database on the disk every 5 minutes

ALL Thread is Protected With Try Except

My clients are recorded in a TThreadList (Using .LockList and UnlockList)

Here is some important part of my server:

MainThread:
[code]Server:=TServerSocket.Create(nil);
Server.Name:='HwServerSocket';
Server.OnThreadEnd:=Events.XSocketThreadStop;
Server.OnGetThread:=Events.XSocketGetThread;
Server.OnClientError:=Events.XSocketError;
Server.ServerType:=stThreadBlocking;
ConnectionCount:=0;
Objects.Add(Server);
Server.Open;
{Start messages loop}[/code]
********************************
Thread 2:

[code]Function HwTimerFunc(Param:Pointer):Cardinal;stdcall;
Var
i:integer;
Client:THwServerThread;
Restart:Boolean;
label RestartTimer;
label WaitAgain;
Begin
IsMultiThread:=True;
RestartTimer:
Try
WaitAgain:
Sleep(HwTimerInterval);
//Bonome
If Server.Socket.ActiveConnections=0 then Goto WaitAgain;
Try
Restart:=False;
With Clients.LockList do
For i:=0 to Count-1 do
Begin
Client:=THwServerThread(Items[i]);
If Client.HwState=0 then If ((Frac(Now-Client.ConnectedTime)/(1/24/60/60))>20) then DisconnectClient(Client,False);
If Client.HwState=1 then MySendFunc(' '+#13#10#13#10,Client,False,False,False);
end;

Finally
Clients.UnlockList;
end;
//
except
on E : Exception do LogString('HwTimerFunc',DateTimeToStr(Now)+'> '+E.Message);
end;
Goto RestartTimer;

end;[/code]
******************************
Thread 3:

[code]Function SaveTimerFunc(Param:Pointer):Cardinal;stdcall;
label WaitEnabled;
Begin
IsMultiThread:=True;
Try
WaitEnabled:
While SaveTimerEnabled=False do
Begin
Sleep(1000);
end;
Sleep(SaveTimerInterval);
SaveData;
SaveTimerEnabled:=False;
Goto WaitEnabled;
except
on E : Exception do LogString('SaveTimerFunc',DateTimeToStr(Now)+'> '+E.Message);
end;
end;[/code]

=====================================
Server Events:

[code]Procedure TEventHandlers.XSocketThreadStop(Sender:TObject;Thread: TServerClientThread);
Var
HwThread:THwServerThread;
Begin
HwThread:=THwServerThread(Thread);
Try
If Assigned(HwThread.MemBuf) then
Begin
Finalize(HwThread.MemBuf);
FreeMem(HwThread.MemBuf);
end;
except
end;

Try
If Assigned(HwThread.DBuf) then
Begin
Finalize(HwThread.DBuf);
FreeMem(HwThread.DBuf);
end;
except
end;


Try
With Clients.LockList do Delete(IndexOf(HwThread));
Finally
Clients.UnlockList;
end;

Try
UpdateConnectedState(HwThread,False);
except
on E : Exception do LogString('ThreadStop',DateTimeToStr(Now)+'> '+E.Message);
end;
end;[/code]

**************************************

[code]Procedure TEventHandlers.XSocketError(Sender:TObject;Socket:TCustomWinSocket;ErrorEvent:TErrorEvent; var ErrorCode:Integer);
Var
ErrStr:String;
Begin
Try
ErrorCode:=0;
LogString('Socket Error',dateTimeToStr(Now));
Except
on E : Exception do LogString('XError',DateTimeToStr(Now)+'> '+E.Message);
end;
end;[/code]

*********************************************

[code]Procedure TEventHandlers.XSocketGetThread(Sender:TObject;ClientSocket:TServerClientWinSocket; Var SocketThread: TServerClientThread);
Begin
Try
ClientSocket.OnErrorEvent:=Events.XSocketError;
SocketThread := THwServerThread.Create(False, ClientSocket);
Clients.Add(THwServerThread(SocketThread));
THwServerThread(SocketThread).ConnectedTime:=Now;

Except
on E : Exception do LogString('GetThread',DateTimeToStr(Now)+'> '+E.Message);
end;
end;[/code]

*****************************

[code]Procedure THwServerThread.ClientExecute;
Var
RcvBuf: Array[0..8192] Of char;
RcvBufLen:Integer;
ErrRead:Boolean;
Label TerminateAndFree;
Label SckLoop;
Begin
inherited FreeOnTerminate :=True;

Try
RcvBufLen:=0;
SocketStream := TWinSocketStream.Create(ClientSocket, 5000);
SckLoop:
If ClientSocket.Connected=False then Goto TerminateAndFree;
While SocketStream.WaitForData(1)=False do If ClientSocket.Connected=False then
Begin
Goto TerminateAndFree;
end;

RcvBufLen:=SocketStream.Read(RcvBuf, SizeOf(RcvBuf));
If RcvBufLen < 1 Then Goto TerminateAndFree;
If ClientSocket.Connected Then
Begin
ProcessData(Self,RcvBuf,RcvBufLen);
Goto SckLoop;
End;
TerminateAndFree:
SocketStream.Free;
ClientSocket.Close;
Terminate;
Except
End;
try
SocketStream.Free;
ClientSocket.Close;

except
LogString('ClientExecute',datetimetostr(now));

end;

End;[/code]

*************************************


My THwServer Class:

[code]Type
THwServerThread = Class(TServerClientThread)
public
SocketStream: TWinSocketStream;
ConnectionID,SrvAddr,SrvPort,DataCommand,LoggedName,AttachedTo,LastDateTime,
Contacts,Data,KLD:String;
DBuf,MemBuf:PChar;
FailedConnect,HwState,PrevHwState,ClientType,ClientVersion:Integer;
DBufLen,TotalDBufLen,MemBufLen:Integer;
Key:String;
ConnectedTime:TDateTime;
Procedure ClientExecute; override;
End;[/code]


When the application terminate, there some errors in the logfile but all these errors are normal.
If you have any idea how can I repair this problem please reply, it will be very appreciated.

Thanks for you attention

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories