Turn: realthinclient linkedobjects Demo Analysis

Source: Internet
Author: User

This demo source code implementation is strange, a bit of brain, the reason is that the author wants to use the control to do simple, and encapsulation too much.

Said here is the analysis, in fact, is a rough analysis, I have no patience every realization point is clear, see the source code is generally even read with a guess.

The meaning of this demo is that in the HTTP access mode, it is very convenient to implement the client and the service side of each other's active communication capabilities , which need real-time message interaction, control interaction, data transmission on the very meaningful, very force.

First, the demo work process

1. Implement the function: the ability to upload files to the HTTP server, large file upload success rate is very high, and always maintain a very small amount of memory.

2. Working process: Client and server Trtclinkedmodule control need to establish a communication connection, in the demo there are two ways, the client first create the Trtclinkedmodule object and the server first create Trtclinkedmodule object, However, whichever end is created first, the peer is raised and the object is created so that the communication connection between the two objects is established. After completion, the server pushes the file storage path to the client, and after the client chooses the file, it invokes the service-side upload process (this is only a process, not a function. Because the remote invocation of the Trtclinkedmodule object always triggers only the peer-to-peer Oncallmethod event, the upload is started by using a string identifier in param, such as "Upload" tells the server which process to use. Server in upload first to determine whether the file exists, does not exist, directly invoke the client's "get" process to upload data, and each "get" call only requires the client to send 10K of data, the client prepared the data after the call server "put" to send the data to the server and write to the file, " Put "process again calls" Get ", so back and forth, until the completion of all data upload.

Second, the realization principle and the key function

1. Trtclinkedmodule controls

(1) This control can be placed on a container such as form, datamodule, frame, a container can only have one control! The module that placed the control becomes a communication module that can be created by the remote initiator instance creation request when the communication is needed (in addition to the controls, there are a few lines of code to do, of course, as described below).

(2) The string property of the control Remoteclass is set to the name of the module at the far end, so that when the link module is created to communicate, RTC sends the name as an identity to the remote, and the remote side creates the correct module based on the name.

(3) The member function Callmethod (const xmethodname:string) directly triggers a peer-to-peer Oncallmethod event in which the processing is determined according to the Xmethodname, which appears to be generally used for data blocks or complex controls.

(4) member function setprop (const xpropname:string; xnow:boolean), SetEvent (const xeventname:string) Direct triggering of Onsetprop and onsetevent events on the end for simple short message interactions or control interactions.

(5) The member function broadcast (const Xchannel, xmethodname:string) is a multicast message within the local module, Xchannel is a multicast pipeline, and the pipeline is composed of member functions subscribe (const Xchannel: String) is established.

(6) The control is fairly simple to use, the above property is set to complete, no tube how and HTTP communication control AH or clientmodule, such as remote control of the connection, throw on it is available (this is also a confusing place, behind the resolution of how it is associated with the HTTP communication control).

2. Function Registerrtcobjectconstructor (const xclassname:string; xproc:trtcobjectconstructor):

(1) must be executed in initialization block, required and unregisterrtcobjectconstructor paired with
(2) This function is used to specify a constructor that can be called remotely to allow the remote to create Trtclinkedmodule objects and modules when needed
(3) The xclassname of the function is specified in the Remoteclass in the Trtclinkedmodule control, xproc as a procedure makeobject (sender:tobject; Param:trtcobjectcall); a type of static function, which is why it is necessary to create the key to the Remote link module, when creating demand, according to the Registerrtcobjectconstructor function registration information, Find the Xproc function call, Ok,link Module object generation, the principle is very simple.
(4) Another alternative is to create work in the Onobjectcreate event in the Trtcclientmodule or Trtcservermodule control, but this requires that the module control be used for communication on both ends.

function meaning: The remote send creates the Linkobject Communication Object command, and the local side finds the registered constructor and invokes it in ObjectManager based on the Xclassname string sent from the remote. So the program needs to create a good object manager using Activateobjectmanager.

3. Function Activateobjectmanager (xcreate:boolean=true), Trtcdataserver and Trtcclientmodule member functions.

(1) This function must be called before communicating with the Trtclinkedmodule control, otherwise it cannot communicate with the remote, and the remote module creation request cannot be initiated properly.

(2) Rtcclientmodule1.objectlinks for "ol_manual" must be manually called, set to "Ol_autoclient" or "Ol_autoboth" the RTC will be automatically called in the background.

I can't help it. A point of view for remote function call schemas:

The Remobject kit is a function call that looks very consistent with conventional understanding, so that you do not feel this is a remote function, in order to achieve such a sense of use, RO with a more sophisticated technology to achieve, admittedly, this is quite gold, not anyone can write out, this technology has so let me praise and learn. and the RTC Remote call all with the string identification way, so simple, do not need complex technology, most of the experience of the programmer based on TCP and other underlying can be done, but for many years now, I found this simple beauty and efficiency, the string can describe from simple to extremely complex structure, Web page Ah , XML AH This is not all described, so its flexibility can be said that there is no fetters, to achieve what can be achieved what how to achieve only care about the user bent. The author of the RTC must have been very well-established in the realm of programming thought.

Three, the Strange place

According to the truth, Trtclinkedmodule control to be able to communicate, there must be a place to set up communication, but to find out the properties of Trtclinkedmodule and demo source, have not found a place to set up, then it is how to complete the communication?

Let's take a look at what Trtclinkedmodule's constructor has done:

1 constructor trtclinkedmodule.create (aowner:tcomponent);
2 Begin
3 inherited Create (Aowner);
4 if not (csdesigning in componentstate) then
5 Begin
6//This would only work if there is a active Object Manager for the current thread
7
8 If assigned (Aowner) then
9 folink:=trtcbasicobjectlink.create (Aowner,fparam.manager)
All else/If No Owner, we are the Object container
folink:=trtcbasicobjectlink.create (Self,fparam.manager);

See Getrtcobjectmanager, this is a global function, according to the thread ID get object manager, ObjectManager estimate is a global, unique singleton pattern of the object, too lazy to see the code, guess, haha.

ObjectManager estimate is a Linkobject manager for the link control to send a request to find the correct module and the correct link control, and then call the corresponding function, now the question is, How does ObjectManager know to send the request to the corresponding form and link? The above code in Folink:=trtcbasicobjectlink.create (Aowner,fparam.manager), see the Incoming Aowner! And manager, what do you do? Keep going and see what trtcobjectlink.create is:

1 constructor trtcobjectlink.create (Xowner:tobject;xmanager:trtcobjectmanager);
2 Begin
3 inherited Create;
4 If Xowner=nil then
5 Raise Ertcobjectlinks.create (' trtcobjectlink.create called with Xowner=nil ');
6 If Xmanager=nil then
7 Raise Ertcobjectlinks.create (' trtcobjectlink.create called with Xmanager=nil ');
8 Fsubs:=nil;
9 Fowner:=xowner;
Ten Fmanager:=xmanager;
if Fmanager.creatingobjectid<>0 then//remote constructor
begin
Foid:=fmanager.creatingobjectid;
fmanager.creatingobjectid:=0;
Fcreator:=false;
End
+ Else
begin
Foid:=fmanager.getnextobjectid;
fcreator:=true;
The end;
Fremotedestroyed:=false;
Fmanager.addobject (Foid,self,fowner);
The end;

The last sentence: Fmanager.addobject (Foid,self,fowner); Obviously, here Trtcobjectlink the object ID foid and its own object pointer self and the window where the Fowner is passed in, three of the corresponding relationship has been, ObjectManager It's easy to find the right module. While the Trtcobjectlink object is embedded in the Trtclinkedmodule control object, it is a composite relationship, and the specific function and event invocation are Trtcobjectlink implementations.

The question now is, how does the link object get the ability to communicate?

Note that Rtcclientmodule1.activateobjectmanager (True), this call, RtcClientModule1 is connected to the Trtchttpclient object, and therefore has the HTTP communication capability, And look at the relationship between RtcClientModule1 and ObjectManager:

1 procedure Trtcclientmodule.activateobjectmanager (xcreate:boolean=true);
2 Begin
3 if Frelease then Exit;

5 If Objectlinks=ol_none then
6 Raise Ertcobjectlinks.create (' activateobjectmanager:objectlinks = Ol_none ')
7 Else if not assigned (Fobjectmanager) then
8 If Xcreate then
9 begin
Ten fobjectmanager:=trtcclientobjectmanager.create (False);
fobjectmanager.broadcastgroup:=string (fobjmansesname);
Fobjectmanager.ondataready:=doobjectmanagerdataready;
The end;

if not assigned (Fobjectmanager) then
Raise Ertcobjectlinks.create (' Activateobjectmanager failed ')
+ Else
begin
Setrtcobjectmanager (Fobjectmanager);
Fobjectmanager.executebroadcast (nil);
The end;
The end;

Oh, so this guy finds out that without ObjectManager, he creates one and holds it and sends it to a global object fobjmanagers with Setrtcobjectmanager, which is a list, And Getrtcobjectmanager is from this fobjmanagers, now the truth, the original is Trtchttpclient control received a request to Trtcclientmodule object, The private member of the Trtcclientmodule object Fobjectmanager holds the ObjectManager object reference, and the ObjectManager object finds the correct module based on the stored owner, link object relationship. Call the link object-related function. Well, but the specific code does not look closely, the above truth has a larger guess ingredients, but I believe that the rationale to deal with, substantive realization, please see this article of the intention of netizens pointed out, criticism, common progress.

Turn: realthinclient linkedobjects Demo Analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.