/********************************
*
* 作者:吴隆健
* 创建时间: 2019/6/24 15:27:55
*
********************************/
#if RABBITMQ
using RabbitMQ.Client;
using System;
using System.Collections.Generic;
using System.Text;
namespace MrWu.RabbitMQ {
///
/// 消息委托
///
///
public delegate void MessageHandle(Message msg);
///
/// 消息
///
public class Message {
///
/// 日志用的查 慢的问题
///
public DateTime logTime;
private static Exception ex {
get {
return new Exception("引用已释放的对象!");
}
}
///
/// 是否已经释放
///
internal bool isFree = false;
private string m_exchange;
///
/// 路由
///
public string exchange {
get {
if (isFree)
throw ex;
return m_exchange;
}
set {
if (isFree)
throw ex;
m_exchange = value;
}
}
private string m_routingkey;
///
/// 路由键
///
public string routingkey {
get {
if (isFree)
throw ex;
return m_routingkey;
}
set {
if (isFree)
throw ex;
m_routingkey = value;
}
}
private bool m_durable;
public bool durable
{
get
{
if (isFree)
throw ex;
return m_durable;
}
set
{
if (isFree)
throw ex;
m_durable = value;
}
}
private bool m_mandatory;
///
/// 无法路由是否触发return事件
///
public bool mandatory {
get {
if (isFree)
throw ex;
return m_mandatory;
}
set {
if (isFree)
throw ex;
m_mandatory = value;
}
}
///
/// 消息内容
///
public byte[] body {
get {
if (isFree)
throw ex;
//Debug.Debug.Log("消息内容:" + bodystr);
return Encoding.UTF8.GetBytes(bodystr);
}
set {
if (isFree)
throw ex;
bodystr = Encoding.UTF8.GetString(value);
}
}
private string m_bodystr;
///
/// 消息内容
///
public string bodystr {
get {
if (isFree)
throw ex;
return m_bodystr;
}
set {
if (isFree)
throw ex;
m_bodystr = value;
}
}
private IBasicProperties m_propertis;
///
/// 消息的属性
///
public IBasicProperties propertis {
get {
if (isFree)
throw ex;
return m_propertis;
}
set {
if (isFree)
throw ex;
m_propertis = value;
}
}
private ulong m_deliveryTag;
///
/// 投递的或接收的消息标志
///
public ulong deliveryTag {
get {
if (isFree)
throw ex;
return m_deliveryTag;
}
internal set {
if (isFree)
throw ex;
m_deliveryTag = value;
}
}
private string m_id;
///
/// 消息的唯一标识
///
public string id {
get {
if (isFree)
throw ex;
return m_id;
}
internal set {
if (isFree)
throw ex;
m_id = value;
}
}
private bool m_success;
///
/// 是否发送成功
///
public bool success {
get {
if (isFree)
throw ex;
return m_success;
}
internal set {
if (isFree)
throw ex;
m_success = value;
}
}
private bool m_isAutoFree;
///
/// 是否发送完毕自动释放
///
public bool isAutoFree {
get {
if (isFree)
throw ex;
return m_isAutoFree;
}
internal set {
if (isFree)
throw ex;
m_isAutoFree = value;
}
}
///
/// 发送失败事件
///
public MessageHandle SendResult = null;
private IDictionary m_useradata;
///
/// 暂存数据,客户端发送失败可能用的上
///
public IDictionary userData {
get {
if (isFree)
throw ex;
return m_useradata;
}
set {
if (isFree)
throw ex;
m_useradata = value;
}
}
private IModel m_channel;
///
/// 处理该消息的信道
///
internal IModel channel {
get {
if (isFree)
throw ex;
return m_channel;
}
set {
if (isFree)
throw ex;
m_channel = value;
}
}
private bool m_noAck;
///
/// 是否需要确认
///
public bool noAck {
get {
if (isFree)
throw ex;
return m_noAck;
}
internal set {
if (isFree)
throw ex;
m_noAck = value;
}
}
private bool m_redelivered;
///
/// 是否被重复接收过
///
public bool redelivered {
get {
if (isFree)
throw ex;
return m_redelivered;
}
internal set {
if (isFree)
throw ex;
m_redelivered = value;
}
}
private uint m_messageCount;
///
/// 剩余消息的数量
///
public uint messageCount {
get {
if (isFree)
throw ex;
return m_messageCount;
}
internal set {
if (isFree)
throw ex;
m_messageCount = value;
}
}
///
/// 确认
///
///
public bool Ack(bool multiple = false) {
if (isFree)
throw ex;
if (channel == null)
return false;
lock (channel) {
try {
channel.BasicAck(deliveryTag, multiple);
} catch (Exception e) {
Debug.Debug.Error("确认消息出错:" + e.ToString());
return false;
}
}
return true;
}
///
/// 拒绝确认
///
///
public bool NAck(bool multiple = false, bool requeue = false) {
if (isFree)
throw ex;
if (channel == null)
return false;
lock (channel) {
try {
channel.BasicNack(deliveryTag, multiple, requeue);
} catch (Exception e) {
Debug.Debug.Error("拒绝消息出错:" + e.ToString());
return false;
}
}
return true;
}
private long m_createTime;
///
/// 发送的逻辑时间
///
internal long createTime {
get {
if (isFree)
throw ex;
return m_createTime;
}
set {
if (isFree)
throw ex;
m_createTime = value;
}
}
///
///
///
internal Message() {
}
/////
/////
/////
/////
//internal Message(string id) {
// this.id = id;
// createTime = Debug.Debug.timevalue;
//}
///
///
///
///
public override string ToString() {
if (isFree)
throw ex;
StringBuilder sb = new StringBuilder();
sb.AppendLine("exchange:" + exchange);
sb.AppendLine("routingkey:" + routingkey);
sb.AppendLine("mandatory:" + mandatory);
sb.AppendLine("deliveryTag:" + deliveryTag);
sb.AppendLine("success:" + success);
sb.AppendLine("body:" + bodystr);
return sb.ToString();
}
///
/// 克隆一个
///
///
public Message Clone() {
Message result = new Message();
result.isFree = this.isFree;
result.exchange = this.exchange;
result.routingkey = this.routingkey;
result.mandatory = this.mandatory;
result.bodystr = this.bodystr;
result.propertis = this.propertis;
result.deliveryTag = this.deliveryTag;
result.id = this.id;
result.success = this.success;
result.isAutoFree = this.isAutoFree;
result.userData = this.userData;
result.SendResult = this.SendResult;
result.channel = this.channel;
result.noAck = this.noAck;
result.redelivered = this.redelivered;
result.messageCount = this.messageCount;
result.createTime = this.createTime;
return result;
}
///
/// 清楚事件
///
public void Clear() {
success = false;
noAck = false;
isAutoFree = false;
SendResult = null;
exchange = null;
routingkey = null;
bodystr = null;
propertis = null;
userData = null;
channel = null;
}
/////
///// Dispose
/////
//public void Dispose() {
// this.id = null;
// this.exchange = null;
// this.routingkey = null;
// this.bodystr = null;
// this.propertis?.Headers?.Clear();
// this.propertis = null;
// this.userData?.Clear();
// this.userData = null;
// this.channel = null;
// if(this.SendResult != null) {
// var deles = this.SendResult.GetInvocationList();
// foreach(MessageHandle dele in deles) {
// this.SendResult -= dele;
// }
// }
// GC.SuppressFinalize(this);
//}
}
}
#endif