Summary of the invention
Main purpose of the present invention is the implementation method providing a kind of asynchronous communication, and it uses a kind of asynchronous communication assembly, uses various agreement to carry out data communication.
The present invention is directed to that prior art problem mainly solved by following technical proposals, a kind of implementation method of asynchronous communication, comprising:
(A) between application A and application B, arrange asynchronous communication assembly, asynchronous communication assembly is divided into management end and agent side two parts, and management end is placed on the application A of initiatively transmission, and agent side is placed on the application B of passive reception;
(B) asynchronous communication is carried out by protocol stack between management end and agent side.
Most of communications protocol is the model work based on request, response, also there are two kinds of roles of request, response in the asynchronous communication assembly of this method design, realized by management end and agent side two part, it is upper with on the application B of passive reception that management end and agent side are placed on the application A initiatively sent respectively.
In concurrent design, event DeMux can be separated with its protocol handling code with connection management, uses the code prototype of agreement to adopt synchronous mode realization event DeMux and connection management.This method adopts a kind of asynchronous mode to achieve event DeMux and connection management, and using this part code as component reusing technology, use multiple application protocol, the agreement API met the demands can be sent by fast open, thus realize using various agreement to carry out data communication by protocol stack between management end and agent side.
As preferably, management end comprise an asynchronous transmission interface, transmit queue, receiving queue, one group send and receiving thread, management end buffer area, one group of worker thread and a monitor thread; Agent side comprises a receiving queue, transmit queue, one group receives and send thread, one group of worker thread, an agent side buffer area, a Chong Bao detection zone and one asynchronously returns interface.
As preferably, application A constantly calls asynchronous transmission interface and various request message is sent to agent side, wherein the request message of a type associates a management end call back function, after receiving return messages from agent side, worker thread carries out DeMux to various return messages, and call different management end call back functions, what complete message returns process, monitor thread is responsible for monitoring the message time-out that various abnormal conditions cause, and carries out message retransmission.
As preferably, after agent side receives various request message, wherein the request message of a type is associated with an agent side call back function, agent side calls different agent side call back functions, under synchronous mode, agent side call back function directly completes the process of message and returns, and under asynchronous mode, message is put into the Message Processing queue of application B by agent side call back function.
As preferably, the transmission flow of management end comprises:
1) apply A to call asynchronous transmission interface with non-blocking fashion and send a request message, and request message is put into management end transmit queue;
2) management end sends thread and take out request message from transmit queue, request message is put into management end buffer area with the management end call back function associated, and is sent by request message;
The reception flow process of described agent side comprises:
3) agent side receiving thread receives request message, and puts into agent side receiving queue;
4) agent side worker thread takes out message from receiving queue, their agent side call back function is called according to type of message, if simultaneous operation, Message Processing is completed in call back function, return to management end immediately, if asynchronous operation, call back function does not complete Message Processing, also management end is not returned immediately, but by request message, transmitting terminal address information stored in agent side buffer area, and obtain an agent side buffer area unique identification, then message is put into together with agent side buffer area unique identification the Message Processing queue of application B;
The Returning process of described agent side comprises:
5) apply B and obtain message from Message Processing queue, process, after being disposed, call that agent side is asynchronous returns interface;
6) the asynchronous interface that returns of agent side is according to agent side buffer area unique identification obtaining request message and context, and then return messages are put into agent side transmit queue by structure return messages;
7) agent side sends thread and take out return messages from transmit queue, and returns to management end;
The reception flow process of described management end comprises:
8) management end receiving thread receives return messages, puts into management end receiving queue;
9) management end worker thread takes out return messages from receiving queue, management end worker thread is according to the unique identification of return messages, request message before finding in management end buffering area and call back function, then inform to corresponding management end call back function by return messages, request message;
10) management end call back function completes the process of return messages.
As preferably, monitor thread carries out intermittent scanning to whole buffer area, the situation of message time-out in monitoring management end buffer area, and each request message has a time-out time and maximum number of retransmissions, if find time-out bag, will retransmit; When repeating transmission exceedes maximum times, management end call back function will be activated, notice application A request message time-out.There is a monitor thread at management end, intermittent scanning is carried out to whole buffer area, the situation of message time-out in monitoring management end buffer area, if find time-out bag, will retransmit, when repeating transmission exceedes maximum times, call back function will be activated, notice application A request message time-out.
As preferably, agent side is provided with heavily bag measuring ability, request message unique identification and return messages are associated, after transmission thread constructing return messages, return messages are stored in Chong Bao detection zone, if there is same uniquely identified request message to come, to not notify to apply B, directly return return messages.Request message unique identification and return messages associate by agent side, after transmission thread constructing return messages, return messages are stored, if there is same uniquely identified request message to come, to not notify to apply B, directly return return messages, doing so avoids the reprocessing of same message.
The beneficial effect that the present invention brings is, uses asynchronous communication assembly conveniently to develop the agreement API of asynchronous communication, realizes using various protocols to carry out data communication.
Embodiment
Below by embodiment, and by reference to the accompanying drawings, technical scheme of the present invention is further described in detail.
Embodiment 1: as shown in Figure 1, the present invention is a kind of implementation method of asynchronous communication,
Between application A 101 and application B105, asynchronous communication assembly is set, asynchronous communication assembly is divided into management end 102 and agent side 104 two parts, management end 102 is placed on the application A 101 of initiatively transmission, and agent side 104 is placed on the application B105 of passive reception; Management end 102 and agent side 104 carry out asynchronous communication by protocol stack 103.
Figure 2 shows that the flow chart of data processing figure of asynchronous communication assembly, as shown in the figure:
The transmission flow of management end 102:
1) apply A101 to call asynchronous transmission interface with non-blocking fashion and send a request message;
2) request message is put into management end 102 transmit queue by asynchronous transmission function;
3) management end 102 sends thread and take out request message from transmit queue, and request message is put into management end buffer area with the management end call back function associated, and then request message sends by invocation protocol stack 103 handling procedure;
The reception flow process of agent side 104:
4) agent side 104 receiving thread invocation protocol stack 103 handling procedure receives request message, and whether be retransmitted packet, be if detecting in agent side 104 Chong Bao detection zone, uses protocol stack 103 handling procedure that the return messages detected are sent to management end 102; Otherwise request message is put into agent side 104 receiving queue;
5) agent side 104 worker thread takes out message from receiving queue, calls their agent side call back function according to type of message; If simultaneous operation, in call back function, complete Message Processing, after structure return messages, return messages are stored in Chong Bao detection zone, return to management end 102 immediately; If asynchronous operation, call back function does not complete Message Processing, also management end is not returned immediately, but by information such as request message, transmitting terminal addresses stored in agent side 104 buffer area, and obtain an agent side buffer area unique identification, then message is put into together with agent side buffer area unique identification the Message Processing queue of application B105;
The Returning process of agent side 104:
6) apply B105 and obtain message from Message Processing queue, process.(just may be disposed through multiple thread) after being disposed, call that agent side 104 is asynchronous returns interface;
7) the asynchronous interface that returns of agent side 104 is according to agent side buffer area unique identification obtaining request message and context, and then return messages are put into agent side transmit queue by structure return messages;
8) agent side 104 sends thread and take out return messages from transmit queue, and after structure return messages, return messages are stored in Chong Bao detection zone, then invocation protocol stack 103 handling procedure returns to management end 102; The reception flow process of management end 102:
9) management end 102 receiving thread invocation protocol stack 103 handling procedure receives return messages, puts into management end 102 receiving queue;
10) management end 102 worker thread takes out return messages from receiving queue; Because return messages are identical with the message unique identity of request message, so management end 102 worker thread is according to the unique identification of return messages, request message before finding in management end 102 buffering area and call back function, then inform to corresponding management end 102 call back function by return messages, request message;
11) management end 102 call back function completes the process of return messages.
Communication part is to the process of abnormal conditions:
Each request message has a time-out time and maximum number of retransmissions.There is a monitor thread at management end 102, intermittent scanning is carried out to whole buffer area, the situation of message time-out in monitoring management end 102 buffer area, if find time-out bag, will retransmit, when repeating transmission exceedes maximum times, call back function will be activated, notice application A request message time-out;
Agent side 104 has heavily bag measuring ability, and request message unique identification and return messages associate by agent side 104, return messages is stored after transmission thread constructing return messages.If there is same uniquely identified request message to come, will not notifies to apply B105, directly return return messages.
Embodiment 2: be the embodiment in conjunction with compilation tool gSOAP and this method exploitation asynchronous communication SOAP API below.
GSOAP compilation tool can generate stake required for exploitation Soap agreement and framing program automatically.Stake is one " agency " being placed on client, and framework is one " agency " being placed on server end.State a function in stake, but content is wherein not concrete algorithm, but the agreement that a series of network connects, automatically remove the main frame at search framework place, then with its swap data, and obtain a series of codes of return value.Now the process of client only needs to adjust the function in stake, can realize the exchanges data with far-end server, so just ensure that the transparency of transfer of data for application process.
The handling process of the stake code that gSOAP generates is:
1. initialization running environment example;
2. construct request message head and entity;
3. connect and complete message and send;
4. wait-receiving mode is receiveed the response;
5. resolve receive the response head and entity, acquisition returns results;
6. close and connect.
The framework using gSOAP to produce needs first in application program, to start listening port, initialization running environment example, and etc. to be connected.After a connection is come, the method that invocation framenort provides processes.The handling process of the skeleton code that gSOAP generates is:
1. receive request message;
2. analysis request message, obtaining request information, activates call back function;
3. call back function returns, and obtains return messages, and structure returns bag;
4. send and return bag.
Use asynchronous communication assembly of the present invention, in conjunction with the code of stake and framework, exploitation asynchronous communication SOAP API.This API comprises the management end part different with agent side two.Application A calls management end asynchronous transmission interface and sends Soap message to opposite end, is received the Soap message returned by management end call back function.
The handling process of management end is:
1. asynchronous transmission interface uses the handling process 1 initialization running environment example of stake code, puts into transmit queue by needing the information sent;
2. sending thread uses the handling process 2 of stake code to construct transmission bag, and transmission bag and call back function are put into management end buffer area, uses the handling process 3 of stake code message to be sent;
3. receiving thread uses the handling process 4 of stake code to receive return messages, uses the handling process 5 of stake code to resolve this return messages, and the message after resolving is put into receiving queue;
4. worker thread takes out message from receiving queue, finds and sends information context, and activate call back function in management end buffer area, and in call back function, API is by the full detail providing transmission and the full detail received;
5. the message sent in monitor thread monitor management end buffer area, if return time-out, will retransmit.Repeatedly repeat still without returning, then to activate call back function.In call back function, API will provide full detail and the timeout flag of transmission.
The receiving thread of agent side starts listening port, initialization running environment example, and etc. to be connected.When a connection comes then, the handling process of agent side is:
1. receiving thread uses the handling process 1 of skeleton code to receive request message, and whether be retransmitted packet, be if detecting in Chong Bao detection zone, use the handling process 4 of skeleton code to send return messages, otherwise request message puts into receiving queue;
2. worker thread uses the handling process 2 of skeleton code to process request message, and activates call back function;
3. request message is put into agent side buffer area by call back function, and proxy caching district can return a unique identification, and request message and proxy caching district unique identification are informed to application B by call back function;
4. apply B after processing request message, call and asynchronously return interface, use agent side buffer area unique identification from buffer area, find the information of this request message, use the handling process 3 of skeleton code to construct return messages, and Chong Bao detection zone and transmit queue will be put into;
5. sending thread uses the handling process 4 of skeleton code return messages to be returned.
Through above-mentioned conversion processing, asynchronous communication assembly by the soap protocol API of a synchronous communication, can become a soap protocol API that is reliable, asynchronous communication efficiently.Can also be applied in other agreement, more efficient, the reliable asynchronous communication agreement API of convenient realization.
So the present invention has the agreement API using asynchronous communication Components Development asynchronous communication, realize using various protocols to carry out data communication, the features such as convenient and swift, strong adaptability of programming.