12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- using XGame.Framework;
- namespace XGame.Framework.Network
- {
- public enum MsgCodeDefine
- {
- SUCCESS_CODE = 0,
- UNIFIED_ERROR_CODE = 1,
- }
- /// <summary>
- /// 网络消息控制器基类
- /// </summary>
- /// <typeparam name="T">消息类型</typeparam>
- public abstract class MsgController<T> : IMsgController where T : IMsgData
- {
- #if FINAL_RELEASE
- public void Process(IMessage msg, IMsgReceiverListener listener)
- #else
- void IMsgController.Process(IMessage msg, IMsgReceiverListener listener)
- #endif
- {
- if (msg is IMsgPush)
- {
- OnProcess((T)msg, msg.Context);
- }
- else if (msg is IMsgResponse resp)
- {
- var code = resp.GetCode();
- if (code == (int)MsgCodeDefine.SUCCESS_CODE)
- {
- OnProcess((T)msg, msg.Context);
- }
- else if (this is IMsgFailProcess<T> failProcess)
- {
- failProcess.FailProcess((T)msg, msg.Context);
- }
- var args = new ResponseEventArgs()
- {
- errorCode = code,
- info = resp.GetInfo(),
- seqId = resp.InstanceID,
- protoId = resp.ProtocolID,
- };
- listener.OnResponseFinish(args);
- }
- else
- {
- Log.Error($"[MsgController] 消息类型错误,Type:{msg.GetType()}");
- }
- }
- protected abstract void OnProcess(T message, object context);
- }
- }
|