. Net Remoting for targeted Broadcast

Source: Internet
Author: User

Compared with WebService, clients using the. Net Remoting technology can subscribe to server events. This feature is amazing.

If you want to use this technology for a simple and typical application, the information broadcasting program is a good choice. The following code is a simple broadcast program. Of course, it is too simple.

 

 

Server:

Code
Class Program
{
Static void Main (string [] args)
{
BinaryServerFormatterSinkProvider sfsp = new BinaryServerFormatterSinkProvider ();
Sfsp. TypeFilterLevel = TypeFilterLevel. Full;
Hashtable props = new Hashtable ();
Props ["port"] = 8086;
TcpChannel channel = new TcpChannel (props, null, sfsp );
ChannelServices. RegisterChannel (channel, false );
SayHello sayHello = new SayHello ();
RemotingServices. Marshal (sayHello, "SayHello ");
Console. ReadKey ();
SayHello. Say ("Mike", "Hello, Mike ");
Console. ReadKey ();
SayHello. Say ("John", "Hello, John ");
Console. ReadKey ();
}
}

Client:

 

 

Code
Class Program
{
Static void Main (string [] args)
{
BinaryServerFormatterSinkProvider sfsp = new BinaryServerFormatterSinkProvider ();
Sfsp. TypeFilterLevel = TypeFilterLevel. Full;
Hashtable props = new Hashtable ();
Props ["port"] = 0;
TcpChannel channel = new TcpChannel (props, null, sfsp );
ChannelServices. RegisterChannel (channel, false );
SayHello sh = (SayHello) Activator. GetObject (typeof (SayHello), "tcp: // localhost: 8086/SayHello ");

 


SayEventReappear re = new SayEventReappear ();
Re. ClientId = "John ";
Sh. OnSay + = new SayHandler (re. Say );
Re. OnSay + = new SayHandler (re_OnSay );
Console. ReadKey ();
}
Static void re_OnSay (string text)
{
Console. WriteLine (text );
}
}

Remote Object, delegate, and event reproducible (must be deployed on both the server and client ):

Code
Public class SayHello: MarshalByRefObject
{
Public event SayHandler OnSay;
Public void Say (string clientId, string text)
{
If (this. OnSay! = Null) this. OnSay (text );
}
}
Public delegate void SayHandler (string text );
Public class SayEventReappear: externalbyrefobject
{
Public event SayHandler OnSay;
Public void Say (string text)
{
If (this. OnSay! = Null) this. OnSay (text );
}
}

OK, my information broadcasting program is finished in this way.

 

 

However, I soon discovered the problem: If I really want all clients that subscribe to my broadcast events to obtain the information I want to broadcast, this implementation should be fine. But now I only want to notify Mike or John (as in the above Code), (note: it may not be called "broadcast" anymore ), my broadcast program still notifies every client of this message.

 

One way to think of it is to let the SayEventReappear determine whether it is sent to itself after receiving the information. Only the information sent to itself can stimulate local events (the code is easier to implement, no longer post source code ). However, this kind of processing only ignores the information on the client, and the server broadcasts the information as usual. If your information is very confidential or the bandwidth is very limited, this is obviously not a good solution.

 

Taking into account the implementation principle of the. Net Remoting client subscription event: the event reproducible is instantiated on the client, and the server remotely calls it by reference (personal understanding, without confirmation, please correct ). If you change the event reproducer subscription server-side event to "register" event reproducer to the server-side, the logic should be feasible, in this way, the event reproducer registered by each client carries its own customer ID, and the server determines whether to trigger events of a specific client based on the ID. The modified code is as follows:

 

Remote Object:

Code
Public class SayHello: MarshalByRefObject
{
Private List reList = new List ();
Public void Say (string clientId, string text)
{
Foreach (SayEventReappear re in this. reList)
{
If (re. ClientId = clientId) re. Say (text );
}
}
Public void AddEventReappear (SayEventReappear re)
{
This. reList. Add (re );
}
}

 

 

Client Program:

 

Sh. OnSay + = new SayHandler (re. Say); changed to sh. AddEventReappear (re );

 

 

OK. The server checks the IDs of each client separately, and then only triggers events of a specific client. The real "targeted broadcast" is implemented.

 

 

However, if you check the above Code, you will find that the problem persists: Because the instance of the event reproducer exists on the client, the server accesses its proxy class, therefore, each ClientId check is a remote call, which is a waste of resources.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.