2
0
mirror of https://github.com/esiur/esiur-dotnet.git synced 2025-12-14 08:50:23 +00:00

Deserialization fix

This commit is contained in:
2025-11-16 11:06:20 +03:00
parent e2e4ac90bc
commit d730fe1b8d
14 changed files with 571 additions and 221 deletions

View File

@@ -360,8 +360,8 @@ public partial class DistributedConnection : NetworkConnection, IStore
}).Error(e =>
{
// do nothing
Console.WriteLine("Queue is empty");
//throw e;
//Console.WriteLine("Queue is empty");
throw e;
});
// set local nonce
@@ -434,7 +434,7 @@ public partial class DistributedConnection : NetworkConnection, IStore
if (packet.DataType == null)
return offset;
Console.WriteLine("Incoming: " + packet + " " + packet.CallbackId);
//Console.WriteLine("Incoming: " + packet + " " + packet.CallbackId);
if (packet.Method == IIPPacketMethod.Notification)
{
@@ -1044,7 +1044,7 @@ public partial class DistributedConnection : NetworkConnection, IStore
SendParams()
.AddUInt8((byte)IIPAuthPacketAcknowledge.NoAuthNoAuth)
.AddUInt8Array(Codec.Compose(localHeaders,this.Instance.Warehouse, this))
.AddUInt8Array(Codec.Compose(localHeaders,Server.Instance.Warehouse, this))
.Done();
}
else
@@ -1711,7 +1711,8 @@ public partial class DistributedConnection : NetworkConnection, IStore
AsyncReply<bool> IStore.Remove(IResource resource)
{
throw new NotImplementedException();
// @TODO: this is called when no connection is possible
return new AsyncReply<bool>(true);
}
public AsyncReply<bool> Remove(string path)

View File

