稳扎稳打Silverlight(54) - 4.0通信之对UDP协议的支持
2010-10-12 12:30:02 来源:WEB开发网UdpAnySourceMulticastChannel.cs
代码
/*
* 通过 UdpAnySourceMulticastClient 实现 ASM(Any Source Multicast),即“任意源多播”
* 多播组基于 IGMP(Internet Group Management Protocol),即“Internet组管理协议”
*
* UdpAnySourceMulticastClient - 一个发送信息到多播组并从任意源接收多播信息的客户端,即 ASM 客户端
* BeginJoinGroup(), EndJoinGroup() - 加入多播组的异步方法
* BeginReceiveFromGroup(), EndReceiveFromGroup() - 从多播组接收信息的异步方法(可以理解为接收多播组内所有成员发送的信息)
* BeginSendToGroup(), EndSendToGroup() - 发送信息到多播组的异步方法(可以理解为发送信息到多播组内的全部成员)
* ReceiveBufferSize - 接收信息的缓冲区大小
* SendBufferSize - 发送信息的缓冲区大小
*
* BeginSendTo(), EndSendTo() - 发送信息到指定目标的异步方法
* BlockSource() - 阻止指定源,以便不再接收该源发来的信息
* UnblockSource() - 取消阻止指定源
* MulticastLoopback - 发出的信息是否需要传给自己
*
* 本例为一个通过 UdpAnySourceMulticastClient 实现 ASM 客户端的帮助类
*/
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Text;
using System.Net.Sockets;
namespace Silverlight40.Communication
{
public class UdpAnySourceMulticastChannel : IDisposable
{
private UdpAnySourceMulticastClient _client;
// 接收信息的缓冲区
private byte[] _buffer;
// 此客户端是否加入了多播组
private bool _isJoined;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="groupAddress">多播组地址</param>
/// <param name="port">客户端端口</param>
/// <param name="maxMessageSize">接收信息的缓冲区大小</param>
public UdpAnySourceMulticastChannel(IPAddress groupAddress, int port, int maxMessageSize)
{
_buffer = new byte[maxMessageSize];
// 实例化 ASM 客户端,需要指定的参数为:多播组地址;客户端端口
_client = new UdpAnySourceMulticastClient(groupAddress, port);
}
// 收到多播信息后触发的事件
public event EventHandler<UdpPacketEventArgs> Received;
private void OnReceived(IPEndPoint source, byte[] data)
{
var handler = Received;
if (handler != null)
handler(this, new UdpPacketEventArgs(data, source));
}
// 加入多播组后触发的事件
public event EventHandler Opening;
private void OnOpening()
{
var handler = Opening;
if (handler != null)
handler(this, EventArgs.Empty);
}
// 断开多播组后触发的事件
public event EventHandler Closing;
private void OnClosing()
{
var handler = Closing;
if (handler != null)
handler(this, EventArgs.Empty);
}
/// <summary>
/// 加入多播组
/// </summary>
public void Open()
{
if (!_isJoined)
{
_client.BeginJoinGroup(
result =>
{
_client.EndJoinGroup(result);
_isJoined = true;
Deployment.Current.Dispatcher.BeginInvoke(
() =>
{
OnOpening();
Receive();
});
}, null);
}
}
public void Close()
{
_isJoined = false;
OnClosing();
Dispose();
}
/// <summary>
/// 发送信息到多播组,即发送信息到多播组内的所有成员
/// </summary>
public void Send(string msg)
{
if (_isJoined)
{
byte[] data = Encoding.UTF8.GetBytes(msg);
_client.BeginSendToGroup(data, 0, data.Length,
result =>
{
_client.EndSendToGroup(result);
}, null);
}
}
/// <summary>
/// 从多播组接收信息,即接收多播组内所有成员发送的信息
/// </summary>
private void Receive()
{
if (_isJoined)
{
Array.Clear(_buffer, 0, _buffer.Length);
_client.BeginReceiveFromGroup(_buffer, 0, _buffer.Length,
result =>
{
IPEndPoint source;
_client.EndReceiveFromGroup(result, out source);
Deployment.Current.Dispatcher.BeginInvoke(
() =>
{
OnReceived(source, _buffer);
Receive();
});
}, null);
}
}
public void Dispose()
{
if (_client != null)
_client.Dispose();
}
}
}
Tags:稳扎稳打 Silverlight 通信
编辑录入:爽爽 [复制链接] [打 印]- ››silverlight全屏显示图片
- ››Silverlight MVVM 模式(一) 切近实战
- ››稳扎稳打Silverlight(53) - 4.0通信之对WCF NetTc...
- ››稳扎稳打Silverlight(54) - 4.0通信之对UDP协议的...
- ››稳扎稳打Silverlight(55) - 4.0通信之对UDP协议的...
- ››稳扎稳打Silverlight(56) - 4.0通信之与 WCF Data...
- ››Silverlight for Windows Phone 7开发系列(1):...
- ››Silverlight for Windows Phone 7开发系列(2):...
- ››Silverlight for Windows Phone 7开发系列(3):...
- ››Silverlight for Windows Phone 7开发系列(4):...
- ››Silverlight for Symbian
- ››Silverlight3系列(四)数据绑定 Data Binding 1
更多精彩
赞助商链接