/// <summary> /// 释放消息的同时调用 /// </summary> /// <param name="msg"></param> protected void OnFreeMessage(Message msg) { //BinaryMessage binaryMessage = msg as BinaryMessage; //if (binaryMessage == null) // return; //NetAdapter.Free(binaryMessage.mBlock); }
/// <summary> /// 提前处理消息 /// </summary> /// <param name="message"></param> protected virtual void OnDispatchMessage(Message message) { }
/// <summary> /// 创建消息时候处理 /// </summary> /// <param name="msg"></param> /// <param name="length"></param> protected void OnCreateMessage(Message msg, int length) { BinaryMessage binaryMessage = msg as BinaryMessage; if (binaryMessage == null) return; binaryMessage.mBlock = NetAdapter.Alloc(length - msg.HeadLength); }
/// <summary> /// 派发消息 /// </summary> /// <param name="lAdapterID"></param> /// <param name="msg"></param> protected void DispatchMessage(long lAdapterID, Message msg) { msg.AdapterId = lAdapterID; MessageHandler handler; mAllMessageHandler.TryGetValue(msg.MsgId, out handler); if (handler == null) return; handler(msg); }
/// <summary> /// 发送消息 /// </summary> /// <param name="net"></param> /// <param name="session"></param> /// <param name="msg"></param> /// <param name="Immediate"></param> public override void SendMessage(ENet net, object session, Message msg, bool Immediate = true) { NetAdapter adapter = GetAdapter(net, session); if (adapter == null) return; SendMessage(adapter.AdapterID, msg, Immediate); }
/// <summary> /// 发送消息 /// </summary> /// <param name="lAdapterID"></param> /// <param name="msg"></param> /// <param name="Immediate">true: 即刻发送; false: 等待发送</param> public override void SendMessage(long lAdapterID, Message msg, bool Immediate = true) { NetAdapter adapter = GetAdapter(lAdapterID); if (adapter == null) return; msg.SetByteArray(mWriteMessage); msg.DeconstructObj(); MemBlock block = NetAdapter.SendAlloc(msg.GetWritePos()); Buffer.BlockCopy(msg.GetBytes(), 0, block.GetBytes(), 0, msg.GetWritePos()); adapter.PushSend(block); if (Immediate) ImmediateSendMessage(lAdapterID); //TerminalFunction.LogMessage(msg.MsgId); }