Disclosure of Invention
The invention provides a communication mode realization method and electronic equipment, which are used for solving or partially solving the technical problem that one-to-many communication cannot be realized under an OSX system at present.
In order to solve the above technical problem, the present invention provides a method for implementing a communication mode, where the method includes:
registering a virtual service name and establishing a monitoring interface of a microkernel;
sending the monitoring interface to a plurality of clients based on the acquisition requests of the plurality of clients, so that the plurality of clients send respective connection requests to the monitoring interface based on the monitoring interface;
receiving respective connection requests sent by the plurality of clients;
and correspondingly creating a microkernel message session instance for each connection request by utilizing the thread of the microkernel information server, generating a connection response message of each microkernel message session instance and sending the connection response message to the client corresponding to each connection request.
Preferably, the registering a virtual service name and creating a monitoring interface of the microkernel Mach includes:
and calling a bootstrap _ check _ in function of the OSX system, registering the character string corresponding to the virtual service name in the OSX system, and acquiring the monitoring interface.
Preferably, the receiving the respective connection requests sent by the multiple clients specifically includes:
and calling a macch _ msg system function by using a thread of the microkernel information server to receive a connection request from each client, wherein the connection request of each client carries a port name of each client.
Preferably, the correspondingly creating a microkernel message session instance for each connection request by using the thread of the microkernel information server specifically includes:
creating a MachMessageSession session instance for each connection request by calling an allocSession function of the microkernel information server;
calling a createFrom method of the MachMessageSession to initialize each MachMessageSession session instance, so that each MachMessageSession session instance comprises two parameters: port name of corresponding client, local port name.
Preferably, the sending the connection response message for generating each microkernel message session instance to the client corresponding to each connection request includes:
and packaging the local port name of each MachMessageSession session instance into each connection response message, and sending the local port name to the port of the corresponding client.
Preferably, after the connection response message for generating each microkernel message session instance is sent to the client corresponding to each connection request, the method further includes:
and putting each MachMessageSession session instance into an m _ sessions list for management.
Preferably, after each mach messagesession instance is put into the m _ sessions list for management, the method further includes:
and reading all MachMessageSession in the m _ sessions list in the recvSessionMsg function so as to obtain the communication message sent by the corresponding client.
The invention discloses an electronic device, comprising:
the registration module is used for registering a virtual service name and establishing a monitoring interface of the microkernel;
the sending module is used for sending the monitoring interface to a plurality of clients based on the acquisition requests of the clients, so that the clients send respective connection requests to the monitoring interface based on the monitoring interface;
a receiving module, configured to receive respective connection requests sent by the multiple clients;
and the creating module is used for correspondingly creating a microkernel message session example for each connection request by utilizing the thread of the microkernel information server, generating a connection response message of each microkernel message session example and sending the connection response message to the client corresponding to each connection request.
The invention discloses a computer-readable storage medium, on which a computer program is stored which, when being executed by a processor, carries out the steps of the above-mentioned method.
The invention discloses a computer device, comprising a memory, a processor and a computer program stored on the memory and capable of running on the processor, wherein the processor realizes the steps of the method when executing the program.
Through one or more technical schemes of the invention, the invention has the following beneficial effects or advantages:
the invention discloses a method for realizing a communication mode and electronic equipment, wherein the method comprises the following steps: registering a virtual service name and establishing a monitoring interface of a microkernel; the method comprises the steps that a monitoring interface is sent to a plurality of clients based on acquisition requests of the clients, so that the clients send respective connection requests to the monitoring interface based on the monitoring interface; then receiving respective connection requests sent by a plurality of clients, correspondingly creating a microkernel message session example for each connection request by using the thread of the microkernel information server, generating a connection response message of each microkernel message session example and sending the connection response message to the corresponding client. Therefore, the monitoring interface is established by registering a virtual service name, and the microkernel message session instance is correspondingly established for each connection request by using the thread of the microkernel information server to communicate with the respective client, so that the communication in a one-to-many mode can be realized with a plurality of clients by using one monitoring interface.
Detailed Description
In order to make the present application more clearly understood by those skilled in the art to which the present application pertains, the following detailed description of the present application is made with reference to the accompanying drawings by way of specific embodiments.
In the embodiment of the invention, the Mach microkernel concept of the OSX system is adopted. A one-to-many mode is realized by registering a virtual service name in a Mach micro-kernel and establishing a monitoring interface of the micro-kernel, and communicating with a plurality of clients by utilizing the monitoring interface. In a specific implementation process, firstly, the monitoring interface is sent to a plurality of clients based on acquisition requests of the plurality of clients, so that the plurality of clients send respective connection requests to the monitoring interface based on the monitoring interface; then receiving respective connection requests sent by the plurality of clients, correspondingly creating a microkernel message session example for each connection request by using the thread of the microkernel information server, generating a connection response message of each microkernel message session example, and sending the connection response message to the corresponding client. Therefore, the monitoring interface is established by registering a virtual service name, and the microkernel message session instance is correspondingly established for each connection request by using the thread of the microkernel information server to communicate with the respective client, so that the communication in a one-to-many mode can be realized with a plurality of clients by using one monitoring interface.
Mach microkernels are described below.
The OSX system is based on the Mach microkernel, in which all communication is based on the Mach Message, i.e., the Mach Message. In the Mach message, the most important concept is the concept of a Mach port, i.e., a Mach port. In the OSX system, messages are transferred from one Mach port to another.
Therefore, according to the OSX feature, the inter-process communication is realized by sending the message to the Mach port of the process B through the Mach port of the process a. The bottom layer of the CFMessagePort of the OSX system depends on the Mach port. Therefore, the method is a method for realizing one-to-many interprocess communication based on the Mach port according to the self-defined service name. At the beginning of the communication process, the server only needs to register a virtual (i.e. any non-existent) service name, and the client can establish a connection. Thereby enabling interprocess communication.
In the process of transmitting and receiving the Mach message, in the OSX, the message is received and transmitted through the macch _ msg. The first parameter of the function is a pointer to the map _ MSG _ header _ t, which indicates a message to be transmitted or received, the second parameter indicates whether a message is to be received or transmitted, which indicates a message to be transmitted when the value is MACH _ SEND _ MSG, which indicates a message to be received when the value is MACH _ RCV _ MSG, the third parameter indicates the size of data to be transmitted, and the fourth parameter indicates the size of data to be received. In this case, the message is received and transmitted by using the map _ msg according to the above method, and therefore, the detailed description thereof is omitted.
The macchmsg header t structure, which is a very important structure, is relied upon for the transmission and reception of any Mach message. The msgh _ size of the structure indicates the size of data to be transmitted or received, and msgh _ remote _ port indicates a remote port to which the field value must be set when a message is transmitted; msgh _ local _ port denotes a local port, and this field value must be set when a message is received.
The technical scheme has the following names:
the MachMessageServer, which represents a server for Mach communication, is used for processing a connection request from a MachMessageClient client through a specific service name character, and creating a MachMessageSession object representing a connection with the MachMessageClient.
The MachMessageSession represents a session device generated by a MachMessageServer class, and when the MachMessageServer detects a connection request from a MachMessageClient, a MachMessageSession instance is created, and then data communication is performed between the MachMessageSession instance and the MachMessageClient. A one-to-many concept is implemented here. One MachMessageServer instance communicates with each MachMessageClient through the MachMessageSession instance by creating multiple MachMessageSession instances.
And the MachMessageClient represents a client of Mach communication, sends a connection request to the MachMessageServer through a specific service name character, waits for the response of the request from the MachMessageServer, establishes a communication channel with the MachMessageServer if the request passes, and fails if the request is rejected.
The structure of the MachMessageConnect is a message used for establishing a connection request and sent by the MachMessageClient to the MachMessageServer in the case. The first field in this message is the header field of type macchmsg _ header _ t (any Mach message must include the header structure of this macchmsg _ header _ t and a string named portName, which represents the local Mach port name of the MachMessageClient.
The structure of the MachMessageConnectResult is a result message sent by the MachMessageServer to the MachMessageClient in this case for answering the connection request. Also the first field is the header field of the macchmsg _ header _ t, an integer field named sessionId, and a string named portName, which represents the remote Mach port name with which the Mach messageclient communicates.
With the above description of the mach messageserver, the mach messagesession, and the mach messageclient, how to establish a connection and receive data with each other will be described below.
Referring to fig. 1, in the implementation process of the present invention, a method for implementing a communication mode is disclosed, the method includes:
step 11, register a virtual service name, and create a monitoring interface of the microkernel.
In a specific implementation process, a bootstrap _ check _ in function of the OSX system is called, a character string corresponding to the virtual service name is registered in the OSX system, and the monitoring interface is obtained.
Further, the mach messageserver calls a bootstrap _ check _ in function of the OSX system, registers a character string with a service name of 'XXX' in the system, judges a return value of the function, if the return value of the function is 0, the registration service is successful, acquires a monitoring interface with a name of m _ listenPort, and proceeds to the following step 2). If the return value is not equal to 0, the registration service fails and the system is directly exited.
And step 12, sending the monitoring interface to a plurality of clients based on the acquisition requests of the plurality of clients, so that the plurality of clients send respective connection requests to the monitoring interface based on the monitoring interface.
And step 13, receiving respective connection requests sent by the plurality of clients.
Specifically, the embodiment of the invention calls and calls the map _ msg system function by using the thread of the microkernel information server to receive the connection request from each client, wherein the connection request of each client carries the port name of each client.
And step 14, correspondingly creating a microkernel message session instance for each connection request by using the thread of the microkernel information server, generating a connection response message of each microkernel message session instance, and sending the connection response message to the client corresponding to each connection request.
In the specific implementation process, a respective MachMessageSession session instance is created for each connection request mainly by calling an allocSession function of a microkernel information server; then, a createFrom method of the MachMessageSession is called to initialize each MachMessageSession session instance, so that each MachMessageSession session instance comprises two parameters: port name of corresponding client, local port name.
Further, the local port name of each mach messagesession instance is encapsulated into each connection response message, and is sent to the port of the corresponding client.
As an optional implementation manner, after the connection response message for generating each microkernel message session instance is sent to the corresponding client, each mach messagesession instance is also put into the m _ sessions list for management. Then, when the server and the client are in formal communication, all the mach messagesessions in the m _ sessions list are read in the recvsensingmsg function, so as to obtain the communication message sent by the corresponding client.
The above is the way in which the present invention establishes each mach messagesession instance for each client to communicate with the corresponding client. For ease of explanation and explanation of the present invention, the following embodiments will be described in terms of establishing communication between an instance of a mach messagesession and a client.
1) Registering service name and creating Mach monitoring interface
Calling a bootstrap _ check _ in function of an OSX system, registering a character string with a service name XXX in the system, judging a return value of the function, if the return value of the function is 0, representing that the registration service is successful, acquiring a monitoring interface with a name of m _ listenPort, and entering the following step 2). If the return value is not equal to 0, the registration service fails and the system is directly exited.
2) Creating a thread loop for a MachMessageServer
And after the calling in the last step is successful, creating a thread to circularly process the connection request from the client, and receiving the message of the MachMessageClient corresponding to each MachMessageSession. In the thread loop, the connection request from the client is processed by calling the listenPortLoop function of the MachMessageServer, and the message from the client is received by recvSessionMsg
3) Processing connection requests by listenPortLoop
In order to prevent the function from blocking a thread loop by calling the macch _ msg system function to receive a connection request from a client, a timeout time of 1 millisecond is set when the function is called. If the return value of the function is MACH _ MSG _ SUCCESS, it indicates that a Mach message was successfully received. Then, whether the msgh _ id field in the map _ msg _ header _ t structure of the message structure is equal to the MMT _ CONNECT value or not is judged, and if yes, the message is a connection request message with the type of MachMessageConnect. Proceeding to the following step a)
a) Creating a MachMessageSession
A MachMessageSession instance is created by calling the allocSession function of the MachMessageServer and the createFrom method of the MachMessageSession is called to initialize the session, which has two particularly important parameters, one is remotePortName, which represents the port name of the MachMessageClient, and one is localPortName, which represents the local port name of the session. When sending a MachMessageConnect connection request message to a MachMessageServer, a client carries a portName string, which is a remotePortName parameter required by createFrom, and localPortName is represented by generating a random GUID string. Inside the createFrom function, the remote port represented by remotePortName is obtained by calling bootstrap _ look _ up, labeled m _ remotePort, and a local port, labeled m _ localPort, is created by calling bootstrap _ check _ in. After completion, the MachMessageSession may send a message to the m _ remotePort and receive the message from the MachMessageClient through the m _ localPort.
b) Returning a connection request result message to the client
After createFrom in the above step is completed, a mach messagesession is successfully created to communicate with the mach messageclient. At this time, a message for connecting the messagemessageconnectioncorresult, which contains the localeportname string created in step a), must be sent to the mach messageclient. The MachMessageClient sends a message to the MachMessageSession through the port corresponding to the localPortName.
c) And putting the created MachMessageSession into an m _ sessions list for management.
4) recvSessionMsg receives client messages
And reading all MachMessageSession in the m _ sessions list in the recvSessionMsg function, namely calling a read method of the MachMessageSession to acquire the communication message from the MachMessageClient.
Client logic of MachMessageClient
The above describes how the mach messageserver listens for a connection request from the mach messageclient, creates a corresponding mach messagesession for data communication with the mach messageclient, and sends a mach messageconnectresult connection result message to the Client. The following mainly describes the main steps of MachMessageClient initiating connection request to Server
1) Obtaining service name port registered by MachMessageServer
Acquiring a monitoring interface with name XXX registered by the Server end by calling a bootstrap _ look _ up function, and if the return value of the function is equal to 0, entering the following step 2)
2) Creating a random guid string and creating a Mach Port through the string
And creating a random guid character string, calling bootstrap _ check _ in to acquire a port corresponding to the guid character end, and receiving the message by the Client depending on the port.
3) Sending a MachMessageConnect connection request message
Filling the guid character string in the step 2) into a portName field of the MachMessageConnect, and calling a macch _ msg function to send a connection request message to the Server.
4) Waiting for a connection request result
After the MachMessageConnect message is sent out, the machMessageConnectResult connection result message from the Server is received by calling the macch _ msg function. And then acquiring a portName field in a MachMessageConnectResult structure, and calling a bootstrap _ loop _ up function to acquire a remote Mach port corresponding to the portName, so that the Client can send a message to the MachMessageSession through the port.
By introducing the connection request and response to the MachMessageServer and the MachMessageclient, the method for performing one-to-many interprocess communication by using the Mach port is realized. When the communication process starts, the server only needs to register an arbitrary non-existent service name, and each client can establish connection. Thereby enabling interprocess communication.
Based on the same inventive concept, the embodiment of the present invention further discloses an electronic device, referring to fig. 2, including:
the registration module 21 is used for registering a virtual service name and establishing a monitoring interface of the microkernel;
a sending module 22, configured to send the monitoring interface to multiple clients based on acquisition requests of the multiple clients, so that the multiple clients send respective connection requests to the monitoring interface based on the monitoring interface;
a receiving module 23, configured to receive respective connection requests sent by the multiple clients;
the creating module 24 is configured to correspondingly create a microkernel message session instance for each connection request by using a thread of the microkernel information server, generate a connection response message of each microkernel message session instance, and send the connection response message to a client corresponding to each connection request.
Based on the same inventive concept as in the previous embodiments, the present invention also provides a computer-readable storage medium, on which a computer program is stored, which when executed by a processor implements the steps of any of the methods described above.
Based on the same inventive concept as in the previous embodiments, the present invention also provides a computer device comprising a memory, a processor and a computer program stored on the memory and executable on the processor, the processor implementing the steps of any of the methods described above when executing the program.
Through one or more embodiments of the present invention, the present invention has the following advantageous effects or advantages:
the invention discloses a method for realizing a communication mode and electronic equipment, wherein a virtual service name is registered first, and a monitoring interface of a microkernel is established; the method comprises the steps that a monitoring interface is sent to a plurality of clients based on acquisition requests of the clients, so that the clients send respective connection requests to the monitoring interface based on the monitoring interface; then receiving respective connection requests sent by a plurality of clients, correspondingly creating a microkernel message session example for each connection request by using the thread of the microkernel information server, generating a connection response message of each microkernel message session example and sending the connection response message to the corresponding client. Therefore, the monitoring interface is established by registering a virtual service name, and the microkernel message session instance is correspondingly established for each connection request by using the thread of the microkernel information server to communicate with the respective client, so that the communication in a one-to-many mode can be realized with a plurality of clients by using one monitoring interface.
While the preferred embodiments of the present application have been described, additional variations and modifications in those embodiments may occur to those skilled in the art once they learn of the basic inventive concepts. Therefore, it is intended that the appended claims be interpreted as including preferred embodiments and all alterations and modifications as fall within the scope of the application.
It will be apparent to those skilled in the art that various changes and modifications may be made in the present application without departing from the spirit and scope of the application. Thus, if such modifications and variations of the present application fall within the scope of the claims of the present application and their equivalents, the present application is intended to include such modifications and variations as well.