Communication method and device, equipment, storage medium and program product
By introducing ZeroMQ's Dealer-Router communication mode and thread pool technology in the field of autonomous driving, the problem of high complexity of vehicle-machine communication in the existing technology is solved, efficient and complex vehicle-machine communication is achieved, and high-performance needs are met.
Patent Information
- Application Number
- CN202510704536.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-05-29
- Publication Date
- 2025-06-27
- Estimated Expiration
- 2045-05-29
AI Technical Summary
In the field of autonomous driving, the existing vehicle-machine communication methods are complex and difficult to meet the needs of high-performance communication, resulting in high communication complexity.
Multithreaded processing of socket messages is achieved by introducing ZeroMQ's Dealer-Router communication mode between the client and the server, and allocating processing threads for each socket message using a thread pool.
It reduces communication complexity, improves communication efficiency, meets the complex and high-performance communication needs of vehicle and computers, and improves the user experience.
Smart Images

Figure CN120223459A_ABST
Abstract
Description
Technical Field
[0001] This 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 technologies and the increasing number of autonomous driving applications, the need for in-domain communication and cross-domain communication is increasing, resulting in higher communication complexity.
[0003] Generally, vehicle communication is mainly carried out through CAN (Controller Area Network) bus, LINK (Local Interconnect Network) bus, etc.
[0004] This implementation method not only has high design complexity but also is difficult to meet the high-performance vehicle communication requirements. Summary of the Invention
[0005] Embodiments of this application provide a communication method, apparatus, device, storage medium, and program product to achieve the effect of reducing communication complexity and improving communication efficiency.
[0006] In a first aspect, embodiments of this application provide a communication method, and the method includes:
[0007] Detect socket messages in a target queue; where the socket messages are socket messages of a client or socket messages of a server;
[0008] If it is detected that there are socket messages, determine the message processing type corresponding to the socket messages;
[0009] According to the message processing type, allocate corresponding processing threads to each of the socket messages, and communicate with the socket messages according to the communication channels established by the processing threads.
[0010] In a possible implementation, the socket messages are socket messages of a client; the message processing type indicates the message sending type; according to the message processing type, allocate corresponding processing threads to each of the socket messages, and communicate with the socket messages according to the communication channels established by the processing threads, including:
[0011] According to the message processing type, allocate a first thread to the socket messages of the client;
[0012] The communication channel established according to the first thread sends the socket message to the first communication middleware; 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 to obtain response data corresponding to the socket message of the client.
[0013] Call the first function to obtain the response data received in the first communication middleware.
[0014] According to the communication channel established by the calling thread of the client, return the response data to the client to complete the communication; wherein, the calling thread indicates a synchronous calling thread or an asynchronous calling thread.
[0015] In a possible implementation manner, the socket message is a socket message of the server; the message processing type indicates the received message type; according to the message processing type, allocate a corresponding processing thread for each socket message, and communicate according to the communication channel established by the processing thread and the socket message, including:
[0016] According to the message processing type, allocate a second thread for the socket message of the server; wherein, the socket message of the server indicates the response data.
[0017] According to the communication channel established by the second thread, send the socket message of the server to the second communication middleware.
[0018] According to the second communication middleware, send 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 manner, before detecting the socket message in the target queue, the method further includes:
[0021] Call the second function to obtain the socket message of the server and save the socket message of the server to the target queue.
[0022] In a possible implementation manner, detecting the socket message in the target queue includes:
[0023] Start the target thread according to the preset thread start requirement.
[0024] Detect the socket messages in the target queue according to the communication channel established for the target thread.
[0025] In a possible implementation manner, before detecting the socket messages in the target queue, the method further includes:
[0026] In response to a data communication request initiated by the client, determine the corresponding request type of the data communication request;
[0027] Create a corresponding call thread according to the request type, and store the socket messages corresponding to the data communication request into the target queue according to the communication channel established for the corresponding call thread.
[0028] In a possible implementation manner, the number of clients is multiple. Storing the socket messages corresponding to the data communication request into the target queue according to the communication channel established for the corresponding call thread includes:
[0029] Add a mutex lock to the socket used for the data communication request according to the communication channel established for the corresponding call thread.
[0030] After storing the socket message into the target queue, release the mutex lock.
[0031] In a second aspect, an embodiment of the present application provides a communication device, including:
[0032] A detection module, configured to detect socket messages in a target queue; wherein, the socket messages are socket messages of a client or socket messages of a server;
[0033] A determination module, configured to determine the message processing type corresponding to the socket message if it is detected that there is a socket message;
[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, configured to communicate according to the communication channel established for the processing thread and the socket message.
[0036] In a possible implementation manner, the socket message is a socket message of a client; the message processing type indicates the message sending type;
[0037] At this time, the allocation module includes a first allocation module, configured to:
[0038] Allocate a first thread to the socket message of the client according to the message processing type;
[0039] A communication module, including a first communication module, for:
[0040] Send the socket message 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 to obtain response data corresponding to the socket message of the client;
[0041] Call the first function to obtain the response data received in the first communication middleware;
[0042] Return the response data to the client according to the communication channel established by the calling thread of 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 manner, the socket message is a socket message of the server; the message processing type indicates the received message type;
[0044] At this time, an allocation module, including a second allocation module, for:
[0045] Allocate 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] A communication module, including a second communication module, for:
[0047] Send the socket message of the server to the second communication middleware according to the communication channel established by the second thread;
[0048] Send the response data indicated by the socket message of the server according to the second communication middleware 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 a possible implementation manner, the device is further used for:
[0051] Before detecting the socket message in the target queue, call the second function to obtain the socket message of the server and save the socket message of the server to the target queue.
[0052] In a possible implementation, a detection module is configured to:
[0053] Start a target thread according to a preset thread start requirement;
[0054] Detect socket messages in the target queue according to the communication channel established by the target thread.
[0055] In a possible implementation, the apparatus further includes a response module configured to:
[0056] Before detecting socket messages in the target queue, in response to a data communication request initiated by the client, determine the corresponding request type of the data communication request;
[0057] Create a corresponding call thread according to the request type, and store the socket message corresponding to the data communication request into the target queue according to the communication channel established by the corresponding call thread.
[0058] In a possible implementation, the number of clients is multiple. At this time, the response module is configured to:
[0059] Add a mutex lock to the socket used for the data communication request according to the communication channel established by the corresponding call thread;
[0060] After storing the socket message into the target queue, release the mutex lock.
[0061] In a third aspect, an embodiment of the present application provides an electronic device, including: 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, and 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, and when the computer program is executed by a processor, it implements the above first aspect and / or various possible implementations of the first aspect.
[0066] The communication method, device, equipment, storage medium, and program product provided by the embodiments of this application can utilize the Dealer-Router communication mode provided by ZeroMQ to achieve intra-domain or inter-domain communication, reduce communication complexity, broaden the application scenarios of the communication method, and meet the complex and high-performance communication requirements of in-vehicle systems. Specifically, during implementation, socket messages sent by at least one client and the server can be saved in a message queue, and by detecting the socket messages in the target queue, it can be determined whether communication is required. At this time, the client can handle multiple communication requests simultaneously, which can not only support complex business processing logics, further enrich the application scenarios, but also improve communication efficiency and enhance the user experience. After that, when it is detected that there are socket messages in the target queue, first determine the message processing type corresponding to the socket message, and then, according to the message processing type, allocate corresponding processing threads for each socket message, so as to communicate according to the communication channels established by the processing threads and the socket messages. This implementation method can, when there are many socket messages, allocate corresponding processing threads for each socket message according to the message processing type corresponding to each socket message for communication, so that the socket messages can be processed by multiple threads, further enhancing communication efficiency. BRIEF DESCRIPTION OF THE DRAWINGS
[0067] The accompanying drawings herein are incorporated into the specification and form a part of the specification, showing embodiments consistent with the present application, and are used together with the specification to explain the principles of the present application.
[0068] Figure 1 Flow schematic of a communication method provided by the present application Figure 1 ;
[0069] Figure 2 Flow schematic of a communication method provided by the present application Figure 2 ;
[0070] Figure 3 Flow schematic diagram of a method for determining message processing type provided by the embodiments of the present application;
[0071] Figure 4 Flow schematic of a communication method provided by the embodiments of the present application Figure 3 ;
[0072] Figure 5 Flow schematic diagram of a synchronous call communication process provided by the embodiments of the present application;
[0073] Figure 6 Flow schematic diagram of an asynchronous call communication process provided by the embodiments of the present application;
[0074] Figure 7A schematic structural diagram of a communication device provided by an embodiment of the present application;
[0075] Figure 8 A schematic structural diagram of another communication device provided by an embodiment of the present application;
[0076] Figure 9 A schematic structural diagram of an electronic device provided by an embodiment of the present application.
[0077] Through the above-mentioned drawings, specific embodiments of the present application have been shown, and there will be more detailed descriptions hereinafter. These drawings and textual descriptions are not intended to limit the scope of the concept of the present application in any way, but to illustrate the concept of the present application to those skilled in the art by referring to specific embodiments. Detailed implementation manners
[0078] Here, the exemplary embodiments will be described in detail, and the examples are shown in the drawings. When the following description refers to the drawings, unless otherwise indicated, the same numbers in different drawings represent the same or similar elements. The implementation manners described in the following exemplary embodiments do not represent all implementation manners consistent with the present application. On the contrary, they are merely examples of devices and methods consistent with some aspects of the present application as detailed in the appended claims.
[0079] First, the nouns involved in the present application are explained:
[0080] API: Application Programming Interface, application programming interface;
[0081] ZeroMQ: An open-source message middleware that can provide sockets across multiple transport protocols, for example, Dealer-Router sockets.
[0082] In the field of autonomous driving, with the development of intelligent technologies and the increase in autonomous driving applications, the requirements for in-domain communication and cross-domain communication are getting higher and higher, resulting in higher and higher communication complexity.
[0083] Generally, vehicle communication is mainly carried out through CAN (Controller Area Network) bus, LINK (Local Interconnect Network) bus, etc.
[0084] This implementation manner not only has high design complexity but also is difficult to meet the high-performance vehicle 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, Dealer can be integrated into the client, and Router can be integrated into the server, thereby constructing a basic communication model, reducing communication complexity and cost.
[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 simultaneously, which affects communication efficiency and cannot meet the high-performance communication requirements.
[0088] The communication method provided by this application introduces a thread pool to allocate corresponding processing threads for each socket message saved in the message queue for communication, and can support multi-threaded read and write operations, thereby solving the above technical problems.
[0089] The following uses specific embodiments to elaborate in detail on the technical solution of this application and how the technical solution of this application solves the above technical problems. These 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 this application will be described below in conjunction with the accompanying drawings.
[0090] Figure 1 Schematic flow of a communication method provided by this application Figure 1 , as Figure 1 shown, the method includes:
[0091] S101. Detect socket messages in the target queue.
[0092] Among them, the socket message is a socket message of the client or a socket message of the server.
[0093] In one example, the target queue can be understood as a message queue for storing socket messages.
[0094] In one example, in the Dealer-Router communication mode, the socket message in the target queue can be a message sent by the Dealer socket (that is, the socket message of the client), or a message sent by the Router socket (that is, the socket message of the server).
[0095] In the embodiments of this application, after receiving a socket message sent by the Dealer socket or the Router socket, it can be saved to the target queue.
[0096] After that, the steps described in S101 can be executed to detect whether there are unprocessed socket messages in the target queue. If it is detected that there are socket messages, the socket messages will be processed according to the process described below, thus 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., thus completing the communication within or between domains.
[0098] Exemplarily, if the client is the cockpit domain and the server is the body domain, then a possible application scenario can 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 there are socket messages, determine the message processing type corresponding to the socket messages.
[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. According to the message processing type, allocate a corresponding processing thread for each socket message, and communicate with the socket message according to the communication channel established by the processing thread.
[0102] In one example, different message processing types can correspond to different message processing methods. Therefore, it is necessary to allocate different processing threads for the socket messages under different message processing types to process the socket messages, thus completing the communication.
[0103] In one example, when there are multiple socket messages in the target queue, the steps described in S102 and S103 above can be executed multiple times, so as to allocate a corresponding processing thread for each socket message according to the thread pool, and thus process the socket messages according to multiple threads to improve the communication efficiency.
[0104] As can be seen from the above description, the communication method provided by the embodiments of the present application can utilize the Dealer-Router communication mode provided by ZeroMQ to implement intra-domain or inter-domain communication, reduce communication complexity, broaden the application scenarios of the communication method, and meet the complex and high-performance communication requirements of in-vehicle devices. Specifically in implementation, socket messages sent by at least one client and a server can be saved in a 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 simultaneously, which can not only support complex service processing logics, further enrich the application scenarios, but also improve communication efficiency and enhance the user experience. After that, when it is detected that there are socket messages in the target queue, first determine the message processing type corresponding to the socket messages, and then, according to the message processing type, allocate corresponding processing threads to each socket message, so as to communicate according to the communication channels established by the processing threads and the socket messages. This implementation method can, when there are a large number of socket messages, allocate corresponding processing threads to each socket message according to the message processing type corresponding to each socket message for communication, so that the socket messages can be processed by multiple threads, further improving communication efficiency.
[0105] Figure 2 Flow schematic of a communication method provided by the present application Figure 2 , as Figure 2 shown, on the basis of the Figure 1 embodiment, the communication method is described in detail. The method includes:
[0106] S201. Start a target thread according to a preset thread start requirement.
[0107] In one example, the preset thread start requirement can be understood as receiving a socket message, or it can also be understood as meeting a preset time interval. The content of the preset thread start requirement is not limited here, and it is subject to meeting the actual communication requirements.
[0108] S202. Detect socket messages in the target queue according to the communication channel established by the target thread.
[0109] Among them, the socket message is a socket message of a client or a socket message of a server.
[0110] In one example, the target thread can be understood as a multiplexing thread. At this time, the socket messages in the target queue can be detected according to the communication channel established by the target thread. For example, it can be detected regularly whether the target queue includes socket messages.
[0111] S203. If a socket message is detected, 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. At this time, the message processing type can be determined according to the source corresponding to the socket message.
[0113] Exemplarily, refer to Figure 3 , Figure 3 which is a schematic flowchart of a process for determining a message processing type provided by an embodiment of the present application. As Figure 3 shown, the target thread is started regularly, and the socket messages in the target queue are detected according to the communication channel established by the target thread. If a socket message is detected, determine the source of the socket message. For example, it can first be determined whether the socket message comes from the Dealer socket. If so, the corresponding message processing type can be determined as the send message type, and at this time, the send message logic can be processed. If not, it is determined whether the socket message comes from the Router socket. If so, the corresponding message processing type can be determined as the receive message type, and at this time, the receive message logic can be processed. If not, 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 embodiment, the socket message can be detected by a separate target thread, that is, a multiplexing thread, and the message processing type of the socket message can be determined, so as to execute the corresponding message processing logic, thereby avoiding simultaneously processing Dealer socket messages and Router socket messages and avoiding communication conflicts, which in turn affect the communication performance.
[0115] S204. According to the message processing type, allocate a corresponding processing thread for each socket message, and communicate according to the communication channel established by the processing thread and the socket message.
[0116] In one example, if the socket message is a socket message of a client; the message processing type indicates the send message type; then, for the above step S204: According to the message processing type, allocate a corresponding processing thread for each socket message, and communicate according to the communication channel established by the processing thread and the socket message, it can specifically include the content described in steps S2041 to S2044 below. Steps S2041 to S2044 are also the processing processes corresponding to the send message logic.
[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 through the communication channel established according to the first thread.
[0119] In one example, the first communication middleware can be understood as the ZeroMQ message middleware corresponding to the client.
[0120] Among them, the first communication middleware is used to send the socket message of the client to the second communication middleware; at this time, the first communication middleware can encapsulate the socket message of the client to obtain a message that the second communication middleware can recognize and process. After receiving the socket message after encapsulation processing sent by the first communication middleware, the second communication middleware parses and processes the socket message after encapsulation processing, and sends the socket message of the client to the server. After receiving the socket message of the client, the server can process the socket message of the client to obtain response data corresponding to the socket message of the client.
[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 to obtain corresponding data from the first communication middleware. For example, this first function can be named the Receive function. At this time, since the data processing method in the ZeroMQ message middleware is an asynchronous processing method, the response data received by the first communication middleware can be obtained in a timely manner by calling the first function, avoiding affecting the communication efficiency.
[0123] In one example, the response data received in the first communication middleware obtained by calling the first function can be called according to the communication channel established by the calling thread of the client, or the first function can be triggered regularly 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 through the communication channel established according to the calling thread of the client to complete the communication.
[0125] Among them, 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] In this implementation, communication between the client and the server can be achieved through synchronous or asynchronous calls, making the communication method more flexible and diverse, thereby meeting the requirements of various communication scenarios.
[0129] In one example, if the socket message is the socket message of the server; the message processing type indicates the received message type; then, for step S204 above: according to the message processing type, allocate a corresponding processing thread for each socket message, and communicate according to the communication channel established by the processing thread and the socket message, which can specifically include the content described in steps S2045~S2048 below. Steps S2045~S2048 are also the processing processes corresponding to the receive message logic.
[0130] S2045. Allocate a second thread for the socket message of the server according to the message processing type.
[0131] Among them, the socket message of 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 by the second communication middleware to the first communication middleware.
[0134] At this time, the second communication middleware can encapsulate the socket message of the server to obtain a message that the first communication middleware can recognize and process, and send the encapsulated socket message to the first communication middleware. After receiving the encapsulated socket message sent by the second communication middleware, the first communication middleware parses the encapsulated socket message, and then sends the parsed socket message to the client according to the first function.
[0135] S2048. 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.
[0136] In the above embodiments, data communication can be performed through the communication channel established by allocating a second thread to the socket message of the server. Furthermore, it is possible to enable the socket message of the client and the socket message of the server to use different threads for communication. Thus, by combining multi-threading technology, the socket messages of the client and the server can be processed in parallel, improving the communication efficiency. At this time, at least one first thread is allocated to the socket message of the client through a thread pool, and at least one second thread is allocated to the socket message of the server, thereby implementing one-to-one, one-to-many, many-to-one, and many-to-many communication topology relationships 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 the socket message of the server, then before detecting the socket message in the target queue, the steps described in S2049 below can also be executed:
[0138] S2049: Call a second function to obtain the socket message of the server and save the socket message of the server to the target queue.
[0139] In one example, the second function can be used to obtain the socket message from the socket of the server, that is, the Router socket, so that the socket message can be transmitted in a timely manner, avoiding affecting the communication efficiency. For example, the second function can be named the Callback function.
[0140] In one example, when completing the complete communication process between the client and the server according to the process described in S2041~S2049 above, it can be executed in the following order:
[0141] S2041: Allocate a first thread to the socket message of the client according to the message processing type.
[0142] S2042: Send the socket message to the first communication middleware through the communication channel established by the first thread.
[0143] Among them, the first communication middleware is used to send the socket message of the client to the second communication middleware; at this time, the first communication middleware can encapsulate the socket message of the client to obtain a message that the second communication middleware can recognize and process. After receiving the encapsulated socket message sent by the first communication middleware, the second communication middleware parses and processes the encapsulated socket message and sends the socket message of the client to the server. After receiving the socket message of the client, the server can process the socket message of the client to obtain response data corresponding to the socket message of the client.
[0144] S2049. Call the second function to obtain the socket message of the server, and save the socket message of the server to 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 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.
[0151] Further, 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 to 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 is a flowchart of a communication method provided by an embodiment of the present application Figure 3 , as Figure 4 shown, the method includes:
[0152] S401. In response to a data communication request initiated by a client, determine the request type corresponding to the data communication request.
[0153] In one example, the embodiment of the present application can provide multiple communication APIs. At this time, 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, and has stronger scalability.
[0154] In one example, the request type corresponding to the data communication request can be a synchronous call type or an asynchronous call type. The request type corresponding to the data communication request is not limited here, and it is subject to actual needs.
[0155] S402. Create a corresponding call thread according to the request type, and store the socket message corresponding to the data communication request in the target queue according to the communication channel established by the corresponding call thread.
[0156] In one example, if the request type is a synchronous call type, create a synchronous call thread according to the request type; if the request type is an asynchronous call type, create an asynchronous call thread according to the request type.
[0157] At this time, after creating the corresponding call thread, 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 call thread.
[0158] For example, if the created call thread is a synchronous call 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 synchronous call thread and receiving the response data returned by the first function, end the synchronous call thread.
[0159] If the created call thread is an asynchronous call 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 call thread, return immediately and end the asynchronous call thread.
[0160] In a possible implementation, since there may be a scenario where one server corresponds to multiple clients and multiple clients communicate with the server simultaneously, 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 call 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 call thread; after storing the socket message in the target queue, release the mutex lock.
[0161] This implementation can enable multiple clients or a single client to use the same socket to transmit socket messages through the mutex lock, saving resources while avoiding socket usage conflicts, thus ensuring smooth communication.
[0162] In one example, see Figure 5 , Figure 5 which is a schematic diagram of a communication process for synchronous call provided by an embodiment of the present application. As Figure 5 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 in the target queue.
[0163] After that, the target thread can be started. When it is determined that the message processing type of the socket message included in the target queue is the send message type according to the communication channel established by the target thread, a first thread can be allocated for the socket message through the thread pool, and the socket message can be sent to the first communication middleware according to the communication channel established by the first thread.
[0164] Next, the first communication middleware can send the client's socket message to the corresponding second communication middleware of 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 processes it according to the client's socket. After processing, response data corresponding to the client's socket message is obtained. At this time, the response data can be transmitted to the client through the server's socket.
[0166] At this time, the server's socket message can be obtained according to the second function first, and the server's socket message (that is, the response data) can be saved to the target queue.
[0167] After that, according to the message processing type of the socket message, a second thread can be allocated for the server's socket message through the thread pool, 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 time, according to the second communication middleware, the response data indicated by the server's socket message can be sent to the corresponding first communication middleware of the client through Ethernet. After that, the response data in the first communication middleware can be returned to the API according to the first function, and then the response data can be returned to the client through the communication channel established by the synchronous call thread, completing the communication between the client and the server.
[0169] In one example, refer to Figure 6 , Figure 6 which is a schematic diagram of a communication process for asynchronous call provided by an embodiment of the present application. As Figure 6 shown, in an asynchronous call scenario, the client can initiate a data communication request by calling the corresponding API through the communication channel established by the asynchronous call thread, and save the socket message corresponding to the data communication request to the target queue. At this time, the asynchronous call thread returns.
[0170] After that, the target thread can be started. When it is determined that the message processing type of the socket message included in the target queue is the send message type according to the communication channel established by the target thread, a first thread can be allocated for the socket message through the thread pool, and the socket message can be sent to the first communication middleware according to the communication channel established by the first thread.
[0171] Next, the first communication middleware can send the socket message of the client to the corresponding second communication middleware of the server through Ethernet.
[0172] The second communication middleware can send the received socket message of the client to the server through the API, so that the server processes according to the socket of the client. After processing, response data corresponding to the socket message of the client is obtained. At this time, the response data can be transmitted to the client through the socket of the server.
[0173] At this time, the socket message of the server can be obtained according to the second function first, and the socket message of the server (that is, the response data) can be saved to the target queue.
[0174] After that, according to the message processing type of the socket message, a second thread can be allocated for the socket message of the server through the thread pool, and the response data indicated by the socket message of the server can be sent to the second communication middleware according to the communication channel established by the second thread.
[0175] At this time, according to the second communication middleware, the response data indicated by the socket message of the server can be sent to the corresponding first communication middleware of the client through Ethernet. After that, the response data in the first communication middleware can be returned to the API according to the first function. At this time, according to the communication channel established by the asynchronous call thread, the third function can be called to return the response data to the client, completing the communication between the client and the server.
[0176] See Figure 7 , Figure 7 which is a schematic structural diagram of a communication device provided by an embodiment of the present application. As Figure 7 shown, the communication device 70 provided by the embodiment of the present application includes:
[0177] A detection module 701, configured to detect a socket message in the target queue; wherein, the socket message is a socket message of the client or a socket message of the server.
[0178] A determination module 702, configured to determine the message processing type corresponding to the socket message if a socket message is detected.
[0179] An allocation module 703, configured to allocate a corresponding processing thread for each socket message according to the message processing type.
[0180] A communication module 704, configured to communicate according to the communication channel established by the processing thread and the socket message.
[0181] See Figure 8 , Figure 8 FIG. Figure 8 is a schematic structural diagram of another communication device provided by an embodiment of the present application. As
[0182] shown, a communication device 80 provided by an embodiment of the present application includes:
[0183] A detection module 801, configured to detect socket messages in a target queue; wherein, the socket messages are socket messages of a client or socket messages of a server.
[0184] A determination module 802, configured to determine the message processing type corresponding to the socket message if a socket message is detected.
[0185] An allocation module 803, configured to allocate a corresponding processing thread for each socket message according to the message processing type.
[0186] A communication module 804, configured to communicate according to the communication channel established by the processing thread and the socket message.
[0187] In a possible implementation manner, the socket message is a socket message of a client; the message processing type indicates the message sending type;
[0188] At this time, the allocation module 803 includes a first allocation module 8031, configured to:
[0189] Allocate a first thread for the socket message of the client according to the message processing type;
[0190] The communication module 804 includes a first communication module 8041, configured to:
[0191] Send the socket message to a first communication middleware according to the communication channel established by the first thread; wherein, the first communication middleware is configured to send the socket message of the client to a second communication middleware; the second communication middleware is configured to send the socket message of the client to the server; the server is configured to process the socket message of the client and obtain response data corresponding to the socket message of the client;
[0192] Call a first function to obtain the response data received in the first communication middleware;The communication channel established according to the calling thread of the client is used to return the response data 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 manner, the socket message is the socket message of the server; the message processing type indicates the received message type;
[0194] At this time, the allocation module 803 includes a second allocation module 8032 for:
[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 the response data;
[0196] The communication module 804 includes a second communication module 8042 for:
[0197] Sending the socket message of the server to the second communication middleware according to the communication channel established by the second thread;
[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 a possible implementation manner, the device is further used for:
[0201] Before detecting the socket message in the target queue, calling a second function to obtain the socket message of the server and saving the socket message of the server to the target queue.
[0202] In a possible implementation manner, the detection module 801 is used for:
[0203] Starting the target thread according to the preset thread start requirement;
[0204] Detecting the socket message in the target queue according to the communication channel established by the target thread.
[0205] In a possible implementation manner, the device further includes a response module 805 for:
[0206] 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;
[0207] Creating a corresponding calling thread according to the request type and storing the socket message corresponding to the data communication request to the target queue according to the communication channel established by the corresponding calling thread.
[0208] In a possible implementation, the number of clients is multiple. At this time, the response module 805 is used for:
[0209] Adding a mutex lock to the socket used for the data communication request according to the communication channel established by the corresponding calling thread;
[0210] After storing the socket message in the target queue, releasing the mutex lock.
[0211] The communication device provided by the embodiment of the present application can be used to execute the method provided by the above method embodiment. The implementation principle and technical effect are similar, and will not be elaborated here in this embodiment.
[0212] See Figure 9 , Figure 9 which is a schematic structural diagram of an electronic device provided by an embodiment of the present application. As Figure 9 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. Among them, the processor 901, the memory 902, and the communication component 903 are connected through a bus 904.
[0213] In the specific implementation process, at least one processor 901 executes the computer execution instructions stored in the memory 902, so that at least one processor 901 executes the above method.
[0214] For the specific implementation process of the processor 901, reference can be made to the above method embodiment. The implementation principle and technical effect are similar, and will not be elaborated here in this embodiment.
[0215] In the above embodiment, it should be understood that the processor may be a central processing unit (English: Central Processing Unit, abbreviated as: CPU), or may also be other general-purpose processors, digital signal processors (English: Digital Signal Processor, abbreviated as: DSP), application specific integrated circuits (English: Application Specific Integrated Circuit, abbreviated as: ASIC), etc. The general-purpose processor may be a microprocessor or the processor may also be any conventional processor, etc. The steps of the method disclosed in combination with the invention can be directly implemented by the execution of the hardware processor, or can be implemented by the combination of the hardware and software modules in the processor.
[0216] The memory may include a high-speed memory (Random Access Memory, RAM), and may also include a non-volatile memory (Non-volatile Memory, NVM), such as at least one disk memory.
[0217] The bus can be an Industry Standard Architecture (ISA) bus, a Peripheral Component Interconnect (PCI) bus, an Extended Industry Standard Architecture (EISA) bus, or the like. The bus can be divided into an address bus, a data bus, a control bus, etc. For ease of representation, the buses in the drawings of this application are not limited to only one bus or one type of bus.
[0218] This application also provides a computer program product, including a computer program, which implements the above method when executed by a processor.
[0219] This application also provides a computer-readable storage medium, in which computer-executable instructions are stored. When the processor executes the computer-executable instructions, the above method is implemented.
[0220] The above-readable storage medium can be implemented by any type of volatile or non-volatile storage 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 memory, flash memory, a magnetic disk, or an optical disk. The readable storage medium can be any available medium accessible by a general-purpose or special-purpose computer.
[0221] An exemplary readable storage medium is coupled to the processor, enabling the processor to read information from the readable storage medium and write information to the readable storage medium. Of course, the readable storage medium can also be a component 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 as discrete components in a device.
[0222] The division of units is only a logical function division. In actual implementation, there can be other division methods. For example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the displayed or discussed coupling or direct coupling or communication connection between each other can be through some interfaces, and the indirect coupling or communication connection of devices or units can be in an electrical, mechanical, or other form.
[0223] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed over multiple network units. Some or all of the units can be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0224] In addition, in each embodiment of the present invention, each functional unit may be integrated in a processing unit, may exist physically separately for each unit, or two or more units may be integrated in one unit.
[0225] If the function is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on such an understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art, or a part of this 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 causing an electronic device (which may be a personal computer, a server, or a network device, etc.) to execute all or part of the steps of the methods in each embodiment of the present invention. The aforementioned storage medium includes: various media such as USB flash drives, mobile hard disks, read-only memories (ROM, Read-Only Memory), random access memories (RAM, Random Access Memory), magnetic disks, or optical discs that can store program codes.
[0226] Those of ordinary skill in the art can understand that all or part of the steps of implementing the above method embodiments can be completed by hardware related to program instructions. The aforementioned program can be stored in a computer-readable storage medium. When the program is executed, it executes the steps including the above method embodiments; and the aforementioned storage medium includes: various media such as ROM, RAM, magnetic disks, or optical discs that can store program codes.
[0227] Finally, it should be noted that: After considering the specification and practicing the invention disclosed herein, those skilled in the art will easily think of other implementation schemes of the present invention. The present invention aims to cover any variations, uses, or adaptive changes of the present invention. These variations, uses, or adaptive changes follow the general principles of the present invention and include common general knowledge or conventional technical means in the technical field not disclosed in the present invention. It is not limited to the exact structures described above and shown in the drawings, and various modifications and changes can be made without departing from its scope. The scope of the present invention is only limited by the appended claims.
Claims
1. A communication method, characterized in that, The method includes: Detecting socket messages in a target queue; wherein, the socket messages are socket messages of a client or socket messages of a server; If it is detected that there are socket messages, determining the message processing type corresponding to the socket messages; According to the message processing type, allocating corresponding processing threads for each of the socket messages, and communicating with the socket messages according to the communication channels established by the processing threads.
2. The method according to claim 1, wherein The socket messages are socket messages of a client; the message processing type is used to indicate the message sending type; according to the message processing type, allocating corresponding processing threads for each of the socket messages, and communicating with the socket messages according to the communication channels established by the processing threads, includes: Allocating a first thread for the socket messages of the client according to the message processing type; Sending the socket messages to a first communication middleware according to the communication channel established by the first thread; wherein, the first communication middleware is used to send the socket messages of the client to a second communication middleware; the second communication middleware is used to send the socket messages of the client to the server; the server is used to process the socket messages of the client to obtain response data corresponding to the socket messages of the client; Invoking a first function to obtain the response data received in the first communication middleware; Returning the response data to the client according to the communication channel established by the calling thread of 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, wherein The socket messages are socket messages of a server; the message processing type indicates the message receiving type; according to the message processing type, allocating corresponding processing threads for each of the socket messages, and communicating with the socket messages according to the communication channels established by the processing threads, includes: Allocating a second thread for the socket messages of the server according to the message processing type; wherein, the socket messages of the server indicate the response data; Sending the socket messages of the server to the second communication middleware according to the communication channel established by the second thread; Sending the response data indicated by the socket messages of the server to the first communication middleware according to the second 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, wherein Before detecting the socket messages in the target queue, the method further includes: Invoking a second function to obtain the socket messages of the server and saving the socket messages of the server to the target queue.
5. The method according to claim 1, wherein Detecting the socket messages in the target queue includes: Starting a target thread according to a preset thread start requirement; Detecting the socket messages in the target queue according to the communication channel established by the target thread.
6. The method according to any one of claims 1-5, characterized in that, Before detecting the socket messages in the target queue, the method further includes: In response to a data communication request initiated by the client, determine the corresponding request type of the data communication request; Create a corresponding calling thread according to the request type, and store the socket message corresponding to the data communication request 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, The number of clients is multiple. 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: Add a mutex lock to the socket used for the data communication request according to the communication channel established by the corresponding calling thread; After storing the socket message in the target queue, release the mutex lock.
8. A communication device, characterized in that, Includes: A detection module for detecting socket messages in the target queue; wherein, the socket message is a socket message of the client or a socket message of the server; A determination module for determining the message processing type corresponding to the socket message if a socket message is detected; An allocation module for allocating a corresponding processing thread to each socket message according to the message processing type; A communication module for communicating according to the communication channel established by the processing thread and the socket message.
9. An electronic device, characterized in that, Includes: A memory, a processor; The memory stores computer execution instructions; The processor executes the computer execution instructions stored in the memory, so that the processor executes the method according to any one of claims 1-7.
10. A computer-readable storage medium, characterized in that, Computer-executable instructions are stored in the computer-readable storage medium, and when the computer-executable instructions are executed by a processor, they are used to implement the method according to any one of claims 1-7.
11. A computer program product, characterized in that, Includes a computer program, and when the computer program is executed by a processor, it implements the method according to any one of claims 1-7.
Citation Information
Patent Citations
JAVA-based network transmission protocol conversion middleware
CN104917814A
Distributed storage system request processing method and device, equipment and medium
CN111309478A
Method and system for realizing synchronous and asynchronous calling among modules based on domain sockets
CN114979233A
Data processing method and device, electronic equipment and storage medium
CN116088853A
Monitoring system and method based on vehicle-mounted service guide architecture, and vehicle
CN116431365A