-
Notifications
You must be signed in to change notification settings - Fork 3
/
u_3C_TCP_Server.pas
160 lines (134 loc) · 3.56 KB
/
u_3C_TCP_Server.pas
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
unit u_3C_TCP_Server;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr, Dialogs
, SimpleServerThread, uServerThread_3C;
type
TsrvTCP_3C = class(TService)
procedure ServiceStart(Sender: TService; var Started: Boolean);
procedure ServiceStop(Sender: TService; var Stopped: Boolean);
procedure ServiceContinue(Sender: TService; var Continued: Boolean);
procedure ServicePause(Sender: TService; var Paused: Boolean);
procedure ServiceShutdown(Sender: TService);
procedure ServiceCreate(Sender: TObject);
procedure ServiceDestroy(Sender: TObject);
private
{ Private declarations }
FServer: TSimpleServerThread;
public
function GetServiceController: TServiceController; override;
{ Public declarations }
end;
var
srvTCP_3C: TsrvTCP_3C;
implementation
uses
LogFileUnit, uServerIni, uDBSetProviderPool,
uDBSetProvider, uConnectParamsProvider, uFileVersion, strConst,
uMultiThreadVars;
{$R *.DFM}
var
IsDebug: Boolean = False;
procedure ServiceController(CtrlCode: DWord); stdcall;
begin
srvTCP_3C.Controller(CtrlCode);
end;
function TsrvTCP_3C.GetServiceController: TServiceController;
begin
Result := ServiceController;
end;
procedure TsrvTCP_3C.ServiceStart(Sender: TService; var Started: Boolean);
var
StopFlag: Boolean;
params: TDBConnectParams;
provider: TDBSetProvider;
begin
LogInfo('The service version is ', ReadVersion);
FullLog := IniLogOn;
LogInfo('Attempt to start the service...');
if not FServer.WSAError then
begin
//FServer.FreeOnTerminate := True;
// There is no necessity to keep connection to the database
// We only need to test the availability of database for further work
params := TConParamsProvider.GetDBParams;
try
provider := ProviderPool.GetDataBaseFromPool(params);
if Assigned(provider) and provider.Active then
begin
ProviderPool.PutDataBaseToPool(provider);
LogInfo('SUCCESS! ' + rcStrTestConnDB, params.DBPath);
FServer.Resume;
Started := True;
end
else
begin
LogError('FAIL! ' + rcStrTestConnDB, params.DBPath);
if Assigned(provider) then
ProviderPool.PutDataBaseToPool(provider);
Started := False;
ServiceStop(Self, StopFlag);
end;
finally
params.Free;
end;
end
else
begin
Started := False;
ServiceStop(Self, StopFlag);
//FServer.Free;
end;
(*try
DBReconnect;
except
on E: Exception do
LogError('DBReconnect', E.Message);
end;
if DBConnected then
begin
FServer.Resume;
Started := True;
end
else
begin
Started := False;
ServiceStop(Self, StopFlag);
end;*)
end;
procedure TsrvTCP_3C.ServiceStop(Sender: TService; var Stopped: Boolean);
begin
FServer.Terminate;
Stopped := True;
LogWarning('ServiceStop');
end;
procedure TsrvTCP_3C.ServiceContinue(Sender: TService;
var Continued: Boolean);
begin
LogWarning('ServiceContinue');
FServer.Resume;
Continued := True;
end;
procedure TsrvTCP_3C.ServicePause(Sender: TService; var Paused: Boolean);
begin
LogWarning('ServicePause');
FServer.Suspend;
Paused := True;
end;
procedure TsrvTCP_3C.ServiceShutdown(Sender: TService);
var
StopFlag: Boolean;
begin
ServiceStop(Sender, StopFlag);
LogWarning('ServiceShutdown');
end;
procedure TsrvTCP_3C.ServiceCreate(Sender: TObject);
begin
FServer := TServerThread_3C.Create(IniPort);
end;
procedure TsrvTCP_3C.ServiceDestroy(Sender: TObject);
begin
FServer.Free;
LogWarning('ServiceDestroy (' + IntToStr(ClientCounter) + ' clients)');
end;
end.