部分原碼 (一)
unit DataServer_form;
{$WARN SYMBOL_PLATFORM OFF}
interface
uses
Windows, Messages, SysUtils, Classes, ComServ, ComObj, VCLCom, DataBkr,
DBClient, swServer2_TLB, Provider, ADODB, DB, hmUniKey,
Variants, hmOleVariant, dmBaseModule, dmBaseService, StdVcl,
hmClientDataSet, hmADQ, hmADC;
type
TDataServer2 = class(TRemoteDataModule, IDataServer2)
dspTest: TDataSetProvider;
SqlLang: TADOQuery;
Connection: TADOConnection;
mdLibrary: TADOQuery;
cdsDelta: THMClientDataSet;
Query: THMADQ;
Pub1: THMClientDataSet;
Cmd: THMADC;
Pub2: THMClientDataSet;
Pub3: THMClientDataSet;
procedure RemoteDataModuleCreate(Sender: TObject);
private
FDataBase: string;
FUKI: string;
FOle: THMOleVariant;
FParams: THMOleVariant;
FBaseService: TBaseService;
protected
class procedure UpdateRegistry(Register: Boolean;
const ClassID, ProgID: string);
override;
function DataModule(ModuleIndex: Integer;
Param: OleVariant;
var Data, Msg: OleVariant): WordBool;
safecall;
function Get_Database: WideString;
safecall;
procedure Set_Database(const Value: WideString);
safecall;
function DefaultModule(Param: OleVariant;
var Data, Msg: OleVariant): WordBool;
safecall;
function CreateModule(const BaseService: IBaseService;
ModuleIndex: integer): IBaseDataModule;
procedure OnLoadOleData(Sender: TObject;
Name: WideString);
public
constructor Create(AOwner: TComponent);
override;
destructor Destroy;
override;
procedure SetDefaultDatabase;
procedure DebugText(Msg: string);
property UKI: string read FUKI;
property Ole: THMOleVariant read FOle;
property OleParams: THMOleVariant read FParams;
end;
implementation
uses MainForm_form, swModuleIndex, dmSystemService;
{$R *.DFM}
class procedure TDataServer2.UpdateRegistry(Register: Boolean;
const ClassID, ProgID: string);
begin
if Register then
begin
inherited UpdateRegistry(Register, ClassID, ProgID);
EnableSocketTransport(ClassID);
EnableWebTransport(ClassID);
end
else
begin
DisableSocketTransport(ClassID);
DisableWebTransport(ClassID);
inherited UpdateRegistry(Register, ClassID, ProgID);
end;
end;
constructor TDataServer2.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FUKI := UniKeyInit;
FOle := THMOleVariant.Create;
FParams := THMOleVariant.Create;
Connection.ConnectionString := frmDataServer.ConnectionString;
SetDefaultDatabase;
FBaseService := TBaseService.Create(Self);
mdLibrary.SQL.Text := 'Select md_ID,md_moduleIndex,md_State,md_ModuleAddr from ' + LibUniKey;
mdLibrary.Open;
Inc(OnLineUserCount);
end;
destructor TDataServer2.Destroy;
begin
FBaseService.Free;
FOle.Free;
FParams.Free;
Dec(OnLineUserCount);
mdLibrary.Close;
inherited Destroy;
end;
function TDataServer2.CreateModule(const BaseService: IBaseService;
ModuleIndex: integer): IBaseDataModule;
var
Module: PModuleLibrary;
begin
if not mdLibrary.Locate('md_ModuleIndex', ModuleIndex, []) then
Exception.Create('服務器裝載出錯
ataMoudule(' + inttostr(ModuleIndex) + ') not found.');
if mdLibrary.FieldByName('md_State').AsInteger <> 1 then
Exception.Create('服務器裝載出錯
ataMoudule(' + inttostr(ModuleIndex) + ') not start.');
Module := Pointer(mdLibrary.FieldByName('md_ModuleAddr').AsInteger);
if (Module <> nil) then
Result := Module^.CreateDataModule(BaseService)
else
raise
Exception.Create('服務器裝載出錯
ataMoudule(' + inttostr(ModuleIndex) + ') not found.');
end;
function TDataServer2.DataModule(ModuleIndex: Integer;
Param: OleVariant;
var Data, Msg: OleVariant): WordBool;
var
FdmModule: IBaseDataModule;
begin
if ServiceState = srStart then
begin
try
FdmModule := CreateModule(FBaseService, ModuleIndex);
FdmModule.Module := ModuleIndex;
except
on E: Exceptiondo
begin
Result := False;
Msg := '服務器出錯:無法建立數據模組,' + E.Message;
exit;
end;
end;
end
else
begin
Msg := '服務器沒有啟動,請通知管理員.';
Result := False;
exit;
end;
try
FBaseService.Reset;
FOle.Clear;
FParams.Clear;
if not VarIsNull(Param) then
FParams.LoadFromOle(Param);
Result := FdmModule.Operation(Data, Msg);
if Result then
begin
case FBaseService.ReceiveDataType of
rdNoData:
begin
Data := Null
end;
rdDefault:
begin
Data := dspTest.Data;
end;
rdCustom:
begin
Ole.SaveToOle(Data);
Ole.Clear;
end;
else
begin
// Data:=Data;
rdResult;
end;
end;
end;
FdmModule := nil;
FOle.Clear;
FParams.Clear;
Query.Close;
if cdsDelta.Active then
cdsDelta.EmptyDataSet;
cdsDelta.Close;
if Pub1.Active then
Pub1.EmptyDataSet;
if Pub2.Active then
Pub2.EmptyDataSet;
if Pub3.Active then
Pub3.EmptyDataSet;
Pub1.Close;
Pub2.Close;
Pub3.Close;
except
on E: Exceptiondo
begin
Msg := '服務器裝載出錯,請通知管理員,錯誤信息:' + E.Message;
Result := False;
end;
end;
end;
function TDataServer2.DefaultModule(Param: OleVariant;
var Data, Msg: OleVariant): WordBool;
begin
Result := DataModule(dmDefault, Param, Data, Msg);
end;
procedure TDataServer2.DebugText(Msg: string);
begin
//if frmDataServer.Status.CheckSQL then
frmDataServer.Status.AddItem(Msg);
end;
function TDataServer2.Get_Database: WideString;
begin
Result := FDatabase;
end;
procedure TDataServer2.Set_Database(const Value: WideString);
begin
FDataBase := Value;
Connection.DefaultDatabase := Value;
DebugText('Change Database:' + Value);
end;
procedure TDataServer2.SetDefaultDatabase;
begin
FDataBase := frmDataServer.DafaultDataBase;
Connection.DefaultDatabase := FDataBase;
end;
procedure TDataServer2.RemoteDataModuleCreate(Sender: TObject);
begin
cdsDelta.OnLoadOleData := OnLoadOleData;
Pub1.OnLoadOleData := OnLoadOleData;
Pub2.OnLoadOleData := OnLoadOleData;
Pub3.OnLoadOleData := OnLoadOleData;
end;
procedure TDataServer2.OnLoadOleData(Sender: TObject;
Name: WideString);
begin
if Sender is THMClientDataSet then
begin
(Sender as THMClientDataSet).Close;
(Sender as THMClientDataSet).Data := FParams[Name];
(Sender as THMClientDataSet).Open;
end
else
raise Exception.Create('(OnLoadOleData)Sender is not a THMClientDataSet');
end;
initialization
TComponentFactory.Create(ComServer, TDataServer2,
Class_DataServer2, ciMultiInstance, tmApartment);
end.