public static byte[] DataHandler(Evaluator evl, byte[] data) { var receive = BinFormater.Read(new MemoryStream(data)); try { var transactLog = new StringBuilder(); transactLog.AppendLine($"<< {receive}"); var res = evl.EvlSeq(receive); transactLog.AppendLine($">> {res}"); Log?.Invoke(transactLog.ToString().Trim()); if (res != null) { return(BinFormater.Write(res).ToArray()); } else { return(new byte[0]); } } catch (Exception e) { var ex = e; var sb = new StringBuilder(); while (ex != null) { sb.AppendLine(ex.Message); ex = ex.InnerException; } Log?.Invoke(sb.ToString()); return(BinFormater.Write(new SpecialMessage(sb.ToString())).ToArray()); } }
static string[] GetAllNames(MethodDescriptors[] descriptors) { var symbols = new List <string>(); symbols.AddRange(descriptors.Select(d => d.DefineName)); var types = descriptors.Select(d => IsNullable(d.ReturnType) ? Nullable.GetUnderlyingType(d.ReturnType) : d.ReturnType).Where(t => !(BinFormater.IsPrimitive(t) || t == typeof(void))).ToList(); types.AddRange(descriptors.SelectMany(d => d.Arguments.Select(p => IsNullable(p.ParameterType) ? Nullable.GetUnderlyingType(p.ParameterType) : p.ParameterType)).Where(t => !(BinFormater.IsPrimitive(t) || t == typeof(void)))); types = types.Distinct().ToList(); var stack = new Stack <Type>(); foreach (var tt in types) { stack.Push(tt); } var checkedTypes = new List <Type>(); while (stack.Any()) { var t = stack.Pop(); var internalTypes = new List <Type>(); foreach (var fi in t.GetFields()) { symbols.Add(fi.Name); internalTypes.Add(fi.FieldType); } foreach (var pt in t.GetProperties()) { symbols.Add(pt.Name); internalTypes.Add(pt.PropertyType); } foreach (var type in internalTypes.ToArray()) { if (type.IsGenericType) { internalTypes.AddRange(type.GetGenericArguments()); } } foreach (var internalType in internalTypes) { var it = IsNullable(internalType) ? Nullable.GetUnderlyingType(internalType) : internalType; if (!checkedTypes.Contains(it)) { if (!(BinFormater.IsPrimitive(it) || it == typeof(void))) { checkedTypes.Add(it); stack.Push(it); } } } } return(symbols.Distinct().ToArray()); }
private static void SerializeMember(Type memberType, string memberName, CodeMemberMethod serializeMethod, CodeMethodReferenceExpression serializeExpr, string objCasted, string current) { CodeExpression memberExpr = new CodeFieldReferenceExpression(new CodeArgumentReferenceExpression(objCasted), memberName); CodeExpression memberTypeExpr = new CodeTypeOfExpression(memberType); var serializeMemberExpr = new CodeMethodInvokeExpression(serializeExpr, new[] { memberExpr, memberTypeExpr }); var toSymbolExpr = new CodeObjectCreateExpression(typeof(Symbol)); toSymbolExpr.Parameters.Add(new CodePrimitiveExpression(memberName)); if (BinFormater.IsPrimitive(memberType)) { serializeMethod.Statements.Add( MAddConsAndAssign(new CodeVariableReferenceExpression(current), toSymbolExpr, memberExpr)); } else { serializeMethod.Statements.Add( new CodeConditionStatement( new CodeBinaryOperatorExpression(memberExpr, CodeBinaryOperatorType.IdentityEquality, new CodePrimitiveExpression(null)), new[] { MAddConsAndAssign(new CodeVariableReferenceExpression(current), toSymbolExpr, new CodePrimitiveExpression(null)) }, new[] { MAddConsAndAssign(new CodeVariableReferenceExpression(current), toSymbolExpr, serializeMemberExpr) })); } }
private static ICollection <string> GetTypeNamespaces(Type t) { var res = new List <string> { t.Namespace }; if (t.IsGenericType) { Array.ForEach <Type>(t.GetGenericArguments(), tt => res.AddRange(GetTypeNamespaces(tt))); } foreach (var mb in t.GetMembers(BindingFlags.Instance | BindingFlags.Public)) { if (mb is FieldInfo) { var f = (FieldInfo)mb; if (BinFormater.IsPrimitive(f.FieldType)) { continue; } res.AddRange(GetTypeNamespaces(f.FieldType)); } else if (mb is PropertyInfo) { var p = (PropertyInfo)mb; if (BinFormater.IsPrimitive(p.PropertyType)) { continue; } res.AddRange(GetTypeNamespaces(p.PropertyType)); } } return(res.Distinct().ToList()); }
static CodeMemberMethod CreateMethod(string methodName, ParameterInfo[] arguments, Type returnType) { var callMethod = new CodeMemberMethod(); callMethod.Attributes = MemberAttributes.Public | MemberAttributes.Final; callMethod.Name = methodName; callMethod.Parameters.AddRange(arguments .Select(a => new CodeParameterDeclarationExpression(GetProxyPType(a.ParameterType), a.Name)).ToArray()); callMethod.ReturnType = new CodeTypeReference(GetProxyPType(returnType)); var ImplRef = new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), NameOfImplementationField); var listBuilderParams = new List <CodeExpression>(); foreach (var arg in arguments) { var argRef = new CodeArgumentReferenceExpression(arg.Name); if (BinFormater.IsPrimitive(arg.ParameterType) || IsNullablePrimitive(arg.ParameterType)) { listBuilderParams.Add(argRef); } else { var deserializeArg = new CodeMethodInvokeExpression( new CodeTypeReferenceExpression(typeof(OSerializer)), nameof(OSerializer.Deserialize), argRef, new CodeTypeOfExpression(arg.ParameterType)); listBuilderParams.Add(new CodeCastExpression(arg.ParameterType, deserializeArg)); } } var invokeImpl = new CodeMethodInvokeExpression(new CodeMethodReferenceExpression(ImplRef, methodName), listBuilderParams.ToArray()); if (returnType == typeof(void)) { callMethod.Statements.Add(invokeImpl); } else if (BinFormater.IsPrimitive(returnType) || IsNullablePrimitive(returnType)) { callMethod.Statements.Add(new CodeMethodReturnStatement(invokeImpl)); } else { var serializeResult = new CodeMethodInvokeExpression( new CodeTypeReferenceExpression(typeof(OSerializer)), nameof(OSerializer.SerializeOnly), invokeImpl); callMethod.Statements.Add(new CodeMethodReturnStatement(serializeResult)); } return(callMethod); }
public object Call(object seq) { var resp = BinFormater.Read(new MemoryStream(Transfering.Send(BinFormater.Write(seq, _queryInterface.GetSymbolTable()).ToArray())), _queryInterface.GetSymbolTable()); if (resp.Car() is SpecialMessage) { OnSpecialMessageEvt(resp.Car() as SpecialMessage, seq); return(null); } return(resp?.Car()); }
public object Call(object seq) { if (_sendSync.Wait(5000)) { var resp = BinFormater.Read(new MemoryStream(_transfering.Send(BinFormater.Write(seq, _symbolTable).ToArray())), _symbolTable); if (resp.Car() is SpecialMessage) { OnSpecialMessageEvt(resp.Car() as SpecialMessage, seq); return(null); } return(resp.Car()); } else { // TODO connection was broken Console.WriteLine("Call error"); OnConnectionError(new Exception("Call error")); return(null); } }
private static void DeserializeMember(CodeMemberMethod deserializeMethod, Type memberType, string name) { var memberNameExpr = new CodeMethodInvokeExpression(MCar(new CodeVariableReferenceExpression("car")), "ToString"); if (BinFormater.IsPrimitive(memberType)) { deserializeMethod.Statements.Add( MIfEqualToMemberName(memberNameExpr, name, new CodeStatement[] { new CodeAssignStatement( new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("result"), name), MAsOperatorExpression(memberType, MCdr(new CodeArgumentReferenceExpression("car")))), MConsNext(new CodeVariableReferenceExpression("p")), new CodeGotoStatement("insideLoop") })); } else { deserializeMethod.Statements.Add( MIfEqualToMemberName(memberNameExpr, name, new CodeStatement[] { new CodeVariableDeclarationStatement(typeof(Cons), "prevalue", MAsOperatorExpression(typeof(Cons), MCdr(new CodeArgumentReferenceExpression("car")))), new CodeConditionStatement( new CodeBinaryOperatorExpression( new CodeVariableReferenceExpression("prevalue"), CodeBinaryOperatorType.IdentityInequality, new CodePrimitiveExpression(null)), new CodeStatement[] { new CodeAssignStatement( new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("result"), name), MAsOperatorExpression(memberType, MDeserializeExpression(memberType, new CodeArgumentReferenceExpression("prevalue")))) }), MConsNext(new CodeVariableReferenceExpression("p")), new CodeGotoStatement("insideLoop") })); } }
public static Func <object, Cons> GenerateSerializer(Type typeParam) { if (typeParam.GetInterfaces().Any(ie => ie == typeof(ICollection))) { var internalType = typeParam.GetInterface("ICollection`1").GetGenericArguments()[0]; return((obj) => { var result = new Cons(); var current = result; var collection = obj as ICollection; collection?.Cast <object>() .Aggregate(current, (current1, item) => current1.Add(BinFormater.IsPrimitive(internalType) ? item : item == null ? null : Serialize(item, internalType))); return result; }); } if (typeParam.IsEnum) { return((obj) => new Cons(Convert.ToInt32(obj))); } if (typeParam.IsGenericType && typeParam.GetGenericTypeDefinition() == typeof(Nullable <>)) { var internalType = typeParam.GetGenericArguments()[0]; return((obj) => { if (obj == null) { return null; } return BinFormater.IsPrimitive(internalType) ? new Cons(obj) : Serialize(obj, internalType); }); } return(GeneratePropsAndFieldsSerializer(typeParam)); }
static Type GetProxyPType(Type t) { return(BinFormater.IsPrimitive(t) || t == typeof(void) ? t : (IsNullablePrimitive(t) ? t : typeof(Cons))); //return BinFormater.IsPrimitive(t) || t == typeof(void) ? t : typeof(Cons); }
static bool IsNullablePrimitive(Type t) { return(IsNullable(t) && BinFormater.IsPrimitive(Nullable.GetUnderlyingType(t))); }
public object Call(object seq) { var resp = BinFormater.Read(new MemoryStream(Transfering.Send(BinFormater.Write(seq).ToArray()))); return(resp?.Car()); }
public object Call(object seq) { var resp = BinFormater.Read(new MemoryStream(Transfering.Send(BinFormater.Write(seq, _queryInterface.GetSymbolTable()).ToArray())), _queryInterface.GetSymbolTable()); return(resp?.Car()); }
private static Func <Cons, object> GenerateDeserializer(Type t) { var targetUnit = new CodeCompileUnit(); CodeNamespace samples = GetCodeNameSpace(t); var targetClass = GetCodeTypeDeclaration(); samples.Types.Add(targetClass); targetUnit.Namespaces.Add(samples); var methodName = $"Deserialize{t.GetHashCode()}"; CodeMemberMethod deserializeMethod = new CodeMemberMethod { Attributes = MemberAttributes.Public | MemberAttributes.Static, Name = methodName, ReturnType = new CodeTypeReference(typeof(object)) }; deserializeMethod.Parameters.Add(new CodeParameterDeclarationExpression(typeof(Cons), "pair")); if (t.IsArray) { deserializeMethod.Statements.Add(new CodeVariableDeclarationStatement(typeof(List <>).MakeGenericType(t.GetElementType()), "result", new CodeObjectCreateExpression(typeof(List <>).MakeGenericType(t.GetElementType())))); } else { deserializeMethod.Statements.Add(new CodeVariableDeclarationStatement(t, "result", new CodeObjectCreateExpression(t))); } if (t.GetInterfaces().Any(ie => ie == typeof(ICollection))) { var internalType = t.GetInterface("ICollection`1").GetGenericArguments()[0]; deserializeMethod.Statements.Add(new CodeLabeledStatement("insideLoop")); deserializeMethod.Statements.Add(new CodeConditionStatement( new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("pair"), CodeBinaryOperatorType.IdentityEquality, new CodePrimitiveExpression(null)), new[] { new CodeGotoStatement("outOfLoop") })); deserializeMethod.Statements.Add(new CodeVariableDeclarationStatement(typeof(object), "p", MCar(new CodeArgumentReferenceExpression("pair")))); deserializeMethod.Statements.Add(new CodeConditionStatement( new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("p"), CodeBinaryOperatorType.IdentityEquality, new CodePrimitiveExpression(null)), new[] { new CodeGotoStatement("outOfLoop") })); if (BinFormater.IsPrimitive(internalType)) { deserializeMethod.Statements.Add(new CodeVariableDeclarationStatement(internalType, "elem", MAsOperatorExpression(internalType, new CodeArgumentReferenceExpression("p")))); } else { deserializeMethod.Statements.Add(new CodeVariableDeclarationStatement(internalType, "elem", MAsOperatorExpression(internalType, MDeserializeExpression(internalType, new CodeArgumentReferenceExpression("p"))))); } if (t.GetInterfaces().Any(ie => ie == typeof(IDictionary))) { deserializeMethod.Statements.Add( MAddExpression(new CodeVariableReferenceExpression("result"), new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("elem"), "Key") , new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("elem"), "Value"))); } else { deserializeMethod.Statements.Add( MAddExpression(new CodeVariableReferenceExpression("result"), new CodeVariableReferenceExpression("elem"))); } deserializeMethod.Statements.Add(MConsNext(new CodeArgumentReferenceExpression("pair"))); deserializeMethod.Statements.Add(new CodeGotoStatement("insideLoop")); deserializeMethod.Statements.Add(new CodeLabeledStatement("outOfLoop")); } else if (t.IsEnum) { deserializeMethod.Statements.Add(new CodeVariableDeclarationStatement(typeof(object), "p", MCar(new CodeArgumentReferenceExpression("pair")))); deserializeMethod.Statements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("result"), MAsOperatorExpression(t, new CodeVariableReferenceExpression("p")))); } else if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable <>)) { var internalType = t.GetGenericArguments()[0]; if (BinFormater.IsPrimitive(internalType)) { deserializeMethod.Statements.Add(new CodeAssignStatement( new CodeVariableReferenceExpression("result"), MAsOperatorExpression(internalType, MCar(new CodeArgumentReferenceExpression("pair"))))); } else { deserializeMethod.Statements.Add(new CodeAssignStatement( new CodeVariableReferenceExpression("result"), MAsOperatorExpression(internalType, MDeserializeExpression(internalType, new CodeArgumentReferenceExpression("pair"))))); } } else if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(KeyValuePair <,>)) { var internalType1 = t.GetGenericArguments()[0]; var internalType2 = t.GetGenericArguments()[1]; deserializeMethod.Statements.Add(new CodeVariableDeclarationStatement(internalType1, "key")); deserializeMethod.Statements.Add(new CodeVariableDeclarationStatement(internalType2, "value")); if (BinFormater.IsPrimitive(internalType1)) { deserializeMethod.Statements.Add(new CodeAssignStatement( new CodeVariableReferenceExpression("key"), MAsOperatorExpression(internalType1, MCdr(MAsOperatorExpression(typeof(Cons), MCar(new CodeArgumentReferenceExpression("pair"))))))); } else { deserializeMethod.Statements.Add(new CodeAssignStatement( new CodeVariableReferenceExpression("key"), MAsOperatorExpression(internalType1, MDeserializeExpression(internalType1, MCdr(MAsOperatorExpression(typeof(Cons), MCar(new CodeArgumentReferenceExpression("pair")))))))); } deserializeMethod.Statements.Add(MConsNext(new CodeArgumentReferenceExpression("pair"))); if (BinFormater.IsPrimitive(internalType2)) { deserializeMethod.Statements.Add(new CodeAssignStatement( new CodeVariableReferenceExpression("value"), MAsOperatorExpression(internalType2, MCdr(MAsOperatorExpression(typeof(Cons), MCar(new CodeArgumentReferenceExpression("pair"))))))); } else { deserializeMethod.Statements.Add(new CodeAssignStatement( new CodeVariableReferenceExpression("value"), MAsOperatorExpression(internalType2, MDeserializeExpression(internalType2, MCdr(MAsOperatorExpression(typeof(Cons), MCar(new CodeArgumentReferenceExpression("pair")))))))); } deserializeMethod.Statements.Add(MConsNext(new CodeArgumentReferenceExpression("pair"))); deserializeMethod.Statements.Add(new CodeAssignStatement( new CodeVariableReferenceExpression("result"), new CodeObjectCreateExpression(t, new CodeVariableReferenceExpression("key"), new CodeVariableReferenceExpression("value")))); } else { deserializeMethod.Statements.Add(new CodeVariableDeclarationStatement(typeof(Cons), "p", new CodeArgumentReferenceExpression("pair"))); deserializeMethod.Statements.Add(new CodeLabeledStatement("insideLoop")); deserializeMethod.Statements.Add(new CodeConditionStatement( new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("p"), CodeBinaryOperatorType.IdentityEquality, new CodePrimitiveExpression(null)), new[] { new CodeGotoStatement("outOfLoop") })); deserializeMethod.Statements.Add(new CodeVariableDeclarationStatement(typeof(Cons), "car", MAsOperatorExpression(typeof(Cons), MCar(new CodeArgumentReferenceExpression("p"))))); deserializeMethod.Statements.Add(new CodeConditionStatement( new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("car"), CodeBinaryOperatorType.IdentityEquality, new CodePrimitiveExpression(null)), new[] { new CodeGotoStatement("outOfLoop") })); foreach (var mb in t.GetMembers(BindingFlags.Instance | BindingFlags.Public)) { if (mb is FieldInfo) { var f = (FieldInfo)mb; if (f.IsLiteral) { continue; } var memberType = f.FieldType; var name = f.Name; DeserializeMember(deserializeMethod, memberType, name); } else if (mb is PropertyInfo) { var p = (PropertyInfo)mb; var memberType = p.PropertyType; var name = p.Name; DeserializeMember(deserializeMethod, memberType, name); } } deserializeMethod.Statements.Add(MConsNext(new CodeVariableReferenceExpression("p"))); deserializeMethod.Statements.Add(new CodeGotoStatement("insideLoop")); deserializeMethod.Statements.Add(new CodeLabeledStatement("outOfLoop")); } CodeMethodReturnStatement returnStatement = new CodeMethodReturnStatement(); if (t.IsArray) { returnStatement.Expression = new CodeMethodInvokeExpression(new CodeVariableReferenceExpression("result"), "ToArray"); } else { returnStatement.Expression = new CodeVariableReferenceExpression("result"); } deserializeMethod.Statements.Add(returnStatement); targetClass.Members.Add(deserializeMethod); var type = CompileUnit(t, targetUnit) .CompiledAssembly.GetType($"{samples.Name}.{targetClass.Name}"); var method = type.GetMethod(methodName); return(pair => method?.Invoke(null, new[] { pair })); }
static CodeMemberMethod CreateMethod(string methodName, string defineName, ParameterInfo[] arguments, Type returnType) { var callMethod = new CodeMemberMethod(); callMethod.Attributes = MemberAttributes.Public | MemberAttributes.Final; callMethod.Name = methodName; callMethod.Parameters.AddRange(arguments.Select(a => new CodeParameterDeclarationExpression(a.ParameterType, a.Name)).ToArray()); callMethod.ReturnType = new CodeTypeReference(returnType); var tcpClientRef = new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), NameOfClientField); var symbolNameRef = new CodeObjectCreateExpression(typeof(Symbol), new CodePrimitiveExpression(defineName)); var listBuilderParams = new List<CodeExpression>(); listBuilderParams.Add(symbolNameRef); foreach (var arg in arguments) { //************************************************** //** TODO AT THIS LINE SHOULD BE THE SERIALIZER ** //************************************************** if (BinFormater.IsPrimitive(arg.ParameterType) || IsNullablePrimitive(arg.ParameterType)) { listBuilderParams.Add(new CodeArgumentReferenceExpression(arg.Name)); } else { listBuilderParams.Add( new CodeMethodInvokeExpression( new CodeMethodReferenceExpression( new CodeTypeReferenceExpression(typeof(OSerializer)) , nameof(OSerializer.SerializeWithQuote)) , new CodeArgumentReferenceExpression(arg.Name))); } } var consLst = new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(Cons)), nameof(Cons.List), listBuilderParams.ToArray()); var invoke = new CodeMethodInvokeExpression(new CodeMethodReferenceExpression(tcpClientRef, nameof(ISomeClient.Call)), consLst); // IS VOID METHOD if (returnType == typeof(void)) { callMethod.Statements.Add(invoke); } else { const string requestResultVarName = "requestResult"; callMethod.Statements.Add( new CodeVariableDeclarationStatement(typeof(object), requestResultVarName, invoke)); var requestResultRef = new CodeVariableReferenceExpression(requestResultVarName); var eql = new CodeBinaryOperatorExpression(requestResultRef, CodeBinaryOperatorType.ValueEquality, new CodePrimitiveExpression(null)); //**************************************************** //** TODO AT THIS LINE SHOULD BE THE DESERIALIZER ** //**************************************************** CodeExpression prepareResult = null; if (BinFormater.IsPrimitive(returnType) || IsNullablePrimitive(returnType)) { prepareResult = requestResultRef; } else { prepareResult = new CodeMethodInvokeExpression( new CodeTypeReferenceExpression(typeof(OSerializer)) , nameof(OSerializer.Deserialize) , new CodeCastExpression (typeof(Cons), requestResultRef) , new CodeTypeOfExpression(returnType)); } var castType = new CodeCastExpression(returnType, prepareResult); CodeStatement returnDefaultValue = null; if (returnType.IsValueType && Nullable.GetUnderlyingType(returnType) == null) { returnDefaultValue = new CodeMethodReturnStatement(new CodeObjectCreateExpression(returnType)); } else { returnDefaultValue = new CodeMethodReturnStatement(new CodePrimitiveExpression(null)); } var conditionalStatement = new CodeConditionStatement(eql, new[] {returnDefaultValue}, new[] {new CodeMethodReturnStatement(castType)}); callMethod.Statements.Add(conditionalStatement); } return callMethod; }