Business response method and device, electronic equipment and storage medium
By establishing a global table binding business identity and code unit object relationship when the server-side program framework is initialized, the cross-operating system applicability problem is solved, and a business response method without configuration files and function signature modification is realized, which improves system applicability and stability.
Patent Information
- Application Number
- CN202510896652.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-07-01
- Publication Date
- 2025-08-01
- Estimated Expiration
- 2045-07-01
AI Technical Summary
In the prior art, the business module is loaded with preset functions between the separate program framework and the business module, making it difficult to apply to different operating systems, with poor applicability, and requires setting configuration files and modifying function signatures, which increases the difficulty of writing and maintenance.
When the program framework on the server is initialized, the business identity of the business module, the memory address of the code unit object and the corresponding relationship between the event callback function is established. Through global table binding, the corresponding code unit object is called in response to business requests to avoid relying on operating system functions to load the business module.
It improves the applicability of business response under different operating systems, reduces the difficulty of writing and maintaining server applications, enhances scalability and stability, and avoids address cast type conversion and non-secure operations.
Smart Images

Figure CN120407043A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of data processing, and in particular, to a service response method, device, electronic device, and storage medium. Background Art
[0002] The service platform integrates a server, and the server, as an application program, can provide various services to users. Currently, the server generally includes a program framework and service modules. Each service module can provide a service externally, and the service modules work under the program framework. With such a structure of the server where the program framework and service modules are separated, it is possible to decouple the program framework and each service module, thereby reducing the direct dependence of the service modules on the program framework and reducing the mutual influence between the technical iteration of the program framework and the changes of the service modules.
[0003] The separated program framework and service modules provide services externally through function calls. The program framework usually depends on preset functions to load service modules, and then the program framework and service modules can perform function calls to provide services externally. In the prior art, for different operating systems, the program framework needs to load service modules through different functions. For example, for the Linux (a computer operating system) system, the program framework needs to load service modules through dlopen (a function in a programming language) or dlsym (a function in a programming language), and for the Windows (a computer operating system) system, the program framework needs to load service modules through LoadLibrary (a function in a programming language) or FreeLibrary (a function in a programming language). The separated program framework and service modules depend on preset functions to load service modules, and such a way of providing services externally is difficult to be applicable to different operating systems and has poor applicability. Summary of the Invention
[0004] The purpose of the embodiments of the present invention is to provide a service response method, device, electronic device, and storage medium to improve the applicability of the way of providing services externally under different operating systems. The specific technical solutions are as follows:
[0005] According to one aspect of the embodiments of the present invention, a service response method is provided, and the method includes:
[0006] In response to the initialization of the application startup on the server side, the object of the first code unit is loaded into the heap memory, and the corresponding relationships among the business identifiers of each business module, the memory address of the object of the first code unit of the business module, and the event callback function are established to obtain a global table containing the established corresponding relationships. Among them, the application includes a program framework and business modules. The first code unit is a code unit constructed in each business module that inherits from the second code unit. The second code unit is a code unit constructed in the program framework. The event callback function is a callback function registered in the second code unit for calling the business callback functions in each business module.
[0007] Before the main function of the application is executed, traverse the global table to bind the business identifier, the memory address of the object of the first code unit, and the event callback function of the same business module.
[0008] In response to receiving a business request, if the identifier of the business requested by the business request is the same as the first business identifier among each business identifier, then by calling the event callback function corresponding to the first business identifier, call the object of the first code unit corresponding to the first business identifier.
[0009] In one embodiment of the present invention, the first code unit is constructed in the following manner:
[0010] Based on an initialization expression including an integer static constant, construct the first code unit under a preset path.
[0011] In one embodiment of the present invention, the step of calling the object of the first code unit corresponding to the first business identifier by calling the event callback function corresponding to the first business identifier includes:
[0012] According to the memory address of the object of the first code unit corresponding to the first business identifier, copy the object of the first code unit corresponding to the first business identifier to create a copy object.
[0013] Use the business request as a member variable and call the copy object.
[0014] In one embodiment of the present invention, the step of copying the object of the first code unit corresponding to the first business identifier according to the memory address of the object of the first code unit corresponding to the first business identifier to create a copy object includes:
[0015] Using the second code unit as a base class, construct an intermediate inheritance code unit that inherits from the second code unit in the framework.
[0016] In the business module, establish an indirect inheritance relationship between the first code unit and the second code unit through the intermediate inheritance code unit.
[0017] Create a copy object under the intermediate inheritance code unit based on the indirect inheritance relationship between the first code unit and the second code unit and the memory address of the object of the code unit corresponding to the first service identifier.
[0018] In one embodiment of the present invention, establishing the correspondence relationship between the service identifier of each service module, the memory address of the object of the first code unit of the service module, and the event callback function includes:
[0019] Generate an initial table;
[0020] Bind the string of the service identifier of the service module, the event callback function, and the callback parameter. Using the memory address of the object of the first code unit of the service module as the callback parameter, establish the correspondence relationship between the service identifier of the service code, the memory address of the object of the first code unit of the service code, and the event callback function in the initial table, where the callback parameter is the memory address of the object of the first code unit of the service module.
[0021] According to another aspect of the embodiments of the present invention, there is provided a service response device, the device includes:
[0022] A table construction module, for responding to the initialization of the application program startup of the server, loading the object of the first code unit into the heap memory, establishing the correspondence relationship between the service identifier of each service module, the memory address of the object of the first code unit of the service module, and the event callback function, and obtaining a global table containing the established correspondence relationship, where the application program includes a program framework and service modules, the first code unit is: a code unit constructed in each service module that inherits the second code unit, and the second code unit is: a code unit constructed in the program framework, and the event callback function is: a callback function registered in the second code unit for calling the service callback functions in each service module;
[0023] A binding module, for traversing the global table before the main function of the application program is executed, and binding the service identifier, the memory address of the object of the first code unit, and the event callback function of the same service module;
[0024] A service response module, for responding to receiving a service request, if the identifier of the service requested by the service request is the same as the first service identifier among each service identifier, then call the object of the first code unit corresponding to the first service identifier by calling the event callback function corresponding to the first service identifier.
[0025] In one embodiment of the present invention, the first code unit is constructed in the following manner:
[0026] Construct the first code unit under a preset path based on an initialization expression including an integer static constant.
[0027] In one embodiment of the present invention, the service response module is specifically configured to copy an object of the first code unit corresponding to the first service identifier according to the memory address of the object of the code unit corresponding to the first service identifier, and create a copy object; use the service request as a member variable and call the copy object.
[0028] In one embodiment of the present invention, the service response module is specifically configured to use the second code unit as a base class to construct an intermediate inheritance code unit that inherits the second code unit in a framework; in a service module, establish an indirect inheritance relationship between the first code unit and the second code unit through the intermediate inheritance code unit; create a copy object under the intermediate inheritance code unit based on the indirect inheritance relationship between the first code unit and the second code unit and the memory address of the object of the code unit corresponding to the first service identifier.
[0029] In one embodiment of the present invention, the table construction module is specifically configured to generate an initial table; bind a string of the service identifier of the service module, an event callback function, and a callback parameter, and use the memory address of the object of the first code unit of the service module as the callback parameter to establish a correspondence between the service identifier of the service code, the memory address of the object of the first code unit of the service code, and the event callback function in the initial table, where the callback parameter is the memory address of the object of the first code unit of the service module.
[0030] According to still another aspect of the embodiments of the present invention, an electronic device is provided, including a processor, a communication interface, a memory, and a communication bus. Among them, the processor, the communication interface, and the memory complete communication with each other through the communication bus;
[0031] The memory is used to store a computer program;
[0032] The processor is configured to implement the service response method described in any one of the above when executing the program stored on the memory.
[0033] According to still another aspect of the embodiments of the present invention, a computer-readable storage medium is provided. The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the service response method described in any one of the above is implemented.
[0034] According to still another aspect of the embodiments of the present invention, a computer program product containing instructions is provided. When it runs on a computer, it causes the computer to execute the service response method described in any one of the above.
[0035] Advantageous effects of the embodiments of the present invention:
[0036] As can be seen from the above, in the service response method provided by the embodiments of the present invention, when the program framework of the server starts and initializes, the object of the first code unit is loaded into the heap memory, and then a global table is constructed that includes the correspondence between the business identifiers of each business module, the memory address of the object of the first code unit of the business module, and the event callback functions. Then, when the program framework of the server starts and executes, the business identifier, the memory address of the object of the first code unit, and the event callback function of the same business module are bound. In this way, when responding to a business request, when the identifier of the business requested by the business request is the same as the first business identifier among each business identifier, according to the above binding relationship, the event callback function bound to the first business identifier can be called, and based on the object of the first code unit corresponding to the first business identifier in the memory, the object of the first code unit corresponding to the first business identifier can be called. In the above process, it is not necessary to rely on functions under the operating system to load business modules. Such a way of providing services externally can be applicable to different operating systems, improving the applicability of service response under different operating systems.
[0037] Of course, it is not necessary for any product or method implementing the present invention to achieve all of the above advantages simultaneously. BRIEF DESCRIPTION OF THE DRAWINGS
[0038] In order to more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the following will briefly introduce the drawings required for use in the description of the embodiments or the prior art. Obviously, the following drawings are only some embodiments of the present invention, and those of ordinary skill in the art can also obtain other embodiments based on these drawings.
[0039] Figure 1 It is a schematic flowchart of a service response method provided by an embodiment of the present invention;
[0040] Figure 2 It is a schematic flowchart of a correspondence establishment method provided by an embodiment of the present invention;
[0041] Figure 3 It is a schematic structural diagram of a service response device provided by an embodiment of the present invention;
[0042] Figure 4 It is a schematic structural diagram of an electronic device provided by an embodiment of the present invention. DETAILED DESCRIPTION OF THE EMBODIMENTS
[0043] Next, the technical solutions in the embodiments of the present invention will be clearly and completely described in conjunction with the accompanying drawings in the embodiments of the present invention. Obviously, the described embodiments are only a part of the embodiments of the present invention, rather than all the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those of ordinary skill in the art based on the present invention belong to the scope of protection of the present invention.
[0044] The following describes the prior art.
[0045] In the prior art, for different operating systems, the program framework needs to load business modules through different functions. For example, for the Linux (a computer operating system) system, the program framework needs to load business modules through dlopen (a function in a programming language) or dlsym (a function in a programming language), and for the Windows (a computer operating system) system, the program framework needs to load business modules through LoadLibrary (a function in a programming language) or FreeLibrary (a function in a programming language). For example, see the following example of a program framework loading business modules through functions:
[0046] void handle = dlopen(“filename.so”, RTLD_NOW); ...
[0048] BusinessHandler handler = dlsym(handle, “functionname”); ...
[0050] handler->(para1, para2,...);
[0051] As can be seen from the above example, the dlopen function needs to use the file name “filename.so” of the business module, and the dlsym function needs to call the function name “functionname”. In this case, a configuration file needs to be set up to record the file names of each business module and the function names of the functions in the business module. Obviously, setting up the configuration file is rather cumbersome, and when changing the business module, the set configuration file also needs to be updated.
[0052] Moreover, the above-mentioned way of the program framework loading business modules through functions is a pure procedure-oriented way. The program framework and the business module can only communicate by passing parameters. If it is necessary to increase the parameters passed between the program framework and a business module, the signature of the function needs to be modified, and the modification of the function signature will affect other business modules.
[0053] In summary, the separated program framework and business modules rely on preset functions to load the business modules. In this way, the method of providing services externally is not only difficult to apply to different operating systems with poor applicability, but also requires setting and updating corresponding configuration files, resulting in greater difficulty in writing and maintaining server-side application programs. Moreover, it is difficult to increase the parameters passed between the program framework and business modules, and the scalability is poor.
[0054] Next, the execution entity of the embodiment of the present invention will be described.
[0055] The solution provided by the embodiment of the present invention can be applied to electronic devices such as desktop computers, laptop computers, tablet computers, and servers, and can also be applied to the server that responds to the service requests sent by the client. For the convenience of description, the execution entity of the service response method provided by the embodiment of the present invention is collectively referred to as the server.
[0056] Next, the service response method provided by the embodiment of the present invention will be described in detail.
[0057] In one embodiment of the present invention, referring to Figure 1 the flowchart of a service response method provided, the above method includes the following steps S101-S103.
[0058] Step S101: In response to the startup initialization of the application program on the server, load the object of the first code unit into the heap memory, and establish the corresponding relationships among the business identifiers of each business module, the memory address of the object of the first code unit of the business module, and the event callback function, so as to obtain a global table containing the established corresponding relationships.
[0059] Among them, the application program on the server includes a program framework and business modules. The program framework is the core program of the server, and the program framework contains the global data structures of the core program. The number of business modules included in the application program can be multiple, and each business module corresponds to the service provided by the server. The business module is a set of executable codes set for the service provided by the server. For example, the business module can be a dynamic library corresponding to the business.
[0060] The event callback function is: a callback function registered in the second code unit for calling the business callback functions in each business module. When receiving a service request sent by the client, the server can implement the preset service by calling the business callback function, and the business callback function is used to implement the preset service.
[0061] The object of the first code unit can include member variables and can also include member functions. Among them, the member functions can include business callback functions.
[0062] The first code unit is: a code unit constructed in each business module that inherits from the second code unit, and the second code unit is: a code unit constructed in the program framework.
[0063] For example, the server can construct the second code unit based on the following code:
[0064] class HttpHandler
[0065] {
[0066] virtual void handler() = 0;
[0067] };
[0068] The server can construct the first code unit based on the following code:
[0069] class EchoRequestHandler : public HttpHandler
[0070] {
[0071] void handler() override { ...};
[0072] };
[0073] In this way, the procedural processing functions of the business module can be converted into class member functions, and such class member functions can use the data members of the base class in the program framework.
[0074] Next, the method of loading the object of the first code unit into the heap memory will be described.
[0075] In one implementation, a preset compiler can be used to set the object of the first code unit as a pre-constructed object based on the preset construction attributes. The server can determine the object of the first code unit that belongs to the pre-constructed object through the preset construction attributes, and load the object of the first code unit determined to be the pre-constructed object into the heap memory.
[0076] In another implementation, the first code unit is constructed in the following way: based on an initialization expression including an integer static constant, the first code unit is constructed under a preset path.
[0077] Among them, the integer static constant can be any integer constant, and the initialization expression can be an integer static constant declaration expression for the first code unit. In this way, when the program framework on the server side starts initialization, the server preferentially constructs the first code unit that declares the integer static constant and loads the object of the first code unit into the heap memory. This implementation method can be applied to programs written in a preset programming language. For example, it can be applied to programs written in C (a programming language) or C++ (a programming language).
[0078] For example, the first code unit inheriting from the second code unit can be constructed in each business module based on the following code:
[0079] class EchoRequestHandler : public HttpHandler ...
[0081] static const int usless = (new EchoRequestHandler(" / echo") , 0);
[0082] Among them, static const int usless = (new EchoRequestHandler(" / echo") , 0); is the initialization expression including the integer static constant, and 0 is the integer static constant.
[0083] It can be seen that the server can determine whether to construct a code unit when the program framework on the server side starts initialization according to whether the code unit declares an integer static constant, which can make the object of the first code unit be preferentially loaded into the heap memory, ensure the accuracy of constructing the global table, and then be able to accurately call the object of the first code unit. Moreover, it can improve the applicability of the way of providing services externally without relying on a preset compiler. In this way, using the initialization expression including the integer static constant can make the construction of the first code unit have a high-priority execution opportunity. And the first code unit inherits from the second code unit. Such an inheritance relationship can trigger the execution of the base class constructor in the program framework, and then realize the construction of the first code unit, and then load the object of the first code unit into the heap memory.
[0084] In addition, using this method, the first code unit is constructed through a high-priority execution opportunity, and then the object of the first code unit is loaded into the heap memory, which can avoid the re-initialization of the filled data structure caused by not specifying the initialization order of global or static objects in different code units and avoid the situation where the object content is empty.
[0085] The method of establishing the corresponding relationship and obtaining the global table in step S101 will be described in the following embodiments and will not be elaborated here for the time being.
[0086] Step S102: Before the main function of the application program is executed, traverse the global table to bind the service identifier of the same business module, the memory address of the object of the first code unit, and the preset event callback function.
[0087] Among them, the global table can be a hash table. The pre-stage of the main function is the initialization stage before the main function of the application program is executed.
[0088] In one implementation, the server can use a preset loop structure for traversing the global table to traverse the global table, and use the event callback function setting function to bind the service identifier of the same business module, the memory address of the object of the first code unit, and the event callback function. Among them, after the object of the first code unit is loaded into the heap memory, through the inheritance relationship of the first code inheriting the second code unit, the base class constructor in the program framework can be triggered when the first code unit is constructed, and then the memory address of the object of the first code unit can be registered into the global data structure of the program framework.
[0089] For example, the server can traverse the global table based on the following code: for (hent = HT_START(htp_handlers, &g_htp_handlers); hent; ). The server can perform the binding based on the following code: evhttp_set_cb(ta._http_base, ent->_uri, ent->_cb, ent->_cbarg);.
[0090] Step S103: In response to receiving a service request, if the identifier of the service requested by the service request is the same as the first service identifier among each service identifier, then by calling the event callback function corresponding to the first service identifier, call the object of the first code unit corresponding to the first service identifier.
[0091] Among them, the service request may carry the identifier of the service requested by the service request.
[0092] The server can traverse and determine each service identifier in the global table according to the identifier of the service requested above, and determine the first service identifier among each service identifier that is the same as the identifier of the service requested above.
[0093] In one implementation, the server can directly call the object of the first code unit corresponding to the first service identifier by calling the event callback function. Among them, the event callback function can be: calling the object of the first code unit corresponding to the first service identifier according to the memory address of the object of the first code unit of the service module.
[0094] In another implementation, the server can copy the object of the first code unit corresponding to the first service identifier according to the memory address of the object of a code unit corresponding to the first service identifier by calling an event callback function, and create a copy object; use the service request as a member variable and call the copy object. It can be seen that for each service request, the server can copy the object of the first code unit, create a copy object corresponding to the service request, and realize the response to the service request by calling the copy object. In this way, the mutual influence between different requests can be reduced.
[0095] In addition, when it is necessary to call, copy, etc. the object of the first code unit in memory, the object in memory cannot be directly operated on. When the application program is in the pre-main function stage, the server can encapsulate the member function of the object of the first code unit into a function object, and then use the function object as the template parameter of a preset template code unit. The preset template code unit declares a static function, and the static function and the function object are of the same function type. When it is necessary to operate on the object of the first code unit in memory, the server can call the above function object in the static function of the above template code unit. In this way, the function object carries the memory address of the object of the first code unit corresponding to the function object, and the operation on the object of the first code unit in memory can be realized based on the above memory address. For example, an object can be copied according to the memory address of the object of the first code unit carried in the function object through a copy constructor of the C++ type.
[0096] For example, the server can call the copy object based on the following code:
[0097] virtual void handler() = 0;
[0098] virtual HttpHandler clone() const = 0;
[0099] static void process(struct evhttp_request req, void arg) {
[0100] HttpHandler hdler = ((HttpHandler )arg)->clone();
[0101] _req = req; ...
[0103] hdler->handler();
[0104] }
[0105] HttpRequest _req;
[0106] Furthermore, the server can copy the object according to the following steps A - C.
[0107] Step A: Using the second code unit as the base class, construct an intermediate inheritance code unit in the framework that inherits from the second code unit.
[0108] Step B: In the business module, establish an indirect inheritance relationship between the first code unit and the second code unit through the intermediate inheritance code unit.
[0109] Step C: Based on the indirect inheritance relationship between the first code unit and the second code unit and the memory address of the object of the code unit corresponding to the first service identifier, create a copy object under the intermediate inheritance code unit.
[0110] In one implementation, the server can construct an intermediate inheritance code unit that inherits from the second code unit in the framework based on the CRTP (Curiously Recurring Template Pattern) pattern, establish the above - mentioned indirect inheritance relationship, and then create a copy object under the intermediate inheritance code unit. By establishing an indirect inheritance relationship between the first code unit and the second code unit through the intermediate inheritance code unit and creating a copy object under the intermediate inheritance code unit, the created copy object can inherit the request members of the second code unit, which is convenient for parameter processing and improves the accuracy of business response. Moreover, the intermediate inheritance code unit constructed in the business module can inherit various characteristics of the second code unit, which is convenient for maintaining and extending the business module and improves the maintainability and scalability of the business module.
[0111] For example, the server can construct an intermediate inheritance code unit based on the code in the following program framework:
[0112] template<typename Base, typename Derived>
[0113] class HttpHandlerCloneable : public HttpHandler {
[0114] using Base::Base;
[0115] virtual Base clone() const
[0116] {
[0117] return new Derived(static_cast<Derived const &>( this));
[0118] }
[0119] };
[0120] The server can establish an indirect inheritance relationship based on the following code:
[0121] class EchoRequestHandler : public HttpHandlerCloneable<HttpHandler,EchoRequestHandler> ...
[0123] static const int usless = (new EchoRequestHandler(" / echo") , 0);
[0124] In this way, when copying the object of the first code unit corresponding to the first service identifier, it is possible to copy the object of the first code unit corresponding to the first service identifier.
[0125] As can be seen from the above, in the service response method provided by the embodiments of the present invention, when the program framework of the server is started and initialized, the object of the first code unit is loaded into the heap memory, and then a global table including the correspondence between the service identifiers of each service module, the memory address of the object of the first code unit of the service module, and the preset event callback function is constructed. Then, when the program framework of the server is started and executed, the service identifier of the same service module, the memory address of the object of the first code unit, and the preset event callback function are bound. In this way, when responding to a service request, when the identifier of the service requested by the service request is the same as the first service identifier among each service identifier, the event callback function bound to the first service identifier can be called according to the above binding relationship, and based on the object of the first code unit corresponding to the first service identifier in the memory, the object of the first code unit corresponding to the first service identifier can be called. In the above process, it is not necessary to rely on functions under the operating system to load service modules. Such a way of providing services externally can be applicable to different operating systems and improve the applicability under different operating systems.
[0126] Moreover, the service response method provided by the embodiments of the present invention can be applied to different operating systems, so that developers do not need to write cross-system code. The service response method provided by the embodiments of the present invention belongs to passive registration, does not require the file name, function name, etc. of the service module, does not require setting an additional configuration file, and does not require modifying the function signature. Such a service response method calls the object of the service module in an object-oriented manner, avoiding the situation where the program framework and the service module can only communicate by passing parameters, facilitating the update, expansion, etc. of the service module. In this way, the difficulty of writing and maintaining the server application program can be reduced.
[0127] In addition, the service response method provided by the embodiments of the present invention is based on the object of the first code unit corresponding to the first service identifier in the memory, and realizes the call of the object of the first code unit corresponding to the first service identifier, which can avoid using non-safe means such as address forced type conversion and memory operation, and avoid the re-initialization of the filled data structure caused by the initialization order of global or static variables, improving the stability of service response.
[0128] The following describes the method of establishing the corresponding relationship in step S101 to obtain the global table.
[0129] In one embodiment of the present invention, refer to Figure 2 The flowchart of a method for establishing a corresponding relationship is provided. The above method includes steps S201 - S202.
[0130] Step S201: Generate an initial table.
[0131] In one implementation, the server can use a preset hash table structure macro definition to generate an initial table.
[0132] For example, the preset hash table structure macro definition can be set based on the following code:
[0133] HT_HEAD(htp_handlers, evhtpcb_t) g_htp_handlers = HT_INITIALIZER();
[0134] Step S202: Bind the string of the service identifier of the service module, the preset event callback function, and the callback parameter. Using the memory address of the object of the first code unit of the service module as the callback parameter, establish the corresponding relationship among the service identifier of the service code, the memory address of the object of the first code unit of the service code, and the preset event callback function in the initial table.
[0135] Among them, the callback parameter is the memory address of the object of the first code unit of the service module
[0136] In one implementation, the server can use a preset registration function declaration function to bind the string of the service identifier of the service module, the preset event callback function, and the callback parameter. Then, use the preset insertion operation function to insert the correspondence between the service identifier of the service code, the memory address of the object of the first code unit of the service code, and the preset event callback function into the initial table.
[0137] For example, the preset registration function declaration function can be set based on the following code:
[0138] void reg(const std::string uri, void ( cb)(struct evhttp_request ,void ), void cbarg) { ...
[0140] hent->_uri = strdup(uri.data());
[0141] hent->_cb = cb;
[0142] hent->_cbarg = cbarg;
[0143] The preset insertion operation function can be set based on the following code:
[0144] HT_INSERT(htp_handlers, &g_htp_handlers, hent);
[0145] By binding the string of the service identifier, the preset event callback function, and the memory address of the object of the first code unit of the service module as the callback parameter, a global table can be constructed based on the object of the first code unit in the memory. In this way, when a service request is received and the identifier of the requested service in the service request hits the service identifier in the global table, the memory address of the object of the first code unit corresponding to the service identifier can be accurately determined, improving the accuracy of service response.
[0146] Corresponding to the above service response method, an embodiment of the present invention further provides a service response device.
[0147] In one embodiment of the present invention, refer to Figure 3 to provide a schematic structural diagram of a service response device. The above device includes:
[0148] The table building module 301 is used to load the object of the first code unit into the heap memory in response to the startup initialization of the application on the server side, establish the correspondence between the service identifiers of each business module, the memory address of the object of the first code unit of the business module, and the event callback function, and obtain a global table containing the established correspondence. Among them, the application includes a program framework and business modules. The first code unit is a code unit constructed in each business module by inheriting the second code unit, and the second code unit is a code unit constructed in the program framework. The event callback function is a callback function registered in the second code unit for calling the business callback functions in each business module.
[0149] The binding module 302 is used to traverse the global table before the main function of the application is executed, and bind the service identifier, the memory address of the object of the first code unit, and the event callback function of the same business module.
[0150] The service response module 303 is used to, in response to receiving a service request, if the identifier of the service requested by the service request is the same as the first service identifier among each service identifier, call the object of the first code unit corresponding to the first service identifier by calling the event callback function corresponding to the first service identifier.
[0151] As can be seen from the above, in the service response method provided by the embodiment of the present invention, when the program framework on the server side starts to initialize, the object of the first code unit is loaded into the heap memory, and then a global table including the correspondence between the service identifiers of each business module, the memory address of the object of the first code unit of the business module, and the preset event callback function is constructed. Then, when the program framework on the server side starts to execute, the service identifier, the memory address of the object of the first code unit, and the preset event callback function of the same business module are bound. In this way, when responding to a service request, when the identifier of the service requested by the service request is the same as the first service identifier among each service identifier, according to the above binding relationship, the event callback function bound to the first service identifier can be called, and based on the object of the first code unit corresponding to the first service identifier in the memory, the object of the first code unit corresponding to the first service identifier can be called. In the above process, it is not necessary to rely on functions under the operating system to load business modules. Such a way of providing services externally can be applicable to different operating systems and improve the applicability under different operating systems.
[0152] Moreover, the service response method provided by the embodiments of the present invention can be applied to different operating systems, so that developers do not need to write cross-system code. The service response method provided by the embodiments of the present invention belongs to passive registration, does not require the file names, function names, etc. of service modules, does not require setting additional configuration files, and does not require modifying the signatures of functions, which is convenient for updating and expanding service modules. In this way, the difficulty of writing and maintaining server-side application programs can be reduced.
[0153] In addition, the service response method provided by the embodiments of the present invention is based on the object of the first code unit corresponding to the first service identifier in memory, and realizes the call of the object of the first code unit corresponding to the first service identifier, which can avoid using non-safe means such as address forced type conversion and memory operation, and avoid the re-initialization of the filled data structure caused by the initialization order of global or static variables, improving the stability of service response.
[0154] In one embodiment of the present invention, the first code unit is constructed in the following manner:
[0155] Based on the initialization expression including integer static constants, the first code unit is constructed under a preset path.
[0156] It can be seen that the server can determine whether to construct the code unit when the program framework of the server starts initialization according to whether the code unit declares integer static constants, which can make the first code unit be preferentially loaded into the heap memory, ensure the accuracy of constructing the global table, and then accurately call the object of the first code unit. Moreover, it can improve the applicability of the way of providing services externally without relying on a preset compiler.
[0157] In one embodiment of the present invention, the service response module is specifically configured to copy the object of the first code unit corresponding to the first service identifier according to the memory address of the object of the first code unit corresponding to the first service identifier, create a copy object; use the service request as a member variable and call the copy object.
[0158] It can be seen that for each service request, the server can copy the object of the first code unit, create a copy object corresponding to the service request, and realize the response to the service request by calling the copy object. In this way, the situation where different requests affect each other can be reduced.
[0159] In one embodiment of the present invention, the service response module is specifically configured to use the second code unit as a base class to construct an intermediate inheritance code unit that inherits the second code unit in the framework; in the service module, an indirect inheritance relationship between the first code unit and the second code unit is established through the intermediate inheritance code unit; based on the indirect inheritance relationship between the first code unit and the second code unit and the memory address of an object of a code unit corresponding to the first service identifier, a copy object is created under the intermediate inheritance code unit.
[0160] In this way, the created copy object can inherit the request members of the second code unit, which is convenient for parameter processing and improves the accuracy of service response. Moreover, the intermediate inheritance code unit constructed in the service module can inherit various characteristics of the second code unit, which is convenient for maintaining and expanding the service module and improves the maintainability and extensibility of the service module.
[0161] In one embodiment of the present invention, the form construction module is specifically configured to generate an initial form; bind the string of the service identifier of the service module, a preset event callback function, and a callback parameter, and use the memory address of an object of the first code unit of the service module as the callback parameter to establish a correspondence relationship among the service identifier of the service code, the memory address of an object of the first code unit of the service code, and the preset event callback function in the initial form, where the callback parameter is the memory address of an object of the first code unit of the service module.
[0162] By binding the string of the service identifier, the preset event callback function, and the memory address of an object of the first code unit of the service module as the callback parameter, a global form can be constructed based on the object of the first code unit in the memory. In this way, when a service request is received and the identifier of the requested service in the service request hits the service identifier in the global form, the memory address of the object of the first code unit corresponding to the service identifier can be accurately determined, improving the accuracy of service response.
[0163] An embodiment of the present invention further provides an electronic device, as Figure 4 shown, including a processor 401, a communication interface 402, a memory 403, and a communication bus 404. Among them, the processor 401, the communication interface 402, and the memory 403 complete communication with each other through the communication bus 404.
[0164] The memory 403 is used to store a computer program;
[0165] When the processor 401 is used to execute the program stored on the memory 403, it implements the service response method described in any one of the above.
[0166] The communication bus mentioned in the above electronic device may be a Peripheral Component Interconnect (PCI) bus, an Extended Industry Standard Architecture (EISA) bus, or the like. The communication bus can be divided into an address bus, a data bus, a control bus, etc. For the sake of convenience in representation, only a thick line is used in the figure, but it does not mean that there is only one bus or one type of bus.
[0167] The communication interface is used for communication between the above electronic device and other devices.
[0168] The memory may include a Random Access Memory (RAM), or may also include a Non-Volatile Memory (NVM), such as at least one disk memory. Optionally, the memory may also be at least one storage device located far from the aforementioned processor.
[0169] The above-mentioned processor may be a general-purpose processor, including a Central Processing Unit (CPU), a Network Processor (NP), etc.; it may also be a Digital Signal Processor (DSP), an Application Specific Integrated Circuit (ASIC), a Field-Programmable Gate Array (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components.
[0170] In another embodiment provided by the present invention, there is also provided a computer-readable storage medium, in which a computer program is stored, and when the computer program is executed by a processor, the steps of any of the above service response methods are implemented.
[0171] In another embodiment provided by the present invention, there is also provided a computer program product containing instructions, which when running on a computer, causes the computer to execute any of the service response methods in the above embodiments.
[0172] In the above embodiments, it can be implemented in whole or in part by software, hardware, firmware, or any combination thereof. When implemented using software, it can be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, the processes or functions described in the embodiments of the present invention are generated in whole or in part. The computer may be a general-purpose computer, a special-purpose computer, a computer network, or other programmable devices. The computer instructions may be stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another. For example, the computer instructions may be transmitted from one website, computer, server, or data center to another website, computer, server, or data center via wired (such as coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (such as infrared, wireless, microwave, etc.) means. The computer-readable storage medium may be any available medium that can be accessed by a computer, or a data storage device such as a server or data center that includes one or more integrated available media. The available medium may be a magnetic medium (such as a floppy disk, hard disk, magnetic tape), an optical medium (such as a DVD), or a semiconductor medium (such as a solid state disk (SSD)).
[0173] It should be noted that, in this document, relational terms such as first and second are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any actual relationship or order between these entities or operations. Moreover, the term "comprising", "including" or any other variation thereof is intended to cover non-exclusive inclusion, such that a process, method, article or device comprising a series of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such process, method, article or device. Without further limitation, an element defined by the statement "comprising a..." does not exclude the presence of additional identical elements in the process, method, article or device comprising the element.
[0174] Each embodiment in this specification is described in a related manner. For the same or similar parts among the embodiments, reference can be made to each other. Each embodiment focuses on the differences from other embodiments. In particular, for the embodiments of the apparatus, electronic device, storage medium, and computer program product, since they are basically similar to the method embodiments, the description is relatively simple, and reference can be made to the partial description of the method embodiments for the relevant parts.
[0175] The above are only the preferred embodiments of the present invention and are not intended to limit the protection scope of the present invention. Any modifications, equivalent replacements, improvements, etc. made within the spirit and principle of the present invention are all included within the protection scope of the present invention.
Claims
1. A service response method, characterized in that, The method includes: In response to the startup initialization of the application on the server side, load the object of the first code unit into the heap memory, establish the correspondence between the service identifiers of each business module, the memory address of the object of the first code unit of the business module, and the event callback function, and obtain a global table containing the established correspondence. Among them, the application includes a program framework and business modules. The first code unit is a code unit constructed in each business module and inheriting from the second code unit. The second code unit is a code unit constructed in the program framework. The event callback function is a callback function registered in the second code unit for calling the business callback functions in each business module; Before the main function of the application is executed, traverse the global table and bind the service identifier of the same business module, the memory address of the object of the first code unit, and the event callback function; In response to receiving a service request, if the identifier of the service requested by the service request is the same as the first service identifier among each service identifier, then call the object of the first code unit corresponding to the first service identifier by calling the event callback function corresponding to the first service identifier.
2. The method according to claim 1, wherein The first code unit is constructed in the following manner: Based on an initialization expression including an integer static constant, construct the first code unit at a preset path.
3. The method according to claim 1, characterized in that, The step of calling the object of the first code unit corresponding to the first service identifier by calling the event callback function corresponding to the first service identifier includes: According to the memory address of the object of the first code unit corresponding to the first service identifier, copy the object of the first code unit corresponding to the first service identifier to create a copy object; Use the service request as a member variable and call the copy object.
4. The method according to claim 3, wherein The step of copying the object of the first code unit corresponding to the first service identifier according to the memory address of the object of the first code unit corresponding to the first service identifier to create a copy object includes: Using the second code unit as the base class, construct an intermediate inheritance code unit that inherits from the second code unit in the framework; In the business module, establish an indirect inheritance relationship between the first code unit and the second code unit through the intermediate inheritance code unit; Based on the indirect inheritance relationship between the first code unit and the second code unit and the memory address of the object of the first code unit corresponding to the first service identifier, create a copy object under the intermediate inheritance code unit.
5. The method according to any one of claims 1 to 4, characterized in that, The step of establishing the correspondence between the service identifiers of each business module, the memory address of the object of the first code unit of the business module, and the event callback function includes: Generate an initial table; Bind the string of the service identifier of the business module, the event callback function, and the callback parameter. Using the memory address of the object of the first code unit of the business module as the callback parameter, establish the correspondence between the service identifier of this business code, the memory address of the object of the first code unit of this business code, and the event callback function in the initial table. The callback parameter is the memory address of the object of the first code unit of the business module.
6. A service response device, characterized in that, The device includes: A table construction module, which, in response to the initialization of the application startup of the server, loads the objects of the first code units into the heap memory, establishes the correspondence between the business identifiers of each business module, the memory addresses of the objects of the first code units of the business modules, and the event callback functions, and obtains a global table containing the established correspondence. Wherein, the application includes a program framework and business modules, the first code unit is: a code unit constructed in each business module and inheriting from the second code unit, and the second code unit is: a code unit constructed in the program framework, and the event callback function is: a callback function registered in the second code unit for calling the business callback functions in each business module; A binding module, which, before the execution of the main function of the application, traverses the global table and binds the business identifiers, the memory addresses of the objects of the first code units, and the event callback functions of the same business module; A business response module, which, in response to receiving a business request, if the identifier of the business requested by the business request is the same as the first business identifier among each business identifier, calls the object of the first code unit corresponding to the first business identifier by calling the event callback function corresponding to the first business identifier.
7. The device according to claim 6, characterized in that The first code unit is constructed in the following manner: Based on an initialization expression including integer static constants, the first code unit is constructed under a preset path.
8. The device according to claim 6, wherein The business response module is specifically configured to copy the object of the first code unit corresponding to the first business identifier according to the memory address of the object of the first code unit corresponding to the first business identifier, create a copy object; use the business request as a member variable and call the copy object; and / or The business response module is specifically configured to use the second code unit as a base class to construct an intermediate inheritance code unit inheriting from the second code unit in the framework; in the business module, establish an indirect inheritance relationship between the first code unit and the second code unit through the intermediate inheritance code unit; based on the indirect inheritance relationship between the first code unit and the second code unit and the memory address of the object of the first code unit corresponding to the first business identifier, create a copy object under the intermediate inheritance code unit; and / or The table construction module is specifically configured to generate an initial table; bind the string of the business identifier of the business module, a preset event callback function, and a callback parameter, and use the memory address of the object of the first code unit of the business module as the callback parameter to establish the correspondence between the business identifier of the business code, the memory address of the object of the first code unit of the business code, and the preset event callback function in the initial table, where the callback parameter is the memory address of the object of the first code unit of the business module.
9. An electronic device, characterized in that, It includes a processor, a communication interface, a memory, and a communication bus. Among them, the processor, the communication interface, and the memory complete communication with each other through the communication bus; The memory is used to store a computer program; A processor, when executing a program stored in a memory, implements the method according to any one of claims 1-5.
10. A computer-readable storage medium, characterized in that, A computer program is stored in the computer-readable storage medium, and when the computer program is executed by a processor, the method according to any one of claims 1-5 is implemented.
Citation Information
Patent Citations
Service event processing system, service event processing method, service event processing device and equipment
CN112579156A
Global transaction processing method and system
CN113849278A
Information recording method and device, equipment, storage medium and program product
CN115629986A
Player callback event processing method and device, computer equipment and storage medium
CN116136789A
System for sharing data objects among applications
US20090254601A1
Cited By
Asynchronous IO calling method and device for service codes, storage medium and computer equipment
CN122132127A