Note: This article is another article: mgen clo engineering 0.1 and a concise tutorial.
The client connection (poolconnection type) of the clo project connection pool also has a spawn method, which allows two clients, namely poolconnection, to directly establish a connection. The connection is transmitted without going through connectionpoolhost.
For example:
Each poolconnection can call the spawn method without restrictions. The spawn function can meet many requirements, such as file transmission between two users in the chat room (or asynchronous image receiving operations), because the file data may be relatively large, it is best not to pass through connectionpoolhost, it is best to establish a connection directly between the resource owner and the demand side to complete transmission.
Spawn is initiated by calling the spawn method of poolconnection. This method is defined as follows:
Public listenerwaiter spawn (ipendpoint to, byte [] Message)
The to parameter is the ipendpoint of another client, and the message is the transmitted message. It returns a listenerwaiter to wait for the connection from the target client. Note that the returned listenerwaiter is not in the waiting state, because if the listenerwaiter in the waiting state is directly returned, if the connection has been established successfully before the user registers the completed event, the user's completed event will not be called. Therefore, the user needs to manually call the start method of listenerwaiter to start the connection wait device.
After the connection is established successfully (that is, after the connection is established), if no exception occurs, the target client will be notified of the spawn event, which is the client processor: poolmemberprocessor. Spawn event. Do you still remember the client processing model of the clo project? Readers can look at this picture again:
Poolmemberprocessor processes messages in the clo project. If a spawn request is received, poolmemberprocessor calls the spawn event, which is defined as follows:
Public event eventhandler <spawneventargs> spawn;
Spawneventargs type:
The messge attribute is the message parameter that initiates the spawn.
The waiter attribute is the connectorwaiter object corresponding to listenerwaiter. That is to say, after the initiator's listenerwaiter start method is called and the connection is established successfully, the completed of the listenerwaiter and connectorwaiter objects will be called.
Let's take a look at the spawn code in the Windows example program of ClO Engineering:
/* The code in front is omitted */
// The-S parameter indicates that the spawn is initiated.
Else if (STR = "-s ")
{
// Select a client ipendpoint from the client list
VaR spawnep = clients. getspawncandidate ();
// If no error message is output from other clients
If (spawnep = NULL)
{
Console. writeline (Idom. spawnnouser );
}
// Start spawn
Else
{
// Perform spawn on the ipendpoint of the specified Client
VaR spawaiter = poolconn. spawn (spawnep, encoding. utf8.getbytes (Idom. spawnmsg ));
Console. writeline (Idom. spawnstarted );
// Register the listenerwaiter. Completed event
Spawaiter. Completed + = (SS, ee) =>
{
// Determine whether an exception exists
If (EE. Exception! = NULL)
{
// Error: output error message
Console. writeline (Idom. spawnerror + ee. Exception. Message );
}
Else
{
// Spawn successful
Console. writeline (Idom. spawnsucc );
// Todo: use ee. Connection to directly transmit information with the specified client!
}
};
// Call listenerwaiter. Start to start waiting.
Spawaiter. Start ();
}
}
/* The rear code is omitted */
Code of the spawn receiver (how to handle the poolmemberprocessor. Spawn event ):
/* The code in front is omitted */
// Poolmemberprocessor. Spawn event handling method
Static void p_spawn (Object sender, spawneventargs E)
{
// Output the message of the spawn request
Console. writeline (Idom. spawnrecv + encoding. utf8.getstring (E. Message ));
// Register connectorwaiter. Completed event
E. Waiter. Completed + = (SS, ee) =>
{
// Determine whether an exception exists
If (EE. Exception! = NULL)
{
// Output error message
Console. writeline (Idom. spawnerror + ee. Exception. Message );
}
Else
{
// Spawn operation successful
Console. writeline (Idom. spawnsucc );
// Connect local and remote ipendpoint
Console. writeline (Idom. localendpoint + ee. Connection. localendpoint );
Console. writeline (Idom. remoteendpoint + ee. Connection. remoteendpoint );
// Todo: use ee. Connection to directly transmit data with the target client
}
};
// Call connectorwaiter. Start to start the connection wait.
E. Waiter. Start ();
}
/* The rear code is omitted */
You can use the clo project windows sample program for testing, such: