mirror of
https://github.com/esiur/esiur-dotnet.git
synced 2025-05-07 12:02:59 +00:00
events
This commit is contained in:
parent
d6e223384c
commit
0e865406eb
@ -120,7 +120,7 @@ public partial class DistributedConnection : NetworkConnection, IStore
|
|||||||
public DistributedServer Server { get; internal set; }
|
public DistributedServer Server { get; internal set; }
|
||||||
|
|
||||||
|
|
||||||
[Public] public virtual ConnectionStatus Status { get; set; }
|
[Export] public virtual ConnectionStatus Status { get; set; }
|
||||||
|
|
||||||
public bool Remove(IResource resource)
|
public bool Remove(IResource resource)
|
||||||
{
|
{
|
||||||
@ -356,7 +356,7 @@ public partial class DistributedConnection : NetworkConnection, IStore
|
|||||||
keepAliveTimer.Elapsed += KeepAliveTimer_Elapsed;
|
keepAliveTimer.Elapsed += KeepAliveTimer_Elapsed;
|
||||||
}
|
}
|
||||||
|
|
||||||
[Public] public virtual uint Jitter { get; set; }
|
[Export] public virtual uint Jitter { get; set; }
|
||||||
|
|
||||||
public uint KeepAliveTime { get; set; } = 10;
|
public uint KeepAliveTime { get; set; } = 10;
|
||||||
|
|
||||||
|
@ -74,13 +74,12 @@ public class ResourceGenerator : ISourceGenerator
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
static string SuggestPropertyName(string propertyName)
|
public static string SuggestExportName(string fieldName)
|
||||||
{
|
{
|
||||||
if (Char.IsUpper(propertyName[0]))
|
if (Char.IsUpper(fieldName[0]))
|
||||||
return propertyName.Substring(0, 1).ToLower() + propertyName.Substring(1);
|
return fieldName.Substring(0, 1).ToLower() + fieldName.Substring(1);
|
||||||
else
|
else
|
||||||
return propertyName.Substring(0, 1).ToUpper() + propertyName.Substring(1);
|
return fieldName.Substring(0, 1).ToUpper() + fieldName.Substring(1);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Execute(GeneratorExecutionContext context)
|
public void Execute(GeneratorExecutionContext context)
|
||||||
@ -173,16 +172,24 @@ namespace { ci.ClassSymbol.ContainingNamespace.ToDisplayString() } {{
|
|||||||
|
|
||||||
foreach (var f in ci.Fields)
|
foreach (var f in ci.Fields)
|
||||||
{
|
{
|
||||||
var givenName = f.GetAttributes().Where(x => x.AttributeClass.Name == "PublicAttribute").FirstOrDefault()?.ConstructorArguments.FirstOrDefault().Value as string;
|
var givenName = f.GetAttributes().Where(x => x.AttributeClass.Name == "ExportAttribute").FirstOrDefault()?.ConstructorArguments.FirstOrDefault().Value as string;
|
||||||
|
|
||||||
var fn = f.Name;
|
var fn = f.Name;
|
||||||
var pn =string.IsNullOrEmpty(givenName) ? SuggestPropertyName(fn): givenName;
|
var pn = string.IsNullOrEmpty(givenName) ? SuggestExportName(fn) : givenName;
|
||||||
|
|
||||||
//System.IO.File.AppendAllText("c:\\gen\\fields.txt", fn + " -> " + pn + "\r\n");
|
//System.IO.File.AppendAllText("c:\\gen\\fields.txt", fn + " -> " + pn + "\r\n");
|
||||||
|
|
||||||
// copy attributes
|
// copy attributes
|
||||||
var attrs = string.Join("\r\n\t", f.GetAttributes().Select(x => $"[{x.ToString()}]"));
|
var attrs = string.Join("\r\n\t", f.GetAttributes().Select(x => $"[{x.ToString()}]"));
|
||||||
code += $"\t{attrs}\r\n\t public {f.Type} {pn} {{ \r\n\t\t get => {fn}; \r\n\t\t set {{ \r\n\t\t this.{fn} = value; \r\n\t\t Instance?.Modified(); \r\n\t\t}}\r\n\t}}\r\n";
|
//Debugger.Launch();
|
||||||
|
if (f.Type.Name.StartsWith("ResourceEventHandler") || f.Type.Name.StartsWith("CustomResourceEventHandler"))
|
||||||
|
{
|
||||||
|
code += $"\t{attrs}\r\n\t public event {f.Type} {pn};\r\n";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
code += $"\t{attrs}\r\n\t public {f.Type} {pn} {{ \r\n\t\t get => {fn}; \r\n\t\t set {{ \r\n\t\t this.{fn} = value; \r\n\t\t Instance?.Modified(); \r\n\t\t}}\r\n\t}}\r\n";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
code += "}}\r\n";
|
code += "}}\r\n";
|
||||||
|
@ -50,7 +50,7 @@ public class ResourceGeneratorReceiver : ISyntaxContextReceiver
|
|||||||
var fields = cds.Members.Where(x => x is FieldDeclarationSyntax)
|
var fields = cds.Members.Where(x => x is FieldDeclarationSyntax)
|
||||||
.Select(x => context.SemanticModel.GetDeclaredSymbol((x as FieldDeclarationSyntax).Declaration.Variables.First()) as IFieldSymbol)
|
.Select(x => context.SemanticModel.GetDeclaredSymbol((x as FieldDeclarationSyntax).Declaration.Variables.First()) as IFieldSymbol)
|
||||||
.Where(x => !x.IsConst)
|
.Where(x => !x.IsConst)
|
||||||
.Where(x => x.GetAttributes().Any(a => a.AttributeClass.ToDisplayString() == "Esiur.Resource.PublicAttribute"))
|
.Where(x => x.GetAttributes().Any(a => a.AttributeClass.ToDisplayString() == "Esiur.Resource.ExportAttribute"))
|
||||||
.ToArray();
|
.ToArray();
|
||||||
|
|
||||||
//if (!Debugger.IsAttached)
|
//if (!Debugger.IsAttached)
|
||||||
|
@ -73,7 +73,7 @@ public static class ResourceProxy
|
|||||||
|
|
||||||
var props = from p in typeInfo.GetProperties(BindingFlags.Instance | BindingFlags.Public)
|
var props = from p in typeInfo.GetProperties(BindingFlags.Instance | BindingFlags.Public)
|
||||||
where p.CanWrite && p.SetMethod.IsVirtual && !p.SetMethod.IsFinal &&
|
where p.CanWrite && p.SetMethod.IsVirtual && !p.SetMethod.IsFinal &&
|
||||||
p.GetCustomAttribute<PublicAttribute>(false) != null
|
p.GetCustomAttribute<ExportAttribute>(false) != null
|
||||||
select p;
|
select p;
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
@ -6,11 +6,11 @@ namespace Esiur.Resource;
|
|||||||
|
|
||||||
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Method | AttributeTargets.Event | AttributeTargets.Class | AttributeTargets.Enum)]
|
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Method | AttributeTargets.Event | AttributeTargets.Class | AttributeTargets.Enum)]
|
||||||
|
|
||||||
public class PublicAttribute : Attribute
|
public class ExportAttribute : Attribute
|
||||||
{
|
{
|
||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
|
|
||||||
public PublicAttribute(string name = null)
|
public ExportAttribute(string name = null)
|
||||||
{
|
{
|
||||||
Name = name;
|
Name = name;
|
||||||
}
|
}
|
@ -3,7 +3,7 @@ using System.Collections.Generic;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace Esiur.Resource;
|
namespace Esiur.Resource;
|
||||||
public class PrivateAttribute : Attribute
|
public class IgnoreAttribute : Attribute
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
@ -588,6 +588,8 @@ public class Instance
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// internal void EmitResourceEvent(string name, string[] users, DistributedConnection[] connections, object[] args)
|
// internal void EmitResourceEvent(string name, string[] users, DistributedConnection[] connections, object[] args)
|
||||||
|
|
||||||
internal void EmitCustomResourceEvent(object issuer, Func<Session, bool> receivers, EventTemplate eventTemplate, object value)
|
internal void EmitCustomResourceEvent(object issuer, Func<Session, bool> receivers, EventTemplate eventTemplate, object value)
|
||||||
|
@ -373,7 +373,7 @@ public class TypeTemplate
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
public static ConstantTemplate MakeConstantTemplate(Type type, FieldInfo ci, PublicAttribute publicAttr, byte index = 0, TypeTemplate typeTemplate = null)
|
public static ConstantTemplate MakeConstantTemplate(Type type, FieldInfo ci, ExportAttribute exportAttr, byte index = 0, TypeTemplate typeTemplate = null)
|
||||||
{
|
{
|
||||||
var annotationAttr = ci.GetCustomAttribute<AnnotationAttribute>(true);
|
var annotationAttr = ci.GetCustomAttribute<AnnotationAttribute>(true);
|
||||||
var nullableAttr = ci.GetCustomAttribute<NullableAttribute>(true);
|
var nullableAttr = ci.GetCustomAttribute<NullableAttribute>(true);
|
||||||
@ -388,7 +388,7 @@ public class TypeTemplate
|
|||||||
if (typeTemplate.Type == TemplateType.Enum)
|
if (typeTemplate.Type == TemplateType.Enum)
|
||||||
value = Convert.ChangeType(value, ci.FieldType.GetEnumUnderlyingType());
|
value = Convert.ChangeType(value, ci.FieldType.GetEnumUnderlyingType());
|
||||||
|
|
||||||
var ct = new ConstantTemplate(typeTemplate, index, publicAttr?.Name ?? ci.Name, ci.DeclaringType != type, valueType, value, annotationAttr?.Annotation);
|
var ct = new ConstantTemplate(typeTemplate, index, exportAttr?.Name ?? ci.Name, ci.DeclaringType != type, valueType, value, annotationAttr?.Annotation);
|
||||||
|
|
||||||
return ct;
|
return ct;
|
||||||
|
|
||||||
@ -589,7 +589,7 @@ public class TypeTemplate
|
|||||||
|
|
||||||
while (type != null)
|
while (type != null)
|
||||||
{
|
{
|
||||||
var classIsPublic = type.IsEnum || (type.GetCustomAttribute<PublicAttribute>() != null);
|
var classIsPublic = type.IsEnum || (type.GetCustomAttribute<ExportAttribute>() != null);
|
||||||
|
|
||||||
if (classIsPublic)
|
if (classIsPublic)
|
||||||
{
|
{
|
||||||
@ -598,11 +598,11 @@ public class TypeTemplate
|
|||||||
.Where(x => x.MemberType == MemberTypes.Property || x.MemberType == MemberTypes.Field
|
.Where(x => x.MemberType == MemberTypes.Property || x.MemberType == MemberTypes.Field
|
||||||
|| x.MemberType == MemberTypes.Event || x.MemberType == MemberTypes.Method)
|
|| x.MemberType == MemberTypes.Event || x.MemberType == MemberTypes.Method)
|
||||||
.Where(x => !(x is FieldInfo c && !c.IsStatic))
|
.Where(x => !(x is FieldInfo c && !c.IsStatic))
|
||||||
.Where(x => x.GetCustomAttribute<PrivateAttribute>() == null)
|
.Where(x => x.GetCustomAttribute<IgnoreAttribute>() == null)
|
||||||
.Where(x => !(x is MethodInfo m && m.IsSpecialName))
|
.Where(x => !(x is MethodInfo m && m.IsSpecialName))
|
||||||
.Select(x => new MemberData()
|
.Select(x => new MemberData()
|
||||||
{
|
{
|
||||||
Name = x.GetCustomAttribute<PublicAttribute>()?.Name ?? x.Name,
|
Name = x.GetCustomAttribute<ExportAttribute>()?.Name ?? x.Name,
|
||||||
Info = x,
|
Info = x,
|
||||||
Order = order
|
Order = order
|
||||||
})
|
})
|
||||||
@ -618,11 +618,11 @@ public class TypeTemplate
|
|||||||
.Where(x => x.MemberType == MemberTypes.Property || x.MemberType == MemberTypes.Field
|
.Where(x => x.MemberType == MemberTypes.Property || x.MemberType == MemberTypes.Field
|
||||||
|| x.MemberType == MemberTypes.Event || x.MemberType == MemberTypes.Method)
|
|| x.MemberType == MemberTypes.Event || x.MemberType == MemberTypes.Method)
|
||||||
.Where(x => !(x is FieldInfo c && !c.IsStatic))
|
.Where(x => !(x is FieldInfo c && !c.IsStatic))
|
||||||
.Where(x => x.GetCustomAttribute<PublicAttribute>() != null)
|
.Where(x => x.GetCustomAttribute<ExportAttribute>() != null)
|
||||||
.Where(x => !(x is MethodInfo m && m.IsSpecialName))
|
.Where(x => !(x is MethodInfo m && m.IsSpecialName))
|
||||||
.Select(x => new MemberData
|
.Select(x => new MemberData
|
||||||
{
|
{
|
||||||
Name = x.GetCustomAttribute<PublicAttribute>()?.Name ?? x.Name,
|
Name = x.GetCustomAttribute<ExportAttribute>()?.Name ?? x.Name,
|
||||||
Info = x,
|
Info = x,
|
||||||
Order = order
|
Order = order
|
||||||
})
|
})
|
||||||
|
@ -375,8 +375,6 @@ public static class Warehouse
|
|||||||
|
|
||||||
public static async AsyncReply<IResource[]> Query(string path)
|
public static async AsyncReply<IResource[]> Query(string path)
|
||||||
{
|
{
|
||||||
var rt = new AsyncReply<IResource[]>();
|
|
||||||
|
|
||||||
var p = path.Trim().Split('/');
|
var p = path.Trim().Split('/');
|
||||||
IResource resource;
|
IResource resource;
|
||||||
|
|
||||||
@ -512,7 +510,7 @@ public static class Warehouse
|
|||||||
|
|
||||||
var res = await Query(path);
|
var res = await Query(path);
|
||||||
|
|
||||||
if (res.Length == 0)
|
if (res == null || res.Length == 0)
|
||||||
return default(T);
|
return default(T);
|
||||||
else
|
else
|
||||||
return (T)res.First();
|
return (T)res.First();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user