unit NTServiceThread;
interface
uses
Windows, Messages, SysUtils, Classes,CheckSum, Help, AMC_Registry,
Db, ZMySqlQuery,
ZQuery, ZTransact, ZMySqlTr, ZConnect, ZMySqlCon, Log,ScktComp,Forms, Dialogs;
type
CustomRec =
record
StartTag: integer;
MSG_1:
String[255];
MSG_2:
String[255];
CheckSum: Integer;
EndTag: integer;
end;
type
TNTServiceThread =
Class(TThread)
private
{ Private declarations }
Public
{ Public declarations }
Interval : Integer;
SrvSock: TServerSocket;
// der Server-Socket
serverthread:TServerClientThread ;
ReceiveRec: CustomRec ;
SendRec : CustomRec ;
Error_Counter:Integer;
cstemp:
String;
//nimmt Einmal-Code auf
help:THelp;
logfile:TLog;
registry: AMCRegistry;
procedure Listening(Sender: TObject; Socket: TCustomWinSocket);
procedure Connecting(Sender: TObject; Socket: TCustomWinSocket);
procedure DisConnecting(Sender: TObject; Socket: TCustomWinSocket);
procedure Reading(Sender: TObject; Socket: TCustomWinSocket);
procedure IsError(Sender: TObject; Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
Procedure Execute;
Override;
Published
{ Published declarations }
End;
implementation
{ TNTServiceThread }
uses unit1;
procedure TNTServiceThread.Execute;
Var
TimeOut : Integer;
begin
{ Do NOT free on termination - The Serivce frees the Thread }
FreeOnTerminate := False;
{ Set Interval }
TimeOut := Interval * 4;
help:=Thelp.Create;
registry:=AMCregistry.Create;
logfile:=TLog.create(true);
logfile.OpenLogFile;
SrvSock:=TServerSocket.Create(unit1.DaVe_Licencing.Owner);
logfile.WriteEntry('
Create TServerSocket');
SrvSock.Port:=registry.getPort;
logfile.WriteEntry('
Get Port');
SrvSock.ServerType:=stNonBlocking;
logfile.WriteEntry('
Servertype stNonBlocking');
SrvSock.OnListen:=Listening;
SrvSock.OnClientConnect:=Connecting;
SrvSock.OnClientDisConnect:=DisConnecting;
SrvSock.OnClientRead:=Reading;
SrvSock.Open;
logfile.WriteEntry('
ServSock.Open');
registry.Free;
beep;
{ Main Loop }
Try
While Not Terminated
do
Begin
{ Decrement timeout }
Dec( TimeOut );
If (TimeOut=0)
then
Begin
{ Reset timer }
TimeOut := Interval * 4;
{ DoWork }
End;
{ Wait 1/4th of a second }
Sleep(250);
End;
Except
On E:
Exception do ;
// TODO: Exception logging...
End;
{ Terminate the Thread - This signals Terminated=True }
logfile.CloseLogFile;
Terminate;
end;
procedure TNTServiceThread.IsError(Sender: TObject; Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
Socket.Close;
logfile.WriteEntry('
Event: IsError - Socket.close');
ErrorCode:=0;
end;
procedure TNTServiceThread.DisConnecting(Sender: TObject; Socket: TCustomWinSocket);
begin
logfile.WriteEntry('
DisConnected: ' + Socket.RemoteHost+'
'+Socket.RemoteAddress);
beep;
end;
procedure TNTServiceThread.Connecting(Sender: TObject; Socket: TCustomWinSocket);
begin
Socket.OnErrorEvent:=IsError;
//Memo1.Lines.add('Connected to ' + Socket.RemoteHost+' '+Socket.RemoteAddress);
logfile.WriteEntry('
Connected to ' + Socket.RemoteHost+'
'+Socket.RemoteAddress);
beep;
end;
procedure TNTServiceThread.Listening(Sender: TObject; Socket: TCustomWinSocket);
begin
// TODO
end;
procedure TNTServiceThread.Reading(Sender: TObject; Socket: TCustomWinSocket);
var Licence:
String;
begin
showMessage('
#Incoming');
Socket.ReceiveBuf (ReceiveRec,sizeof(ReceiveRec));
logfile.WriteEntry(ReceiveRec.MSG_1);
end;
end.