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.
183 lines
4.6 KiB
183 lines
4.6 KiB
3 years ago
|
using System;
|
||
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using QFramework;
|
||
|
using UnityEngine;
|
||
|
using UnityEngine.Events;
|
||
|
|
||
|
|
||
3 years ago
|
//任何通讯类围绕DataEventModel进行
|
||
|
public abstract class DataEventModel : AbstractModel, ICanSendCommand, ICanRegisterEvent
|
||
|
{
|
||
|
public IOCContainer mCommandContainer = new IOCContainer();
|
||
|
public UnityStringEvent onDataRecived = new UnityStringEvent();
|
||
2 years ago
|
public Color printColor = Color.white;
|
||
|
public void sendResponseCommand<T>(string json) where T : IResponse, new()
|
||
3 years ago
|
{
|
||
|
if (mCommandContainer.Get<ExcuteResponseCommand<T>>() == null)
|
||
3 years ago
|
{
|
||
3 years ago
|
mCommandContainer.Register(new ExcuteResponseCommand<T>(new T()));
|
||
3 years ago
|
}
|
||
3 years ago
|
mCommandContainer.Get<ExcuteResponseCommand<T>>().setJson(json);
|
||
|
this.SendCommand(mCommandContainer.Get<ExcuteResponseCommand<T>>());
|
||
3 years ago
|
}
|
||
2 years ago
|
|
||
3 years ago
|
/// <summary>
|
||
3 years ago
|
/// 开启接收指定数据
|
||
3 years ago
|
/// </summary>
|
||
3 years ago
|
/// <typeparam name="T">数据格式类型</typeparam>
|
||
|
public void onReceive<T>() where T : IResponse, new()
|
||
3 years ago
|
{
|
||
2 years ago
|
onDataRecived.AddListener(sendResponseCommand<T>);
|
||
3 years ago
|
}
|
||
|
/// <summary>
|
||
|
/// 关闭接收指定数据
|
||
|
/// </summary>
|
||
|
/// <typeparam name="T">数据格式类型</typeparam>
|
||
|
public void offReceive<T>() where T : IResponse, new()
|
||
|
{
|
||
2 years ago
|
onDataRecived.RemoveListener(sendResponseCommand<T>);
|
||
3 years ago
|
}
|
||
2 years ago
|
|
||
3 years ago
|
/// <summary>
|
||
|
/// 发送数据
|
||
|
/// </summary>
|
||
|
/// <typeparam name="T">数据类型</typeparam>
|
||
|
/// <param name="request">请求数据</param>
|
||
|
public void sendRequestCommand<T>(T request) where T : IRequest
|
||
|
{
|
||
|
if (mCommandContainer.Get<SendRequestCommand<T>>() == null)
|
||
3 years ago
|
{
|
||
3 years ago
|
mCommandContainer.Register(new SendRequestCommand<T>(request));
|
||
3 years ago
|
}
|
||
3 years ago
|
mCommandContainer.Get<SendRequestCommand<T>>().setRequest(request);
|
||
|
this.SendCommand(mCommandContainer.Get<SendRequestCommand<T>>());
|
||
3 years ago
|
}
|
||
3 years ago
|
}
|
||
3 years ago
|
|
||
3 years ago
|
#region Command
|
||
|
/// <summary>
|
||
|
/// 处理响应数据,添加对应的Command进行处理,需要提前生成命令池
|
||
|
/// </summary>
|
||
|
/// <typeparam name="TResponse">响应数据格式</typeparam>
|
||
|
public class ExcuteResponseCommand<TResponse> : AbstractCommand where TResponse : IResponse
|
||
|
{
|
||
|
public string json;
|
||
|
private TResponse response;
|
||
2 years ago
|
private Color printColor = Color.white;
|
||
3 years ago
|
public ExcuteResponseCommand(TResponse response)
|
||
3 years ago
|
{
|
||
3 years ago
|
if (this.response == null)
|
||
3 years ago
|
{
|
||
3 years ago
|
this.response = response;
|
||
3 years ago
|
}
|
||
3 years ago
|
}
|
||
|
protected override void OnExecute()
|
||
|
{
|
||
|
bool isSet = response.trySetData(json);
|
||
|
if (isSet)
|
||
3 years ago
|
{
|
||
3 years ago
|
Debug.Log("Received:" + response.toJson() + response.GetType());
|
||
|
this.SendEvent(new ResponseMsgEvent(response));
|
||
3 years ago
|
}
|
||
|
}
|
||
3 years ago
|
public void setJson(string json)
|
||
3 years ago
|
{
|
||
3 years ago
|
this.json = json;
|
||
3 years ago
|
}
|
||
3 years ago
|
}
|
||
3 years ago
|
|
||
3 years ago
|
public class SendRequestCommand<TRequest> : AbstractCommand where TRequest : IRequest
|
||
|
{
|
||
|
public TRequest request;
|
||
|
public SendRequestCommand(TRequest request)
|
||
3 years ago
|
{
|
||
3 years ago
|
this.request = request;
|
||
3 years ago
|
}
|
||
3 years ago
|
protected override void OnExecute()
|
||
|
{
|
||
2 years ago
|
//此处需要修改
|
||
3 years ago
|
this.SendEvent(new RequestMsgEvent(request));
|
||
|
}
|
||
|
public void setRequest(TRequest request)
|
||
|
{
|
||
|
this.request = request;
|
||
|
}
|
||
|
}
|
||
|
#endregion
|
||
3 years ago
|
|
||
3 years ago
|
#region interface
|
||
|
public interface IResponse
|
||
|
{
|
||
|
string toJson();
|
||
3 years ago
|
/// <summary>
|
||
3 years ago
|
/// 尝试填充数据,如果json不合法,则返回false,忽略该条数据响应
|
||
3 years ago
|
/// </summary>
|
||
3 years ago
|
/// <param name="json"></param>
|
||
|
/// <returns></returns>
|
||
|
bool trySetData(string json);
|
||
|
string getException();
|
||
|
}
|
||
|
|
||
|
public interface IRequest
|
||
|
{
|
||
|
string toJson();
|
||
|
}
|
||
|
#endregion
|
||
|
|
||
|
#region AbstractClass
|
||
|
/// <summary>
|
||
|
/// 使用抽象类时,必须满足可序列化
|
||
|
/// </summary>
|
||
|
[Serializable]
|
||
|
public abstract class AbstractResponse : IResponse
|
||
|
{
|
||
|
private string exceptionMsg;
|
||
|
public virtual string toJson()
|
||
3 years ago
|
{
|
||
3 years ago
|
return JsonUtility.ToJson(this, true);
|
||
|
}
|
||
|
public virtual bool trySetData(string json)
|
||
|
{
|
||
|
try
|
||
3 years ago
|
{
|
||
3 years ago
|
JsonUtility.FromJsonOverwrite(json, this);
|
||
|
return true;
|
||
3 years ago
|
}
|
||
3 years ago
|
catch (Exception e)
|
||
3 years ago
|
{
|
||
3 years ago
|
exceptionMsg = e.ToString();
|
||
|
return false;
|
||
3 years ago
|
}
|
||
|
}
|
||
3 years ago
|
public string getException()
|
||
3 years ago
|
{
|
||
3 years ago
|
return exceptionMsg;
|
||
|
}
|
||
|
}
|
||
|
#endregion
|
||
3 years ago
|
|
||
3 years ago
|
#region event
|
||
|
public struct ResponseMsgEvent
|
||
|
{
|
||
|
public IResponse res;
|
||
|
public ResponseMsgEvent(IResponse res_)
|
||
|
{
|
||
|
res = res_;
|
||
|
}
|
||
|
};
|
||
2 years ago
|
public struct RequestMsgEvent
|
||
|
{
|
||
3 years ago
|
public IRequest req;
|
||
2 years ago
|
public RequestMsgEvent(IRequest req_)
|
||
|
{
|
||
3 years ago
|
req = req_;
|
||
3 years ago
|
}
|
||
|
}
|
||
3 years ago
|
#endregion
|
||
|
|
||
|
[Serializable]
|
||
|
public class UnityStringEvent : UnityEvent<string>
|
||
|
{
|
||
|
|
||
|
}
|