@@ -35,6 +35,7 @@ using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
@@ -180,17 +181,19 @@ partial class DistributedConnection
}
public AsyncReply StaticCall(UUID classId, byte index, Map<byte, object> parameters)
public AsyncReply StaticCall(UUID classId, byte index, object parameters)
{
return SendRequest(IIPPacketRequest.StaticCall, classId, index, parameters);
}
public AsyncReply Call(string procedureCall, params object[] parameters)
{
var args = new Map<byte, object>();
for (byte i = 0; i < parameters.Length; i++)
args.Add(i, parameters[i]);
return Call(procedureCall, args);
//var args = new Map<byte, object>();
//for (byte i = 0; i < parameters.Length; i++)
// args.Add(i, parameters[i]);
// return Call(procedureCall, parameters);
return SendRequest(IIPPacketRequest.ProcedureCall, procedureCall, parameters);
}
public AsyncReply Call(string procedureCall, Map<byte, object> parameters)
@@ -198,7 +201,7 @@ partial class DistributedConnection
return SendRequest(IIPPacketRequest.ProcedureCall, procedureCall, parameters);
}
internal AsyncReply SendInvoke(uint instanceId, byte index, Map<byte, object> parameters)
internal AsyncReply SendInvoke(uint instanceId, byte index, object parameters)
{
return SendRequest(IIPPacketRequest.InvokeFunction, instanceId, index, parameters);
}
@@ -266,7 +269,7 @@ partial class DistributedConnection
{
var req = requests.Take(callbackId);
Console.WriteLine("Completed " + callbackId);
//Console.WriteLine("Completed " + callbackId);
if (req == null)
{
@@ -283,7 +286,7 @@ partial class DistributedConnection
})
.Error(e =>
{
Console.WriteLine(callbackId + ": failed");
//Console.WriteLine(callbackId + ": failed");
req.TriggerError(e);
});
}
@@ -1016,7 +1019,7 @@ partial class DistributedConnection
{
reply.Then(results =>
{
var arguments = (Map<byte, object>)results;
//var arguments = (Map<byte, object>)results;
// un hold the socket to send data immediately
this.Socket.Unhold();
@@ -1029,7 +1032,7 @@ partial class DistributedConnection
// return;
//}
InvokeFunction(call.Value.Template, callback, arguments, IIPPacketRequest.ProcedureCall, call.Value.Delegate.Target);
InvokeFunction(call.Value.Template, callback, results, IIPPacketRequest.ProcedureCall, call.Value.Delegate.Target);
}).Error(x =>
{
@@ -1038,13 +1041,13 @@ partial class DistributedConnection
}
else
{
var arguments = (Map<byte, object>)parsed;
//var arguments = (Map<byte, object>)parsed;
// un hold the socket to send data immediately
this.Socket.Unhold();
// @TODO: Make managers for procedure calls
InvokeFunction(call.Value.Template, callback, arguments, IIPPacketRequest.ProcedureCall, call.Value.Delegate.Target);
InvokeFunction(call.Value.Template, callback, parsed, IIPPacketRequest.ProcedureCall, call.Value.Delegate.Target);
}
}
@@ -1090,7 +1093,7 @@ partial class DistributedConnection
{
reply.Then(results =>
{
var arguments = (Map<byte, object>)results;
//var arguments = (Map<byte, object>)results;
// un hold the socket to send data immediately
this.Socket.Unhold();
@@ -1104,7 +1107,7 @@ partial class DistributedConnection
// return;
//}
InvokeFunction(ft, callback, arguments, IIPPacketRequest.StaticCall, null);
InvokeFunction(ft, callback, results, IIPPacketRequest.StaticCall, null);
}).Error(x =>
{
@@ -1113,7 +1116,7 @@ partial class DistributedConnection
}
else
{
var arguments = (Map<byte, object>)parsed;
//var arguments = (Map<byte, object>)parsed;
// un hold the socket to send data immediately
this.Socket.Unhold();
@@ -1121,7 +1124,7 @@ partial class DistributedConnection
// @TODO: Make managers for static calls
InvokeFunction(ft, callback, arguments, IIPPacketRequest.StaticCall, null);
InvokeFunction(ft, callback, parsed, IIPPacketRequest.StaticCall, null);
}
}
@@ -1157,14 +1160,14 @@ partial class DistributedConnection
{
(parsed as AsyncReply).Then(result =>
{
var arguments = (Map<byte, object>)result;
// var arguments = result;
// un hold the socket to send data immediately
this.Socket.Unhold();
if (r is DistributedResource)
{
var rt = (r as DistributedResource)._Invoke(index, arguments);
var rt = (r as DistributedResource)._Invoke(index, result);
if (rt != null)
{
rt.Then(res =>
@@ -1187,20 +1190,20 @@ partial class DistributedConnection
return;
}
InvokeFunction(ft, callback, arguments, IIPPacketRequest.InvokeFunction, r);
InvokeFunction(ft, callback, result, IIPPacketRequest.InvokeFunction, r);
}
});
}
else
{
var arguments = (Map<byte, object>)parsed;
//var arguments = (Map<byte, object>)parsed;
// un hold the socket to send data immediately
this.Socket.Unhold();
if (r is DistributedResource)
{
var rt = (r as DistributedResource)._Invoke(index, arguments);
var rt = (r as DistributedResource)._Invoke(index, parsed);
if (rt != null)
{
rt.Then(res =>
@@ -1223,7 +1226,7 @@ partial class DistributedConnection
return;
}
InvokeFunction(ft, callback, arguments, IIPPacketRequest.InvokeFunction, r);
InvokeFunction(ft, callback, parsed, IIPPacketRequest.InvokeFunction, r);
}
}
@@ -1232,7 +1235,7 @@ partial class DistributedConnection
void InvokeFunction(FunctionTemplate ft, uint callback, Map<byte, object> arguments, IIPPacketRequest actionType, object target = null)
void InvokeFunction(FunctionTemplate ft, uint callback, object arguments, IIPPacketRequest actionType, object target = null)
{
// cast arguments
@@ -1246,15 +1249,36 @@ partial class DistributedConnection
{
if (pis.Last().ParameterType == typeof(DistributedConnection))
{
for (byte i = 0; i < pis.Length - 1; i++)
if (arguments is Map<byte, object> indexedArguments)
{
if (arguments.ContainsKey(i))
args[i] = RuntimeCaster.Cast(arguments[i], pis[i].ParameterType);
else if (ft.Arguments[i].Type.Nullable)
args[i] = null;
else
args[i] = Type.Missing;
for (byte i = 0; i < pis.Length - 1; i++)
{
if (indexedArguments.ContainsKey(i))
args[i] = RuntimeCaster.Cast(indexedArguments[i], pis[i].ParameterType);
else if (ft.Arguments[i].Type.Nullable)
args[i] = null;
else
args[i] = Type.Missing;
}
}
else if (arguments is object[] arrayArguments)
{
for (var i = 0; (i < arrayArguments.Length) && (i < pis.Length - 1); i++)
{
args[i] = RuntimeCaster.Cast(arrayArguments[i], pis[i].ParameterType);
}
for (var i = arrayArguments.Length; i < pis.Length - 1; i++)
{
args[i] = Type.Missing;
}
}
else
{
// assume first argument
// Note: if object[] is intended, sender should send nest it withing object[] { object[] }
if (pis.Length > 1)
args[0] = RuntimeCaster.Cast(arguments, pis[0].ParameterType);
}
args[args.Length - 1] = this;
@@ -1262,16 +1286,39 @@ partial class DistributedConnection
else if (pis.Last().ParameterType == typeof(InvocationContext))
{
context = new InvocationContext(this, callback);
for (byte i = 0; i < pis.Length - 1; i++)
if (arguments is Map<byte, object> indexedArguments)
{
if (arguments.ContainsKey(i))
args[i] = RuntimeCaster.Cast(arguments[i], pis[i].ParameterType);
else if (ft.Arguments[i].Type.Nullable)
args[i] = null;
else
args[i] = Type.Missing;
for (byte i = 0; i < pis.Length - 1; i++)
{
if (indexedArguments.ContainsKey(i))
args[i] = RuntimeCaster.Cast(indexedArguments[i], pis[i].ParameterType);
else if (ft.Arguments[i].Type.Nullable)
args[i] = null;
else
args[i] = Type.Missing;
}
}
else if (arguments is object[] arrayArguments)
{
for (var i = 0; (i < arrayArguments.Length) && (i < pis.Length - 1); i++)
{
args[i] = RuntimeCaster.Cast(arrayArguments[i], pis[i].ParameterType);
}
for (var i = arrayArguments.Length; i < pis.Length - 1; i++)
{
args[i] = Type.Missing;
}
}
else
{
// assume first argument
// Note: if object[] is intended, sender should send nest it withing object[] { object[] }
if (pis.Length > 1)
args[0] = RuntimeCaster.Cast(arguments, pis[0].ParameterType);
//throw new NotImplementedException("Arguments type not supported.");
}
args[args.Length - 1] = context;
@@ -1279,15 +1326,38 @@ partial class DistributedConnection
}
else
{
for (byte i = 0; i < pis.Length; i++)
if (arguments is Map<byte, object> indexedArguments)
{
if (arguments.ContainsKey(i))
args[i] = RuntimeCaster.Cast(arguments[i], pis[i].ParameterType);
else if (ft.Arguments[i].Type.Nullable) //Nullable.GetUnderlyingType(pis[i].ParameterType) != null)
args[i] = null;
else
args[i] = Type.Missing;
for (byte i = 0; i < pis.Length; i++)
{
if (indexedArguments.ContainsKey(i))
args[i] = RuntimeCaster.Cast(indexedArguments[i], pis[i].ParameterType);
else if (ft.Arguments[i].Type.Nullable) //Nullable.GetUnderlyingType(pis[i].ParameterType) != null)
args[i] = null;
else
args[i] = Type.Missing;
}
}
else if (arguments is object[] arrayArguments)
{
for (var i = 0; (i < arrayArguments.Length) && (i < pis.Length); i++)
{
args[i] = RuntimeCaster.Cast(arrayArguments[i], pis[i].ParameterType);
}
for (var i = arrayArguments.Length; i < pis.Length; i++)
{
args[i] = Type.Missing;
}
}
else
{
// assume first argument
// Note: if object[] is intended, sender should send nest it withing object[] { object[] }
if (pis.Length > 0)
args[0] = RuntimeCaster.Cast(arguments, pis[0].ParameterType);
}
}
}
@@ -1647,13 +1717,11 @@ partial class DistributedConnection
SendRequest(IIPPacketRequest.TemplateFromClassId, classId)
.Then((result) =>
{
Console.WriteLine("Parsing template...");
var tt = TypeTemplate.Parse((byte[])result);
templateRequests.Remove(classId);
templates.Add(tt.ClassId, tt);
Instance.Warehouse.PutTemplate(tt);
reply.Trigger(tt);
Console.WriteLine("Done parsing template...");
}).Error((ex) =>
{
@@ -1834,7 +1902,7 @@ partial class DistributedConnection
{
template = Instance.Warehouse.GetTemplateByClassId(classId, TemplateType.Resource);
if (template?.DefinedType != null && template.IsWrapper)
dr = Activator.CreateInstance(template.DefinedType, this, id, (ulong)args[1], (string)args[2]) as DistributedResource;
dr = Activator.CreateInstance(template.DefinedType, this, id,Convert.ToUInt64( args[1]), (string)args[2]) as DistributedResource;
else
dr = new DistributedResource(this, id, Convert.ToUInt64(args[1]), (string)args[2]);
}

View File

@@ -177,8 +177,8 @@ public class DistributedResource : DynamicObject, IResource, INotifyPropertyChan
var props = new PropertyValue[properties.Length];
for (byte i = 0; i < properties.Length; i++)
props[i] = new PropertyValue(properties[i],
Instance.GetAge(i),
props[i] = new PropertyValue(properties[i],
Instance.GetAge(i),
Instance.GetModificationDate(i));
return props;
@@ -193,7 +193,7 @@ public class DistributedResource : DynamicObject, IResource, INotifyPropertyChan
rt.Add(i, new PropertyValue(properties[i],
Instance.GetAge(i),
Instance.GetModificationDate(i)));
return rt;
}
@@ -238,7 +238,7 @@ public class DistributedResource : DynamicObject, IResource, INotifyPropertyChan
Instance.EmitResourceEvent(et, args);
}
public AsyncReply _Invoke(byte index, Map<byte, object> args)
public AsyncReply _Invoke(byte index, object args)
{
if (destroyed)
throw new Exception("Trying to access a destroyed object.");
@@ -312,7 +312,6 @@ public class DistributedResource : DynamicObject, IResource, INotifyPropertyChan
if (attached && ft != null)
{
var indexedArgs = new Map<byte, object>();
if (args.Length == 1)
{
@@ -322,6 +321,7 @@ public class DistributedResource : DynamicObject, IResource, INotifyPropertyChan
if (Codec.IsAnonymous(type))
{
var indexedArgs = new Map<byte, object>();
var pis = type.GetProperties();
@@ -334,18 +334,19 @@ public class DistributedResource : DynamicObject, IResource, INotifyPropertyChan
result = _Invoke(ft.Index, indexedArgs);
}
else if (args[0] is object[] || args[0] is Map<byte, object>)
{
result = _Invoke(ft.Index, new object[] { args });
}
else
{
indexedArgs.Add((byte)0, args[0]);
result = _Invoke(ft.Index, indexedArgs);
result = _Invoke(ft.Index, args);
}
}
else
{
for (byte i = 0; i < args.Length; i++)
indexedArgs.Add(i, args[i]);
result = _Invoke(ft.Index, indexedArgs);
result = _Invoke(ft.Index, args);
}
return true;
}