Summary of the invention
A kind of method that the technical problem to be solved in the present invention is in that to avoid the deficiencies in the prior art part and proposes to give remote procedure call, it is possible to realize local or based on intranet INTRANET or based on the remote procedure call of the Internet INTERNET;The present invention achieves remote method invocation (RMI) by underlay approach, it is possible to be applied to remote data access, it is achieved the logic business middleware of data base.
The method of the remote procedure call that the present invention proposes, grasping on the basis of bottom layer realization basic principle, realized the suction parameter of remote method and the data interaction of outlet parameter by the mode of TCP/UDP, realized the concurrent access mechanism of process by Thread Pool Technology.The remote method that the present invention relates to accesses unrelated with concrete application, as long as the development language of the application based on TCP/UDP can be developed, namely the basic data type that all development languages are all supported is merely related to, developing instrument can both realize calling of remote method, and server end can use different developing instruments and language to realize respectively from client.System can be deployed under the operating system of any support TCP/UDP communication modes, supports that the remote method of portable operating system accesses.Greatly simplify the deployment of system and realization, one client Client/ server end Server model application of framework is served to the effect of basic framework.
This invention address that described technical problem can by realizing by the following technical solutions:
A kind of method proposing remote procedure call based on transmission control protocol TCP/UDP UDP, for including the network system of server end and client;Described client establishes the link with server end by the socket Socket based on transmission control protocol TransmissionControlProtocol/ UDP UserDatagramProtocol.Described method comprises the steps:
System object pond and system thread pool is set up at server end;Preserving the system interface object called for complete process in described system object pond, described system interface object is assigned with unique object identity code IDentity and unique interface identifier code IDentity;Thus system interface object can be determined uniquely according to object identity code ID and interface identifier code ID;Described system thread pool includes at least two can the worker thread of simultaneously concurrent working;Thus making server end and client can complete remote procedure call RemoteProcedureCall according to following steps,
A. the procedure call data calling main body input is encapsulated as and calls packet by client, and this calls packet and includes object identity code ID, interface identifier code ID and arrange parameter for what complete process called needs;
B. client described in step A is set up and the linking of server end by socket Socket;
C. client sends calling packet described in step A to server end;
D. server end finds corresponding system interface object and interface object to perform method according to the object identity code ID called in packet received and interface identifier code ID in system object pond;
E. server end finds the worker thread of free time in object pool, the system interface object, interface object execution method and the described parameter that arranges called in packet that are found by step D input to worker thread, start worker thread and perform the processing procedure of system interface object;
F. worker thread is performed the result of system interface object and sends to client with the form of return parameters bag by server end;
G. client performs corresponding operating according to the return parameters bag received for calling main body.
Specifically, described in step A, call the time-out time of the data to be returned such as packet also includes, return the pointer of depositing of data, and spread out of parameter for what show byte quantity that this invocation of procedure obtains.
In order to ensure the integrity establishing the link process, step B also includes as follows step by step,
B1. client is initiated and the linking of server end according to socket Socket,
If B2. client and server establishes the link successfully, carry out step C;
If B3. client and server establishes the link failure, terminate this invocation of procedure.
In order to prevent far call process time-out, described in step A, call the time-out time of the data to be returned such as packet also includes, then step C includes as follows step by step,
C1. client sends calling packet described in step A to server end;
C2. client terminal start-up returns data waiting-timeout timing;
Meanwhile, step G includes as follows step by step,
G1. comparing the return data waiting-timeout timing time of C2 step by step and the data timeout time to be returned such as call described in packet, if returning data waiting-timeout timing time the data timeout time to be returned such as to be not more than, performing G2 step by step;If return data waiting-timeout timing time more than etc. data timeout time to be returned, terminate this invocation of procedure;
G2. client performs corresponding operating according to the return parameters bag received for calling main body.
Specifically, G2 includes as follows step by step step by step,
G21. return parameters bag intrinsic parameter is resolved, it is judged that whether the invocation of procedure is successful;
G22. to calling the parameter that main body returns in the return parameters bag of parsing, corresponding operating is performed for calling main body;
G23. the connection with server end is closed.
Received server-side to a kind of concrete processing scheme after calling packet is, described step D include following step by step,
Whether the object identity ID called in packet and interface identifier code ID that D11. server end judgement receives be legal;D12 step by step is carried out if it is determined that legal;If it is determined that illegal, unsuccessful for invocation of procedure result is sent to client with the form of return parameters bag, perform step G;
D12. server end finds corresponding system interface object and interface object to perform method according to the step C object identity code ID called in packet received and interface identifier code ID in system object pond.
Received server-side to the concrete processing scheme after calling packet is, also includes connecting worker thread in the described thread pool of server end, then described step D include following step by step,
D21. start and connect worker thread;
D22. will receive and call data packet sequencing, and successively each packet that calls be performed D23 step by step, D24 step by step, and step E, step F;
Whether the object identity ID called in packet and interface identifier code ID that D23. judgement receives be legal;D24 step by step is carried out if it is determined that legal;If it is determined that illegal, unsuccessful for invocation of procedure result is sent to client with the form of return parameters bag, perform step G;
D24. server end finds corresponding system interface object and interface object to perform method according to the step C object identity code ID called in packet received and interface identifier code ID in system object pond;
And then described step E include following step by step,
E1. whether available free in server-side search object pool worker thread, if available free worker thread, perform E2 step by step;Without idle worker thread, it is maintained at calling packet in the sequencing queue connecting worker thread, returns D22 step by step;
E2. server end finds the worker thread of free time in object pool, the system interface object found by step D24, the interface object needing execution perform method and the described parameter that arranges called in packet inputs to worker thread, start worker thread and perform the processing procedure of system interface object.
Client receives the concrete processing method of the return parameters bag that server end is sent, and step G includes as follows step by step,
G31. return parameters bag intrinsic parameter is resolved, it is judged that whether the invocation of procedure is successful;
G32. to calling the parameter that main body returns in the return parameters bag of parsing, corresponding operating is performed for calling main body;
G33. the connection with server end is closed.
Compared with the existing technology relatively, the having the technical effect that of the present invention " method of remote procedure call based on TCP/UDP "
1. exploitation is simple, realizes unrelated with concrete application with the invocation of procedure under unified shader;Under the remote procedure call framework that the present invention provides, system bottom code realizes simple, reproducibility is strong, developer need not grasp bottom code and realize, system is to concurrent access, the encapsulation of message packet is realized by unified interface function, and developer has only to pay close attention to business characteristic, and exploitation business realizing code can realize the exploitation of application server and client;
2. the present invention borrows and rents physical layer interface link, system platform is not specially required, it is possible in different platform, realizes remote procedure call under different development environments;The present invention does not limit to concrete development language, if the scheme provided according to the present invention, it is possible to achieve cross-platform, across the remote procedure call support of development environment;In system message, the data type of transmission is common to different development languages and development platform, possesses good transplanting characteristic;
3. the present invention makes thread parallel perform, and utilizes the parallel processing capability of multi-core processor, it is achieved the concurrent access of remote method or data;
4. system expandable ability is strong;Owing to adopting bottom layer realization mechanism, the present invention can be extended according to concrete service application, and including security feature, the extension of concurrent characteristic realizes;Such as in the exploitation of network management platform, in order to realize the polling operation to equipment, it is only necessary to the functional extension of adding portion code and feasible system in main business thread.
Detailed description of the invention
It is described in further detail below in conjunction with preferred embodiment shown in accompanying drawing.
The present invention proposes a kind of method of remote procedure call based on transmission control protocol TCP/UDP UDP, for including the network system of server end and client.Described server end is made up of the hardware being used for realizing server capability.Described client is made up of the terminal hardware being used for realizing client functionality.Described client establishes the link with server end by the socket Socket based on transmission control protocol TCP/UDP UDP.Described method comprises the steps:
System object pond and system thread pool is set up at server end.Preserving the system interface object called for complete process in described system object pond, described system interface object is assigned with unique object identity code ID and unique interface identifier code ID.Thus system interface object can be determined uniquely according to object identity code ID and interface identifier code ID.System object pond preserves system interface object by the mode of container.So-called system interface object, namely can provide the instantiation of the interface class of remote method invocation (RMI), and total interface prototype all defines at interface apoplexy due to endogenous wind, by instantiation mode by far call.Server end start after, instantiation total interface class, and load all systems support interface object in object pool.Just can determine that, with interface identifier code ID, the execution method that this interface object is corresponding by the object identity code ID of this interface object, namely interface object performs method.Service end start after, instantiation total interface class, and load all systems support interface object in object pool.
Described system thread pool includes at least two can the worker thread of simultaneously concurrent working.As it is shown in figure 1, when server end initializes, all of thread broadly falls into idle condition.Worker thread can be parallel the method for execution interface object, the parallel processing capability of system is decided by worker thread quantity, quantity generally according to each 2 concurrent thread of core CPU support is set, and in thread pool, worker thread quantity can input when system initialization or load from configuration file.
The preferred embodiment of the present invention, as it is shown in figure 1, the remote procedure call that completes of described method needs to carry out between client 100 and server end 200, the present invention makes server end and client can complete remote procedure call according to following steps,
A. as shown in Figure 1, after enabling remote procedure call 101, performing 102, the procedure call data calling main body input is encapsulated as and calls packet by client, and this calls packet and includes object identity code ID, interface identifier code ID and arrange parameter for what complete process called needs;Described main body of calling can be that terminal use inputs, or from feedback information;
B. client described in step A is set up and the linking of server end by socket Socket;
C. as shown in Figure 1 104, client sends calling packet described in step A to server end;
D. as shown in Figure 1 201, after server end completes to initialize, server end finds corresponding system interface object and interface object execution method according to the object identity code ID called in packet received and interface identifier code ID in system object pond;
E. server end finds the worker thread of free time, the system interface object found by step D and the described parameter that arranges called in packet to input to worker thread in object pool, starts worker thread and performs the processing procedure of system interface object;Described worker thread performs interface object execution method according to arranging parameter;
F. as shown in Figure 1 206, worker thread is performed the result of system interface object and sends to client with the form of return parameters bag by server end;It is exactly that worker thread is according to arranging the method return value obtained after parameter performs interface object execution method that worker thread performs the result of system interface object, the method return value is encapsulated as return parameters bag, this return parameters bag is presented as data message, namely returns data message;The socket SOCKET connection that described return data message is set up by client and server returns to client from server end;
G. client performs corresponding operating according to the return parameters bag received for calling main body.
Specifically, described in step A, call the time-out time of the data to be returned such as packet also includes, return the pointer of depositing of data, and spread out of parameter for what show byte quantity that this invocation of procedure obtains.
The preferred embodiment of the present invention, when packet is called in system encapsulation, developer have employed the mode of template class and achieves the encapsulation calling packet, is referred to following message encapsulation function false code prototype,
template<classSTR_T>intsendDataStream(intmethod_id,STR_T*str_method,inttime_out,_UC**recv_buff,int&recv_size,intobj_id)
This function can generate the data stream of class members's method, and passes to remote server by socket SOCKET, calls remote object method, and return data is spread out of.System adopts the mode of template class to realize the fusion of different types of data structure, adopts unified mode to realize the generation of outgoi8ng data.The implementation adopting template class is because incoming structure and not can determine that at present, and the moment that can only by the time run just knows the type of incoming method structure
Being defined as follows of above-mentioned parameter,
Intmethod_id calls the method ID of remote object, macrodefinition determine;
STR_T*str_method template class, it is necessary to pass to the argument structure of remote object;
The time-out time of the data to be returned such as inttime_out, the unit second;
_ UC**recv_buff returns the pointer that data are deposited, and states and apply for internal memory in the function call this function, deletes releasing memory (single byte integer) in the function call this function;
Int&recv_size spreads out of parameter, it was shown that the byte quantity that this far call obtains;
Intobj_id calls the object ID of remote object;
The return value of above-mentioned encapsulation be return SOCKET function return value, time-out or be successfully transmitted.
In order to prevent far call process time-out, call the time-out time of the data to be returned such as packet also includes described in step A, as shown in Figure 1, then step C includes as follows step by step,
C1. as shown in Figure 1 104, client sends calling packet described in step A to server end;
C2. client terminal start-up returns data waiting-timeout timing;
Meanwhile, step G includes as follows step by step,
G1. as shown in Figure 1 105, relatively step by step the return data waiting-timeout timing time of C2 with the data timeout time to be returned such as call described in packet, if returning data waiting-timeout timing time the data timeout time to be returned such as to be not more than, perform G2 step by step;If return data waiting-timeout timing time more than etc. data timeout time to be returned, perform shown in Fig. 1 107, terminate this invocation of procedure;
G2. client performs corresponding operating according to the return parameters bag received for calling main body.
Specifically, step by step G2 include following step by step, as shown in Figure 1 106,
G21. return parameters bag intrinsic parameter is resolved, it is judged that whether the invocation of procedure is successful;
G22. to calling the parameter that main body returns in the return parameters bag of parsing, corresponding operating is performed for calling main body;
G23. the connection with server end is closed.
In order to ensure the integrity establishing the link process, step B also includes as follows step by step,
B1. client is initiated and the linking of server end according to socket Socket,
B2. as shown in Figure 1 103 to 104, if client and server establishes the link successfully, carry out step C;
B3. as shown in Figure 1 103 to 107, if client and server establishes the link failure, terminate this invocation of procedure.
Received server-side to a kind of concrete processing scheme after calling packet is, as it is shown in figure 1, described step D include following step by step,
D11. as shown in Figure 1 202, server end judges that whether the object identity ID called in packet that receives and interface identifier code ID is legal;D12 step by step is carried out if it is determined that legal;If it is determined that illegal, as shown in Figure 1 205, unsuccessful for invocation of procedure result is sent to client with the form of return parameters bag, performs step G;
D12. server end finds corresponding system interface object and interface object to perform method according to the step C object identity code ID called in packet received and interface identifier code ID in system object pond.
Received server-side to the another kind of concrete processing scheme after calling packet is, as it is shown in figure 1, also include in the described thread pool of server end connecting worker thread, then described step D include following step by step,
D21. start and connect worker thread;
D22. will receive and call data packet sequencing, and successively each packet that calls be performed D23 step by step, D24 step by step, and step E, step F;
D23. as shown in Figure 1 202, it is judged that whether the object identity ID called in packet and the interface identifier code ID that receive be legal;D24 step by step is carried out if it is determined that legal;If it is determined that illegal, as shown in Figure 1 205, unsuccessful for invocation of procedure result is sent to client with the form of return parameters bag, performs step G;
D24. server end finds corresponding system interface object and interface object to perform method according to the step C object identity code ID called in packet received and interface identifier code ID in system object pond;
And then described step E include following step by step,
E1. as shown in Figure 1 203, worker thread whether available free in server-side search object pool, if available free worker thread, perform E2 step by step;Without idle worker thread, it is maintained at calling packet in the sequencing queue connecting worker thread, returns D22 step by step;
E2. as shown in Figure 1 204, server end finds the worker thread of free time in object pool, the system interface object found by step D24, the interface object needing execution perform method and the described parameter that arranges called in packet inputs to worker thread, start worker thread and perform the processing procedure of system interface object.
Client receives the concrete processing method of the return parameters bag that server end is sent, as shown in Figure 1 106, step G include following step by step,
G31. return parameters bag intrinsic parameter is resolved, it is judged that whether the invocation of procedure is successful;
G32. to calling the parameter that main body returns in the return parameters bag of parsing, corresponding operating is performed for calling main body;
G33. the connection with server end is closed.
Above steps can adopt Implementation of pseudocode as shown in the table at this preferred embodiment:
Code analysis
TypedefstructPARA_STRUCT is the parameter of set form
Describing, its definition is as follows:
_ULdevice_id;Object ID
_ULmethod_id;Interface method ID
_ULdata_struct_size;Interface parameters size
void*para_data;Interface parameters structured fingers
}PARA_STRUCT;
// perform object method core code
voidExecute_Object_Method(PARA_STRUCT&
_ para, TClientConnThread*client) function prototype, client is SOCKET output lead
{ journey is used for returning the transmission of result message
TDevice*device=getDevice(_para.device_id);
Device is interface object, from input parameter
If (device==NULL) { obtains object ID, and finds interface object
If interface object (can not find connecing of correspondence for empty
// ... mouth object)
return;Carrying out fault processing, code is slightly
And return, do not continue executing with subsequent operation
TprocessThread*_idle_thread=_threadpool->
_getIdleThread();_ threadpool is thread pool object, at thread pool
If (_ idle_thread==NULL) in find idle thread
//……
Without idle thread
return;Message packet is saved in queue, follow-up free
Idle thread continues with, code is slightly
device->setPARA(_para);Return, do not continue executing with subsequent operation
_idle_thread->setDevice(device);Set the parameter that object to perform, describe and to hold
_idle_thread->setClient(client);The method of row
_idle_thread->Resume();Set the object that idle thread performs
Set the SOCKET passage returning result message
// thread performs body core code thread to start to independently execute object method (parallel)
voidTprocessThread::Execute()
After thread RESUME, the work shape of thread
while(!Terminated) { state becomes BUSY, and performs following code
device->execute_method();
RESULT_STRUCTresult=performs the method for object, and object method performs result
device->getRESULT();It is saved in the result structure of object,
generatePackage(result);Obtain the result set that interface object performs to return
If (result.result_data) { carries out the encapsulation operation of returned packet
deleteresult.result_data;If returning valid data (pointer is not empty)
result.result_data=NULL;Delete the internal memory that release has been applied for
Pointer is set to sky
sendResult();Send returned packet
Delete [] _ data._data_stream deletes the internal memory of returned packet application
Suspend();Thread suspension (state becomes IDLE)
}
}
// object execution method core code
voidTDevice::execute_method()
{
// ... object execution method prototype, before execution, perform need
The parameter wanted is before already by thread
intresult_value;Device-> setPARA (_ para) method sets
Switch (para_struct.method_id), before performing concrete method, does the school of parameter
{ testing work, code is slightly
CaseGET_SYS_DESCRIPTION_METHOD_ID: the method for returning returns value statement
result_value=Get_sys_description();Method ID according to parameter, carries out performing method choice
break;
//……
Perform selected method, and result is saved in
Result_value, for follow-up return result message
Follow-up code is slightly.
The present invention can as the exploitation basis of data access middleware.The access of heterogeneous database be service application have in the face of and solve problem, by the problem that the mode of data access middleware can well solve this type.Data access middleware is by the encapsulation to database access, it is achieved the access to distinct type data-base.On the remote method basis that the present invention provides, in conjunction with XML(extensible markup language: ExtensibleMarkupLanguage) the technology realization encapsulation to data set, return the data set returned packet as remote method, client aspect increases the parsing to XML can realize the middleware of business independence, client need not directly set up the connection with database server, replace different types of database server, have only to change different database-driven, without client is done any kind of change.
The present invention can be additionally used in the exploitation of application service middleware.For concrete service application, can by business logically independent out, method provided by the invention is adopted to realize the encapsulation of service logic, client represents layer as business, do not realize concrete service logic, by the remote method invocation (RMI) that the present invention designs, it is achieved the access to service logic, make the system application of three-tier architecture.