Commit d3e677c0 authored by 潘栩锋's avatar 潘栩锋 🚴

1. 修复 COMMON.NodeDispose 当子property不是INotifyProperty 会出错

2. 修复 刚连接成功,当全部properties 太多,导致下载慢,此时服务器有属性改变了,新改变很快的到达客户端,但之前的 GetProperty 后来才到达。 数据都不同步了。
parent 1b294c14
...@@ -91,7 +91,7 @@ namespace WSCF ...@@ -91,7 +91,7 @@ namespace WSCF
var list = node.Obj as System.Collections.IList; var list = node.Obj as System.Collections.IList;
if (list != null && list.Count>0) if (list != null && list.Count > 0)
{ {
InitSubPropertyPushs(list, node, sub_PropertyChanged); InitSubPropertyPushs(list, node, sub_PropertyChanged);
} }
...@@ -164,8 +164,10 @@ namespace WSCF ...@@ -164,8 +164,10 @@ namespace WSCF
} }
public static string GetNodePath(SubPropertyNode node) { public static string GetNodePath(SubPropertyNode node)
if (string.IsNullOrEmpty(node.Name)) { {
if (string.IsNullOrEmpty(node.Name))
{
//已经是最上级 //已经是最上级
return null; return null;
} }
...@@ -173,13 +175,15 @@ namespace WSCF ...@@ -173,13 +175,15 @@ namespace WSCF
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.Append(node.Name); sb.Append(node.Name);
while (node.Parent != null) { while (node.Parent != null)
{
node = node.Parent; node = node.Parent;
if (string.IsNullOrEmpty(node.Name)) { if (string.IsNullOrEmpty(node.Name))
{
//到了最上级 //到了最上级
break; break;
} }
sb.Insert(0, node.Name+"."); sb.Insert(0, node.Name + ".");
} }
return sb.ToString(); return sb.ToString();
} }
...@@ -192,13 +196,16 @@ namespace WSCF ...@@ -192,13 +196,16 @@ namespace WSCF
var nodes = rootNode.Children; var nodes = rootNode.Children;
//分解路径 //分解路径
string[] names = path.Split('.'); string[] names = path.Split('.');
foreach (string name in names) { foreach (string name in names)
if (string.IsNullOrEmpty(name)) { {
if (string.IsNullOrEmpty(name))
{
//这个不算 //这个不算
continue; continue;
} }
node = nodes.Find(n => n.Name == name); node = nodes.Find(n => n.Name == name);
if (node == null) { if (node == null)
{
//异常 //异常
return null; return null;
} }
...@@ -217,7 +224,7 @@ namespace WSCF ...@@ -217,7 +224,7 @@ namespace WSCF
{ {
if (!node.IsArray) if (!node.IsArray)
{ {
if(node.Obj!=null) if (node.Obj != null && node.Obj is INotifyPropertyChanged)
((INotifyPropertyChanged)(node.Obj)).PropertyChanged -= node.PropertyChanged; ((INotifyPropertyChanged)(node.Obj)).PropertyChanged -= node.PropertyChanged;
} }
...@@ -286,7 +293,8 @@ namespace WSCF ...@@ -286,7 +293,8 @@ namespace WSCF
public PropertyChangedEventHandler PropertyChanged; public PropertyChangedEventHandler PropertyChanged;
public SubPropertyChangedEventHandler SubPropertyChanged; public SubPropertyChangedEventHandler SubPropertyChanged;
public SubPropertyNode() { public SubPropertyNode()
{
PropertyChanged = new PropertyChangedEventHandler(_PropertyChanged); PropertyChanged = new PropertyChangedEventHandler(_PropertyChanged);
} }
public void _PropertyChanged(object sender, PropertyChangedEventArgs e) public void _PropertyChanged(object sender, PropertyChangedEventArgs e)
......
...@@ -13,6 +13,7 @@ namespace WSCF ...@@ -13,6 +13,7 @@ namespace WSCF
/// </summary> /// </summary>
public class PushAttribute : Attribute public class PushAttribute : Attribute
{ {
public const string DefaultTriggerNameHeader = "Trigger_";
public Type EventArgsType; public Type EventArgsType;
public string TriggerName; public string TriggerName;
public PushAttribute(Type eventArgsType) public PushAttribute(Type eventArgsType)
......
...@@ -11,19 +11,35 @@ using WebSocketSharp.Server; ...@@ -11,19 +11,35 @@ using WebSocketSharp.Server;
namespace WSCF namespace WSCF
{ {
public class Reflect_Proxy : WebSocketBehavior,IDisposable public class Reflect_Proxy : WebSocketBehavior, IDisposable
{ {
static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
//对象的接口类型 #region Core
/// <summary>
/// 对象的接口类型
/// </summary>
Type interfaceType; Type interfaceType;
//代理对象 /// <summary>
/// 代理对象
/// </summary>
object obj; object obj;
//忽略设置 /// <summary>
/// 忽略设置
/// </summary>
public bool ignoreSet = false; public bool ignoreSet = false;
/// <summary>
/// 对象的全部event
/// </summary>
List<AnyEvent> anyEvents = new List<AnyEvent>(); List<AnyEvent> anyEvents = new List<AnyEvent>();
/// <summary>
/// [PropertyPush] 标签树
/// </summary>
SubPropertyNode rootNode; SubPropertyNode rootNode;
/// <summary>
/// interfaceType 及其父类 的全部属性
/// </summary>
List<string> properties = new List<string>(); List<string> properties = new List<string>();
#endregion
public bool isAlive = false; public bool isAlive = false;
...@@ -31,6 +47,8 @@ namespace WSCF ...@@ -31,6 +47,8 @@ namespace WSCF
{ {
} }
#region Core
public void Init(Type interfaceType, object obj) public void Init(Type interfaceType, object obj)
{ {
this.interfaceType = interfaceType; this.interfaceType = interfaceType;
...@@ -52,9 +70,6 @@ namespace WSCF ...@@ -52,9 +70,6 @@ namespace WSCF
//处理[Push] //处理[Push]
InitEventPush(); InitEventPush();
} }
#region Init
void InitPropertyChanged() void InitPropertyChanged()
{ {
if (!typeof(INotifyPropertyChanged).IsAssignableFrom(interfaceType)) if (!typeof(INotifyPropertyChanged).IsAssignableFrom(interfaceType))
...@@ -77,7 +92,6 @@ namespace WSCF ...@@ -77,7 +92,6 @@ namespace WSCF
} }
} }
void InitEventPush() void InitEventPush()
{ {
var interfaceTypes = new List<Type>(); var interfaceTypes = new List<Type>();
...@@ -104,18 +118,7 @@ namespace WSCF ...@@ -104,18 +118,7 @@ namespace WSCF
var anyEvent = new AnyEvent() var anyEvent = new AnyEvent()
{ {
eventName = eventInfo.Name, eventName = eventInfo.Name,
PushObjInfoEx = (rd) => { PushObjInfoEx = (rd) => reponse_PushObjInfoEx(rd, true)
//数据推送!!!
//再嵌套
Reflect_OBJ_INTERFACE.PkgData pkgData = new Reflect_OBJ_INTERFACE.PkgData(
Reflect_OBJ_INTERFACE.PkgName.PUSH_Event,
null,
JObject.FromObject(rd)
);
string json = JsonConvert.SerializeObject(pkgData);
SendEx(json);
}
}; };
//这个事件需要推送 //这个事件需要推送
eventInfo.AddEventHandler(obj, anyEvent.eventHandler); eventInfo.AddEventHandler(obj, anyEvent.eventHandler);
...@@ -123,19 +126,7 @@ namespace WSCF ...@@ -123,19 +126,7 @@ namespace WSCF
} }
} }
void Sub_PropertyChanged(SubPropertyNode node, PropertyChangedEventArgs e)
void SendEx(string msg) {
try
{
Send(msg);
}
catch (Exception e)
{
//异常,通常是断开了!!
logger.Error(e);
}
}
private void Sub_PropertyChanged(SubPropertyNode node, PropertyChangedEventArgs e)
{ {
//if (ignoreSet)//从服务器接收的数据,不用再推送给服务器 //if (ignoreSet)//从服务器接收的数据,不用再推送给服务器
// return; // return;
...@@ -160,50 +151,10 @@ namespace WSCF ...@@ -160,50 +151,10 @@ namespace WSCF
name = COMMON.GetNodePath(node), name = COMMON.GetNodePath(node),
data = jObject data = jObject
}; };
reponse_PushObjInfoEx(rData, false);
Reflect_OBJ_INTERFACE.PkgData pkgData = new Reflect_OBJ_INTERFACE.PkgData(
Reflect_OBJ_INTERFACE.PkgName.PUSH_PropertyChanged,
null,
JToken.FromObject(rData)
);
string json = JsonConvert.SerializeObject(pkgData);
SendEx(json);
}
#endregion
protected override void OnOpen()
{
base.OnOpen();
isAlive = true;
AfterOpen?.Invoke(this);
} }
public Action<Reflect_Proxy> AfterOpen; Reflect_OBJ_INTERFACE.ReflectData request_CALL_GetAllProperties()
protected override void OnClose(CloseEventArgs e)
{
base.OnClose(e);
isAlive = false;
Dispose();
}
protected override void OnMessage(MessageEventArgs e)
{
if (!e.IsText)
return;
string json = e.Data;
var p = JsonConvert.DeserializeObject<Reflect_OBJ_INTERFACE.PkgData>(json);
OnMessage(p);
}
void OnMessage(Reflect_OBJ_INTERFACE.PkgData pkgData)
{
var pkgName = Enum.Parse(typeof(Reflect_OBJ_INTERFACE.PkgName), pkgData.name);
switch (pkgName)
{
case Reflect_OBJ_INTERFACE.PkgName.CALL_GetAllProperties:
{ {
var jObject = new JObject(); var jObject = new JObject();
...@@ -224,26 +175,20 @@ namespace WSCF ...@@ -224,26 +175,20 @@ namespace WSCF
{ {
data = jObject data = jObject
}; };
return rData;
pkgData.data = JToken.FromObject(rData);
string json = JsonConvert.SerializeObject(pkgData);
SendEx(json);
} }
break;
case Reflect_OBJ_INTERFACE.PkgName.CALL_SetProperty: void request_CALL_SetProperty(Reflect_OBJ_INTERFACE.ReflectData rData)
{ {
//ignoreSet = true; //ignoreSet = true;
var rData = pkgData.data.ToObject<Reflect_OBJ_INTERFACE.ReflectData>();
//sub property //sub property
var node = COMMON.FindNode(rootNode, rData.name); var node = COMMON.FindNode(rootNode, rData.name);
if (node == null) { if (node == null)
{
//异常 //异常
//客户端乱发过来 //客户端乱发过来
break; return;
} }
string json = rData.data.ToString(); string json = rData.data.ToString();
...@@ -251,10 +196,9 @@ namespace WSCF ...@@ -251,10 +196,9 @@ namespace WSCF
//ignoreSet = false; //ignoreSet = false;
} }
break;
case Reflect_OBJ_INTERFACE.PkgName.CALL_MethodInvoke: void request_CALL_MethodInvoke(Reflect_OBJ_INTERFACE.ReflectData rData, CC cc)
{ {
var rData = pkgData.data.ToObject<Reflect_OBJ_INTERFACE.ReflectData>();
var type = obj.GetType(); var type = obj.GetType();
var paramNames_req = rData.data.Children().OfType<JProperty>().Select(p => p.Name); var paramNames_req = rData.data.Children().OfType<JProperty>().Select(p => p.Name);
MethodInfo methodInfo = GetMethodInfo(type, rData.name, paramNames_req); MethodInfo methodInfo = GetMethodInfo(type, rData.name, paramNames_req);
...@@ -276,7 +220,8 @@ namespace WSCF ...@@ -276,7 +220,8 @@ namespace WSCF
} }
else if (string.Compare(pname, Reflect_OBJ_INTERFACE.asyncContext, true) == 0) else if (string.Compare(pname, Reflect_OBJ_INTERFACE.asyncContext, true) == 0)
{ {
parameters[i] = new CC() { methodName = rData.name, guid = pkgData.guid }; cc.methodName = rData.name;
parameters[i] = cc;
} }
else else
{ {
...@@ -284,11 +229,21 @@ namespace WSCF ...@@ -284,11 +229,21 @@ namespace WSCF
} }
} }
methodInfo.Invoke(obj, parameters); methodInfo.Invoke(obj, parameters);
}break;
} }
void asyncDelegate(object asyncContext, object retData)
{
var cc = (CC)asyncContext;
var rData = new Reflect_OBJ_INTERFACE.ReflectData()
{
name = cc.methodName,
data = retData == null ? null : JToken.FromObject(retData)
};
reponse_CALL_MethodInvoke(rData, cc);
} }
MethodInfo GetMethodInfo(Type type, string name, IEnumerable<string> parameterNames) MethodInfo GetMethodInfo(Type type, string name, IEnumerable<string> parameterNames)
{ {
...@@ -324,46 +279,145 @@ namespace WSCF ...@@ -324,46 +279,145 @@ namespace WSCF
} }
return null; return null;
} }
void asyncDelegate(object asyncContext, object retData)
public void Dispose()
{ {
var cc = (CC)asyncContext; if (typeof(INotifyPropertyChanged).IsAssignableFrom(interfaceType))
{
//继承了INotifyPropertyChanged
((INotifyPropertyChanged)(this.obj)).PropertyChanged -= rootNode.PropertyChanged;
}
var rData = new Reflect_OBJ_INTERFACE.ReflectData() //释放subProperties
COMMON.NodeDispose(rootNode);
}
#endregion
void reponse_PushObjInfoEx(Reflect_OBJ_INTERFACE.ReflectData rData, bool isEvent)
{ {
name = cc.methodName, //数据推送!!!
data = retData == null ? null : JToken.FromObject(retData) //再嵌套
var pkgName = isEvent ? Reflect_OBJ_INTERFACE.PkgName.PUSH_Event : Reflect_OBJ_INTERFACE.PkgName.PUSH_PropertyChanged;
Reflect_OBJ_INTERFACE.PkgData pkgData = new Reflect_OBJ_INTERFACE.PkgData()
{
guid = Guid.NewGuid().ToString(),
name = pkgName.ToString(),
data = JToken.FromObject(rData)
}; };
string json = JsonConvert.SerializeObject(pkgData);
SendEx(json);
}
void reponse_CALL_MethodInvoke(Reflect_OBJ_INTERFACE.ReflectData rData, CC cc)
{
Reflect_OBJ_INTERFACE.PkgData pkgData = new Reflect_OBJ_INTERFACE.PkgData( Reflect_OBJ_INTERFACE.PkgData pkgData = new Reflect_OBJ_INTERFACE.PkgData()
Reflect_OBJ_INTERFACE.PkgName.CALL_MethodInvoke, {
cc.guid, guid = cc.guid,
JToken.FromObject(rData) name = Reflect_OBJ_INTERFACE.PkgName.CALL_MethodInvoke.ToString(),
); data = JToken.FromObject(rData)
};
string json = JsonConvert.SerializeObject(pkgData); string json = JsonConvert.SerializeObject(pkgData);
SendEx(json); SendEx(json);
} }
public void Dispose() void reponse_CALL_GetAllProperties(Reflect_OBJ_INTERFACE.ReflectData rData, CC cc)
{ {
if (typeof(INotifyPropertyChanged).IsAssignableFrom(interfaceType))
Reflect_OBJ_INTERFACE.PkgData pkgData = new Reflect_OBJ_INTERFACE.PkgData()
{ {
//继承了INotifyPropertyChanged guid = cc.guid,
((INotifyPropertyChanged)(this.obj)).PropertyChanged -= rootNode.PropertyChanged; name = Reflect_OBJ_INTERFACE.PkgName.CALL_GetAllProperties.ToString(),
data = JToken.FromObject(rData)
};
string json = JsonConvert.SerializeObject(pkgData);
SendEx(json);
} }
//释放subProperties void OnMessage(Reflect_OBJ_INTERFACE.PkgData pkgData)
COMMON.NodeDispose(rootNode); {
var pkgName = Enum.Parse(typeof(Reflect_OBJ_INTERFACE.PkgName), pkgData.name);
switch (pkgName)
{
case Reflect_OBJ_INTERFACE.PkgName.CALL_GetAllProperties:
{
var rData = request_CALL_GetAllProperties();
reponse_CALL_GetAllProperties(rData, new CC() { guid = pkgData.guid });
}
break;
case Reflect_OBJ_INTERFACE.PkgName.CALL_SetProperty:
{
//ignoreSet = true;
var rData = pkgData.data.ToObject<Reflect_OBJ_INTERFACE.ReflectData>();
request_CALL_SetProperty(rData);
}
break;
case Reflect_OBJ_INTERFACE.PkgName.CALL_MethodInvoke:
{
var rData = pkgData.data.ToObject<Reflect_OBJ_INTERFACE.ReflectData>();
request_CALL_MethodInvoke(rData, new CC() { guid = pkgData.guid });
}
break;
}
} }
class CC class CC
{ {
public string methodName;
public string guid; public string guid;
/// <summary>
/// CALL_MethodInvoke 时使用
/// </summary>
public string methodName;
} }
void SendEx(string msg)
{
try
{
Send(msg);
}
catch (Exception e)
{
//异常,通常是断开了!!
logger.Error(e);
}
}
protected override void OnOpen()
{
base.OnOpen();
isAlive = true;
AfterOpen?.Invoke(this);
}
public Action<Reflect_Proxy> AfterOpen;
protected override void OnClose(CloseEventArgs e)
{
base.OnClose(e);
isAlive = false;
Dispose();
}
protected override void OnMessage(MessageEventArgs e)
{
if (!e.IsText)
return;
string json = e.Data;
var p = JsonConvert.DeserializeObject<Reflect_OBJ_INTERFACE.PkgData>(json);
OnMessage(p);
}
} }
/// <summary> /// <summary>
......
...@@ -13,6 +13,7 @@ namespace WSCF ...@@ -13,6 +13,7 @@ namespace WSCF
{ {
public class Reflect_SeviceClient : WebSocketClient, IDisposable public class Reflect_SeviceClient : WebSocketClient, IDisposable
{ {
#region Core
/// <summary> /// <summary>
/// 忽略设置 /// 忽略设置
/// </summary> /// </summary>
...@@ -22,6 +23,16 @@ namespace WSCF ...@@ -22,6 +23,16 @@ namespace WSCF
/// </summary> /// </summary>
protected virtual Type InterfaceType { get; } protected virtual Type InterfaceType { get; }
/// <summary>
/// 已经同步完成;
/// 已经收到了 CALL_GetAllProperties, 全部属性都与服务器一致
/// </summary>
public bool IsSynced { get; private set; }
/// <summary>
/// 当前 INotifyPropertyChanged 对象 引发了 PropertyChanged, 是由于 接收到数据导致的
/// </summary>
public bool IsInPushValue { get; protected set; }
class AnyEvent class AnyEvent
{ {
...@@ -34,6 +45,27 @@ namespace WSCF ...@@ -34,6 +45,27 @@ namespace WSCF
public string name; public string name;
public Type retType; public Type retType;
} }
List<AnyEvent> anyEvents = new List<AnyEvent>();
List<AnyCall> anyCalls = new List<AnyCall>();
SubPropertyNode rootNode;
List<string> properties = new List<string>();
/// <summary>
/// 缓存 发出 CALL_GetAllProperties指令,到收到回复 之间时段的 全部推送
/// </summary>
List<PushInfoData> pushInfoList = new List<PushInfoData>();
class PushInfoData
{
public bool isPushPropertyChanged;
public Reflect_OBJ_INTERFACE.ReflectData rData;
}
#endregion
class AnyMethodInvoke class AnyMethodInvoke
{ {
public string guid; public string guid;
...@@ -41,11 +73,9 @@ namespace WSCF ...@@ -41,11 +73,9 @@ namespace WSCF
public object asyncContext; public object asyncContext;
} }
List<AnyEvent> anyEvents = new List<AnyEvent>();
List<AnyCall> anyCalls = new List<AnyCall>();
List<AnyMethodInvoke> anyMethodInvokes = new List<AnyMethodInvoke>(); List<AnyMethodInvoke> anyMethodInvokes = new List<AnyMethodInvoke>();
SubPropertyNode rootNode;
List<string> properties = new List<string>();
public Reflect_SeviceClient(string serverName,bool isByConnName, string addrOrConnName):base(serverName) public Reflect_SeviceClient(string serverName,bool isByConnName, string addrOrConnName):base(serverName)
{ {
...@@ -59,6 +89,7 @@ namespace WSCF ...@@ -59,6 +89,7 @@ namespace WSCF
init(); init();
} }
#region Core
void init() void init()
{ {
rootNode = new SubPropertyNode rootNode = new SubPropertyNode
...@@ -72,6 +103,18 @@ namespace WSCF ...@@ -72,6 +103,18 @@ namespace WSCF
COMMON.InitPropertyPush(rootNode); COMMON.InitPropertyPush(rootNode);
InitEventPush(); InitEventPush();
InitCall(); InitCall();
this.PropertyChanged += (s, e) =>
{
if (e.PropertyName == nameof(IsConnected))
{
if (IsConnected == false)
{
IsSynced = false;
pushInfoList.Clear();
}
}
};
} }
void InitPropertyChanged() void InitPropertyChanged()
{ {
...@@ -120,7 +163,7 @@ namespace WSCF ...@@ -120,7 +163,7 @@ namespace WSCF
string triggerName; string triggerName;
if (string.IsNullOrEmpty(pushAttribute.TriggerName)) if (string.IsNullOrEmpty(pushAttribute.TriggerName))
{ {
triggerName = "Trigger_" + eventInfo.Name; triggerName = PushAttribute.DefaultTriggerNameHeader + eventInfo.Name;
} }
else else
{ {
...@@ -199,65 +242,105 @@ namespace WSCF ...@@ -199,65 +242,105 @@ namespace WSCF
name = COMMON.GetNodePath(node), name = COMMON.GetNodePath(node),
data = jObject data = jObject
}; };
send_CALL_SetProperty(rData);
}
Reflect_OBJ_INTERFACE.PkgData pkgData = new Reflect_OBJ_INTERFACE.PkgData(
Reflect_OBJ_INTERFACE.PkgName.CALL_SetProperty,
Guid.NewGuid().ToString(),
JToken.FromObject(rData)
);
string json = JsonConvert.SerializeObject(pkgData); public override void Dispose()
Send(json); {
base.Dispose();
if (typeof(INotifyPropertyChanged).IsAssignableFrom(InterfaceType))
{
//继承了INotifyPropertyChanged
((INotifyPropertyChanged)(this)).PropertyChanged -= rootNode.PropertyChanged;
} }
#region FObj //释放subProperties
protected override void OnOpen() COMMON.NodeDispose(rootNode);
}
protected void Call(string methodName, object parameters, AsyncCBHandler asyncDelegate, object asyncContext)
{ {
Reflect_OBJ_INTERFACE.PkgData pkgData = new Reflect_OBJ_INTERFACE.PkgData( var rData = new Reflect_OBJ_INTERFACE.ReflectData()
Reflect_OBJ_INTERFACE.PkgName.CALL_GetAllProperties, {
Guid.NewGuid().ToString(), name = methodName,
null data = parameters == null ? null : JObject.FromObject(parameters)
); };
string json = JsonConvert.SerializeObject(pkgData); send_CALL_MethodInvoke(rData, asyncDelegate, asyncContext);
Send(json);
} }
protected override void OnMessage(string msg)
protected void Call(string methodName, object parameters)
{ {
var pkgData = JsonConvert.DeserializeObject<Reflect_OBJ_INTERFACE.PkgData>(msg); Call(methodName, parameters, null, null);
}
var pkgName = Enum.Parse(typeof(Reflect_OBJ_INTERFACE.PkgName), pkgData.name); protected void Call(string methodName)
{
Call(methodName, null, null, null);
}
switch (pkgName)
/// <summary>
/// 处理 从服务器 回复的 CALL_GetAllProperties
/// </summary>
/// <param name="rData"></param>
void receive_CALL_GetAllProperties(Reflect_OBJ_INTERFACE.ReflectData rData)
{ {
case Reflect_OBJ_INTERFACE.PkgName.PUSH_PropertyChanged: receive_PUSH_PropertyChanged(rData);
case Reflect_OBJ_INTERFACE.PkgName.CALL_GetAllProperties: IsSynced = true;
DealPushInfoList();
}
/// <summary>
/// 处理全部 缓存的推送数据
/// </summary>
void DealPushInfoList()
{ {
ignoreSet = true; for (int i = 0; i < pushInfoList.Count(); i++)
{
var pushInfoData = pushInfoList[i];
if (pushInfoData.isPushPropertyChanged)
receive_PUSH_PropertyChanged(pushInfoData.rData);
else
receive_PUSH_Event(pushInfoData.rData);
}
var rData = pkgData.data.ToObject<Reflect_OBJ_INTERFACE.ReflectData>(); pushInfoList.Clear();
}
/// <summary>
/// 处理 从服务器 回复的 PUSH_PropertyChanged
/// </summary>
/// <param name="rData"></param>
void receive_PUSH_PropertyChanged(Reflect_OBJ_INTERFACE.ReflectData rData)
{
ignoreSet = true;
IsInPushValue = true;
var node = COMMON.FindNode(rootNode, rData.name); var node = COMMON.FindNode(rootNode, rData.name);
if (node == null) if (node == null)
{ {
//异常 //异常
//服务器乱发过来 //服务器乱发过来
break; return;
} }
string json = rData.data.ToString(); string json = rData.data.ToString();
JsonConvert.PopulateObject(json, node.Obj); JsonConvert.PopulateObject(json, node.Obj);
IsInPushValue = false;
ignoreSet = false; ignoreSet = false;
} }
break;
case Reflect_OBJ_INTERFACE.PkgName.PUSH_Event: /// <summary>
/// 处理 从服务器 回复的 PUSH_Event
/// </summary>
/// <param name="rData"></param>
void receive_PUSH_Event(Reflect_OBJ_INTERFACE.ReflectData rData)
{ {
if (InterfaceType == null) if (InterfaceType == null)
return; return;
var rData = pkgData.data.ToObject<Reflect_OBJ_INTERFACE.ReflectData>();
var anyEvent = anyEvents.Find(ae => ae.name == rData.name); var anyEvent = anyEvents.Find(ae => ae.name == rData.name);
if (anyEvent == null) if (anyEvent == null)
return;//异常!!! return;//异常!!!
...@@ -273,72 +356,142 @@ namespace WSCF ...@@ -273,72 +356,142 @@ namespace WSCF
//出错,就提示,肯定是客户端忘记写 "Trigger_XXXX" //出错,就提示,肯定是客户端忘记写 "Trigger_XXXX"
methodInfo.Invoke(this, new object[] { obj }); methodInfo.Invoke(this, new object[] { obj });
} }
break;
case Reflect_OBJ_INTERFACE.PkgName.CALL_MethodInvoke: /// <summary>
/// 处理 从服务器 回复的 CALL_MethodInvoke
/// </summary>
/// <param name="rData"></param>
/// <param name="asyncDelegate"></param>
/// <param name="asyncContext"></param>
void receive_CALL_MethodInvoke(Reflect_OBJ_INTERFACE.ReflectData rData, AsyncCBHandler asyncDelegate, object asyncContext)
{ {
if (InterfaceType == null) var anyCall = anyCalls.Find(ac => ac.name == rData.name);
return;
var reponse = pkgData.data.ToObject<Reflect_OBJ_INTERFACE.ReflectData>();
var anyCall = anyCalls.Find(ac => ac.name == reponse.name);
if (anyCall == null) if (anyCall == null)
{ {
//异常!!!应该有[Call]注明返回的类型 //异常!!!应该有[Call]注明返回的类型
throw new Exception($"{reponse.name} 异常!!!应该有[Call]注明返回的类型"); throw new Exception($"{rData.name} 异常!!!应该有[Call]注明返回的类型");
} }
var retData = reponse.data.ToObject(anyCall.retType); var retData = rData.data.ToObject(anyCall.retType);
var anyMethodInvoke = anyMethodInvokes.Find(ami => ami.guid == pkgData.guid);
if (anyMethodInvoke == null) asyncDelegate.Invoke(asyncContext, retData);
{
//异常!!!应该有对应的回调
throw new Exception($"{reponse.name} 异常!!!应该有回调");
} }
#endregion
anyMethodInvoke.asyncDelegate.Invoke(anyMethodInvoke.asyncContext, retData); /// <summary>
anyMethodInvokes.Remove(anyMethodInvoke); /// 发送 CALL_MethodInvoke 请求到服务器
/// </summary>
/// <param name="rData"></param>
void send_CALL_MethodInvoke(Reflect_OBJ_INTERFACE.ReflectData rData, AsyncCBHandler asyncDelegate, object asyncContext)
{
Reflect_OBJ_INTERFACE.PkgData pkgData = new Reflect_OBJ_INTERFACE.PkgData(
Reflect_OBJ_INTERFACE.PkgName.CALL_MethodInvoke,
Guid.NewGuid().ToString(),
JToken.FromObject(rData)
);
anyMethodInvokes.Add(new AnyMethodInvoke()
{
guid = pkgData.guid,
asyncDelegate = asyncDelegate,
asyncContext = asyncContext
});
string json = JsonConvert.SerializeObject(pkgData);
Send(json);
} }
break;
} /// <summary>
/// 发送 CALL_SetProperty 请求到服务器
/// </summary>
/// <param name="rData"></param>
void send_CALL_SetProperty(Reflect_OBJ_INTERFACE.ReflectData rData)
{
Reflect_OBJ_INTERFACE.PkgData pkgData = new Reflect_OBJ_INTERFACE.PkgData(
Reflect_OBJ_INTERFACE.PkgName.CALL_SetProperty,
Guid.NewGuid().ToString(),
JToken.FromObject(rData)
);
string json = JsonConvert.SerializeObject(pkgData);
Send(json);
} }
protected void Call(string methodName, object parameters, AsyncCBHandler asyncDelegate, object asyncContext) #region FObj
{ protected override void OnOpen()
var rData = new Reflect_OBJ_INTERFACE.ReflectData()
{ {
name = methodName, Reflect_OBJ_INTERFACE.PkgData pkgData = new Reflect_OBJ_INTERFACE.PkgData(
data = parameters == null ? null : JObject.FromObject(parameters) Reflect_OBJ_INTERFACE.PkgName.CALL_GetAllProperties,
}; Guid.NewGuid().ToString(),
var pkgData = new Reflect_OBJ_INTERFACE.PkgData(Reflect_OBJ_INTERFACE.PkgName.CALL_MethodInvoke, Guid.NewGuid().ToString(), JObject.FromObject(rData)); null
anyMethodInvokes.Add(new AnyMethodInvoke() { guid = pkgData.guid, asyncDelegate = asyncDelegate, asyncContext = asyncContext }); );
string json = JsonConvert.SerializeObject(pkgData); string json = JsonConvert.SerializeObject(pkgData);
Send(json); Send(json);
} }
protected void Call(string methodName, object parameters)
protected override void OnMessage(string msg)
{ {
Call(methodName, parameters, null, null); var pkgData = JsonConvert.DeserializeObject<Reflect_OBJ_INTERFACE.PkgData>(msg);
}
protected void Call(string methodName) var pkgName = (Reflect_OBJ_INTERFACE.PkgName)Enum.Parse(typeof(Reflect_OBJ_INTERFACE.PkgName), pkgData.name);
switch (pkgName)
{ {
Call(methodName, null, null, null); case Reflect_OBJ_INTERFACE.PkgName.PUSH_PropertyChanged:
case Reflect_OBJ_INTERFACE.PkgName.PUSH_Event:
{
var rData = pkgData.data.ToObject<Reflect_OBJ_INTERFACE.ReflectData>();
if (!IsSynced)
{
//还没同步完成,先缓存
pushInfoList.Add(
new PushInfoData()
{
isPushPropertyChanged = pkgName == Reflect_OBJ_INTERFACE.PkgName.PUSH_PropertyChanged,
rData = rData
});
return;
} }
#endregion
public void Dispose() if (pkgName == Reflect_OBJ_INTERFACE.PkgName.PUSH_PropertyChanged)
receive_PUSH_PropertyChanged(rData);
else
receive_PUSH_Event(rData);
}
break;
case Reflect_OBJ_INTERFACE.PkgName.CALL_GetAllProperties:
{ {
if (typeof(INotifyPropertyChanged).IsAssignableFrom(InterfaceType)) var rData = pkgData.data.ToObject<Reflect_OBJ_INTERFACE.ReflectData>();
receive_CALL_GetAllProperties(rData);
}
break;
case Reflect_OBJ_INTERFACE.PkgName.CALL_MethodInvoke:
{ {
//继承了INotifyPropertyChanged if (InterfaceType == null)
((INotifyPropertyChanged)(this)).PropertyChanged -= rootNode.PropertyChanged; return;
var reponse = pkgData.data.ToObject<Reflect_OBJ_INTERFACE.ReflectData>();
var anyMethodInvoke = anyMethodInvokes.Find(ami => ami.guid == pkgData.guid);
if (anyMethodInvoke == null)
{
//异常!!!应该有对应的回调
throw new Exception($"{reponse.name} 异常!!!应该有回调");
} }
//释放subProperties receive_CALL_MethodInvoke(reponse, anyMethodInvoke.asyncDelegate, anyMethodInvoke.asyncContext);
COMMON.NodeDispose(rootNode);
anyMethodInvokes.Remove(anyMethodInvoke);
} }
break;
}
}
#endregion
} }
} }
...@@ -175,7 +175,7 @@ namespace WSCF ...@@ -175,7 +175,7 @@ namespace WSCF
public void Dispose() public virtual void Dispose()
{ {
ws.Close(); ws.Close();
FObjServiceClientManager.Instance.ObjClientDisponse(this); FObjServiceClientManager.Instance.ObjClientDisponse(this);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment