You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
259 lines
7.5 KiB
259 lines
7.5 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace IOTContainer.MvvmBase
|
|
{
|
|
/// <summary>
|
|
/// 消息基础类
|
|
/// </summary>
|
|
public class MessageBase
|
|
{
|
|
#region 静态函数
|
|
private static MessageBase _defult;
|
|
/// <summary>
|
|
/// 默认消息体
|
|
/// </summary>
|
|
public static MessageBase Defult
|
|
{
|
|
get
|
|
{
|
|
if (_defult == null)
|
|
_defult = new MessageBase();
|
|
|
|
return _defult;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region 构造函数
|
|
/// <summary>
|
|
/// 初始化插件消息器
|
|
/// </summary>
|
|
public MessageBase()
|
|
{
|
|
this._messages = new List<MessageItem>();
|
|
}
|
|
#endregion
|
|
|
|
#region 字段属性
|
|
/// <summary>
|
|
/// 消息列表
|
|
/// </summary>
|
|
private List<MessageItem> _messages;
|
|
#endregion
|
|
|
|
#region 对外方法
|
|
/// <summary>
|
|
/// 注册消息
|
|
/// </summary>
|
|
/// <param name="recipient">接收者</param>
|
|
/// <param name="token">消息令牌</param>
|
|
/// <param name="handler">消息操作事件</param>
|
|
public void Register(object recipient, string token, MessageHandler handler)
|
|
{
|
|
if (recipient == null)
|
|
throw new Exception("Recipient Is Null!");
|
|
if (string.IsNullOrWhiteSpace(token))
|
|
throw new Exception("Toke Is Null Or WhiteSpace!");
|
|
if (handler == null)
|
|
throw new Exception("Handler Is Null!");
|
|
|
|
this._messages.Add(new MessageItem(recipient, token, handler));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 广播消息
|
|
/// </summary>
|
|
/// <param name="sender">发送者</param>
|
|
/// <param name="token">消息令牌</param>
|
|
/// <param name="args">消息信息</param>
|
|
public void Send(object sender, string token, MessageArgs args)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(token))
|
|
throw new Exception("Toke Is Null Or WhiteSpace!");
|
|
|
|
var messages = this._messages.Where(x => x.Token == token);
|
|
|
|
foreach (var item in messages)
|
|
{
|
|
item.Do(sender, args);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 发送消息
|
|
/// </summary>
|
|
/// <param name="sender">发送者</param>
|
|
/// <param name="recipient">接收者</param>
|
|
/// <param name="token">消息令牌</param>
|
|
/// <param name="args">消息信息</param>
|
|
public void Send(object sender, object recipient, string token, MessageArgs args)
|
|
{
|
|
if (recipient == null)
|
|
throw new Exception("Recipient Is Null!");
|
|
if (string.IsNullOrWhiteSpace(token))
|
|
throw new Exception("Toke Is Null Or WhiteSpace!");
|
|
|
|
var messages = this._messages.Where(x => x.Recipient == recipient && x.Token == token);
|
|
|
|
foreach (var item in messages)
|
|
{
|
|
item.Do(sender, args);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 取消注册,移除所有包含recipient的消息
|
|
/// </summary>
|
|
/// <param name="recipient">接收者</param>
|
|
public void Unregister(object recipient)
|
|
{
|
|
if (recipient == null)
|
|
throw new Exception("Recipient Is Null!");
|
|
|
|
this._messages.RemoveAll(x => x.Recipient == recipient);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 取消注册,移除所有包含recipient,token的消息
|
|
/// </summary>
|
|
/// <param name="recipient">接收者</param>
|
|
/// <param name="token">消息令牌</param>
|
|
public void Unregister(object recipient, string token)
|
|
{
|
|
if (recipient == null)
|
|
throw new Exception("Recipient Is Null!");
|
|
if (string.IsNullOrWhiteSpace(token))
|
|
throw new Exception("Toke Is Null Or WhiteSpace!");
|
|
|
|
this._messages.RemoveAll(x => x.Recipient == recipient && x.Token == token);
|
|
}
|
|
#endregion
|
|
|
|
#region 消息子项
|
|
/// <summary>
|
|
/// 消息子项
|
|
/// </summary>
|
|
private class MessageItem : IDisposable
|
|
{
|
|
public MessageItem(object recipient, string token, MessageHandler handler)
|
|
{
|
|
this.Recipient = recipient;
|
|
this.Token = token;
|
|
this._handler = handler;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 注册者
|
|
/// </summary>
|
|
public object Recipient { get; private set; }
|
|
|
|
/// <summary>
|
|
/// 令牌
|
|
/// </summary>
|
|
public string Token { get; private set; }
|
|
|
|
/// <summary>
|
|
/// 操作事件
|
|
/// </summary>
|
|
private event MessageHandler _handler;
|
|
|
|
/// <summary>
|
|
/// 执行发送消息事件
|
|
/// </summary>
|
|
/// <param name="sender">发送者</param>
|
|
/// <param name="args">消息</param>
|
|
public void Do(object sender, MessageArgs args)
|
|
{
|
|
this._handler?.Invoke(sender, args);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 销毁程序
|
|
/// </summary>
|
|
public void Dispose()
|
|
{
|
|
this.Recipient = null;
|
|
this.Token = null;
|
|
this._handler = null;
|
|
}
|
|
}
|
|
#endregion
|
|
}
|
|
|
|
#region 消息委托
|
|
/// <summary>
|
|
/// 消息传递委托定义
|
|
/// </summary>
|
|
/// <param name="sender">发送者</param>
|
|
/// <param name="e">消息</param>
|
|
public delegate void MessageHandler(object sender, MessageArgs e);
|
|
|
|
/// <summary>
|
|
/// 消息回调委托定义
|
|
/// </summary>
|
|
/// <param name="sender">发送者</param>
|
|
/// <param name="e">消息</param>
|
|
public delegate void MessageCallBack(object sender, object message);
|
|
#endregion
|
|
|
|
#region 消息体定义
|
|
/// <summary>
|
|
/// 消息体
|
|
/// </summary>
|
|
public sealed class MessageArgs
|
|
{
|
|
public static readonly MessageArgs Empty = new MessageArgs();
|
|
|
|
/// <summary>
|
|
/// 无参构造函数
|
|
/// </summary>
|
|
public MessageArgs()
|
|
{
|
|
this.Message = null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 构造函数
|
|
/// </summary>
|
|
/// <param name="message">消息</param>
|
|
public MessageArgs(object message)
|
|
{
|
|
this.Message = message;
|
|
this._callback = null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 构造函数
|
|
/// </summary>
|
|
/// <param name="message">消息</param>
|
|
/// <param name="callback">回调函数</param>
|
|
public MessageArgs(object message, MessageCallBack callback)
|
|
{
|
|
this.Message = message;
|
|
this._callback = callback;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 消息
|
|
/// </summary>
|
|
public object Message { get; }
|
|
|
|
/// <summary>
|
|
/// 操作事件
|
|
/// </summary>
|
|
private event MessageCallBack _callback;
|
|
|
|
/// <summary>
|
|
/// 执行回调事件
|
|
/// </summary>
|
|
/// <param name="sender">发送者</param>
|
|
/// <param name="message">消息</param>
|
|
public void Callback(object sender, object message)
|
|
{
|
|
this._callback?.Invoke(sender, message);
|
|
}
|
|
}
|
|
#endregion
|
|
}
|