Create an unigui application server using the Wizard. There are three files in the project:
TUniServerModule = class(TUniGUIServerModule)TUniMainModule = class(TUniGUIMainModule)TMainForm = class(TUniForm)
(1) The Singleton mode is used. All clients share a tuniservermodule instance object. The following Code shows that:
function UniServerModule: TUniServerModule; implementation {$R *.dfm} uses UniGUIVars;function UniServerModule: TUniServerModule;begin Result:=TUniServerModule(UniGUIServerInstance);end;initializationRegisterServerModuleClass(TUniServerModule);
The global variable uniguiserverinstance is used to implement a unique tuniservermodule instance object. You can view the object as a global control object and construct some objects that need to be unique in the tuniguiservermodule class. For example, you can consider building a database connection pool, you can also build an object pool if necessary.
(2) After each client is connected, the system creates a tunimainmodule object to support and manage each client connection. The implementation mechanism is unknown because no source code is available. The instance obtains the following code:
function UniMainModule: TUniMainModule;implementation{$R *.dfm}uses UniGUIVars, ServerModule, uniGUIApplication;function UniMainModule: TUniMainModule;begin Result := TUniMainModule(UniApplication.UniMainModule)end;
The unimainmodule attribute of the global variable uniapplication is used to call the tunimainmodule instance object. It is not clear how to differentiate connections between different clients. The registration code of the class is as follows:
initialization RegisterMainModuleClass(TUniMainModule);
The tuniguimainmodule instance is the control object of each connection. You can store database connections and datasets in this class. If there are many datasets, you can also consider creating multiple tdatamodule dynamically, it is managed and maintained by the tuniguimainmodule instance. If it is a three-tier structure, tsqlconnection should also be placed in this module.
(3) The main form is a tuniform class and a form built by an application. It is implemented through registration as follows:
function MainForm: TMainForm;implementation{$R *.dfm}uses uniGUIVars, MainModule, uniGUIApplication;function MainForm: TMainForm;begin Result := TMainForm(UniMainModule.GetFormInstance(TMainForm));end;
Use the following code to register
initialization RegisterAppFormClass(TMainForm);
All application construction forms are implemented in the preceding method, and the application controls its lifecycle. You can also customize the free form to control the creation and release of the form.
In short, you can regard tuniservermodule instance objects as global control objects, tunimainmodule objects as each connection control object, and the first registered tuniform application form is the main form.
(4) access to each connection thread can be obtained through uniservermodule. sessionmanager. sessions for further processing, as shown below:
Procedure tunimainmodule. uniguimainmodulecreate (Sender: tobject); var I: integer; asessionlist: tlist; asession: tuniguisession; begin {lock list} asessionlist: = uniservermodule. sessionmanager. sessions. sessionlist. locklist; try {access to each thread} For I: = 0 to asessionlist. count-1 do begin asession: = tuniguisession (asessionlist [I]); if not asession. isterminated then {for processing, such as the client address asession. uniapplication. remoteaddress} end; finally {release list} uniservermodule. sessionmanager. sessions. sessionlist. unlocklist; end;
You can also directly access the current thread object in the tunimainmodule instance or tuniform instance, for example:
Procedure tunimainmodule. uniguimainmoduledestroy (Sender: tobject); var strclientip: string; begin strclientip: = unisession. uniapplication. remoteaddress; {further processing} end;
Unigui trial notes (1)