2
0
mirror of https://github.com/esiur/esiur-dotnet.git synced 2025-05-06 11:32:59 +00:00
This commit is contained in:
Ahmed Zamil 2020-02-26 03:14:22 +03:00
parent 7a21f6a928
commit fde1b1d8ad
39 changed files with 1108 additions and 502 deletions

View File

@ -3,6 +3,11 @@
<PropertyGroup> <PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework> <TargetFramework>netstandard2.0</TargetFramework>
<AssemblyName>Esyur.Stores.EntityCore</AssemblyName> <AssemblyName>Esyur.Stores.EntityCore</AssemblyName>
<Authors>Ahmed Kh. Zamil</Authors>
<Company>Esyur Foundation</Company>
<Description>Esyur for Entity Framework Core</Description>
<Product>Esyur Entity Framework Extension</Product>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@ -35,18 +35,28 @@ namespace Esyur.Stores.EntityCore
{ {
public static class EsyurExtensions public static class EsyurExtensions
{ {
public static T CreateResource<T>(this DbContext dbContext) where T:IResource public static T CreateResource<T>(this DbContext dbContext, object properties = null) where T:class,IResource
{ {
return dbContext.GetInfrastructure().CreateResource<T>(); return dbContext.GetInfrastructure().CreateResource<T>(properties);
} }
public static T CreateResource<T>(this IServiceProvider serviceProvider) where T:IResource public static T CreateResource<T>(this DbSet<T> dbSet, object properties = null) where T:class,IResource
{
var resource = dbSet.GetInfrastructure().CreateResource<T>(properties);
dbSet.Add(resource);
return resource;
}
public static T CreateResource<T>(this IServiceProvider serviceProvider, object properties = null) where T:class,IResource
{ {
var options = serviceProvider.GetService<IDbContextOptions>().FindExtension<EsyurExtensionOptions>(); var options = serviceProvider.GetService<IDbContextOptions>().FindExtension<EsyurExtensionOptions>();
var manager = options.Store.Instance.Managers.Count > 0 ? options.Store.Instance.Managers.First() : null;
return Warehouse.New<T>("", options.Store, null, manager); var resource = Warehouse.New<T>("", options.Store, null, null, null, properties);
resource.Instance.Managers.AddRange(options.Store.Instance.Managers.ToArray());
return resource;
} }
public static DbContextOptionsBuilder UseEsyur(this DbContextOptionsBuilder optionsBuilder, public static DbContextOptionsBuilder UseEsyur(this DbContextOptionsBuilder optionsBuilder,

View File

@ -11,7 +11,7 @@
<PackageProjectUrl>http://www.esyur.com</PackageProjectUrl> <PackageProjectUrl>http://www.esyur.com</PackageProjectUrl>
<RepositoryUrl>https://github.com/esyur/esyur-dotnet/</RepositoryUrl> <RepositoryUrl>https://github.com/esyur/esyur-dotnet/</RepositoryUrl>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild> <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Version>1.3.3</Version> <Version>1.4.0</Version>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@ -51,15 +51,15 @@ namespace Esyur.Stores.MongoDB
Dictionary<string, WeakReference> resources = new Dictionary<string, WeakReference>(); Dictionary<string, WeakReference> resources = new Dictionary<string, WeakReference>();
[ResourceEvent] [Public]
public event ResourceEventHanlder ResourceAdded; public event ResourceEventHanlder ResourceAdded;
[ResourceEvent] [Public]
public event ResourceEventHanlder ResourceRemoved; public event ResourceEventHanlder ResourceRemoved;
int count = 0; int count = 0;
[ResourceProperty] [Public]
public virtual int Count public virtual int Count
{ {
get get
@ -109,7 +109,7 @@ namespace Esyur.Stores.MongoDB
return true; return true;
} }
[ResourceFunction] [Public]
public bool Remove(IResource resource) public bool Remove(IResource resource)
{ {
var objectId = resource.Instance.Variables["objectId"].ToString(); var objectId = resource.Instance.Variables["objectId"].ToString();
@ -510,11 +510,11 @@ namespace Esyur.Stores.MongoDB
throw new NotImplementedException(); throw new NotImplementedException();
} }
[ResourceAttribute] [Attribute]
public string Connection { get; set; } public string Connection { get; set; }
[ResourceAttribute] [Attribute]
public string Collection { get; set; } public string Collection { get; set; }
[ResourceAttribute] [Attribute]
public string Database { get; set; } public string Database { get; set; }
public AsyncReply<bool> Trigger(ResourceTrigger trigger) public AsyncReply<bool> Trigger(ResourceTrigger trigger)
{ {
@ -725,7 +725,7 @@ namespace Esyur.Stores.MongoDB
AsyncReply<KeyList<PropertyTemplate, PropertyValue[]>> GetRecordByAge(IResource resource, ulong fromAge, ulong toAge) AsyncReply<KeyList<PropertyTemplate, PropertyValue[]>> GetRecordByAge(IResource resource, ulong fromAge, ulong toAge)
{ {
var properties = resource.Instance.Template.Properties.Where(x => x.Storage == StorageMode.Recordable).ToList(); var properties = resource.Instance.Template.Properties.Where(x => x.Recordable).ToList();
var reply = new AsyncReply<KeyList<PropertyTemplate, PropertyValue[]>>(); var reply = new AsyncReply<KeyList<PropertyTemplate, PropertyValue[]>>();
@ -751,7 +751,7 @@ namespace Esyur.Stores.MongoDB
public AsyncReply<KeyList<PropertyTemplate, PropertyValue[]>> GetRecord(IResource resource, DateTime fromDate, DateTime toDate) public AsyncReply<KeyList<PropertyTemplate, PropertyValue[]>> GetRecord(IResource resource, DateTime fromDate, DateTime toDate)
{ {
var properties = resource.Instance.Template.Properties.Where(x => x.Storage == StorageMode.Recordable).ToList(); var properties = resource.Instance.Template.Properties.Where(x => x.Recordable).ToList();
var reply = new AsyncReply<KeyList<PropertyTemplate, PropertyValue[]>>(); var reply = new AsyncReply<KeyList<PropertyTemplate, PropertyValue[]>>();

View File

@ -35,13 +35,15 @@ namespace Esyur.Stores.MongoDB
{ {
public class MongoDBStore<T> : MongoDBStore where T:IResource public class MongoDBStore<T> : MongoDBStore where T:IResource
{ {
[ResourceFunction] [Public]
public T Create(string name, Structure values) public T New(string name = null, object properties = null)
{ {
return Warehouse.New<T>(name, this, null, null, null, null, values); var resource = Warehouse.New<T>(name, this, null, null, null, properties);
resource.Instance.Managers.AddRange(this.Instance.Managers.ToArray());
return resource;
} }
[ResourceFunction] [Public]
public async AsyncReply<IResource[]> Slice(int index, int limit) public async AsyncReply<IResource[]> Slice(int index, int limit)
{ {
var list = await this.Instance.Children<IResource>(); var list = await this.Instance.Children<IResource>();

View File

@ -32,7 +32,7 @@ using System.Reflection;
namespace Esyur.Data namespace Esyur.Data
{ {
public class AutoList<T, ST> : IEnumerable<T> public class AutoList<T, ST> : IEnumerable<T>, ICollection, ICollection<T>
{ {
private readonly object syncRoot = new object(); private readonly object syncRoot = new object();
@ -239,6 +239,10 @@ namespace Esyur.Data
get { return list.Count; } get { return list.Count; }
} }
public bool IsSynchronized => (list as ICollection).IsSynchronized;
public bool IsReadOnly => throw new NotImplementedException();
/// <summary> /// <summary>
/// Check if an item exists in the list /// Check if an item exists in the list
@ -282,5 +286,20 @@ namespace Esyur.Data
{ {
return ((IEnumerable<T>)list).GetEnumerator(); return ((IEnumerable<T>)list).GetEnumerator();
} }
public void CopyTo(Array array, int index)
{
(list as ICollection).CopyTo(array, index);
}
public void CopyTo(T[] array, int arrayIndex)
{
list.CopyTo(array, arrayIndex);
}
bool ICollection<T>.Remove(T item)
{
return list.Remove(item);
}
} }
} }

View File

@ -43,13 +43,9 @@ namespace Esyur.Data
{ {
public static object CastConvert(object value, Type destinationType) public static object CastConvert(object value, Type destinationType)
{ {
if (value == null) if (value == null)
return null; return null;
//if (destinationType.IsArray && destinationType.GetElementType().IsArray)
// Console.Beep();
var sourceType = value.GetType(); var sourceType = value.GetType();
if (destinationType == sourceType) if (destinationType == sourceType)
@ -69,22 +65,6 @@ namespace Esyur.Data
for (var i = 0; i < rt.Length; i++) for (var i = 0; i < rt.Length; i++)
{ {
rt.SetValue(CastConvert(v.GetValue(i), destinationType), i); rt.SetValue(CastConvert(v.GetValue(i), destinationType), i);
// try
// {
//#if NETSTANDARD
// if (destinationType.GetTypeInfo().IsInstanceOfType(v.GetValue(i)))
//#else
// if (destinationType.IsInstanceOfType(v.GetValue(i)))
//#endif
// rt.SetValue(v.GetValue(i), i);
// else
// rt.SetValue(Convert.ChangeType(v.GetValue(i), destinationType), i);
// }
// catch
// {
// rt.SetValue(null, i);
// }
} }
return rt; return rt;
@ -94,8 +74,6 @@ namespace Esyur.Data
{ {
try try
{ {
var underType = Nullable.GetUnderlyingType(destinationType); var underType = Nullable.GetUnderlyingType(destinationType);
if (underType != null) if (underType != null)
{ {
@ -105,7 +83,6 @@ namespace Esyur.Data
destinationType = underType; destinationType = underType;
} }
if (destinationType.IsInstanceOfType(value)) if (destinationType.IsInstanceOfType(value))
{ {
return value; return value;
@ -134,8 +111,6 @@ namespace Esyur.Data
} }
public static byte[] ToBytes(sbyte value) public static byte[] ToBytes(sbyte value)
{ {
return new byte[1] { (byte)value }; return new byte[1] { (byte)value };
@ -602,8 +577,6 @@ namespace Esyur.Data
return rt; return rt;
} }
public static bool TryParse<T>(object Input, out T Results) public static bool TryParse<T>(object Input, out T Results)
{ {
try try
@ -918,20 +891,6 @@ namespace Esyur.Data
return rt; return rt;
} }
/*
public static PhysicalAddress GetPhysicalAddress(this byte[] data, uint offset)
{
return new PhysicalAddress(Clip(data, offset, 6));
}
public static PhysicalAddress[] GetPhysicalAddressArray(this byte[] data, uint offset, uint length)
{
var rt = new PhysicalAddress[length / 6];
for (var i = 0; i < length; i += 6)
rt[i] = GetPhysicalAddress(data, (uint)(offset + i));
return rt;
}
*/
public static IPAddress GetIPv4Address(this byte[] data, uint offset) public static IPAddress GetIPv4Address(this byte[] data, uint offset)
{ {
return new IPAddress((long)GetUInt32(data, offset)); return new IPAddress((long)GetUInt32(data, offset));
@ -958,83 +917,7 @@ namespace Esyur.Data
return rt; return rt;
} }
/*
public static T FromBytes<T>(byte[] data, uint offset, uint length = 0)
{
if (typeof(T) == typeof(bool))
{
return (T)(object)(data[offset] == 1);
}
else if (typeof(T) == typeof(byte))
{
return (T)(object)data[offset];
}
else if (typeof(T) == typeof(char))
{
return (T)(object)BitConverter.ToChar(ReverseArray(data, offset, 2), 0);
}
else if (typeof(T) == typeof(short))
{
return (T)(object)BitConverter.ToInt16(ReverseArray(data, offset, 2), 0);
}
else if (typeof(T) == typeof(ushort))
{
return (T)(object)BitConverter.ToUInt16(ReverseArray(data, offset, 2), 0);
}
else if (typeof(T) == typeof(int))
{
return (T)(object)BitConverter.ToInt32(ReverseArray(data, offset, 4), 0);
}
else if (typeof(T) == typeof(uint))
{
return (T)(object)BitConverter.ToUInt32(ReverseArray(data, offset, 4), 0);
}
else if (typeof(T) == typeof(long))
{
return (T)(object)BitConverter.ToInt64(ReverseArray(data, offset, 8), 0);
}
else if (typeof(T) == typeof(ulong))
{
return (T)(object)BitConverter.ToUInt64(ReverseArray(data, offset, 8), 0);
}
else if (typeof(T) == typeof(float))
{
return (T)(object)BitConverter.ToSingle(ReverseArray(data, offset, 4), 0);
}
else if (typeof(T) == typeof(double))
{
return (T)(object)BitConverter.ToDouble(ReverseArray(data, offset, 8), 0);
}
else if (typeof(T) == typeof(string))
{
return (T)(object)Encoding.UTF8.GetString(data, (int)offset, (int)length);
}
else if (typeof(T) == typeof(Guid))
{
return (T)(object)new Guid(DC.Clip(data, offset, 16));
}
else if (typeof(T) == typeof(IPAddress))
{
if (length == 0)
return (T)(object)(new IPAddress((long)GetUInt32(data, offset)));
else
return (T)(object)(new IPAddress(Clip(data, offset, length)));
}
else if (typeof(T) == typeof(PhysicalAddress))
{
return (T)(object)new PhysicalAddress(Clip(data, offset, 6));
}
else if (typeof(T) == typeof(DateTime))
{
long ticks = BitConverter.ToInt64(ReverseArray(data, offset, 8), 0);
return (T)(object)new DateTime(ticks, DateTimeKind.Utc);
}
else
{
return default(T);
}
}
*/
public static byte[] Clip(this byte[] data, uint offset, uint length) public static byte[] Clip(this byte[] data, uint offset, uint length)
{ {

274
Esyur/Data/ResourceList.cs Normal file
View File

@ -0,0 +1,274 @@
/*
Copyright (c) 2020 Ahmed Kh. Zamil
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using Esyur.Core;
using System.Reflection;
namespace Esyur.Data
{
public class ResourceList<T, ST> : IEnumerable<T>, ICollection, ICollection<T>
{
private readonly object syncRoot = new object();
private List<T> list = new List<T>();
public delegate void Modified(ST sender, int index, T oldValue, T newValue);
public delegate void Added(ST sender, T value);
public delegate void Removed(ST sender, int index, T value);
public delegate void Cleared(ST sender);
public event Modified OnModified;
public event Removed OnRemoved;
public event Cleared OnCleared;
public event Added OnAdd;
ST state;
public void Sort()
{
list.Sort();
}
public void Sort(IComparer<T> comparer)
{
list.Sort(comparer);
}
public void Sort(Comparison<T> comparison)
{
list.Sort(comparison);
}
public IEnumerable<T> Where(Func<T, bool> predicate)
{
return list.Where(predicate);
}
/// <summary>
/// Convert AutoList to array
/// </summary>
/// <returns>Array</returns>
public T[] ToArray()
{
// list.OrderBy()
return list.ToArray();
}
/// <summary>
/// Create a new instance of AutoList
/// </summary>
/// <param name="state">State object to be included when an event is raised.</param>
public ResourceList(ST state)
{
this.state = state;
}
/// <summary>
/// Create a new instance of AutoList
/// </summary>
/// <param name="values">Populate the list with items</param>
/// <returns></returns>
public ResourceList(ST state, T[] values)
{
this.state = state;
AddRange(values);
}
/// <summary>
/// Synchronization lock of the list
/// </summary>
public object SyncRoot
{
get
{
return syncRoot;
}
}
/// <summary>
/// First item in the list
/// </summary>
public T First()
{
return list.First();
}
/// <summary>
/// Get an item at a specified index
/// </summary>
public T this[int index]
{
get
{
return list[index];
}
set
{
var oldValue = list[index];
lock (syncRoot)
list[index] = value;
OnModified?.Invoke(state, index, oldValue, value);
}
}
/// <summary>
/// Add item to the list
/// </summary>
public void Add(T value)
{
lock (syncRoot)
list.Add(value);
OnAdd?.Invoke(state, value);
}
/// <summary>
/// Add an array of items to the list
/// </summary>
public void AddRange(T[] values)
{
foreach (var v in values)
Add(v);
}
private void ItemDestroyed(object sender)
{
Remove((T)sender);
}
/// <summary>
/// Clear the list
/// </summary>
public void Clear()
{
lock (syncRoot)
list.Clear();
OnCleared?.Invoke(state);
}
/// <summary>
/// Remove an item from the list
/// <param name="value">Item to remove</param>
/// </summary>
public void Remove(T value)
{
var index = 0;
lock (syncRoot)
{
index = list.IndexOf(value);
if (index == -1)
return;
list.RemoveAt(index);
}
OnRemoved?.Invoke(state, index, value);
}
/// <summary>
/// Number of items in the list
/// </summary>
public int Count
{
get { return list.Count; }
}
public bool IsSynchronized => (list as ICollection).IsSynchronized;
public bool IsReadOnly => throw new NotImplementedException();
/// <summary>
/// Check if an item exists in the list
/// </summary>
/// <param name="value">Item to check if exists</param>
public bool Contains(T value)
{
return list.Contains(value);
}
/// <summary>
/// Check if any item of the given array is in the list
/// </summary>
/// <param name="values">Array of items</param>
public bool ContainsAny(T[] values)
{
foreach (var v in values)
if (list.Contains(v))
return true;
return false;
}
/// <summary>
/// Check if any item of the given list is in the list
/// </summary>
/// <param name="values">List of items</param>
public bool ContainsAny(AutoList<T, ST> values)
{
foreach (var v in values)
if (list.Contains((T)v))
return true;
return false;
}
public IEnumerator<T> GetEnumerator()
{
return ((IEnumerable<T>)list).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable<T>)list).GetEnumerator();
}
public void CopyTo(Array array, int index)
{
(list as ICollection).CopyTo(array, index);
}
public void CopyTo(T[] array, int arrayIndex)
{
list.CopyTo(array, arrayIndex);
}
bool ICollection<T>.Remove(T item)
{
return list.Remove(item);
}
}
}

View File

@ -7,7 +7,7 @@
<PackageLicenseUrl>https://github.com/Esyur/Esyur-dotnet/blob/master/LICENSE</PackageLicenseUrl> <PackageLicenseUrl>https://github.com/Esyur/Esyur-dotnet/blob/master/LICENSE</PackageLicenseUrl>
<PackageProjectUrl>http://www.esyur.com</PackageProjectUrl> <PackageProjectUrl>http://www.esyur.com</PackageProjectUrl>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>1.4.9</Version> <Version>1.5.0</Version>
<RepositoryUrl>https://github.com/esyur/esyur-dotnet</RepositoryUrl> <RepositoryUrl>https://github.com/esyur/esyur-dotnet</RepositoryUrl>
<Authors>Ahmed Kh. Zamil</Authors> <Authors>Ahmed Kh. Zamil</Authors>
<AssemblyVersion>1.3.1.0</AssemblyVersion> <AssemblyVersion>1.3.1.0</AssemblyVersion>
@ -29,7 +29,9 @@
<ItemGroup> <ItemGroup>
<Compile Remove="Core\AsyncReplyNon.cs" /> <Compile Remove="Core\AsyncReplyNon.cs" />
<Compile Remove="Core\IAsyncReply.cs" /> <Compile Remove="Core\IAsyncReply.cs" />
<Compile Remove="Net\UDP\UDPServer.cs" /> <Compile Remove="Resource\ResourceEvent.cs" />
<Compile Remove="Resource\ResourceFunction.cs" />
<Compile Remove="Resource\ResourceProperty.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
@ -40,7 +42,9 @@
<ItemGroup> <ItemGroup>
<None Include="Core\AsyncReplyNon.cs" /> <None Include="Core\AsyncReplyNon.cs" />
<None Include="Core\IAsyncReply.cs" /> <None Include="Core\IAsyncReply.cs" />
<None Include="Net\UDP\UDPServer.cs" /> <None Include="Resource\ResourceEvent.cs" />
<None Include="Resource\ResourceFunction.cs" />
<None Include="Resource\ResourceProperty.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -141,6 +141,12 @@ namespace Esyur.Net.HTTP
} }
} }
public void Send(WebsocketPacket packet)
{
if (packet.Data != null)
base.Send(packet.Data);
}
public override void Send(string data) public override void Send(string data)
{ {
Response.Message = Encoding.UTF8.GetBytes(data); Response.Message = Encoding.UTF8.GetBytes(data);

View File

@ -44,7 +44,7 @@ namespace Esyur.Net.HTTP
public class HTTPServer : NetworkServer<HTTPConnection>, IResource public class HTTPServer : NetworkServer<HTTPConnection>, IResource
{ {
Dictionary<string, HTTPSession> sessions= new Dictionary<string, HTTPSession>(); Dictionary<string, HTTPSession> sessions= new Dictionary<string, HTTPSession>();
HTTPFilter[] filters = null; HTTPFilter[] filters = new HTTPFilter[0];
public Instance Instance public Instance Instance
{ {
@ -52,63 +52,55 @@ namespace Esyur.Net.HTTP
set; set;
} }
[Storable] [Attribute]
public virtual string ip public virtual string IP
{
get;
set;
}
[Storable]
public virtual ushort port
{ {
get; get;
set; set;
} }
[Storable] [Attribute]
public virtual uint timeout public virtual ushort Port
{ {
get; get;
set; set;
} }
[Storable] [Attribute]
public virtual uint clock public virtual uint Timeout
{ {
get; get;
set; set;
} }
[Storable] [Attribute]
public virtual uint maxPost public virtual uint Clock
{ {
get; get;
set; set;
} }
[Storable] [Attribute]
public virtual bool ssl public virtual uint MaxPost
{ {
get; get;
set; set;
} }
[Storable] [Attribute]
public virtual string certificate public virtual bool SSL
{ {
get; get;
set; set;
} }
//public override void ClientConnected(TClient Sender) [Attribute]
//{ public virtual string Certificate
//}
/*
public DStringDictionary Configurations
{ {
get { return config; } get;
set;
} }
*/
public enum ResponseCodes : int public enum ResponseCodes : int
{ {
@ -133,27 +125,6 @@ namespace Esyur.Net.HTTP
return s; return s;
} }
/*
protected override void SessionModified(NetworkSession session, string key, object oldValue, object newValue)
{
foreach (var instance in Instance.Children)
{
var f = (HTTPFilter)instance;
f.SessionModified(session as HTTPSession, key, oldValue, newValue);
}
}
*/
//public override object InitializeLifetimeService()
//{
// return null;
//}
public static string MakeCookie(string Item, string Value, DateTime Expires, string Domain, string Path, bool HttpOnly) public static string MakeCookie(string Item, string Value, DateTime Expires, string Domain, string Path, bool HttpOnly)
{ {
@ -222,11 +193,11 @@ namespace Esyur.Net.HTTP
} }
else if (BL > 0) else if (BL > 0)
{ {
if (BL > maxPost) if (BL > MaxPost)
{ {
sender.Send( sender.Send(
"<html><body>POST method content is larger than " "<html><body>POST method content is larger than "
+ maxPost + MaxPost
+ " bytes.</body></html>"); + " bytes.</body></html>");
sender.Close(); sender.Close();
@ -326,7 +297,7 @@ namespace Esyur.Net.HTTP
*/ */
public AsyncReply<bool> Trigger(ResourceTrigger trigger) public async AsyncReply<bool> Trigger(ResourceTrigger trigger)
{ {
if (trigger == ResourceTrigger.Initialize) if (trigger == ResourceTrigger.Initialize)
@ -341,19 +312,19 @@ namespace Esyur.Net.HTTP
ISocket listener; ISocket listener;
IPAddress ipAdd; IPAddress ipAdd;
if (ip == null) if (IP == null)
ipAdd = IPAddress.Any; ipAdd = IPAddress.Any;
else else
ipAdd = IPAddress.Parse(ip); ipAdd = IPAddress.Parse(IP);
// if (ssl) // if (ssl)
// listener = new SSLSocket(new IPEndPoint(ipAdd, port), new X509Certificate2(certificate)); // listener = new SSLSocket(new IPEndPoint(ipAdd, port), new X509Certificate2(certificate));
// else // else
listener = new TCPSocket(new IPEndPoint(ipAdd, port)); listener = new TCPSocket(new IPEndPoint(ipAdd, Port));
Start(listener, Start(listener,
timeout, Timeout,
clock); Clock);
} }
else if (trigger == ResourceTrigger.Terminate) else if (trigger == ResourceTrigger.Terminate)
{ {
@ -361,15 +332,15 @@ namespace Esyur.Net.HTTP
} }
else if (trigger == ResourceTrigger.SystemReload) else if (trigger == ResourceTrigger.SystemReload)
{ {
Trigger(ResourceTrigger.Terminate); await Trigger(ResourceTrigger.Terminate);
Trigger(ResourceTrigger.Initialize); await Trigger(ResourceTrigger.Initialize);
} }
else if (trigger == ResourceTrigger.SystemInitialized) else if (trigger == ResourceTrigger.SystemInitialized)
{ {
Instance.Children<HTTPFilter>().Then(x => filters = x); filters = await Instance.Children<HTTPFilter>();
} }
return new AsyncReply<bool>(true); return true;
} }
@ -380,7 +351,7 @@ namespace Esyur.Net.HTTP
sender.SetParent(this); sender.SetParent(this);
//Console.WriteLine("IN: " + this.Connections.Count); //Console.WriteLine("IN: " + this.Connections.Count);
if (filters == null) if (filters.Length == 0)
{ {
sender.Close(); sender.Close();
return; return;

View File

@ -36,8 +36,8 @@ namespace Esyur.Net.HTTP
{ {
public class IIPoWS: HTTPFilter public class IIPoWS: HTTPFilter
{ {
[ResourceProperty] [Attribute]
public DistributedServer DistributedServer public DistributedServer Server
{ {
get; get;
set; set;
@ -48,7 +48,7 @@ namespace Esyur.Net.HTTP
if (sender.IsWebsocketRequest()) if (sender.IsWebsocketRequest())
{ {
if (DistributedServer == null) if (Server == null)
return false; return false;
var tcpSocket = sender.Unassign(); var tcpSocket = sender.Unassign();
@ -62,7 +62,7 @@ namespace Esyur.Net.HTTP
var iipConnection = new DistributedConnection(); var iipConnection = new DistributedConnection();
DistributedServer.AddConnection(iipConnection); Server.AddConnection(iipConnection);
iipConnection.Assign(wsSocket); iipConnection.Assign(wsSocket);
wsSocket.Begin(); wsSocket.Begin();

View File

@ -801,13 +801,13 @@ namespace Esyur.Net.IIP
} }
[ResourceAttribute] [Attribute]
public string Username { get; set; } public string Username { get; set; }
[ResourceAttribute] [Attribute]
public string Password { get; set; } public string Password { get; set; }
[ResourceAttribute] [Attribute]
public string Domain { get; set; } public string Domain { get; set; }
/// <summary> /// <summary>
/// Resource interface /// Resource interface

View File

@ -39,48 +39,44 @@ namespace Esyur.Net.IIP
{ {
public class DistributedServer : NetworkServer<DistributedConnection>, IResource public class DistributedServer : NetworkServer<DistributedConnection>, IResource
{ {
[Attribute]
//[Storable] public string IP
//[ResourceProperty]
public string ip
{ {
get; get;
set; set;
} }
//[Storable] [Attribute]
//[ResourceProperty]
public IMembership Membership public IMembership Membership
{ {
get; get;
set; set;
} }
[Attribute]
public EntryPoint EntryPoint public EntryPoint EntryPoint
{ {
get; get;
set; set;
} }
//[Storable] [Attribute]
//[ResourceProperty] public ushort Port
public ushort port
{ {
get; get;
set; set;
} }
//[Storable] [Attribute]
//[ResourceProperty] public uint Timeout
public uint timeout
{ {
get; get;
set; set;
} }
//[Storable]
//[ResourceProperty] [Attribute]
public uint clock public uint Clock
{ {
get; get;
set; set;
@ -99,12 +95,12 @@ namespace Esyur.Net.IIP
{ {
TCPSocket listener; TCPSocket listener;
if (ip != null) if (IP != null)
listener = new TCPSocket(new IPEndPoint(IPAddress.Parse(ip), port)); listener = new TCPSocket(new IPEndPoint(IPAddress.Parse(IP), Port));
else else
listener = new TCPSocket(new IPEndPoint(IPAddress.Any, port)); listener = new TCPSocket(new IPEndPoint(IPAddress.Any, Port));
Start(listener, timeout, clock); Start(listener, Timeout, Clock);
} }
else if (trigger == ResourceTrigger.Terminate) else if (trigger == ResourceTrigger.Terminate)
{ {

View File

@ -35,6 +35,6 @@ namespace Esyur.Net.IIP
{ {
public abstract AsyncReply<IResource[]> Query(string path, DistributedConnection sender); public abstract AsyncReply<IResource[]> Query(string path, DistributedConnection sender);
public abstract override bool Create(); protected abstract override bool Create();
} }
} }

View File

@ -31,18 +31,19 @@ using Esyur.Core;
using Esyur.Net.Sockets; using Esyur.Net.Sockets;
using Esyur.Resource; using Esyur.Resource;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Diagnostics;
namespace Esyur.Net namespace Esyur.Net
{ {
public abstract class NetworkServer<TConnection>: IDestructible where TConnection : NetworkConnection, new() public abstract class NetworkServer<TConnection> : IDestructible where TConnection : NetworkConnection, new()
{ {
//private bool isRunning; //private bool isRunning;
uint clock; uint clock;
private ISocket listener; private ISocket listener;
private AutoList<TConnection, NetworkServer<TConnection>> connections; private AutoList<TConnection, NetworkServer<TConnection>> connections;
//private Thread thread; private Thread thread;
protected abstract void DataReceived(TConnection sender, NetworkBuffer data); protected abstract void DataReceived(TConnection sender, NetworkBuffer data);
protected abstract void ClientConnected(TConnection sender); protected abstract void ClientConnected(TConnection sender);
@ -179,14 +180,21 @@ namespace Esyur.Net
listener = socket; listener = socket;
// Start accepting // Start accepting
var r = listener.Accept(); //var r = listener.Accept();
r.Then(NewConnection); //r.Then(NewConnection);
//r.timeout?.Dispose(); //r.timeout?.Dispose();
//var rt = listener.Accept().Then() //var rt = listener.Accept().Then()
//thread = new Thread(new System.Threading.ThreadStart(ListenForConnections)); thread = new Thread(new ThreadStart(() =>
{
while (true)
{
var s = listener.Accept();
NewConnection(s);
}
}));
//thread.Start(); thread.Start();
} }
@ -268,28 +276,16 @@ namespace Esyur.Net
private void NewConnection(ISocket sock) private void NewConnection(ISocket sock)
{ {
try try
{ {
/*
if (listener.State == SocketState.Closed || listener.State == SocketState.Terminated)
{
Console.WriteLine("Listen socket break ");
Console.WriteLine(listener.LocalEndPoint.Port);
break;
}
*/
if (sock == null) if (sock == null)
{ {
//Console.Write("sock == null"); Console.Write("sock == null");
return; return;
} }
//sock.ReceiveBufferSize = 102400;
//sock.SendBufferSize = 102400;
TConnection c = new TConnection(); TConnection c = new TConnection();
AddConnection(c); AddConnection(c);
@ -305,10 +301,9 @@ namespace Esyur.Net
// something wrong with the child. // something wrong with the child.
} }
// Accept more
listener.Accept().Then(NewConnection);
//l.timeout?.Dispose();
// Accept more
//listener.Accept().Then(NewConnection);
sock.Begin(); sock.Begin();

View File

@ -58,7 +58,9 @@ namespace Esyur.Net.Sockets
AsyncReply<bool> Connect(string hostname, ushort port); AsyncReply<bool> Connect(string hostname, ushort port);
bool Begin(); bool Begin();
//ISocket Accept(); //ISocket Accept();
AsyncReply<ISocket> Accept(); AsyncReply<ISocket> AcceptAsync();
ISocket Accept();
IPEndPoint RemoteEndPoint { get; } IPEndPoint RemoteEndPoint { get; }
IPEndPoint LocalEndPoint { get; } IPEndPoint LocalEndPoint { get; }

View File

@ -311,24 +311,26 @@ namespace Esyur.Net.Sockets
OnDestroy?.Invoke(this); OnDestroy?.Invoke(this);
} }
public AsyncReply<ISocket> Accept() public async AsyncReply<ISocket> AcceptAsync()
{ {
var reply = new AsyncReply<ISocket>(); //var reply = new AsyncReply<ISocket>();
try try
{ {
sock.AcceptAsync().ContinueWith((x) => return new SSLSocket(await sock.AcceptAsync(), cert, true);
{
try
{
reply.Trigger(new SSLSocket(x.Result, cert, true));
}
catch
{
reply.Trigger(null);
}
}, null); //sock.AcceptAsync().ContinueWith((x) =>
//{
// try
// {
// reply.Trigger(new SSLSocket(x.Result, cert, true));
// }
// catch
// {
// reply.Trigger(null);
// }
//}, null);
} }
catch catch
@ -337,7 +339,7 @@ namespace Esyur.Net.Sockets
return null; return null;
} }
return reply; //return reply;
} }
public void Hold() public void Hold()
@ -354,5 +356,18 @@ namespace Esyur.Net.Sockets
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public ISocket Accept()
{
try
{
return new SSLSocket(sock.Accept(), cert, true);
}
catch
{
state = SocketState.Terminated;
return null;
}
}
} }
} }

View File

@ -178,7 +178,9 @@ namespace Esyur.Net.Sockets
//lock (receiveNetworkBuffer.SyncLock) //lock (receiveNetworkBuffer.SyncLock)
// Console.WriteLine(e. + " " + e.BytesTransferred); // Console.WriteLine(e. + " " + e.BytesTransferred);
receiveNetworkBuffer.Write(receiveBuffer, 0, (uint)e.BytesTransferred); var recCount = e.BytesTransferred > e.Count ? e.Count : e.BytesTransferred;
receiveNetworkBuffer.Write(receiveBuffer, 0, (uint)recCount);
//Console.WriteLine("TC IN: " + (uint)e.BytesTransferred + " " + DC.ToHex(receiveBuffer, 0, (uint)e.BytesTransferred)); //Console.WriteLine("TC IN: " + (uint)e.BytesTransferred + " " + DC.ToHex(receiveBuffer, 0, (uint)e.BytesTransferred));
@ -365,7 +367,15 @@ namespace Esyur.Net.Sockets
else else
{ {
asyncSending = true; asyncSending = true;
try
{
sock.BeginSend(msg, 0, msg.Length, SocketFlags.None, PacketSent, null); sock.BeginSend(msg, 0, msg.Length, SocketFlags.None, PacketSent, null);
}
catch {
asyncSending = false;
state = SocketState.Terminated;
Close();
}
//sock.SendAsync(new ArraySegment<byte>(msg), SocketFlags.None).ContinueWith(DataSent); //sock.SendAsync(new ArraySegment<byte>(msg), SocketFlags.None).ContinueWith(DataSent);
} }
} }
@ -403,7 +413,7 @@ namespace Esyur.Net.Sockets
} }
catch (Exception ex2) catch (Exception ex2)
{ {
Console.WriteLine("Level 2 {0}", ex2); state = SocketState.Terminated;
} }
Global.Log("TCPSocket", LogType.Error, ex.ToString()); Global.Log("TCPSocket", LogType.Error, ex.ToString());
@ -427,33 +437,35 @@ namespace Esyur.Net.Sockets
OnDestroy?.Invoke(this); OnDestroy?.Invoke(this);
} }
public AsyncReply<ISocket> Accept() public ISocket Accept()
{
var reply = new AsyncReply<ISocket>();
try
{
sock.AcceptAsync().ContinueWith((x) =>
{ {
try try
{ {
reply.Trigger(new TCPSocket(x.Result)); var s = sock.Accept();
} return new TCPSocket(s);
catch
{
reply.Trigger(null);
}
});
} }
catch catch
{ {
state = SocketState.Terminated; state = SocketState.Terminated;
return null; return null;
} }
return reply;
} }
public async AsyncReply<ISocket> AcceptAsync()
{
try
{
var s = await sock.AcceptAsync();
return new TCPSocket(s);
}
catch
{
state = SocketState.Terminated;
return null;
}
}
public void Hold() public void Hold()
{ {
held = true; held = true;
@ -494,12 +506,23 @@ namespace Esyur.Net.Sockets
else else
{ {
asyncSending = true; asyncSending = true;
try
{
sock.BeginSend(msg, 0, msg.Length, SocketFlags.None, PacketSent, rt);// null); sock.BeginSend(msg, 0, msg.Length, SocketFlags.None, PacketSent, rt);// null);
}
catch
{
asyncSending = false;
state = SocketState.Terminated;
Close();
}
//sock.SendAsync(new ArraySegment<byte>(msg), SocketFlags.None).ContinueWith(DataSent); //sock.SendAsync(new ArraySegment<byte>(msg), SocketFlags.None).ContinueWith(DataSent);
} }
return rt; return rt;
} }
} }
} }
} }

View File

@ -200,14 +200,14 @@ namespace Esyur.Net.Sockets
public void Send(WebsocketPacket packet) public void Send(WebsocketPacket packet)
{ {
lock(sendLock) lock (sendLock)
if (packet.Compose()) if (packet.Compose())
sock.Send(packet.Data); sock.Send(packet.Data);
} }
public void Send(byte[] message) public void Send(byte[] message)
{ {
lock(sendLock) lock (sendLock)
{ {
if (held) if (held)
{ {
@ -219,8 +219,11 @@ namespace Esyur.Net.Sockets
//Console.WriteLine("TX " + message.Length +"/"+totalSent);// + " " + DC.ToHex(message, 0, (uint)size)); //Console.WriteLine("TX " + message.Length +"/"+totalSent);// + " " + DC.ToHex(message, 0, (uint)size));
pkt_send.Message = message; pkt_send.Message = message;
if (pkt_send.Compose()) if (pkt_send.Compose())
sock.Send(pkt_send.Data); sock.Send(pkt_send.Data);
} }
} }
} }
@ -275,7 +278,7 @@ namespace Esyur.Net.Sockets
OnDestroy?.Invoke(this); OnDestroy?.Invoke(this);
} }
public AsyncReply<ISocket> Accept() public AsyncReply<ISocket> AcceptAsync()
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
@ -288,7 +291,7 @@ namespace Esyur.Net.Sockets
public void Unhold() public void Unhold()
{ {
lock(sendLock) lock (sendLock)
{ {
held = false; held = false;
@ -314,5 +317,10 @@ namespace Esyur.Net.Sockets
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public ISocket Accept()
{
throw new NotImplementedException();
}
} }
} }

View File

@ -39,26 +39,26 @@ namespace Esyur.Net.TCP
public class TCPServer : NetworkServer<TCPConnection>, IResource public class TCPServer : NetworkServer<TCPConnection>, IResource
{ {
[Storable] [Attribute]
string ip public string IP
{
get;
set;
}
[Attribute]
public ushort Port
{ {
get; get;
set; set;
} }
[Storable] [Storable]
ushort port public uint Timeout
{ {
get; get;
set; set;
} }
[Storable] [Attribute]
uint timeout public uint Clock
{
get;
set;
}
[Storable]
uint clock
{ {
get; get;
set; set;
@ -75,12 +75,12 @@ namespace Esyur.Net.TCP
TCPSocket listener; TCPSocket listener;
if (ip != null) if (IP != null)
listener =new TCPSocket(new IPEndPoint(IPAddress.Parse(ip), port)); listener = new TCPSocket(new IPEndPoint(IPAddress.Parse(IP), Port));
else else
listener = new TCPSocket(new IPEndPoint(IPAddress.Any, port)); listener = new TCPSocket(new IPEndPoint(IPAddress.Any, Port));
Start(listener, timeout, clock); Start(listener, Timeout, Clock);
} }

View File

@ -30,8 +30,8 @@ using System.Text;
using System.Collections; using System.Collections;
using Esyur.Data; using Esyur.Data;
using Esyur.Misc; using Esyur.Misc;
using Esyur.Engine;
using Esyur.Resource; using Esyur.Resource;
using Esyur.Core;
namespace Esyur.Net.UDP namespace Esyur.Net.UDP
{ {
@ -43,8 +43,9 @@ namespace Esyur.Net.UDP
}*/ }*/
public class UDPServer : IResource public class UDPServer : IResource
{ {
Thread Receiver; Thread receiver;
UdpClient Udp; UdpClient udp;
UDPFilter[] filters = new UDPFilter[0];
public event DestroyedEvent OnDestroy; public event DestroyedEvent OnDestroy;
@ -54,38 +55,20 @@ namespace Esyur.Net.UDP
set; set;
} }
[Storable] [Attribute]
string ip string IP
{ {
get; get;
set; set;
} }
[Storable] [Attribute]
ushort port ushort Port
{ {
get; get;
set; set;
} }
public bool Trigger(ResourceTrigger trigger)
{
if (trigger == ResourceTrigger.Initialize)
{
var address = ip == null ? IPAddress.Any : IPAddress.Parse(ip);
Udp = new UdpClient(new IPEndPoint(address, (int)port));
Receiver = new Thread(Receiving);
Receiver.Start();
}
else if (trigger == ResourceTrigger.Terminate)
{
if (Receiver != null)
Receiver.Abort();
}
return true;
}
private void Receiving() private void Receiving()
{ {
IPEndPoint ep = new IPEndPoint(IPAddress.Any, 0); IPEndPoint ep = new IPEndPoint(IPAddress.Any, 0);
@ -93,9 +76,9 @@ namespace Esyur.Net.UDP
while (true) while (true)
{ {
byte[] b = Udp.Receive(ref ep); byte[] b = udp.Receive(ref ep);
foreach(var child in Instance.Children) foreach (var child in filters)
{ {
var f = child as UDPFilter; var f = child as UDPFilter;
@ -119,7 +102,7 @@ namespace Esyur.Net.UDP
{ {
try try
{ {
Udp.Send(Data, Count, EP); udp.Send(Data, Count, EP);
return true; return true;
} }
catch catch
@ -131,7 +114,7 @@ namespace Esyur.Net.UDP
{ {
try try
{ {
Udp.Send(Data, Data.Length, EP); udp.Send(Data, Data.Length, EP);
return true; return true;
} }
catch catch
@ -143,7 +126,7 @@ namespace Esyur.Net.UDP
{ {
try try
{ {
Udp.Send(Data, Count, Host, Port); udp.Send(Data, Count, Host, Port);
return true; return true;
} }
catch catch
@ -155,7 +138,7 @@ namespace Esyur.Net.UDP
{ {
try try
{ {
Udp.Send(Data, Data.Length, Host, Port); udp.Send(Data, Data.Length, Host, Port);
return true; return true;
} }
catch catch
@ -167,7 +150,7 @@ namespace Esyur.Net.UDP
{ {
try try
{ {
Udp.Send(Encoding.Default.GetBytes(Data), Data.Length, EP); udp.Send(Encoding.Default.GetBytes(Data), Data.Length, EP);
return true; return true;
} }
catch catch
@ -179,7 +162,7 @@ namespace Esyur.Net.UDP
{ {
try try
{ {
Udp.Send(Encoding.Default.GetBytes(Data), Data.Length, Host, Port); udp.Send(Encoding.Default.GetBytes(Data), Data.Length, Host, Port);
return true; return true;
} }
catch catch
@ -190,8 +173,32 @@ namespace Esyur.Net.UDP
public void Destroy() public void Destroy()
{ {
Udp.Close(); udp.Close();
OnDestroy?.Invoke(this); OnDestroy?.Invoke(this);
} }
async AsyncReply<bool> IResource.Trigger(ResourceTrigger trigger)
{
if (trigger == ResourceTrigger.Initialize)
{
var address = IP == null ? IPAddress.Any : IPAddress.Parse(IP);
udp = new UdpClient(new IPEndPoint(address, Port));
receiver = new Thread(Receiving);
receiver.Start();
}
else if (trigger == ResourceTrigger.Terminate)
{
if (receiver != null)
receiver.Abort();
}
else if (trigger == ResourceTrigger.SystemInitialized)
{
filters = await Instance.Children<UDPFilter>();
}
return true;
}
} }
} }

View File

@ -51,9 +51,9 @@ namespace Esyur.Proxy
if (typeInfo.IsSealed || typeInfo.IsAbstract) if (typeInfo.IsSealed || typeInfo.IsAbstract)
throw new Exception("Sealed/Abastract classes can't be proxied."); throw new Exception("Sealed/Abastract classes can't be proxied.");
var props = from p in typeInfo.GetProperties() var props = from p in typeInfo.GetProperties(BindingFlags.Instance | BindingFlags.Public)
where p.CanWrite && p.GetSetMethod().IsVirtual && where p.CanWrite && p.SetMethod.IsVirtual &&
p.GetCustomAttributes(typeof(ResourceProperty), false).Count() > 0 p.GetCustomAttribute<PublicAttribute>(false) != null
select p; select p;
#else #else

View File

@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Esyur.Resource
{
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Method | AttributeTargets.Event)]
public class AnnotationAttribute : Attribute
{
public string Annotation { get; set; }
public AnnotationAttribute(string annotation)
{
this.Annotation = annotation;
}
}
}

View File

@ -31,10 +31,10 @@ namespace Esyur.Resource
{ {
[AttributeUsage(AttributeTargets.Property)] [AttributeUsage(AttributeTargets.Property)]
public class ResourceAttribute : System.Attribute public class AttributeAttribute : System.Attribute
{ {
public ResourceAttribute() public AttributeAttribute()
{ {
} }

View File

@ -176,7 +176,8 @@ namespace Esyur.Resource
if (at != null) if (at != null)
if (at.Info.CanWrite) if (at.Info.CanWrite)
at.Info.SetValue(res, kv.Value); at.Info.SetValue(res, DC.CastConvert(kv.Value, at.Info.PropertyType));
} }
} }
@ -442,7 +443,7 @@ namespace Esyur.Resource
IResource res; IResource res;
if (resource.TryGetTarget(out res)) if (resource.TryGetTarget(out res))
{ {
var rt = pt.Serilize ? pt.Info.GetValue(res, null) : null; var rt = pt.Info.GetValue(res, null);// pt.Serilize ? pt.Info.GetValue(res, null) : null;
props.Add(new PropertyValue(rt, ages[pt.Index], modificationDates[pt.Index])); props.Add(new PropertyValue(rt, ages[pt.Index], modificationDates[pt.Index]));
} }
@ -546,13 +547,14 @@ namespace Esyur.Resource
ages[pt.Index] = instanceAge; ages[pt.Index] = instanceAge;
modificationDates[pt.Index] = now; modificationDates[pt.Index] = now;
if (pt.Storage == StorageMode.NonVolatile) if (pt.Recordable)
{
store.Modify(res, pt.Name, value, ages[pt.Index], now);
}
else if (pt.Storage == StorageMode.Recordable)
{ {
store.Record(res, pt.Name, value, ages[pt.Index], now); store.Record(res, pt.Name, value, ages[pt.Index], now);
}
else //if (pt.Storage == StorageMode.Recordable)
{
store.Modify(res, pt.Name, value, ages[pt.Index], now);
} }
ResourceModified?.Invoke(res, pt.Name, value); ResourceModified?.Invoke(res, pt.Name, value);
@ -839,7 +841,7 @@ namespace Esyur.Resource
this.store = store; this.store = store;
this.resource = new WeakReference<IResource>(resource); this.resource = new WeakReference<IResource>(resource);
this.id = id; this.id = id;
this.name = name; this.name = name ?? "";
this.instanceAge = age; this.instanceAge = age;
//this.attributes = new KeyList<string, object>(this); //this.attributes = new KeyList<string, object>(this);
@ -875,30 +877,32 @@ namespace Esyur.Resource
var events = t.GetEvents(BindingFlags.Public | BindingFlags.Instance);// | BindingFlags.DeclaredOnly); var events = t.GetEvents(BindingFlags.Public | BindingFlags.Instance);// | BindingFlags.DeclaredOnly);
#endif #endif
foreach (var evt in events)
foreach (var evt in template.Events)
{ {
//if (evt.EventHandlerType != typeof(ResourceEventHanlder)) //if (evt.EventHandlerType != typeof(ResourceEventHanlder))
// continue; // continue;
if (evt.EventHandlerType == typeof(ResourceEventHanlder)) if (evt.Info.EventHandlerType == typeof(ResourceEventHanlder))
{ {
var ca = (ResourceEvent[])evt.GetCustomAttributes(typeof(ResourceEvent), true);
if (ca.Length == 0) // var ca = (ResourceEvent[])evt.GetCustomAttributes(typeof(ResourceEvent), true);
continue; // if (ca.Length == 0)
// continue;
ResourceEventHanlder proxyDelegate = (args) => EmitResourceEvent(null, null, evt.Name, args); ResourceEventHanlder proxyDelegate = (args) => EmitResourceEvent(null, null, evt.Name, args);
evt.AddEventHandler(resource, proxyDelegate); evt.Info.AddEventHandler(resource, proxyDelegate);
} }
else if (evt.EventHandlerType == typeof(CustomResourceEventHanlder)) else if (evt.Info.EventHandlerType == typeof(CustomResourceEventHanlder))
{ {
var ca = (ResourceEvent[])evt.GetCustomAttributes(typeof(ResourceEvent), true); //var ca = (ResourceEvent[])evt.GetCustomAttributes(typeof(ResourceEvent), true);
if (ca.Length == 0) //if (ca.Length == 0)
continue; // continue;
CustomResourceEventHanlder proxyDelegate = (issuer, receivers, args) => EmitResourceEvent(issuer, receivers, evt.Name, args); CustomResourceEventHanlder proxyDelegate = (issuer, receivers, args) => EmitResourceEvent(issuer, receivers, evt.Name, args);
evt.AddEventHandler(resource, proxyDelegate); evt.Info.AddEventHandler(resource, proxyDelegate);
} }

View File

@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Esyur.Resource
{
public class PrivateAttribute:Attribute
{
}
}

View File

@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Esyur.Resource
{
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Method | AttributeTargets.Event | AttributeTargets.Class)]
public class PublicAttribute : Attribute
{
// public StorageMode Storage { get; set; }
//public bool Serialize { get; set; }
public PublicAttribute()//StorageMode storage = StorageMode.NonVolatile, bool serialize = true)
{
// Storage = storage;
//Serialize = serialize;
}
}
}

View File

@ -47,7 +47,7 @@ namespace Esyur.Resource
return new AsyncReply<bool>(true); return new AsyncReply<bool>(true);
} }
public virtual bool Create() protected virtual bool Create()
{ {
return true; return true;
} }

View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Esyur.Resource
{
[AttributeUsage(AttributeTargets.Property)]
public class StorageAttribute:Attribute
{
public StorageMode Mode { get; set; }
public StorageAttribute(StorageMode mode)
{
Mode = mode;
}
}
}

View File

@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Text;
using Esyur.Core;
using Esyur.Data;
using Esyur.Resource.Template;
namespace Esyur.Resource
{
public abstract class Store<T> : IStore where T:IResource
{
public Instance Instance { get; set; }
public event DestroyedEvent OnDestroy;
public abstract AsyncReply<bool> AddChild(IResource parent, IResource child);
public abstract AsyncReply<bool> AddParent(IResource child, IResource parent);
public abstract AsyncBag<T1> Children<T1>(IResource resource, string name) where T1 : IResource;
public virtual void Destroy()
{
OnDestroy?.Invoke(this);
}
public abstract AsyncReply<IResource> Get(string path);
public abstract AsyncReply<KeyList<PropertyTemplate, PropertyValue[]>> GetRecord(IResource resource, DateTime fromDate, DateTime toDate);
public abstract string Link(IResource resource);
public abstract bool Modify(IResource resource, string propertyName, object value, ulong age, DateTime dateTime);
public abstract AsyncBag<T1> Parents<T1>(IResource resource, string name) where T1 : IResource;
public abstract AsyncReply<bool> Put(IResource resource);
public abstract bool Record(IResource resource, string propertyName, object value, ulong age, DateTime dateTime);
public abstract bool Remove(IResource resource);
public abstract AsyncReply<bool> RemoveChild(IResource parent, IResource child);
public abstract AsyncReply<bool> RemoveParent(IResource child, IResource parent);
public abstract AsyncReply<bool> Trigger(ResourceTrigger trigger);
public T New(string name = null, object attributes = null, object properties = null)
{
var resource = Warehouse.New<T>(name, this, null, null, attributes, properties);
resource.Instance.Managers.AddRange(this.Instance.Managers.ToArray());
return resource;
}
}
}

View File

@ -2,6 +2,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -15,6 +16,8 @@ namespace Esyur.Resource.Template
set; set;
} }
public EventInfo Info { get; set; }
public override byte[] Compose() public override byte[] Compose()
{ {
var name = base.Compose(); var name = base.Compose();
@ -39,7 +42,7 @@ namespace Esyur.Resource.Template
} }
public EventTemplate(ResourceTemplate template, byte index, string name, string expansion) public EventTemplate(ResourceTemplate template, byte index, string name, string expansion = null)
:base(template, MemberType.Property, index, name) :base(template, MemberType.Property, index, name)
{ {
this.Expansion = expansion; this.Expansion = expansion;

View File

@ -45,7 +45,7 @@ namespace Esyur.Resource.Template
} }
public FunctionTemplate(ResourceTemplate template, byte index, string name,bool isVoid, string expansion) public FunctionTemplate(ResourceTemplate template, byte index, string name,bool isVoid, string expansion = null)
:base(template, MemberType.Property, index, name) :base(template, MemberType.Property, index, name)
{ {
this.IsVoid = isVoid; this.IsVoid = isVoid;

View File

@ -24,10 +24,12 @@ namespace Esyur.Resource.Template
set; set;
} }
/*
public bool Serilize public bool Serilize
{ {
get;set; get;set;
} }
*/
//bool ReadOnly; //bool ReadOnly;
//IIPTypes::DataType ReturnType; //IIPTypes::DataType ReturnType;
public PropertyPermission Permission { public PropertyPermission Permission {
@ -35,18 +37,19 @@ namespace Esyur.Resource.Template
set; set;
} }
/*
public bool Recordable public bool Recordable
{ {
get; get;
set; set;
}*/ }
public StorageMode Storage /*
public PropertyType Mode
{ {
get; get;
set; set;
} }*/
public string ReadExpansion public string ReadExpansion
{ {
@ -71,7 +74,7 @@ namespace Esyur.Resource.Template
public override byte[] Compose() public override byte[] Compose()
{ {
var name = base.Compose(); var name = base.Compose();
var pv = ((byte)(Permission) << 1) | (Storage == StorageMode.Recordable ? 1 : 0); var pv = ((byte)(Permission) << 1) | (Recordable ? 1 : 0);
if (WriteExpansion != null && ReadExpansion != null) if (WriteExpansion != null && ReadExpansion != null)
{ {
@ -117,11 +120,11 @@ namespace Esyur.Resource.Template
.ToArray(); .ToArray();
} }
public PropertyTemplate(ResourceTemplate template, byte index, string name, string read, string write, StorageMode storage) public PropertyTemplate(ResourceTemplate template, byte index, string name, string read = null, string write = null, bool recordable = false)
:base(template, MemberType.Property, index, name) :base(template, MemberType.Property, index, name)
{ {
//this.Recordable = recordable; this.Recordable = recordable;
this.Storage = storage; //this.Storage = storage;
this.ReadExpansion = read; this.ReadExpansion = read;
this.WriteExpansion = write; this.WriteExpansion = write;
} }

View File

@ -8,6 +8,7 @@ using Esyur.Data;
using Esyur.Core; using Esyur.Core;
using System.Security.Cryptography; using System.Security.Cryptography;
using Esyur.Proxy; using Esyur.Proxy;
using Esyur.Net.IIP;
namespace Esyur.Resource.Template namespace Esyur.Resource.Template
{ {
@ -159,40 +160,67 @@ namespace Esyur.Resource.Template
MethodInfo[] methodsInfo = type.GetMethods(BindingFlags.Public | BindingFlags.Instance);// | BindingFlags.DeclaredOnly); MethodInfo[] methodsInfo = type.GetMethods(BindingFlags.Public | BindingFlags.Instance);// | BindingFlags.DeclaredOnly);
#endif #endif
//byte currentIndex = 0;
bool classIsPublic = type.GetCustomAttribute<PublicAttribute>() != null;
byte i = 0; byte i = 0;
if (classIsPublic)
{
}
else
{
foreach (var pi in propsInfo) foreach (var pi in propsInfo)
{ {
var rp = pi.GetCustomAttribute<ResourceProperty>(true); var publicAttr = pi.GetCustomAttribute<PublicAttribute>(true);
if (rp != null) if (publicAttr != null)
{ {
var pt = new PropertyTemplate(this, i++, pi.Name, rp.ReadExpansion, rp.WriteExpansion, rp.Storage); var annotationAttr = pi.GetCustomAttribute<AnnotationAttribute>(true);
var storageAttr = pi.GetCustomAttribute<StorageAttribute>(true);
var pt = new PropertyTemplate(this, i++, pi.Name);//, rp.ReadExpansion, rp.WriteExpansion, rp.Storage);
if (storageAttr != null)
pt.Recordable = storageAttr.Mode == StorageMode.Recordable;
if (annotationAttr != null)
pt.ReadExpansion = annotationAttr.Annotation;
else
pt.ReadExpansion = pi.PropertyType.Name;
pt.Info = pi; pt.Info = pi;
pt.Serilize = rp.Serialize; //pt.Serilize = publicAttr.Serialize;
properties.Add(pt); properties.Add(pt);
} }
else
var ra = pi.GetCustomAttribute<ResourceAttribute>(true);
if (ra != null)
{ {
var at = new AttributeTemplate(this, i++, pi.Name); var attributeAttr = pi.GetCustomAttribute<AttributeAttribute>(true);
if (attributeAttr != null)
{
var at = new AttributeTemplate(this, 0, pi.Name);
at.Info = pi; at.Info = pi;
attributes.Add(at); attributes.Add(at);
} }
} }
}
i = 0; i = 0;
foreach (var ei in eventsInfo) foreach (var ei in eventsInfo)
{ {
var es = ei.GetCustomAttributes<ResourceEvent>(true).ToArray(); var publicAttr = ei.GetCustomAttribute<PublicAttribute>(true);
if (es.Length > 0) if (publicAttr != null)
{ {
var et = new EventTemplate(this, i++, ei.Name, es[0].Expansion); var annotationAttr = ei.GetCustomAttribute<AnnotationAttribute>(true);
var et = new EventTemplate(this, i++, ei.Name);
et.Info = ei;
if (annotationAttr != null)
et.Expansion = annotationAttr.Annotation;
events.Add(et); events.Add(et);
} }
} }
@ -200,13 +228,21 @@ namespace Esyur.Resource.Template
i = 0; i = 0;
foreach (MethodInfo mi in methodsInfo) foreach (MethodInfo mi in methodsInfo)
{ {
var fs = mi.GetCustomAttributes<ResourceFunction>(true).ToArray(); var publicAttr = mi.GetCustomAttribute<PublicAttribute>(true);
if (fs.Length > 0) if (publicAttr != null)
{ {
var ft = new FunctionTemplate(this, i++, mi.Name, mi.ReturnType == typeof(void), fs[0].Expansion); var annotationAttr = mi.GetCustomAttribute<AnnotationAttribute>(true);
var ft = new FunctionTemplate(this, i++, mi.Name, mi.ReturnType == typeof(void));
if (annotationAttr != null)
ft.Expansion = annotationAttr.Annotation;
else
ft.Expansion = "(" + String.Join(",", mi.GetParameters().Where(x=>x.ParameterType != typeof(DistributedConnection)).Select(x=> "[" + x.ParameterType.Name + "] " + x.Name)) + ") -> " + mi.ReturnType.Name;
functions.Add(ft); functions.Add(ft);
} }
} }
}
// append signals // append signals
for (i = 0; i < events.Count; i++) for (i = 0; i < events.Count; i++)
@ -323,7 +359,7 @@ namespace Esyur.Resource.Template
offset += cs; offset += cs;
} }
var pt = new PropertyTemplate(od, propertyIndex++, name, readExpansion, writeExpansion, recordable ? StorageMode.Recordable : StorageMode.Volatile); var pt = new PropertyTemplate(od, propertyIndex++, name, readExpansion, writeExpansion, recordable);
od.properties.Add(pt); od.properties.Add(pt);
} }

View File

@ -35,6 +35,7 @@ using System.Threading.Tasks;
using Esyur.Net.IIP; using Esyur.Net.IIP;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using Esyur.Misc; using Esyur.Misc;
using System.Collections.Concurrent;
namespace Esyur.Resource namespace Esyur.Resource
{ {
@ -43,8 +44,11 @@ namespace Esyur.Resource
{ {
//static byte prefixCounter; //static byte prefixCounter;
static AutoList<IStore, Instance> stores = new AutoList<IStore, Instance>(null); //static AutoList<IStore, Instance> stores = new AutoList<IStore, Instance>(null);
static Dictionary<uint, WeakReference<IResource>> resources = new Dictionary<uint, WeakReference<IResource>>(); static ConcurrentDictionary<uint, WeakReference<IResource>> resources = new ConcurrentDictionary<uint, WeakReference<IResource>>();
static ConcurrentDictionary<IStore, List<WeakReference<IResource>>> stores = new ConcurrentDictionary<IStore, List<WeakReference<IResource>>>();
static uint resourceCounter = 0; static uint resourceCounter = 0;
static KeyList<Guid, ResourceTemplate> templates = new KeyList<Guid, ResourceTemplate>(); static KeyList<Guid, ResourceTemplate> templates = new KeyList<Guid, ResourceTemplate>();
@ -61,7 +65,7 @@ namespace Esyur.Resource
private static Regex urlRegex = new Regex(@"^(?:([\S]*)://([^/]*)/?)"); private static Regex urlRegex = new Regex(@"^(?:([\S]*)://([^/]*)/?)");
private static object resourcesLock = new object(); //private static object resourcesLock = new object();
static KeyList<string, Func<IStore>> getSupportedProtocols() static KeyList<string, Func<IStore>> getSupportedProtocols()
{ {
@ -78,8 +82,8 @@ namespace Esyur.Resource
public static IStore GetStore(string name) public static IStore GetStore(string name)
{ {
foreach (var s in stores) foreach (var s in stores)
if (s.Instance.Name == name) if (s.Key.Instance.Name == name)
return s as IStore; return s.Key;
return null; return null;
} }
@ -113,13 +117,14 @@ namespace Esyur.Resource
{ {
warehouseIsOpen = true; warehouseIsOpen = true;
var resSnap = resources.Select(x => { var resSnap = resources.Select(x =>
{
IResource r; IResource r;
if (x.Value.TryGetTarget(out r)) if (x.Value.TryGetTarget(out r))
return r; return r;
else else
return null; return null;
}).Where(r=>r!=null).ToArray(); }).Where(r => r != null).ToArray();
foreach (var r in resSnap) foreach (var r in resSnap)
{ {
@ -127,8 +132,13 @@ namespace Esyur.Resource
//if (rk.Value.TryGetTarget(out r)) //if (rk.Value.TryGetTarget(out r))
//{ //{
var rt = await r.Trigger(ResourceTrigger.Initialize); var rt = await r.Trigger(ResourceTrigger.Initialize);
//if (!rt)
// return false;
if (!rt) if (!rt)
return false; {
Console.WriteLine($"Resource failed at Initialize {r.Instance.Name} [{r.Instance.Template.ClassName}]");
}
//} //}
} }
@ -139,7 +149,10 @@ namespace Esyur.Resource
//{ //{
var rt = await r.Trigger(ResourceTrigger.SystemInitialized); var rt = await r.Trigger(ResourceTrigger.SystemInitialized);
if (!rt) if (!rt)
return false; {
Console.WriteLine($"Resource failed at SystemInitialized {r.Instance.Name} [{r.Instance.Template.ClassName}]");
}
//return false;
//} //}
} }
@ -217,7 +230,7 @@ namespace Esyur.Resource
} }
foreach (var store in stores) foreach (var store in stores)
bag.Add(store.Trigger(ResourceTrigger.Terminate)); bag.Add(store.Key.Trigger(ResourceTrigger.Terminate));
foreach (var resource in resources.Values) foreach (var resource in resources.Values)
@ -232,7 +245,7 @@ namespace Esyur.Resource
foreach (var store in stores) foreach (var store in stores)
bag.Add(store.Trigger(ResourceTrigger.SystemTerminated)); bag.Add(store.Key.Trigger(ResourceTrigger.SystemTerminated));
bag.Seal(); bag.Seal();
@ -318,7 +331,7 @@ namespace Esyur.Resource
var p = path.Trim().Split('/'); var p = path.Trim().Split('/');
IResource resource; IResource resource;
foreach (var store in stores) foreach (var store in stores.Keys)
if (p[0] == store.Instance.Name) if (p[0] == store.Instance.Name)
{ {
@ -433,19 +446,26 @@ namespace Esyur.Resource
if (resource.Instance != null) if (resource.Instance != null)
throw new Exception("Resource has a store."); throw new Exception("Resource has a store.");
var resourceReference = new WeakReference<IResource>(resource);
if (store == null) if (store == null)
{ {
// assign parent as a store // assign parent as a store
if (parent is IStore) if (parent is IStore)
{
store = (IStore)parent; store = (IStore)parent;
stores[store].Add(resourceReference);
}
// assign parent's store as a store // assign parent's store as a store
else if (parent != null) else if (parent != null)
{
store = parent.Instance.Store; store = parent.Instance.Store;
stores[store].Add(resourceReference);
}
// assign self as a store (root store) // assign self as a store (root store)
else if (resource is IStore) else if (resource is IStore)
{ {
store = (IStore)resource; store = (IStore)resource;
stores.Add(resource as IStore);
} }
else else
throw new Exception("Can't find a store for the resource."); throw new Exception("Can't find a store for the resource.");
@ -475,7 +495,10 @@ namespace Esyur.Resource
if (resource is IStore) if (resource is IStore)
{
stores.TryAdd(resource as IStore, new List<WeakReference<IResource>>());
StoreConnected?.Invoke(resource as IStore, name); StoreConnected?.Invoke(resource as IStore, name);
}
//else //else
@ -493,15 +516,17 @@ namespace Esyur.Resource
var t = resource.GetType(); var t = resource.GetType();
Global.Counters["T-" + t.Namespace + "." + t.Name]++; Global.Counters["T-" + t.Namespace + "." + t.Name]++;
lock (resourcesLock) //var wr = new WeakReference<IResource>(resource);
resources.Add(resource.Instance.Id, new WeakReference<IResource>(resource));
//lock (resourcesLock)
resources.TryAdd(resource.Instance.Id, resourceReference);
if (warehouseIsOpen) if (warehouseIsOpen)
resource.Trigger(ResourceTrigger.Initialize); resource.Trigger(ResourceTrigger.Initialize);
} }
public static IResource New(Type type, string name, IStore store = null, IResource parent = null, IPermissionsManager manager = null, Structure attributes = null, Structure arguments = null, Structure properties = null) public static IResource New(Type type, string name, IStore store = null, IResource parent = null, IPermissionsManager manager = null, object attributes = null, object properties = null)
{ {
type = ResourceProxy.GetProxy(type); type = ResourceProxy.GetProxy(type);
@ -539,9 +564,12 @@ namespace Esyur.Resource
*/ */
var res = Activator.CreateInstance(type) as IResource; var res = Activator.CreateInstance(type) as IResource;
if (properties != null) if (properties != null)
{ {
foreach (var p in properties) var ps = Structure.FromObject(properties);
foreach (var p in ps)
{ {
var pi = type.GetProperty(p.Key, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.DeclaredOnly); var pi = type.GetProperty(p.Key, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.DeclaredOnly);
if (pi != null) if (pi != null)
@ -556,10 +584,10 @@ namespace Esyur.Resource
} }
public static T New<T>(string name, IStore store = null, IResource parent = null, IPermissionsManager manager = null, Structure attributes = null, Structure arguments = null, Structure properties = null) public static T New<T>(string name, IStore store = null, IResource parent = null, IPermissionsManager manager = null, object attributes = null, object properties = null)
where T : IResource where T : IResource
{ {
return (T)New(typeof(T), name, store, parent, manager, attributes, arguments, properties); return (T)New(typeof(T), name, store, parent, manager, attributes, properties);
} }
/// <summary> /// <summary>
@ -623,27 +651,41 @@ namespace Esyur.Resource
if (resource.Instance == null) if (resource.Instance == null)
return false; return false;
//lock (resourcesLock)
//{
WeakReference<IResource> resourceReference;
if (resources.ContainsKey(resource.Instance.Id)) if (resources.ContainsKey(resource.Instance.Id))
lock(resourcesLock) resources.TryRemove(resource.Instance.Id, out resourceReference);
resources.Remove(resource.Instance.Id);
else else
return false; return false;
//}
if (resource != resource.Instance.Store)
stores[resource.Instance.Store].Remove(resourceReference);
if (resource is IStore) if (resource is IStore)
{ {
stores.Remove(resource as IStore); var store = resource as IStore;
WeakReference<IResource>[] toBeRemoved; List<WeakReference<IResource>> toBeRemoved;// = stores[store];
stores.TryRemove(store, out toBeRemoved);
//lock (resourcesLock)
//{
// // remove all objects associated with the store
// toBeRemoved = resources.Values.Where(x =>
// {
// IResource r;
// if (x.TryGetTarget(out r))
// return r.Instance.Store == resource;
// else
// return false;
// }).ToArray();
//}
lock (resourcesLock)
{
// remove all objects associated with the store
toBeRemoved = resources.Values.Where(x =>
{
IResource r;
return x.TryGetTarget(out r) && r.Instance.Store == resource;
}).ToArray();
}
foreach (var o in toBeRemoved) foreach (var o in toBeRemoved)
{ {

View File

@ -0,0 +1,82 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Esyur.Security.Integrity
{
public class CRC16IBM
{
static UInt16[] table = {
0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241,
0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440,
0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40,
0x0A00, 0xCAC1, 0xCB81, 0x0B40, 0xC901, 0x09C0, 0x0880, 0xC841,
0xD801, 0x18C0, 0x1980, 0xD941, 0x1B00, 0xDBC1, 0xDA81, 0x1A40,
0x1E00, 0xDEC1, 0xDF81, 0x1F40, 0xDD01, 0x1DC0, 0x1C80, 0xDC41,
0x1400, 0xD4C1, 0xD581, 0x1540, 0xD701, 0x17C0, 0x1680, 0xD641,
0xD201, 0x12C0, 0x1380, 0xD341, 0x1100, 0xD1C1, 0xD081, 0x1040,
0xF001, 0x30C0, 0x3180, 0xF141, 0x3300, 0xF3C1, 0xF281, 0x3240,
0x3600, 0xF6C1, 0xF781, 0x3740, 0xF501, 0x35C0, 0x3480, 0xF441,
0x3C00, 0xFCC1, 0xFD81, 0x3D40, 0xFF01, 0x3FC0, 0x3E80, 0xFE41,
0xFA01, 0x3AC0, 0x3B80, 0xFB41, 0x3900, 0xF9C1, 0xF881, 0x3840,
0x2800, 0xE8C1, 0xE981, 0x2940, 0xEB01, 0x2BC0, 0x2A80, 0xEA41,
0xEE01, 0x2EC0, 0x2F80, 0xEF41, 0x2D00, 0xEDC1, 0xEC81, 0x2C40,
0xE401, 0x24C0, 0x2580, 0xE541, 0x2700, 0xE7C1, 0xE681, 0x2640,
0x2200, 0xE2C1, 0xE381, 0x2340, 0xE101, 0x21C0, 0x2080, 0xE041,
0xA001, 0x60C0, 0x6180, 0xA141, 0x6300, 0xA3C1, 0xA281, 0x6240,
0x6600, 0xA6C1, 0xA781, 0x6740, 0xA501, 0x65C0, 0x6480, 0xA441,
0x6C00, 0xACC1, 0xAD81, 0x6D40, 0xAF01, 0x6FC0, 0x6E80, 0xAE41,
0xAA01, 0x6AC0, 0x6B80, 0xAB41, 0x6900, 0xA9C1, 0xA881, 0x6840,
0x7800, 0xB8C1, 0xB981, 0x7940, 0xBB01, 0x7BC0, 0x7A80, 0xBA41,
0xBE01, 0x7EC0, 0x7F80, 0xBF41, 0x7D00, 0xBDC1, 0xBC81, 0x7C40,
0xB401, 0x74C0, 0x7580, 0xB541, 0x7700, 0xB7C1, 0xB681, 0x7640,
0x7200, 0xB2C1, 0xB381, 0x7340, 0xB101, 0x71C0, 0x7080, 0xB041,
0x5000, 0x90C1, 0x9181, 0x5140, 0x9301, 0x53C0, 0x5280, 0x9241,
0x9601, 0x56C0, 0x5780, 0x9741, 0x5500, 0x95C1, 0x9481, 0x5440,
0x9C01, 0x5CC0, 0x5D80, 0x9D41, 0x5F00, 0x9FC1, 0x9E81, 0x5E40,
0x5A00, 0x9AC1, 0x9B81, 0x5B40, 0x9901, 0x59C0, 0x5880, 0x9841,
0x8801, 0x48C0, 0x4980, 0x8941, 0x4B00, 0x8BC1, 0x8A81, 0x4A40,
0x4E00, 0x8EC1, 0x8F81, 0x4F40, 0x8D01, 0x4DC0, 0x4C80, 0x8C41,
0x4400, 0x84C1, 0x8581, 0x4540, 0x8701, 0x47C0, 0x4680, 0x8641,
0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040};
public static ushort Compute(byte[] data)
{
return Compute(data, 0, (uint)data.Length);
}
public static ushort Compute(byte[] data, uint offset, uint length)
{
ushort crc = 0;// 0xffff;
ushort x;
for (var i = offset; i < length; i++)
{
x = (ushort)(crc ^ data[i]);
crc = (UInt16)((crc >> 8) ^ table[x & 0x00FF]);
}
return crc;
}
public static ushort Compute2(byte[] data, uint offset, uint length)
{
ushort crc = 0;
for (var i = offset; i < length; i++)
{
crc ^= data[i];
for (var j = 0; j < 8; j++)
{
var carry = crc & 0x1;
crc >>= 1;
if (carry == 1)
crc ^= 0xa001;
}
}
return crc;
}
}
}

View File

@ -0,0 +1,66 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Esyur.Security.Integrity
{
public class CRC16ITU
{
static UInt16[] table =
{
0X0000, 0X1189, 0X2312, 0X329B, 0X4624, 0X57AD, 0X6536, 0X74BF,
0X8C48, 0X9DC1, 0XAF5A, 0XBED3, 0XCA6C, 0XDBE5, 0XE97E, 0XF8F7,
0X1081, 0X0108, 0X3393, 0X221A, 0X56A5, 0X472C, 0X75B7, 0X643E,
0X9CC9, 0X8D40, 0XBFDB, 0XAE52, 0XDAED, 0XCB64, 0XF9FF, 0XE876,
0X2102, 0X308B, 0X0210, 0X1399, 0X6726, 0X76AF, 0X4434, 0X55BD,
0XAD4A, 0XBCC3, 0X8E58, 0X9FD1, 0XEB6E, 0XFAE7, 0XC87C, 0XD9F5,
0X3183, 0X200A, 0X1291, 0X0318, 0X77A7, 0X662E, 0X54B5, 0X453C,
0XBDCB, 0XAC42, 0X9ED9, 0X8F50, 0XFBEF, 0XEA66, 0XD8FD, 0XC974,
0X4204, 0X538D, 0X6116, 0X709F, 0X0420, 0X15A9, 0X2732, 0X36BB,
0XCE4C, 0XDFC5, 0XED5E, 0XFCD7, 0X8868, 0X99E1, 0XAB7A, 0XBAF3,
0X5285, 0X430C, 0X7197, 0X601E, 0X14A1, 0X0528, 0X37B3, 0X263A,
0XDECD, 0XCF44, 0XFDDF, 0XEC56, 0X98E9, 0X8960, 0XBBFB, 0XAA72,
0X6306, 0X728F, 0X4014, 0X519D, 0X2522, 0X34AB, 0X0630, 0X17B9,
0XEF4E, 0XFEC7, 0XCC5C, 0XDDD5, 0XA96A, 0XB8E3, 0X8A78, 0X9BF1,
0X7387, 0X620E, 0X5095, 0X411C, 0X35A3, 0X242A, 0X16B1, 0X0738,
0XFFCF, 0XEE46, 0XDCDD, 0XCD54, 0XB9EB, 0XA862, 0X9AF9, 0X8B70,
0X8408, 0X9581, 0XA71A, 0XB693, 0XC22C, 0XD3A5, 0XE13E, 0XF0B7,
0X0840, 0X19C9, 0X2B52, 0X3ADB, 0X4E64, 0X5FED, 0X6D76, 0X7CFF,
0X9489, 0X8500, 0XB79B, 0XA612, 0XD2AD, 0XC324, 0XF1BF, 0XE036,
0X18C1, 0X0948, 0X3BD3, 0X2A5A, 0X5EE5, 0X4F6C, 0X7DF7, 0X6C7E,
0XA50A, 0XB483, 0X8618, 0X9791, 0XE32E, 0XF2A7, 0XC03C, 0XD1B5,
0X2942, 0X38CB, 0X0A50, 0X1BD9, 0X6F66, 0X7EEF, 0X4C74, 0X5DFD,
0XB58B, 0XA402, 0X9699, 0X8710, 0XF3AF, 0XE226, 0XD0BD, 0XC134,
0X39C3, 0X284A, 0X1AD1, 0X0B58, 0X7FE7, 0X6E6E, 0X5CF5, 0X4D7C,
0XC60C, 0XD785, 0XE51E, 0XF497, 0X8028, 0X91A1, 0XA33A, 0XB2B3,
0X4A44, 0X5BCD, 0X6956, 0X78DF, 0X0C60, 0X1DE9, 0X2F72, 0X3EFB,
0XD68D, 0XC704, 0XF59F, 0XE416, 0X90A9, 0X8120, 0XB3BB, 0XA232,
0X5AC5, 0X4B4C, 0X79D7, 0X685E, 0X1CE1, 0X0D68, 0X3FF3, 0X2E7A,
0XE70E, 0XF687, 0XC41C, 0XD595, 0XA12A, 0XB0A3, 0X8238, 0X93B1,
0X6B46, 0X7ACF, 0X4854, 0X59DD, 0X2D62, 0X3CEB, 0X0E70, 0X1FF9,
0XF78F, 0XE606, 0XD49D, 0XC514, 0XB1AB, 0XA022, 0X92B9, 0X8330,
0X7BC7, 0X6A4E, 0X58D5, 0X495C, 0X3DE3, 0X2C6A, 0X1EF1, 0X0F78,
};
public static ushort Compute(byte[] data)
{
return Compute(data, 0, (uint)data.Length);
}
public static ushort Compute(byte[] data, uint offset, uint length)
{
ushort fcs = 0xffff; // initialization
while (length > 0)
{
fcs = (ushort)((fcs >> 8) ^ table[(fcs ^ data[offset++]) & 0xff]);
length--;
}
return (ushort)~fcs; // negated
}
}
}

View File

@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Esyur.Security.Integrity
{
public class NMEA0183
{
public static byte Compute(string data)
{
return Compute(data, 0, (uint)data.Length);
}
public static byte Compute(string data, uint offset, uint length)
{
byte rt = 0;
var ends = offset + length;
for (int i = (int)offset; i < ends; i++)
rt ^= (byte)data[i];
return rt;
}
}
}