Communication method, device, equipment, storage medium and program product
By applying ZeroMQ's Dealer-Router communication mode in the field of autonomous driving and introducing a thread pool, it allocates processing threads to socket messages, solving the problem of high communication complexity, achieving efficient vehicle-machine communication, and meeting high performance needs.
Patent Information
- Application Number
- CN202510704536.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-05-29
- Publication Date
- 2025-08-19
- Estimated Expiration
- 2045-05-29
AI Technical Summary
In the field of autonomous driving, the existing communication methods are complex and difficult to meet the high-performance automotive and machine communication needs.
It adopts ZeroMQ's Dealer-Router communication mode, and uses the introduction of a thread pool to allocate processing threads to the socket messages in the message queue, supporting multi-threaded read and write operations to realize intra-domain or inter-domain communication.
It reduces communication complexity, improves communication efficiency, supports complex business processing logic, enriches application scenarios, and improves user experience.
Smart Images

Figure CN120223459B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of autonomous driving technology, and in particular to a communication method, apparatus, device, storage medium, and program product. Background Art
[0002] In the field of autonomous driving, with the development of intelligent technology and the increase in autonomous driving applications, the demand for intra-domain communication and cross-domain communication is increasing, resulting in increasing complexity of communication.
[0003] Generally, vehicle-computer communication is mainly carried out through CAN (Controller Area Network) bus, LINK (Local Interconnect Network) bus, etc.
[0004] This implementation method is not only highly complex in design, but also difficult to meet the high-performance vehicle-to-machine communication requirements. Summary of the Invention
[0005] The embodiments of the present application provide a communication method, apparatus, device, storage medium, and program product to reduce communication complexity and improve communication efficiency.
[0006] In a first aspect, an embodiment of the present application provides a communication method, the method comprising:
[0007] Detecting a socket message in a target queue; wherein the socket message is a socket message of a client or a socket message of a server;
[0008] If a socket message is detected, determining a message processing type corresponding to the socket message;
[0009] According to the message processing type, a corresponding processing thread is allocated to each socket message, and communication is performed with the socket message according to a communication channel established by the processing thread.
[0010] In a possible implementation, the socket message is a socket message from a client; the message processing type indicates a type of message to be sent; and according to the message processing type, a corresponding processing thread is assigned to each socket message, and communication is performed with the socket message using a communication channel established by the processing thread, including:
[0011] Allocating a first thread for the socket message of the client according to the message processing type;
[0012] The socket message is sent to the first communication middleware according to the communication channel established by the first thread; wherein the first communication middleware is used to send the socket message of the client to the second communication middleware; the second communication middleware is used to send the socket message of the client to the server; the server is used to process the socket message of the client and obtain response data corresponding to the socket message of the client;
[0013] Calling a first function to obtain response data received in the first communication middleware;
[0014] According to the communication channel established by the calling thread of the client, the response data is returned to the client to complete the communication; wherein the calling thread indicates a synchronous calling thread or an asynchronous calling thread.
[0015] In one possible implementation, the socket message is a socket message from the server; the message processing type indicates a received message type; and according to the message processing type, a corresponding processing thread is assigned to each socket message, and communication is performed with the socket message according to a communication channel established by the processing thread, including:
[0016] Allocating a second thread for the socket message of the server according to the message processing type; wherein the socket message of the server indicates the response data;
[0017] Sending the socket message of the server to the second communication middleware according to the communication channel established by the second thread;
[0018] sending, according to the second communication middleware, the response data indicated by the socket message of the server to the first communication middleware;
[0019] After the first function obtains the response data and returns the response data to the client according to the communication channel established by the calling thread of the client, the communication is completed.
[0020] In a possible implementation, before detecting the socket message in the target queue, the method further includes:
[0021] The second function is called to obtain the socket message of the server, and the socket message of the server is saved in the target queue.
[0022] In one possible implementation, detecting a socket message in a target queue includes:
[0023] Start the target thread according to the preset thread startup requirements;
[0024] Detecting the socket message in the target queue according to the communication channel established by the target thread.
[0025] In a possible implementation, before detecting the socket message in the target queue, the method further includes:
[0026] In response to a data communication request initiated by the client, determining a request type corresponding to the data communication request;
[0027] A corresponding calling thread is created according to the request type, and a socket message corresponding to the data communication request is stored in the target queue according to the communication channel established by the corresponding calling thread.
[0028] In a possible implementation, there are multiple clients, and storing the socket message corresponding to the data communication request in the target queue according to the communication channel established by the corresponding calling thread includes:
[0029] Adding a mutex lock to the socket used by the data communication request according to the communication channel established by the corresponding calling thread;
[0030] After storing the socket message in the target queue, the mutex lock is released.
[0031] In a second aspect, an embodiment of the present application provides a communication device, including:
[0032] A detection module, configured to detect a socket message in a target queue; wherein the socket message is a socket message of a client or a socket message of a server;
[0033] a determination module, configured to determine a message processing type corresponding to a socket message if a socket message is detected;
[0034] An allocation module, configured to allocate a corresponding processing thread to each of the socket messages according to the message processing type;
[0035] A communication module is used to communicate according to the communication channel established by the processing thread and the socket message.
[0036] In a possible implementation, the socket message is a socket message of the client; the message processing type indicates a type of message to be sent;
[0037] At this time, the allocation module includes a first allocation module, which is used to:
[0038] Allocating a first thread for the socket message of the client according to the message processing type;
[0039] The communication module includes a first communication module configured to:
[0040] The socket message is sent to the first communication middleware according to the communication channel established by the first thread; wherein the first communication middleware is used to send the socket message of the client to the second communication middleware; the second communication middleware is used to send the socket message of the client to the server; the server is used to process the socket message of the client and obtain response data corresponding to the socket message of the client;
[0041] Calling a first function to obtain response data received in the first communication middleware;
[0042] According to the communication channel established by the calling thread of the client, the response data is returned to the client to complete the communication; wherein the calling thread indicates a synchronous calling thread or an asynchronous calling thread.
[0043] In a possible implementation, the socket message is a socket message of the server; the message processing type indicates a received message type;
[0044] At this time, the allocation module, including the second allocation module, is used to:
[0045] Allocating a second thread for the socket message of the server according to the message processing type; wherein the socket message of the server indicates the response data;
[0046] The communication module includes a second communication module, configured to:
[0047] Sending the socket message of the server to the second communication middleware according to the communication channel established by the second thread;
[0048] sending, according to the second communication middleware, the response data indicated by the socket message of the server to the first communication middleware;
[0049] After the first function obtains the response data and returns the response data to the client according to the communication channel established by the calling thread of the client, the communication is completed.
[0050] In one possible embodiment, the device is further used for:
[0051] Before detecting the socket message in the target queue, a second function is called to obtain the socket message of the server, and the socket message of the server is saved in the target queue.
[0052] In one possible implementation, the detection module is configured to:
[0053] Start the target thread according to the preset thread startup requirements;
[0054] Detecting the socket message in the target queue according to the communication channel established by the target thread.
[0055] In a possible implementation, the device further includes a response module configured to:
[0056] Before detecting the socket message in the target queue, in response to the data communication request initiated by the client, determining the request type corresponding to the data communication request;
[0057] A corresponding calling thread is created according to the request type, and a socket message corresponding to the data communication request is stored in the target queue according to the communication channel established by the corresponding calling thread.
[0058] In a possible implementation manner, there are multiple clients. In this case, the response module is configured to:
[0059] Adding a mutex lock to the socket used by the data communication request according to the communication channel established by the corresponding calling thread;
[0060] After storing the socket message in the target queue, the mutex lock is released.
[0061] In a third aspect, an embodiment of the present application provides an electronic device, comprising: a memory, a processor;
[0062] The memory stores computer-executable instructions;
[0063] The processor executes the computer-executable instructions stored in the memory, so that the processor executes the above first aspect and / or various possible implementations of the first aspect.
[0064] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium, in which computer-executable instructions are stored. When the computer-executable instructions are executed by a processor, they are used to implement the above first aspect and / or various possible implementations of the first aspect.
[0065] In a fifth aspect, an embodiment of the present application provides a computer program product, including a computer program, which, when executed by a processor, implements the above first aspect and / or various possible implementation methods of the first aspect.
[0066] The communication method, apparatus, device, storage medium and program product provided in the embodiments of the present application can utilize the Dealer-Router communication mode provided by ZeroMQ to realize intra-domain or inter-domain communication, reduce the complexity of communication, broaden the application scenarios of the communication method, and meet the complex and high-performance communication needs of the vehicle. In specific implementation, the socket messages sent by at least one client and the server can be saved in the message queue, and by detecting the socket messages in the target queue, it is determined whether communication is required. At this time, the client can process multiple communication requests at the same time, which not only supports complex business processing logic and further enriches the application scenarios, but also improves communication efficiency and enhances the user experience. Afterwards, when it is detected that there is a socket message in the target queue, the message processing type corresponding to the socket message is first determined, and then, according to the message processing type, the corresponding processing thread is assigned to each socket message, so that communication is carried out according to the communication channel and socket message established by the processing thread. This implementation can allocate corresponding processing threads for communication to each socket message according to the message processing type corresponding to each socket message when there are many socket messages, thereby further improving communication efficiency by processing socket messages through multiple threads. BRIEF DESCRIPTION OF THE DRAWINGS
[0067] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present application and, together with the description, serve to explain the principles of the present application.
[0068] Figure 1 A schematic diagram of a communication method provided in this application Figure 1 ;
[0069] Figure 2 A schematic diagram of a communication method provided in this application Figure 2 ;
[0070] Figure 3 A schematic diagram of a process for determining a message processing type provided in an embodiment of the present application;
[0071] Figure 4 A schematic diagram of a communication method provided in an embodiment of the present application Figure 3 ;
[0072] Figure 5 A schematic diagram of a communication process for synchronous calls provided in an embodiment of the present application;
[0073] Figure 6 A schematic diagram of the communication process of an asynchronous call provided in an embodiment of the present application;
[0074] Figure 7A schematic diagram of the structure of a communication device provided in an embodiment of the present application;
[0075] Figure 8 A schematic structural diagram of another communication device provided in an embodiment of the present application;
[0076] Figure 9 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application.
[0077] The above drawings illustrate specific embodiments of the present application, which will be described in more detail below. These drawings and the textual description are not intended to limit the scope of the present application in any way, but rather to illustrate the concepts of the present application to those skilled in the art by reference to specific embodiments. DETAILED DESCRIPTION
[0078] Exemplary embodiments will be described in detail herein, with examples illustrated in the accompanying drawings. In the following description, when referring to the drawings, identical numerals in different figures represent identical or similar elements, unless otherwise indicated. The embodiments described in the following exemplary embodiments are not intended to represent all embodiments consistent with the present application. Rather, they are merely examples of apparatus and methods consistent with certain aspects of the present application, as detailed in the appended claims.
[0079] First, let’s explain the terms involved in this application:
[0080] API: Application Programming Interface, application programming interface;
[0081] ZeroMQ: An open source messaging middleware that provides sockets across multiple transport protocols, for example, Dealer-Router sockets.
[0082] In the field of autonomous driving, with the development of intelligent technology and the increase in autonomous driving applications, the demand for intra-domain communication and cross-domain communication is increasing, resulting in increasing complexity in communication.
[0083] Generally, vehicle-computer communication is mainly carried out through CAN (Controller Area Network) bus, LINK (Local Interconnect Network) bus, etc.
[0084] This implementation method is not only highly complex in design, but also difficult to meet the high-performance vehicle-to-machine communication requirements.
[0085] Based on this, the Dealer-Router communication mode provided by ZeroMQ can be applied in the field of autonomous driving to achieve intra-domain or inter-domain communication.
[0086] In specific implementation, the Dealer can be integrated into the client and the Router can be integrated into the server, thereby building a basic communication model, reducing communication complexity and reducing costs.
[0087] However, in the Dealer-Router communication mode, a socket can only be used in one thread, and the Dealer-Router socket cannot send and receive messages at the same time, which affects communication efficiency and cannot meet high-performance communication requirements.
[0088] The communication method provided in this application introduces a thread pool to assign corresponding processing threads to each socket message stored in the message queue for communication, which can support multi-threaded read and write operations, thereby solving the above technical problems.
[0089] The following specific embodiments describe in detail the technical solution of the present application and how the technical solution of the present application solves the above-mentioned technical problems. The following specific embodiments can be combined with each other, and the same or similar concepts or processes may not be repeated in some embodiments. The embodiments of the present application will be described below in conjunction with the accompanying drawings.
[0090] Figure 1 A schematic diagram of a communication method provided in this application Figure 1 ,like Figure 1 As shown, the method includes:
[0091] S101: Detect socket messages in a target queue.
[0092] The socket message is a socket message of the client or a socket message of the server.
[0093] In an example, the target queue can be understood as a message queue for storing socket messages.
[0094] In an example, in the Dealer-Router communication mode, the socket messages in the target queue can be messages sent by the Dealer socket (i.e., the client's socket message) or messages sent by the Router socket (i.e., the server's socket message).
[0095] In an embodiment of the present application, after receiving the socket message sent by the Dealer socket or the Router socket, it can be saved in the target queue.
[0096] Afterwards, the step described in S101 can be executed to detect whether there is an unprocessed socket message in the target queue. If a socket message is detected, the socket message is processed according to the process described below, thereby completing the communication between the client and the server.
[0097] Among them, the client can be understood as the end that initiates the communication request, and the server is the end that processes the corresponding communication request. For example, the client can be the cockpit domain and the server can be the body domain; or, the client can be the cockpit domain and the server can be the cockpit domain; or, the client can be the body domain and the server can be the body domain, etc., thereby completing communication within or between domains.
[0098] For example, if the client is in the cockpit domain and the server is in the body domain, then a possible application scenario may be: adjusting the vehicle's rearview mirror through the display screen in the cockpit domain. At this time, it is necessary to send the message in the cockpit domain to the body domain for execution, so as to realize the adjustment of the vehicle's rearview mirror in the cockpit domain through cross-domain communication.
[0099] S102: If it is detected that a socket message exists, determine a message processing type corresponding to the socket message.
[0100] In one example, the message processing type can indicate the source of the socket message. For example, if the socket message comes from the Dealer socket, then the corresponding message processing type can be the send message type; if the socket message comes from the Router socket, then the corresponding message processing type can be the receive message type.
[0101] S103. Allocate a corresponding processing thread for each socket message according to the message processing type, and communicate using the communication channel established by the processing thread and the socket message.
[0102] In one example, different message processing types may correspond to different message processing methods. Therefore, different processing threads need to be allocated to socket messages under different message processing types to process the socket messages and thus complete communication.
[0103] In one example, when the target queue includes multiple socket messages, the steps described in S102 and S103 above can be executed multiple times, so that a corresponding processing thread is assigned to each socket message according to the thread pool, thereby processing the socket messages according to multiple threads and improving communication efficiency.
[0104] From the above description, it can be seen that the communication method provided in the embodiment of the present application can utilize the Dealer-Router communication mode provided by ZeroMQ to realize intra-domain or inter-domain communication, reduce the complexity of communication, broaden the application scenarios of the communication method, and meet the complex and high-performance communication needs of the vehicle computer. During specific implementation, the socket messages sent by at least one client and the server can be saved in the message queue, and by detecting the socket messages in the target queue, it is determined whether communication is required. At this time, the client can process multiple communication requests at the same time, which not only supports complex business processing logic and further enriches the application scenarios, but also improves communication efficiency and enhances the user experience. Afterwards, when it is detected that there is a socket message in the target queue, the message processing type corresponding to the socket message is first determined, and then, according to the message processing type, the corresponding processing thread is assigned to each socket message, so that communication is carried out according to the communication channel and socket message established by the processing thread. This implementation can allocate corresponding processing threads for communication to each socket message according to the message processing type corresponding to each socket message when there are many socket messages, thereby further improving communication efficiency by processing socket messages through multiple threads.
[0105] Figure 2 A schematic diagram of a communication method provided in this application Figure 2 ,like Figure 2 As shown, this embodiment Figure 1 Based on the embodiment, the communication method is described in detail, and the method includes:
[0106] S201: Start the target thread according to the preset thread startup requirement.
[0107] In one example, the preset thread startup requirement can be understood as receiving a socket message, or can also be understood as meeting a preset time interval. The content of the preset thread startup requirement is not limited here, and is subject to meeting actual communication needs.
[0108] S202: Detect the socket message in the target queue according to the communication channel established by the target thread.
[0109] The socket message is a socket message of the client or a socket message of the server.
[0110] In one example, the target thread can be understood as a multiplexed thread. In this case, the socket message in the target queue can be detected according to the communication channel established by the target thread. For example, whether the target queue includes the socket message can be detected periodically.
[0111] S203: If it is detected that a socket message exists, determine the message processing type corresponding to the socket message.
[0112] In one example, when the target thread detects that the target queue includes a socket message, it can also determine the message processing type of the socket message included in the target queue. In this case, the message processing type can be determined based on the source corresponding to the socket message.
[0113] For example, see Figure 3 , Figure 3 A flow chart of determining a message processing type provided in an embodiment of the present application is as follows: Figure 3 As shown, the target thread is started at a fixed time, and the socket message in the target queue is detected according to the communication channel established by the target thread. If a socket message is detected, the source of the socket message is determined. For example, it can be determined first whether the socket message comes from the Dealer socket. If so, it can be determined that the corresponding message processing type is the send message type, at which point the send message logic can be processed. If not, it is determined whether the socket message comes from the Router socket. If so, it can be determined that the corresponding message processing type is the receive message type, at which point the receive message logic can be processed. If not, it can wait for the next detection. Among them, the send message logic and the receive message logic can refer to the process described in S204 below.
[0114] In the above implementation, a separate target thread, that is, a multiplexing thread, can be used to detect the socket message and determine the message processing type of the socket message, thereby executing the corresponding message processing logic, thereby avoiding processing the Dealer socket message and the Router socket message at the same time, avoiding communication conflicts, and thus affecting communication performance.
[0115] S204: Allocate a corresponding processing thread to each socket message according to the message processing type, and communicate with the socket message through the communication channel established by the processing thread.
[0116] In an example, if the socket message is a socket message of the client; the message processing type indicates the type of message to be sent; then, for the above step S204: according to the message processing type, a corresponding processing thread is assigned to each socket message, and communication is performed according to the communication channel and socket message established by the processing thread, which may specifically include the contents described in the following steps S2041~S2044, and steps S2041~S2044 are the processing process corresponding to the logic of sending messages.
[0117] S2041. Allocate a first thread for the socket message of the client according to the message processing type.
[0118] S2042: Send the socket message to the first communication middleware according to the communication channel established by the first thread.
[0119] In one example, the first communication middleware can be understood as the ZeroMQ message middleware corresponding to the client.
[0120] The first communication middleware is configured to send a client's socket message to the second communication middleware. The first communication middleware can then encapsulate the client's socket message to obtain a message that the second communication middleware can recognize and process. Upon receiving the encapsulated socket message from the first communication middleware, the second communication middleware parses the encapsulated socket message and sends the client's socket message to the server. Upon receiving the client's socket message, the server can process the client's socket message to obtain response data corresponding to the client's socket message.
[0121] S2043: Call the first function to obtain the response data received in the first communication middleware.
[0122] In one example, the first function can be understood as a function that obtains corresponding data from the first communication middleware. For example, the first function can be named the Receive function. In this case, because data processing in the ZeroMQ message middleware is asynchronous, the first function can be called to promptly obtain the response data received by the first communication middleware, thereby avoiding affecting communication efficiency.
[0123] In one example, the response data received in the first communication middleware can be obtained by calling the first function according to the communication channel established by the client's calling thread, or the first function can be called by triggering at a fixed time to obtain the response data received in the first communication middleware according to the first function.
[0124] S2044. Return the response data to the client according to the communication channel established by the calling thread of the client, thereby completing the communication.
[0125] The calling thread indicates a synchronous calling thread or an asynchronous calling thread.
[0126] In one example, if the calling thread indicates a synchronous calling thread, then the response data can be directly returned to the client according to the communication channel established by the synchronous calling thread; if the calling thread indicates an asynchronous calling thread, then the third function can be called according to the communication channel established by the asynchronous calling thread, and the response data can be returned to the client through the third function.
[0127] In one example, the third function can be understood as a function that calls back the response data obtained by the first function. For example, the third function can be named the response function.
[0128] This implementation method can realize communication between the client and the server through synchronous calls or asynchronous calls, making the communication method more flexible and diverse, thereby meeting the needs of various communication scenarios.
[0129] In one example, if the socket message is a socket message from the server; the message processing type indicates the type of received message; then, for the above step S204: according to the message processing type, a corresponding processing thread is assigned to each socket message, and communication is performed according to the communication channel and socket message established by the processing thread, which may specifically include the contents described in the following steps S2045~S2048, and steps S2045~S2048 are also the processing process corresponding to the receiving message logic.
[0130] S2045. Allocate a second thread for the socket message of the server according to the message processing type.
[0131] The socket message from the server indicates the response data.
[0132] S2046: Send the socket message of the server to the second communication middleware according to the communication channel established by the second thread.
[0133] S2047. Send the response data indicated by the socket message of the server to the first communication middleware according to the second communication middleware.
[0134] At this point, the second communication middleware can encapsulate the server's socket message to obtain a message that the first communication middleware can recognize and process, and then send the encapsulated socket message to the first communication middleware. After receiving the encapsulated socket message from the second communication middleware, the first communication middleware parses the encapsulated socket message and sends the parsed socket message to the client according to the first function.
[0135] S2048. After the first function obtains response data and returns the response data to the client according to the communication channel established by the calling thread of the client, the communication is completed.
[0136] In the above implementation, data communication can be performed by assigning a communication channel established by a second thread to the server's socket message, thereby enabling the client's socket message and the server's socket message to communicate using different threads. This allows the client's socket message and the server's socket message to be processed in parallel using multi-threading technology, thereby improving communication efficiency. In this case, the thread pool assigns at least one first thread to the client's socket message and at least one second thread to the server's socket message, thereby achieving one-to-one, one-to-many, many-to-one, and many-to-many communication topologies between the client and the server, further broadening the application scenarios of the communication method.
[0137] In a possible implementation, if the socket message is a socket message from the server, then before detecting the socket message in the target queue, the following steps described in S2049 may be further performed:
[0138] S2049. Call the second function to obtain the socket message of the server, and save the socket message of the server into the target queue.
[0139] In one example, the second function can be used to obtain a socket message from a server-side socket, that is, a router socket, so that the socket message can be transmitted in a timely manner to avoid affecting communication efficiency. For example, the second function can be named a Callback function.
[0140] In one example, when the entire communication process between the client and the server is completed according to the process described in S2041 to S2049 above, the following sequence can be followed:
[0141] S2041. Allocate a first thread for the socket message of the client according to the message processing type.
[0142] S2042: Send the socket message to the first communication middleware according to the communication channel established by the first thread.
[0143] The first communication middleware is configured to send a client's socket message to the second communication middleware. The first communication middleware can then encapsulate the client's socket message to obtain a message that the second communication middleware can recognize and process. Upon receiving the encapsulated socket message from the first communication middleware, the second communication middleware parses the encapsulated socket message and sends the client's socket message to the server. Upon receiving the client's socket message, the server can process the client's socket message to obtain response data corresponding to the client's socket message.
[0144] S2049. Call the second function to obtain the socket message of the server, and save the socket message of the server into the target queue.
[0145] S2045. Allocate a second thread for the socket message of the server according to the message processing type.
[0146] S2046: Send the socket message of the server to the second communication middleware according to the communication channel established by the second thread.
[0147] S2047. Send the response data indicated by the socket message of the server to the first communication middleware according to the second communication middleware.
[0148] S2043: Call the first function to obtain the response data received in the first communication middleware.
[0149] S2044. Return the response data to the client according to the communication channel established by the calling thread of the client.
[0150] S2048. After the first function obtains response data and returns the response data to the client according to the communication channel established by the calling thread of the client, the communication is completed.
[0151] Furthermore, based on any of the above embodiments, before detecting the socket message in the target queue, the embodiment of the present application can also enable the calling thread to store the socket message corresponding to the data communication request in the target queue according to the data communication request initiated by the client, so as to communicate according to the communication method described above. Based on this, see Figure 4 , Figure 4 A schematic diagram of a communication method provided in an embodiment of the present application Figure 3 ,like Figure 4 As shown, the method includes:
[0152] S401: In response to a data communication request initiated by a client, determine a request type corresponding to the data communication request.
[0153] In one example, the present application embodiment can provide multiple communication APIs. In this case, the client can initiate a data communication request by calling the corresponding API. This implementation method can enable the communication method provided by the present application to be used in multiple application scenarios through the communication API, making it more scalable.
[0154] In one example, the request type corresponding to the data communication request may be a synchronous call type or an asynchronous call type. There is no limitation on the request type corresponding to the data communication request, and the request type is determined based on actual needs.
[0155] S402: Create a corresponding calling thread according to the request type, and store the socket message corresponding to the data communication request in a target queue according to the communication channel established by the corresponding calling thread.
[0156] In one example, if the request type is a synchronous call type, a synchronous call thread is created according to the request type; if the request type is an asynchronous call type, an asynchronous call thread is created according to the request type.
[0157] At this time, after the corresponding calling thread is created, the socket message corresponding to the data communication request can be stored in the target queue according to the communication channel established by the corresponding calling thread.
[0158] For example, if the created calling thread is a synchronous calling thread, then after the socket message corresponding to the data communication request is stored in the target queue according to the communication channel established by the synchronous calling thread, and after receiving the response data returned by the first function, the synchronous calling thread is ended.
[0159] If the created calling thread is an asynchronous calling thread, then after storing the socket message corresponding to the data communication request in the target queue according to the communication channel established by the asynchronous calling thread, the process returns to end the asynchronous calling thread.
[0160] In one possible implementation, since there may be a scenario where one server corresponds to multiple clients and multiple clients communicate with the server at the same time, at this time, in the process of storing the socket message corresponding to the data communication request in the target queue according to the communication channel established by the corresponding calling thread, a mutex lock can be added to the socket used for the data communication request according to the communication channel established by the corresponding calling thread; after the socket message is stored in the target queue, the mutex lock is released.
[0161] This implementation method can enable multiple clients or a single client to use the same socket to transmit socket messages through a mutex lock, thereby saving resources and avoiding socket usage conflicts, thereby ensuring smooth communication.
[0162] For an example, see Figure 5 , Figure 5 A communication flow diagram of a synchronous call provided in an embodiment of the present application is shown as follows: Figure 5 As shown, in a synchronous call scenario, the client can initiate a data communication request by calling the corresponding API through a synchronous call thread, and save the socket message corresponding to the data communication request into the target queue.
[0163] Afterwards, the target thread can be started, and if it is determined that the message processing type of the socket message included in the target queue is a sending message type based on the communication channel established by the target thread, the first thread can be allocated to the socket message through the thread pool, and the socket message can be sent to the first communication middleware based on the communication channel established by the first thread.
[0164] Then, the first communication middleware may send the socket message of the client to the second communication middleware corresponding to the server through Ethernet.
[0165] The second communication middleware can send the received client's socket message to the server through the API, so that the server can process it according to the client's socket. After processing, it can obtain response data corresponding to the client's socket message. At this time, the response data can be transmitted to the client through the server's socket.
[0166] At this time, the socket message of the server can be obtained according to the second function, and the socket message of the server (that is, the response data) can be saved in the target queue.
[0167] Afterwards, a second thread can be allocated to the server's socket message through the thread pool according to the message processing type of the socket message, and the response data indicated by the server's socket message can be sent to the second communication middleware according to the communication channel established by the second thread.
[0168] At this point, the second communication middleware can send the response data indicated by the server's socket message to the first communication middleware corresponding to the client via Ethernet. The response data in the first communication middleware can then be returned to the API according to the first function. This data is then returned to the client via the communication channel established by the synchronous call thread, completing communication between the client and the client.
[0169] For an example, see Figure 6 , Figure 6 A communication flow diagram of an asynchronous call provided in an embodiment of the present application is shown as follows: Figure 6 As shown, in the asynchronous call scenario, the client can call the corresponding API through the communication channel established by the asynchronous call thread to initiate a data communication request and save the socket message corresponding to the data communication request to the target queue. At this time, the asynchronous call thread returns.
[0170] Afterwards, the target thread can be started, and if it is determined that the message processing type of the socket message included in the target queue is a sending message type based on the communication channel established by the target thread, the first thread can be allocated to the socket message through the thread pool, and the socket message can be sent to the first communication middleware based on the communication channel established by the first thread.
[0171] Then, the first communication middleware may send the socket message of the client to the second communication middleware corresponding to the server through Ethernet.
[0172] The second communication middleware can send the received client's socket message to the server through the API, so that the server can process it according to the client's socket. After processing, it can obtain response data corresponding to the client's socket message. At this time, the response data can be transmitted to the client through the server's socket.
[0173] At this time, the socket message of the server can be obtained according to the second function, and the socket message of the server (that is, the response data) can be saved in the target queue.
[0174] Afterwards, a second thread can be allocated to the server's socket message through the thread pool according to the message processing type of the socket message, and the response data indicated by the server's socket message can be sent to the second communication middleware according to the communication channel established by the second thread.
[0175] At this point, the second communication middleware can send the response data indicated by the server's socket message to the first communication middleware corresponding to the client via Ethernet. The response data in the first communication middleware can then be returned to the API according to the first function. At this point, the third function can be called based on the communication channel established by the asynchronous call thread to return the response data to the client, completing the communication between the client and the end.
[0176] See also Figure 7 , Figure 7 A schematic diagram of the structure of a communication device provided in an embodiment of the present application is shown in FIG. Figure 7 As shown, the communication device 70 provided in this embodiment of the present application includes:
[0177] The detection module 701 is used to detect the socket message in the target queue; wherein the socket message is the socket message of the client or the socket message of the server.
[0178] The determination module 702 is configured to determine a message processing type corresponding to the socket message if a socket message is detected.
[0179] The allocation module 703 is used to allocate a corresponding processing thread to each socket message according to the message processing type.
[0180] The communication module 704 is used to communicate according to the communication channel and socket messages established by the processing thread.
[0181] See also Figure 8 , Figure 8 A structural diagram of another communication device provided in an embodiment of the present application is shown in FIG. Figure 8 As shown, the communication device 80 provided in this embodiment of the present application includes:
[0182] The detection module 801 is used to detect the socket message in the target queue; wherein the socket message is the socket message of the client or the socket message of the server.
[0183] The determination module 802 is configured to determine a message processing type corresponding to the socket message if the presence of the socket message is detected.
[0184] The allocation module 803 is used to allocate a corresponding processing thread to each socket message according to the message processing type.
[0185] The communication module 804 is used to communicate according to the communication channel and socket messages established by the processing thread.
[0186] In a possible implementation, the socket message is a socket message of the client; the message processing type indicates the type of the message to be sent;
[0187] At this time, the allocation module 803 includes a first allocation module 8031, which is used to:
[0188] Allocate a first thread for the client's socket message according to the message processing type;
[0189] The communication module 804 includes a first communication module 8041, which is used to:
[0190] The socket message is sent to the first communication middleware according to the communication channel established by the first thread; wherein the first communication middleware is used to send the socket message of the client to the second communication middleware; the second communication middleware is used to send the socket message of the client to the server; the server is used to process the socket message of the client and obtain response data corresponding to the socket message of the client;
[0191] Calling the first function to obtain the response data received in the first communication middleware;
[0192] According to the communication channel established by the calling thread of the client, the response data is returned to the client to complete the communication; wherein the calling thread indicates a synchronous calling thread or an asynchronous calling thread.
[0193] In a possible implementation, the socket message is a socket message of the server; the message processing type indicates the type of received message;
[0194] At this time, the allocation module 803 includes a second allocation module 8032, which is used to:
[0195] Allocating a second thread for the socket message of the server according to the message processing type; wherein the socket message of the server indicates response data;
[0196] The communication module 804 includes a second communication module 8042, configured to:
[0197] According to the communication channel established by the second thread, the socket message of the server is sent to the second communication middleware;
[0198] Sending the response data indicated by the socket message of the server to the first communication middleware according to the second communication middleware;
[0199] After the first function obtains the response data and returns the response data to the client according to the communication channel established by the calling thread of the client, the communication is completed.
[0200] In one possible embodiment, the device is further used for:
[0201] Before detecting the socket message in the target queue, the second function is called to obtain the socket message of the server, and the socket message of the server is saved in the target queue.
[0202] In a possible implementation, the detection module 801 is configured to:
[0203] Start the target thread according to the preset thread startup requirements;
[0204] According to the communication channel established by the target thread, the socket message in the target queue is detected.
[0205] In a possible implementation, the apparatus further includes a response module 805 configured to:
[0206] Before detecting the socket message in the target queue, in response to the data communication request initiated by the client, determining a request type corresponding to the data communication request;
[0207] A corresponding calling thread is created according to the request type, and a socket message corresponding to the data communication request is stored in a target queue according to the communication channel established by the corresponding calling thread.
[0208] In a possible implementation, there are multiple clients. In this case, the response module 805 is configured to:
[0209] According to the communication channel established by the corresponding calling thread, add a mutex lock to the socket used for the data communication request;
[0210] After storing the socket message in the destination queue, release the mutex lock.
[0211] The communication device provided in the embodiment of the present application can be used to execute the method provided in the above method embodiment. Its implementation principle and technical effects are similar, and are not described in detail in this embodiment.
[0212] See also Figure 9 , Figure 9 This is a schematic diagram of the structure of an electronic device provided in an embodiment of the present application. Figure 9 As shown, the electronic device 90 provided in this embodiment includes: at least one processor 901 and a memory 902. Optionally, the electronic device 90 further includes a communication component 903. The processor 901, the memory 902, and the communication component 903 are connected via a bus 904.
[0213] During the specific implementation process, at least one processor 901 executes the computer-executable instructions stored in the memory 902, so that the at least one processor 901 performs the above method.
[0214] The specific implementation process of the processor 901 can be found in the above method embodiment. Its implementation principle and technical effects are similar and will not be repeated here in this embodiment.
[0215] In the above embodiments, it should be understood that the processor may be a central processing unit (CPU), other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASICs), etc. A general-purpose processor may be a microprocessor or any conventional processor. The steps of the method disclosed in the present invention may be directly executed by a hardware processor or by a combination of hardware and software modules within the processor.
[0216] The memory may include random access memory (RAM) and may also include non-volatile memory (NVM), such as at least one disk storage.
[0217] A bus can be an Industry Standard Architecture (ISA) bus, a Peripheral Component Interconnect (PCI) bus, or an Extended Industry Standard Architecture (EISA) bus. Buses can be categorized as address buses, data buses, and control buses. For ease of illustration, the buses in the drawings of this application are not limited to just one bus or just one type of bus.
[0218] The present application also provides a computer program product, including a computer program, which implements the above method when executed by a processor.
[0219] The present application also provides a computer-readable storage medium, in which computer-executable instructions are stored. When a processor executes the computer-executable instructions, the above method is implemented.
[0220] The readable storage medium may be implemented by any type of volatile or non-volatile memory device, or a combination thereof, such as static random access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic storage, flash memory, magnetic disk, or optical disk. The readable storage medium may be any available medium that can be accessed by a general-purpose or special-purpose computer.
[0221] An exemplary readable storage medium is coupled to a processor so that the processor can read information from the readable storage medium and write information to the readable storage medium. Of course, the readable storage medium can also be an integral part of the processor. The processor and the readable storage medium can be located in an application specific integrated circuit (ASIC). Of course, the processor and the readable storage medium can also exist in the device as discrete components.
[0222] The division of units is merely a logical functional division; actual implementations may employ alternative divisions, such as combining or integrating multiple units or components into another system, or omitting or disabling certain features. Furthermore, any direct coupling or communication connection shown or discussed may be an indirect coupling or communication connection between devices or units, either through an interface, electrical, mechanical, or other means.
[0223] Units described as separate components may or may not be physically separate, and components shown as units may or may not be physical units, that is, they may be located in one place or distributed across multiple network units. Some or all of these units may be selected to achieve the purpose of this embodiment according to actual needs.
[0224] In addition, each functional unit in each embodiment of the present invention may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit.
[0225] If a function is implemented as a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, or the portion that contributes to the prior art, or a portion of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions for enabling an electronic device (which can be a personal computer, server, or network device, etc.) to perform all or part of the steps of the various embodiments of the method of the present invention. The aforementioned storage medium includes various media that can store program code, such as USB flash drives, mobile hard drives, read-only memories (ROMs), random access memories (RAMs), magnetic disks, or optical disks.
[0226] Those skilled in the art will appreciate that all or part of the steps in the above-described method embodiments can be implemented using hardware associated with program instructions. The aforementioned program can be stored in a computer-readable storage medium. When executed, the program performs the steps of the above-described method embodiments. The aforementioned storage medium includes various media capable of storing program code, such as ROM, RAM, magnetic disks, or optical disks.
[0227] Finally, it should be noted that those skilled in the art will readily identify other embodiments of the present invention after considering the specification and practicing the invention disclosed herein. The present invention is intended to cover any variations, uses, or adaptations of the present invention that follow the general principles of the present invention and include common knowledge or customary techniques in the art not disclosed herein. The present invention is not limited to the precise structure described above and illustrated in the accompanying drawings, and various modifications and variations may be made without departing from the scope thereof. The scope of the present invention is limited solely by the appended claims.
Claims
1. A communication method, characterized in that: The method comprises: Detecting a socket message in a target queue; wherein the socket message is a socket message of a client or a socket message of a server; If a socket message is detected, determining a message processing type corresponding to the socket message; According to the message processing type, a corresponding processing thread is assigned to each socket message, and communication is performed with the socket message according to the communication channel established by the processing thread; the message processing type indicates the source of the socket message, wherein, if the socket message is a socket message from the client, the socket message is sent to the first communication middleware according to the communication channel established by the processing thread; if the socket message is a socket message from the server, the socket message is sent to the second communication middleware according to the communication channel established by the processing thread.
2. The method according to claim 1, characterized in that The socket message is a socket message of the client; the message processing type is used to indicate the type of message to be sent; according to the message processing type, a corresponding processing thread is assigned to each of the socket messages, and communication is performed with the socket message according to a communication channel established by the processing thread, including: Allocating a first thread for the socket message of the client according to the message processing type; The socket message is sent to the first communication middleware according to the communication channel established by the first thread; wherein the first communication middleware is used to send the socket message of the client to the second communication middleware; the second communication middleware is used to send the socket message of the client to the server; the server is used to process the socket message of the client and obtain response data corresponding to the socket message of the client; Calling a first function to obtain response data received in the first communication middleware; According to the communication channel established by the calling thread of the client, the response data is returned to the client to complete the communication; wherein the calling thread indicates a synchronous calling thread or an asynchronous calling thread.
3. The method according to claim 2, characterized in that The socket message is a socket message of the server; the message processing type indicates the type of received message; according to the message processing type, a corresponding processing thread is assigned to each of the socket messages, and communication is performed with the socket message according to a communication channel established by the processing thread, including: Allocating a second thread for the socket message of the server according to the message processing type; wherein the socket message of the server indicates the response data; Sending the socket message of the server to the second communication middleware according to the communication channel established by the second thread; sending, according to the second communication middleware, the response data indicated by the socket message of the server to the first communication middleware; After the first function obtains the response data and returns the response data to the client according to the communication channel established by the calling thread of the client, the communication is completed.
4. The method according to claim 3, characterized in that Before detecting the socket message in the target queue, the method further includes: The second function is called to obtain the socket message of the server, and the socket message of the server is saved in the target queue.
5. The method according to claim 1, wherein Detect socket messages in the target queue, including: Start the target thread according to the preset thread startup requirements; Detecting the socket message in the target queue according to the communication channel established by the target thread.
6. The method according to any one of claims 1 to 5, characterized in that Before detecting the socket message in the target queue, the method further includes: In response to a data communication request initiated by the client, determining a request type corresponding to the data communication request; A corresponding calling thread is created according to the request type, and a socket message corresponding to the data communication request is stored in the target queue according to the communication channel established by the corresponding calling thread.
7. The method according to claim 6, characterized in that There are multiple clients, and according to the communication channel established by the corresponding calling thread, the socket message corresponding to the data communication request is stored in the target queue, including: Adding a mutex lock to the socket used by the data communication request according to the communication channel established by the corresponding calling thread; After storing the socket message in the target queue, the mutex lock is released.
8. A communication device, characterized in that: include: A detection module, configured to detect a socket message in a target queue; wherein the socket message is a socket message of a client or a socket message of a server; a determination module, configured to determine a message processing type corresponding to a socket message if a socket message is detected; an allocation module, configured to allocate a corresponding processing thread to each of the socket messages according to the message processing type; the message processing type indicates the source of the socket message; A communication module is used to communicate according to the communication channel established by the processing thread and the socket message, wherein, if the socket message is a socket message of the client, the socket message is sent to the first communication middleware according to the communication channel established by the processing thread; if the socket message is a socket message of the server, the socket message is sent to the second communication middleware according to the communication channel established by the processing thread.
9. An electronic device, characterized in that: include: Memory, processor; The memory stores computer-executable instructions; The processor executes the computer-executable instructions stored in the memory, so that the processor performs the method according to any one of claims 1 to 7.
10. A computer-readable storage medium, characterized in that The computer-readable storage medium stores computer-executable instructions, which are used to implement the method according to any one of claims 1 to 7 when executed by a processor.
11. A computer program product, characterized in that The invention comprises a computer program, which implements the method according to any one of claims 1 to 7 when executed by a processor.
Citation Information
Patent Citations
Database data processing method, device, system and equipment
CN118520025A
Method and system for maximizing the use of threads in a file server for processing network requests
US5796954A