Method, device, terminal device and storage medium for synchronously returning request results
By establishing a communication connection with the client on the server and encapsulating the request data, detecting the return data, the problem of bidirectional communication and data logical association in the existing communication protocol is solved, and the synchronous return of request results and data logical association are realized.
Patent Information
- Application Number
- CN202211698041.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-12-28
- Publication Date
- 2025-08-22
- Estimated Expiration
- 2042-12-28
AI Technical Summary
The existing Tcp-based communication protocol cannot realize bidirectional communication and data logical association in the web application layer, resulting in the server being unable to actively obtain client information and the data transmission and reception cannot be logical association.
The communication connection with the client is established through the server, and the request instructions and message identification are encapsulated by the sending logic module as requested data, and the return data is received within a preset time to achieve synchronous return of the request result.
It realizes the logical association of sending and receiving data in full-duplex communication, ensures the synchronous return of request results, and improves the flexibility and efficiency of communication protocols.
Smart Images

Figure CN115967709B_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the field of communication technology, and in particular relates to a method, an apparatus, a terminal device and a computer-readable storage medium for synchronously returning request results. Background Art
[0002] With the rapid development of science and technology, application layer communication protocols have become a vital part of the field of communication technology.
[0003] Currently, in web application layer communication scenarios, there are two existing technical solutions for common and easy-to-develop protocols based on TCP (Transmission Control Protocol). The first category is based on short-link protocols, such as Http (Hypertext Transfer Protocol). Typically, an HTTP client initiates a request, establishing a TCP connection to a specified port on the server. The HTTP server then listens for client requests on that port and, upon receiving a request, returns a status to the client. The second category is based on long-link protocols, such as WebSocket. The WebSocket protocol supports interaction between web browsers or other client applications and web servers, has low overhead, and facilitates real-time data transmission between clients and servers. However, the first category of existing technical solutions has the disadvantage that the server cannot proactively request client information from the client unless the client proactively reports it. The second category of existing technical solutions also has the disadvantage of not being able to logically associate sent and received data.
[0004] In summary, the existing TCP-based communication protocol has the problem of being unable to simultaneously achieve two-way communication and logically associate the sent and received data. Summary of the Invention
[0005] The main purpose of the present invention is to provide a method, apparatus, terminal device, and computer-readable storage medium for synchronously returning request results. The method aims to utilize the convenience of full-duplex communication to establish a logical association between sent and received data, thereby achieving synchronous return of request results.
[0006] In order to achieve the above object, the present invention provides a request result synchronous return method, which is applied to a server, and the server includes a sending logic module;
[0007] The request result synchronous return method includes:
[0008] Establishing a communication connection with the client, and encapsulating the request instruction and the message identifier generated according to the request instruction into request data through the sending logic module;
[0009] Sending the request data to the client through the sending logic module, so that the client encapsulates the return message corresponding to the request instruction and the message identifier into return data;
[0010] Detecting whether the return data sent by the client is received within a preset time;
[0011] When it is detected that the return data is received within the preset time, the return message is sent to the caller that initiated the request instruction through the sending logic module.
[0012] Optionally, after the step of establishing a communication connection with the client, the method further includes:
[0013] Generate a link handle corresponding to the client, and encapsulate the link handle and the hash table into a link encapsulation;
[0014] A data reading process is started based on the link package.
[0015] Optionally, after the step of encapsulating the request instruction and the message identifier generated according to the request instruction into request data by the sending logic module, the method further includes:
[0016] The request data is written into the data association body in the hash table through the sending logic module.
[0017] Optionally, the step of sending the request data to the client through the sending logic module includes:
[0018] Converting the request data into a preset data format through the sending logic module to obtain target request data;
[0019] The target request data is sent to the client based on the connection handle by the sending logic module.
[0020] Optionally, after the step of detecting whether the return data sent by the client is received within a preset time, the method further includes:
[0021] When it is detected that the return data is not received within the preset time, an error signal is sent to the caller who initiated the request instruction through the sending logic module.
[0022] Optionally, before the step of sending the return message to the caller that initiated the request instruction through the sending logic module, the method further includes:
[0023] Parsing the returned data through the data reading process to obtain the returned message and the message identifier, and determining the target data association in the hash table according to the message identifier;
[0024] The returned data is written into the target data association body through the data reading process, and an end signal is sent to the sending logic module.
[0025] Optionally, the step of sending the return message to the caller that initiated the request instruction through the sending logic module includes:
[0026] When the end signal is received, the sending logic module sends the return message to the caller that initiated the request instruction.
[0027] In addition, to achieve the above-mentioned purpose, the present invention further provides a request result synchronous return device, the request result synchronous return device is applied to a server, and the server includes a sending logic module;
[0028] The request result synchronous return device includes:
[0029] A request data module establishes a communication connection with the client and encapsulates a request instruction and a message identifier generated according to the request instruction into request data through the sending logic module;
[0030] a return data module, which sends the request data to the client through the sending logic module, so that the client can encapsulate the return message corresponding to the request instruction and the message identifier into return data;
[0031] A detection module, detecting whether the return data sent by the client is received within a preset time;
[0032] The caller module, when detecting that the return data is received within the preset time, sends the return message to the caller that initiated the request instruction through the sending logic module.
[0033] In addition, to achieve the above-mentioned purpose, the present invention also provides a terminal device, which includes: a memory, a processor, and a request result synchronous return program stored on the memory and runnable on the processor. When the request result synchronous return program of the terminal device is executed by the processor, the steps of the request result synchronous return method described above are implemented.
[0034] In addition, to achieve the above-mentioned purpose, the present invention also provides a computer-readable storage medium, on which a request result synchronous return program is stored. When the request result synchronous return program is executed by the processor, the steps of the request result synchronous return method as described above are implemented.
[0035] An embodiment of the present invention proposes a method, apparatus, terminal device and computer-readable storage medium for synchronously returning request results. The method is applied to a server, and the server includes a sending logic module; the method establishes a communication connection with a client, and encapsulates a request instruction and a message identifier generated according to the request instruction into request data through the sending logic module; sends the request data to the client through the sending logic module, so that the client can encapsulate the return message corresponding to the request instruction and the message identifier into return data; detects whether the return data sent by the client is received within a preset time; when it is detected that the return data is received within the preset time, sends the return message to the caller who initiated the request instruction through the sending logic module.
[0036] The embodiment of the present invention establishes a communication connection with the client through the server, and after successfully establishing the communication connection, the request instruction and the message identifier generated according to the request instruction are encapsulated into request data through the sending logic module of the server, and then the encapsulated request data is sent to the client through the sending logic module, so that the client can encapsulate the return message corresponding to the request instruction in the request data and the above-mentioned message identifier as return data. The server detects whether the return data returned by the client is received within the preset time. When it is detected that the return data is received within the preset time, the return message in the return data is sent to the business caller who initiated the request instruction through the sending logic module. In this way, the present invention uses the convenience of full-duplex communication to establish a logical association between sending and receiving data, thereby realizing the synchronous return of request results. BRIEF DESCRIPTION OF THE DRAWINGS
[0037] Figure 1 Schematic diagram of the device structure of the hardware operating environment of the terminal device involved in the embodiment of the present invention;
[0038] Figure 2 This is a schematic diagram of the steps of the first embodiment of the method for synchronously returning request results of the present invention;
[0039] Figure 3 This is a schematic diagram of the link encapsulation structure involved in an embodiment of the request result synchronous return method of the present invention;
[0040] Figure 4 This is a schematic diagram of the data association structure involved in an embodiment of the request result synchronous return method of the present invention;
[0041] Figure 5 Schematic diagram of a request result synchronous return process involved in an embodiment of a request result synchronous return method of the present invention;
[0042] Figure 6 This is a functional module diagram of an embodiment of a request result synchronous return device of the present invention;
[0043] The purpose, features and advantages of the present invention will be further described with reference to the accompanying drawings and in conjunction with the embodiments. DETAILED DESCRIPTION
[0044] It should be understood that the specific embodiments described herein are only used to explain the present invention and are not intended to limit the present invention.
[0045] Reference Figure 1 , Figure 1 This is a schematic diagram of the device structure of the hardware operating environment of the terminal device involved in the embodiment of the present invention.
[0046] The terminal device of the embodiment of the present invention can be a server-side terminal device used in the field of communication technology. In addition, the server includes a sending logic module. Specifically, the terminal device can be a smartphone, a PC (personal computer), a tablet computer, a portable computer, etc.
[0047] like Figure 1 As shown, the terminal device may include: a processor 1001, such as a CPU, a communication bus 1002, a user interface 1003, a network interface 1004, and a memory 1005. Among them, the communication bus 1002 is used to realize the connection and communication between these components. The user interface 1003 may include a display screen (Display), an input unit such as a keyboard (Keyboard), and the user interface 1003 may also include a standard wired interface and a wireless interface. The network interface 1004 may optionally include a standard wired interface and a wireless interface (such as a Wi-Fi interface). The memory 1005 may be a high-speed RAM memory or a stable memory (non-volatile memory), such as a disk memory. The memory 1005 may optionally be a storage device independent of the aforementioned processor 1001.
[0048] Those skilled in the art will understand that Figure 1 The terminal device structure shown in the figure does not constitute a limitation on the terminal device, and may include more or fewer components than shown in the figure, or combine certain components, or arrange the components differently.
[0049] like Figure 1As shown, the memory 1005 as a computer storage medium may include an operating system, a network communication module, a user interface module, and a request result synchronous return program.
[0050] exist Figure 1 In the terminal shown, the network interface 1004 is mainly used to connect to the backend server and communicate data with the backend server; the user interface 1003 is mainly used to connect to the client and communicate data with the client; and the processor 1001 can be used to call the request result synchronous return program stored in the memory 1005 and perform the following operations:
[0051] Establishing a communication connection with the client, and encapsulating the request instruction and the message identifier generated according to the request instruction into request data through the sending logic module;
[0052] Sending the request data to the client through the sending logic module, so that the client encapsulates the return message corresponding to the request instruction and the message identifier into return data;
[0053] Detecting whether the return data sent by the client is received within a preset time;
[0054] When it is detected that the return data is received within the preset time, the return message is sent to the caller that initiated the request instruction through the sending logic module.
[0055] Optionally, the processor 1001 may also be configured to call a request result synchronous return program stored in the memory 1005, and after the step of establishing a communication connection with the client, further perform the following 5 operations:
[0056] Generate a link handle corresponding to the client, and encapsulate the link handle and the hash table into a link encapsulation;
[0057] A data reading process is started based on the link package.
[0058] Optionally, the processor 1001 may also be configured to call a request result synchronous return program stored in the memory 1005, and after the step of encapsulating the request instruction and the message identifier generated according to the request instruction into request data through the sending logic module, further perform the following operations:
[0059] The request data is written into the data association body in the hash table through the sending logic module.
[0060] Optionally, the processor 1001 may also be configured to call a request result synchronous return program stored in the memory 1005 and perform the following operations:
[0061] 5. Converting the request data into a preset data format through the sending logic module to obtain target request data;
[0062] The target request data is sent to the client based on the connection handle by the sending logic module.
[0063] Optionally, the processor 1001 may also be configured to call a request result synchronous return program stored in the memory 1005, and after the step of detecting whether the return data sent by the client is received within a preset time, further perform the following operations:
[0064] When it is detected that the return data is not received within the preset time, an error signal is sent to the caller who initiated the request instruction through the sending logic module.
[0065] Optionally, the processor 1001 may also be configured to call the request result and step 5 return program stored in the memory 1005, and before the step of sending the return message to the caller that initiated the request instruction through the sending logic module, further perform the following operations:
[0066] Parsing the returned data through the data reading process to obtain the returned message and the message identifier, and determining the target data association in the hash table according to the message identifier;
[0067] The return data is written into the target data association body through the data reading process, and a 0 end signal is sent to the sending logic module.
[0068] Optionally, the processor 1001 may also be configured to call a request result synchronous return program stored in the memory 1005 and perform the following operations:
[0069] When the end signal is received, the sending logic module sends the return message to the caller that initiated the request instruction.
[0070] Based on the above-mentioned terminal device, various embodiments of the request result synchronous return method of the present invention are proposed.
[0071] Currently, in Web application layer communication scenarios, there are two existing technical solutions for common and easy-to-develop TCP-based protocols that need to be mentioned. The first category is based on short-link protocol solutions, such as Http. Typically, an HTTP client initiates a request, creating a TCP connection to a specified port on the server. The HTTP server listens for client requests on that port and returns a status upon receiving the request. The second category is based on long-link protocol solutions, such as WebSocket. The WebSocket protocol supports interaction between web browsers or other client applications and web servers, has low overhead, and facilitates real-time data transmission between clients and servers. However, the disadvantage of the first category of existing technical solutions is that the server cannot actively initiate a request to the client to obtain client information unless the client actively reports it. The disadvantage of the second category of existing technical solutions is that they do not have the ability to logically associate the sent and received data.
[0072] In view of the above phenomenon, the present invention proposes a request result synchronous return method. The request result synchronous return method of the present invention is applied to a server, and the server includes a sending logic module.
[0073] The synchronous return method of the request result of the present invention establishes a communication connection between the server and the client, and after successfully establishing the communication connection, the request instruction and the message identifier generated according to the request instruction are encapsulated into request data through the sending logic module of the server, and then the encapsulated request data is sent to the client through the sending logic module, so that the client can encapsulate the return message corresponding to the request instruction in the request data and the above-mentioned message identifier as return data. The server detects whether the return data returned by the client is received within a preset time. When it is detected that the return data is received within the preset time, the return message in the return data is sent to the business caller who initiated the request instruction through the sending logic module. In this way, the present invention uses the convenience of full-duplex communication to establish a logical association between sending and receiving data, thereby realizing the synchronous return of request results.
[0074] Please refer to Figure 2 , Figure 2 This is a flowchart of a first embodiment of the method for synchronously returning request results of the present invention. It should be noted that although a logical order is shown in the flowchart, in some cases, the method for synchronously returning request results of the present invention may also perform the steps shown or described in a different order than that shown or described herein.
[0075] In a first embodiment of the method for synchronously returning a request result of the present invention, the method for synchronously returning a request result of the present invention includes:
[0076] Step S10: establishing a communication connection with the client, and encapsulating the request instruction and the message identifier generated according to the request instruction into request data through the sending logic module.
[0077] In this embodiment, the server establishes a communication connection with the client, and then the sending logic module of the server encapsulates the request instruction to be sent and the message identifier generated according to the request instruction into request data.
[0078] For example, first, the server monitors connection requests in real time. When the client initiates a ws (Websocket, a two-way communication protocol) connection request to the server, the server receives the ws connection request and establishes a communication connection with the client. Then, the server receives the request instruction initiated by the service caller through the sending logic module of the server. The request instruction is the actual request message, and a globally unique string is generated based on the request instruction as the message unique identifier, i.e., the message identifier. It should be noted that the message type of the request instruction is the data type agreed upon by the communicating parties. For example, the data used in the actual communication is classified into the required type. The required type can be the client's active report, the client's response data to the server, etc. Finally, the sending logic module encapsulates the actual request message, the message unique identifier, and the message type into one as the request data.
[0079] Step S20: sending the request data to the client through the sending logic module, so that the client can encapsulate the return message corresponding to the request instruction and the message identifier into return data;
[0080] In this embodiment, the server sends the encapsulated request data to the client through a sending logic module, so that the client can determine the return message that needs to be returned based on the request instruction in the request data, keep the message identifier unchanged, and encapsulate the return data and the message identifier together as return data.
[0081] Exemplarily, the server sends the request data to the client through the sending logic module. After receiving the request data, the client parses the request data to obtain the actual request message, the message unique identifier and the message type, and then determines the actual return message according to the requirements of the actual request message, and keeps the message unique identifier unchanged. In addition, the message type at this time is the response data of the client to the server. Finally, the client encapsulates the actual return message, the message unique identifier and the message type into one as the return data, and writes it into the link handle corresponding to the server according to the format required by Websocket, and then sends it to the server.
[0082] It's important to note that a connection handle is an associated data structure generated after a successful connection between a server and a client. It serves as the underlying channel for sending Web Services (WS) data. Upon successful connection, the server generates a connection handle corresponding to the client, storing the client's Internet Protocol (IP) and port information. The client also generates a connection handle corresponding to the server, storing the server's IP and port information. The connection handle also provides read and write capabilities.
[0083] Step S30, detecting whether the return data sent by the client is received within a preset time;
[0084] In this embodiment, the server detects whether the return data sent by the client is received within a preset time.
[0085] For example, after the sending logic module of the server writes the request data of the server into the data association body of the hash table, the timing task of the sending logic module starts timing according to the preset default timeout period, wherein the default timeout period is the preset time.
[0086] Step S40: When it is detected that the return data is received within the preset time, the return message is sent to the caller that initiated the request instruction through the sending logic module.
[0087] In this embodiment, when the server detects that it has received the return data sent by the client within a preset time, it sends the return message in the return data to the caller who initiated the request through the sending logic module.
[0088] Furthermore, in a feasible embodiment, after the above step S30, the request result synchronous return method of the present invention may further include:
[0089] Step A10: When it is detected that the return data is not received within the preset time, an error signal is sent to the caller who initiated the request instruction through the sending logic module.
[0090] In this embodiment, when the server detects that the return data returned by the client is not received within a preset time, the server sends an error signal to the caller who initiated the request through the sending logic module.
[0091] Exemplarily, the data association body maintained by the server includes a data structure of error information. When the server detects that the return data is not received within the preset time, the data reading process opened by the server using the link encapsulation body sends an error signal to the sending logic module of the server. The sending logic module determines that the request has timed out and returns the error signal to the caller who initiated the request.
[0092] In this embodiment, the request result synchronous return method of the present invention establishes a communication connection between the server and the client, and then encapsulates the request instruction to be sent and the message identifier generated according to the request instruction into request data through the sending logic module of the server; the server sends the encapsulated request data to the client through the sending logic module, so that the client can determine the return message to be returned according to the request instruction in the request data, and keep the message identifier unchanged, and encapsulate the return data and the message identifier into return data; the server detects whether the return data sent by the client is received within the preset time; when the server detects that the return data sent by the client is received within the preset time, the server sends the return message in the return data to the caller who initiated this request through the sending logic module; when the server detects that the return data returned by the client is not received within the preset time, the server sends an error signal to the caller who initiated this request through the sending logic module.
[0093] In this way, the embodiment of the present invention establishes a communication connection between the server and the client, and after successfully establishing the communication connection, the server's sending logic module encapsulates the request instruction and the message identifier generated according to the request instruction into request data, and then sends the encapsulated request data to the client through the sending logic module, so that the client can encapsulate the return message corresponding to the request instruction in the request data and the above-mentioned message identifier into return data. The server detects whether the return data returned by the client is received within the preset time. When it is detected that the return data is received within the preset time, the return message in the return data is sent to the business caller who initiated the request instruction through the sending logic module. In this way, the present invention uses the convenience of full-duplex communication to establish a logical association between sending and receiving data, thereby realizing the synchronous return of request results.
[0094] Furthermore, based on the first embodiment of the method for synchronously returning a request result of the present invention, a second embodiment of the method for synchronously returning a request result of the present invention is proposed.
[0095] In this embodiment, after the step of "establishing a communication connection with the client" in step S10, the request result synchronous return method of the present invention may further include:
[0096] Step B10: Generate a link handle corresponding to the client, and encapsulate the link handle and the hash table into a link encapsulation;
[0097] In this embodiment, after successfully establishing a communication connection with the client, the server generates a link handle corresponding to the client, and encapsulates the link handle and the hash table into a link encapsulation.
[0098] For example, after establishing a communication connection with a client, the server generates a connection handle corresponding to the client, then encapsulates the connection handle and adds other information. Figure 3 As shown in FIG, a schematic diagram of the link encapsulation structure of the present invention is shown, and the encapsulation information includes but is not limited to the original link handle, the read-write lock of the hash table, abnormal monitoring flags such as link disconnection and error reporting, the default timeout period, and the hash table of maintenance requests.
[0099] Step B20: Start a data reading process based on the link package.
[0100] In this embodiment, after establishing the link package, the server uses the link package to start a data reading process running in the background.
[0101] It should be noted that the ws link handle provides read and write capabilities, but the read and write processes need to be maintained by programmers.
[0102] Furthermore, in a feasible embodiment, after the above step S10, the request result synchronous return method of the present invention may further include:
[0103] Step C10: writing the request data into the data association body in the hash table through the sending logic module.
[0104] In this embodiment, the server writes the encapsulated request data into the data association body of the hash table through the sending logic module.
[0105] For example, Figure 4 The following figure shows a schematic diagram of the data association structure of the present invention. The hash table used to maintain instruction requests in the link encapsulation uses a conventional data structure. The key is a unique string of a certain length, and the value is a message association structure of the request data and the return data. In addition, the value also includes an end semaphore and error information. It should be noted that the request data and the return data in the round-trip data association structure share the same data structure.
[0106] Exemplarily, after the server encapsulates the actual request message, message unique identifier and message type into request data, it writes the encapsulated request data into the data association body of the hash table. At the same time, the sending logic module starts listening to the end semaphore in the data association body and starts the timer according to the default timeout parameter to determine whether the subsequent server receives the return data sent by the client within the preset time.
[0107] Furthermore, in a feasible embodiment, in the above step S20, the step of “sending the request data to the client through the sending logic module” may include:
[0108] Step S201, converting the request data into a preset data format through the sending logic module to obtain target request data;
[0109] Step S202: Send the target request data to the client based on the link handle through the sending logic module.
[0110] In this embodiment, the server converts the encapsulated request data into a preset data format through a sending logic module to obtain the converted target request data, and then sends the target request data to the client based on the link handle corresponding to the client.
[0111] Exemplarily, the server converts the request data into the format required by Websocket through the sending logic module, which can be a string or binary, etc., that is, a preset format, to obtain the converted request data, that is, the target request data, and then sends the target request data to the client based on the link handle corresponding to the client. Then, the link handle write method converts the target request data into a specific IP+port for data transmission.
[0112] Furthermore, in a feasible embodiment, in the above step S40, before the step of "sending the return message to the caller that initiated the request instruction through the sending logic module", the request result synchronous return method of the present invention may further include:
[0113] Step D10, parsing the returned data through the data reading process to obtain the returned message and the message identifier, and determining the target data association in the hash table according to the message identifier;
[0114] In this embodiment, the data reading process generated by the server parses the return data returned by the client to obtain a return message and a message identifier, and then determines the target data association in the hash table according to the message identifier.
[0115] For example, when the server's data reader process parses the returned data and obtains a unique message identifier, it determines the target data association corresponding to the unique message identifier in the hash table based on the unique message identifier. It should be noted that when the same client returns multiple data items simultaneously or initiates a data request, multiple data associations will exist in the hash table, each with a different unique message identifier.
[0116] Step D20 , writing the returned data into the target data association body through the data reading process, and sending an end signal to the sending logic module.
[0117] In this embodiment, the server writes the return data returned by the client into the target data association body through the data reading process, and sends an end signal to the sending logic module.
[0118] For example, after receiving the return data returned by the client, the server-side data reading process writes the return data into the data association body where the request data uniquely identified by the same message is located, that is, the target data association body, and then the data reading process sends an end signal to the end semaphore in the target data association body. Since the sending logic module monitors the end semaphore, the sending logic module can be informed that the request has ended.
[0119] Furthermore, in a feasible embodiment, in the above step S40, the step of “sending the return message to the caller that initiated the request instruction through the sending logic module” may include:
[0120] Step S401: When the sending logic module receives the end signal, the return message is sent to the caller that initiated the request instruction.
[0121] In this embodiment, upon receiving the end signal sent by the data reading process, the server sends the return message returned by the client to the caller that initiated the request through the sending logic module.
[0122] For example, Figure 5As shown in the figure, the request result synchronous return process diagram is as follows: first, the client initiates a ws link request. After receiving the link request, the server successfully establishes a communication connection with the client; the server initializes the data structure in the data association structure to obtain the initialized data association structure; then it encapsulates the data required by the link encapsulation; after the encapsulation is completed, it uses the link encapsulation to start a server-side data reading process; the business caller initiates a control instruction to the sending logic module of the server, and puts the control instruction into the data association structure in the hash table through the sending logic module; starts the timer and ends the semaphore listening; sends the request data to the client through the sending logic module of the server; after the client receives the request data, it The request instruction writes the return data to the write process and returns it to the server; the read process on the server parses the return data, obtains the message unique identifier, searches the target associated structure in the hash table according to the message unique identifier, and writes the return data into the target associated structure; determines the semaphore in the target data associated body according to the message unique identifier, sends an end signal to the semaphore, and sends the actual message in the return data through the semaphore, wherein the semaphore is also a message channel, and the receiver receives the data, which means the signal is received; returns the actual message in the return data to the caller through the sending logic module, wherein the sending logic module is actually the caller's agent, and the agent caller processes the semaphore, which is the actual semaphore receiver and is responsible for returning the data to the caller after receiving the signal.
[0123] In this embodiment, the request result synchronous return method of the present invention is as follows: after successfully establishing a communication connection with a client, the server generates a link handle corresponding to the client and encapsulates the link handle and a hash table into a link encapsulation. After establishing the link encapsulation, the server uses the link encapsulation to start a background data reading process. The server writes the encapsulated request data into a data association of the hash table through a sending logic module. The server converts the encapsulated request data into a preset data format through the sending logic module to obtain converted target request data, and then sends the target request data to the client based on the link handle corresponding to the client. The server-generated data reading process parses the return data returned by the client to obtain a return message and a message identifier, and then determines the target data association in the hash table based on the message identifier. The server writes the return data returned by the client into the target data association through the reading process and sends an end signal to the sending logic module. Upon receiving the end signal sent by the reading process, the server sends the return message returned by the client to the caller that initiated the request through the sending logic module.
[0124] In this way, the present invention uses the convenience of full-duplex communication and customizes the synchronization logic on the application layer based on WebSocket. Specifically, a link encapsulation structure and a data association structure are established, thereby realizing the logical association of round-trip data based on full-duplex communication.
[0125] In addition, an embodiment of the present invention also provides a request result synchronous return device.
[0126] Please refer to Figure 6 , Figure 6 This is a functional module diagram of an embodiment of a request result synchronous return device of the present invention, as shown in FIG. Figure 6 As shown, the request result synchronous return device of the present invention includes:
[0127] The request data module 10 is used to establish a communication connection with the client and encapsulate the request instruction and the message identifier generated according to the request instruction into request data through the sending logic module;
[0128] a return data module 20, configured to send the request data to the client through the sending logic module, so that the client can encapsulate the return message corresponding to the request instruction and the message identifier into return data;
[0129] A detection module 30 is configured to detect whether the return data sent by the client is received within a preset time;
[0130] The caller module 40 is configured to send the return message to the caller that initiated the request instruction through the sending logic module when detecting that the return data is received within the preset time.
[0131] Optionally, the request result synchronous return device of the present invention further includes:
[0132] A link encapsulation module, configured to generate a link handle corresponding to the client, and encapsulate the link handle and a hash table into a link encapsulation;
[0133] The data reading process module is used to start the data reading process based on the link package.
[0134] Optionally, the request result synchronous return device of the present invention further includes:
[0135] The data association body module is configured to write the request data into the data association body in the hash table through the sending logic module.
[0136] Optionally, the data return module 20 includes:
[0137] a target request data unit, configured to convert the request data into a preset data format through the sending logic module to obtain target request data;
[0138] A sending unit is configured to send the target request data to the client based on the link handle through the sending logic module.
[0139] Optionally, the request result synchronous return device of the present invention further includes:
[0140] The timeout module is used to send an error signal to the caller who initiated the request instruction through the sending logic module when it detects that the return data is not received within the preset time.
[0141] Optionally, the request result synchronous return device of the present invention further includes:
[0142] a target data association module, configured to parse the returned data through the data reading process to obtain the returned message and the message identifier, and determine the target data association in the hash table according to the message identifier;
[0143] An end signal module is used to write the returned data into the target data association body through the data reading process and send an end signal to the sending logic module.
[0144] Optionally, the caller module 40 includes:
[0145] The caller unit is configured to send the return message to the caller that initiated the request instruction when receiving the end signal through the sending logic module.
[0146] The present invention also provides a computer storage medium on which a request result synchronous return program is stored. When the request result synchronous return program is executed by a processor, the steps of the request result synchronous return program method described in any of the above embodiments are implemented.
[0147] The specific embodiments of the computer storage medium of the present invention are basically the same as the above-mentioned embodiments of the request result synchronous return program method of the present invention, and will not be described in detail here.
[0148] The present invention also provides a computer program product, which includes a computer program. When the computer program is executed by a processor, it implements the steps of the request result synchronous return method of the present invention as described in any of the above embodiments, which will not be repeated here.
[0149] It should be noted that, in this document, the terms "comprises," "includes," or any other variations thereof are intended to encompass non-exclusive inclusion, such that a process, method, article, or system comprising a series of elements includes not only those elements but also other elements not explicitly listed, or elements inherent to such process, method, article, or system. In the absence of further limitations, an element defined by the phrase "comprising a ..." does not exclude the presence of other identical elements in the process, method, article, or system comprising the element.
[0150] The serial numbers of the above embodiments of the present invention are for description only and do not represent the advantages or disadvantages of the embodiments.
[0151] Through the description of the above implementation methods, those skilled in the art can clearly understand that the above-mentioned embodiment methods can be implemented by means of software plus the necessary general hardware platform, and of course by hardware, but in many cases the former is a better implementation method. Based on this understanding, the technical solution of the present invention is essentially or the part that contributes to the prior art can be embodied in the form of a software product, which is stored in a storage medium (such as ROM / RAM, magnetic disk, optical disk) as described above, and includes several instructions for enabling a terminal device (which can be a TWS headset, etc.) to execute the methods described in each embodiment of the present invention.
[0152] The above are only preferred embodiments of the present invention and are not intended to limit the patent scope of the present invention. Any equivalent structure or equivalent process transformation made using the contents of the present invention description and drawings, or directly or indirectly applied in other related technical fields, are also included in the patent protection scope of the present invention.
Claims
1. A method for synchronously returning a request result, characterized in that: The request result synchronous return method is applied to the server, and the server includes a sending logic module; The request result synchronous return method includes: Establishing a communication connection with the client, and encapsulating the request instruction and the message identifier generated according to the request instruction into request data through the sending logic module; Sending the request data to the client through the sending logic module, so that the client encapsulates the return message corresponding to the request instruction and the message identifier into return data; Detecting whether the return data sent by the client is received within a preset time; When it is detected that the return data is received within the preset time, the return message is sent to the caller that initiated the request instruction through the sending logic module; After the step of establishing a communication connection with the client, the method further includes: Generate a link handle corresponding to the client, and encapsulate the link handle and the hash table into a link encapsulation; Starting a data reading process based on the link package; After the step of encapsulating the request instruction and the message identifier generated according to the request instruction into request data by the sending logic module, the method further includes: The request data is written into the data association body in the hash table through the sending logic module.
2. The request result synchronous return method according to claim 1, characterized in that: The step of sending the request data to the client through the sending logic module includes: Converting the request data into a preset data format through the sending logic module to obtain target request data; The target request data is sent to the client based on the connection handle by the sending logic module.
3. The request result synchronous return method according to claim 1, characterized in that: After the step of detecting whether the return data sent by the client is received within a preset time, the method further includes: When it is detected that the return data is not received within the preset time, an error signal is sent to the caller who initiated the request instruction through the sending logic module.
4. The request result synchronous return method according to claim 1, characterized in that: Before the step of sending the return message to the caller that initiated the request instruction through the sending logic module, the method further includes: Parsing the returned data through the data reading process to obtain the returned message and the message identifier, and determining the target data association in the hash table according to the message identifier; The returned data is written into the target data association body through the data reading process, and an end signal is sent to the sending logic module.
5. The request result synchronous return method according to claim 4, characterized in that: The step of sending the return message to the caller that initiated the request instruction through the sending logic module includes: When the end signal is received, the sending logic module sends the return message to the caller that initiated the request instruction.
6. A device for synchronously returning request results, characterized in that: The request result synchronous return device is applied to the server, and the server includes a sending logic module; The request result synchronous return device includes: A request data module establishes a communication connection with the client and encapsulates a request instruction and a message identifier generated according to the request instruction into request data through the sending logic module; a return data module, which sends the request data to the client through the sending logic module, so that the client can encapsulate the return message corresponding to the request instruction and the message identifier into return data; A detection module, detecting whether the return data sent by the client is received within a preset time; The caller module, when detecting that the return data is received within the preset time, sends the return message to the caller that initiated the request instruction through the sending logic module; The request result synchronous return device also includes: A link encapsulation module, configured to generate a link handle corresponding to the client, and encapsulate the link handle and a hash table into a link encapsulation; A data reading process module, configured to start a data reading process based on the link package; The data association body module is configured to write the request data into the data association body in the hash table through the sending logic module.
7. A terminal device, characterized in that: The terminal device includes: a memory, a processor, and a request result synchronous return program stored in the memory and executable on the processor. When the request result synchronous return program is executed by the processor, the steps of the request result synchronous return method according to any one of claims 1 to 5 are implemented.
8. A computer-readable storage medium, characterized in that The computer-readable storage medium stores a request result synchronous return program, which, when executed by a processor, implements the steps of the request result synchronous return method according to any one of claims 1 to 5.
Citation Information
Patent Citations
Data synchronization method, client, electronic equipment and computer readable storage medium
CN110636132A
Message routing method based on DHT network, electronic device, proxy node and medium
CN111245727A