mirror of
https://github.com/esiur/esiur-dotnet.git
synced 2025-05-06 19:42:58 +00:00
Bugfix
This commit is contained in:
parent
8c2d616d62
commit
ba084b79e6
@ -36,8 +36,8 @@ namespace Esyur.Stores.EntityCore
|
|||||||
{
|
{
|
||||||
public class EntityResource : IResource
|
public class EntityResource : IResource
|
||||||
{
|
{
|
||||||
[NotMapped]
|
//[NotMapped]
|
||||||
internal object _PrimaryId;
|
//internal object _PrimaryId;
|
||||||
|
|
||||||
public event DestroyedEvent OnDestroy;
|
public event DestroyedEvent OnDestroy;
|
||||||
public event PropertyChangedEventHandler PropertyChanged;
|
public event PropertyChangedEventHandler PropertyChanged;
|
||||||
|
@ -29,9 +29,6 @@ using Esyur.Resource.Template;
|
|||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Text;
|
|
||||||
using Microsoft.EntityFrameworkCore.Proxies;
|
|
||||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
|
||||||
using Esyur.Proxy;
|
using Esyur.Proxy;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Microsoft.EntityFrameworkCore.Metadata;
|
using Microsoft.EntityFrameworkCore.Metadata;
|
||||||
@ -65,7 +62,8 @@ namespace Esyur.Stores.EntityCore
|
|||||||
{
|
{
|
||||||
var p = path.Split('/');
|
var p = path.Split('/');
|
||||||
var ti = TypesByName[p[0]];
|
var ti = TypesByName[p[0]];
|
||||||
var id = Convert.ToInt32(p[1]);
|
var id = Convert.ChangeType(p[1], ti.PrimaryKey.PropertyType);// Convert.ToInt32();
|
||||||
|
//Type.cas ti.PrimaryKey.PropertyType.
|
||||||
return DbContextProvider().Find(ti.Type.ClrType, id) as IResource;
|
return DbContextProvider().Find(ti.Type.ClrType, id) as IResource;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -76,7 +74,9 @@ namespace Esyur.Stores.EntityCore
|
|||||||
|
|
||||||
var type = ResourceProxy.GetBaseType(resource);//.GetType().;
|
var type = ResourceProxy.GetBaseType(resource);//.GetType().;
|
||||||
|
|
||||||
var eid = (resource as EntityResource)._PrimaryId;// (int)resource.Instance.Variables["eid"];
|
//var eid = (resource as EntityResource)._PrimaryId;// (int)resource.Instance.Variables["eid"];
|
||||||
|
|
||||||
|
var eid = TypesByType[type].PrimaryKey.GetValue(resource);
|
||||||
|
|
||||||
if (DB[type].ContainsKey(eid))
|
if (DB[type].ContainsKey(eid))
|
||||||
DB[type].Remove(eid);
|
DB[type].Remove(eid);
|
||||||
@ -86,7 +86,7 @@ namespace Esyur.Stores.EntityCore
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IResource GetById(Type type, int id)
|
public IResource GetById(Type type, object id)
|
||||||
{
|
{
|
||||||
if (!DB[type].ContainsKey(id))
|
if (!DB[type].ContainsKey(id))
|
||||||
return null;
|
return null;
|
||||||
@ -188,29 +188,37 @@ namespace Esyur.Stores.EntityCore
|
|||||||
if (DbContextProvider == null)
|
if (DbContextProvider == null)
|
||||||
DbContextProvider = () => Activator.CreateInstance(Options.Options.ContextType, Options.Options) as DbContext;
|
DbContextProvider = () => Activator.CreateInstance(Options.Options.ContextType, Options.Options) as DbContext;
|
||||||
|
|
||||||
var context = Activator.CreateInstance(Options.Options.ContextType, Options.Options) as DbContext;
|
ReloadModel();
|
||||||
|
|
||||||
var types = context.Model.GetEntityTypes();
|
|
||||||
foreach (var t in types)
|
|
||||||
{
|
|
||||||
var ti = new TypeInfo()
|
|
||||||
{
|
|
||||||
Name = t.ClrType.Name,
|
|
||||||
PrimaryKey = t.FindPrimaryKey().Properties.FirstOrDefault()?.PropertyInfo,
|
|
||||||
Type = t
|
|
||||||
};
|
|
||||||
|
|
||||||
TypesByName.Add(t.ClrType.Name, ti);
|
|
||||||
TypesByType.Add(t.ClrType, ti);
|
|
||||||
|
|
||||||
DB.Add(t.ClrType, new Dictionary<object, WeakReference>());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return new AsyncReply<bool>(true);
|
return new AsyncReply<bool>(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void ReloadModel()
|
||||||
|
{
|
||||||
|
TypesByName.Clear();
|
||||||
|
TypesByType.Clear();
|
||||||
|
|
||||||
|
var context = DbContextProvider();// Activator.CreateInstance(Options.Options.ContextType, Options.Options) as DbContext;
|
||||||
|
|
||||||
|
var types = context.Model.GetEntityTypes();
|
||||||
|
foreach (var t in types)
|
||||||
|
{
|
||||||
|
var ti = new TypeInfo()
|
||||||
|
{
|
||||||
|
Name = t.ClrType.Name,
|
||||||
|
PrimaryKey = t.FindPrimaryKey().Properties.FirstOrDefault()?.PropertyInfo,
|
||||||
|
Type = t
|
||||||
|
};
|
||||||
|
|
||||||
|
TypesByName.Add(t.ClrType.Name, ti);
|
||||||
|
TypesByType.Add(t.ClrType, ti);
|
||||||
|
|
||||||
|
if (!DB.ContainsKey(t.ClrType))
|
||||||
|
DB.Add(t.ClrType, new Dictionary<object, WeakReference>());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void Destroy()
|
public void Destroy()
|
||||||
{
|
{
|
||||||
//throw new NotImplementedException();
|
//throw new NotImplementedException();
|
||||||
|
@ -11,14 +11,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.3" />
|
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.1.4" />
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="3.1.3" />
|
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.3">
|
|
||||||
<PrivateAssets>all</PrivateAssets>
|
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
|
||||||
</PackageReference>
|
|
||||||
<PackageReference Include="Npgsql" Version="4.1.3.1" />
|
|
||||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.1.3" />
|
|
||||||
<PackageReference Include="System.Collections" Version="4.3.0" />
|
<PackageReference Include="System.Collections" Version="4.3.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
@ -30,7 +30,6 @@ using System.Text;
|
|||||||
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Infrastructure;
|
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Infrastructure;
|
||||||
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal;
|
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal;
|
||||||
using Microsoft.EntityFrameworkCore.Utilities;
|
using Microsoft.EntityFrameworkCore.Utilities;
|
||||||
using Microsoft.EntityFrameworkCore.Proxies.Internal;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Microsoft.EntityFrameworkCore.Metadata;
|
using Microsoft.EntityFrameworkCore.Metadata;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
@ -60,7 +59,7 @@ namespace Esyur.Stores.EntityCore
|
|||||||
|
|
||||||
public void ApplyServices(IServiceCollection services)
|
public void ApplyServices(IServiceCollection services)
|
||||||
{
|
{
|
||||||
services.AddEntityFrameworkProxies();
|
// services.AddEntityFrameworkProxies();
|
||||||
|
|
||||||
new EntityFrameworkServicesBuilder(services)
|
new EntityFrameworkServicesBuilder(services)
|
||||||
.TryAdd<IConventionSetPlugin, EsyurPlugin>();
|
.TryAdd<IConventionSetPlugin, EsyurPlugin>();
|
||||||
|
@ -29,6 +29,7 @@ using Microsoft.EntityFrameworkCore.Infrastructure;
|
|||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace Esyur.Stores.EntityCore
|
namespace Esyur.Stores.EntityCore
|
||||||
@ -41,18 +42,27 @@ namespace Esyur.Stores.EntityCore
|
|||||||
|
|
||||||
//}
|
//}
|
||||||
|
|
||||||
public static T AddResource<T>(this DbSet<T> dbSet, object properties = null) where T:class,IResource
|
public static T AddResource<T>(this DbSet<T> dbSet, object properties = null) where T : class, IResource
|
||||||
{
|
{
|
||||||
var store = dbSet.GetInfrastructure().GetService<IDbContextOptions>().FindExtension<EsyurExtensionOptions>().Store;
|
var store = dbSet.GetInfrastructure().GetService<IDbContextOptions>().FindExtension<EsyurExtensionOptions>().Store;
|
||||||
|
var manager = store.Instance.Managers.FirstOrDefault();// > 0 ? store.Instance.Managers.First() : null;
|
||||||
|
|
||||||
|
//var db = dbSet.GetService<ICurrentDbContext>().Context;
|
||||||
|
|
||||||
//var resource = dbSet.GetInfrastructure().CreateResource<T>(properties);
|
//var resource = dbSet.GetInfrastructure().CreateResource<T>(properties);
|
||||||
//var resource = Warehouse.New<T>("", options.Store, null, null, null, properties);
|
//var resource = Warehouse.New<T>("", options.Store, null, null, null, properties);
|
||||||
var resource = Warehouse.New<T>("", null, null, null, null, properties);
|
var resource = Warehouse.New<T>("", null, null, null, null, properties);
|
||||||
dbSet.Add(resource);
|
var entity = dbSet.Add(resource);
|
||||||
|
entity.Context.SaveChanges();
|
||||||
|
|
||||||
|
var id = store.TypesByType[typeof(T)].PrimaryKey.GetValue(resource);
|
||||||
|
|
||||||
|
Warehouse.Put(resource, id.ToString(), store, null, null, 0, manager);
|
||||||
|
|
||||||
return resource;
|
return resource;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static T CreateResource<T>(this IServiceProvider serviceProvider, object properties = null) where T:class,IResource
|
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>();
|
||||||
|
|
||||||
@ -90,7 +100,7 @@ namespace Esyur.Stores.EntityCore
|
|||||||
|
|
||||||
public static DbContextOptionsBuilder<TContext> UseEsyur<TContext>(
|
public static DbContextOptionsBuilder<TContext> UseEsyur<TContext>(
|
||||||
this DbContextOptionsBuilder<TContext> optionsBuilder,
|
this DbContextOptionsBuilder<TContext> optionsBuilder,
|
||||||
//DbContext context,
|
//DbContext context,
|
||||||
string name = null,
|
string name = null,
|
||||||
IResource parent = null,
|
IResource parent = null,
|
||||||
IPermissionsManager manager = null,
|
IPermissionsManager manager = null,
|
||||||
|
@ -25,7 +25,7 @@ SOFTWARE.
|
|||||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||||
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
|
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
|
||||||
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Infrastructure;
|
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Infrastructure;
|
||||||
using Microsoft.EntityFrameworkCore.Proxies.Internal;
|
//using Microsoft.EntityFrameworkCore.Proxies.Internal;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
@ -47,15 +47,26 @@ namespace Esyur.Stores.EntityCore
|
|||||||
|
|
||||||
private readonly ConstructorBindingConvention _directBindingConvention;
|
private readonly ConstructorBindingConvention _directBindingConvention;
|
||||||
|
|
||||||
|
//public static object CreateInstance(IDbContextOptions dbContextOptions, IEntityType entityType,
|
||||||
|
// object[] constructorArguments, DbContext context, long id)
|
||||||
|
//{
|
||||||
|
// return CreateInstance(dbContextOptions, entityType,
|
||||||
|
// constructorArguments, context, id);
|
||||||
|
//}
|
||||||
|
|
||||||
public static object CreateInstance(
|
public static object CreateInstance(
|
||||||
IDbContextOptions dbContextOptions,
|
IDbContextOptions dbContextOptions,
|
||||||
IEntityType entityType,
|
IEntityType entityType,
|
||||||
// ILazyLoader loader,
|
object[] properties
|
||||||
object[] constructorArguments,
|
|
||||||
DbContext context,
|
// ILazyLoader loader,
|
||||||
int id = 0
|
// object[] constructorArguments,
|
||||||
)
|
//DbContext context,
|
||||||
|
)
|
||||||
{
|
{
|
||||||
|
///var id = constructorArguments.Last();
|
||||||
|
var id = properties.First();
|
||||||
|
|
||||||
var options = dbContextOptions.FindExtension<EsyurExtensionOptions>();
|
var options = dbContextOptions.FindExtension<EsyurExtensionOptions>();
|
||||||
var manager = options.Store.Instance.Managers.Count > 0 ? options.Store.Instance.Managers.First() : null;
|
var manager = options.Store.Instance.Managers.Count > 0 ? options.Store.Instance.Managers.First() : null;
|
||||||
|
|
||||||
@ -66,11 +77,11 @@ namespace Esyur.Stores.EntityCore
|
|||||||
|
|
||||||
// check if the object exists
|
// check if the object exists
|
||||||
var obj = Warehouse.New(entityType.ClrType) as EntityResource;//, "", options.Store, null, manager);
|
var obj = Warehouse.New(entityType.ClrType) as EntityResource;//, "", options.Store, null, manager);
|
||||||
obj._PrimaryId = id;
|
//obj._PrimaryId = id;
|
||||||
options.Store.TypesByType[entityType.ClrType].PrimaryKey.SetValue(obj, id);
|
options.Store.TypesByType[entityType.ClrType].PrimaryKey.SetValue(obj, id);
|
||||||
Warehouse.Put(obj, id.ToString(), options.Store, null, null, 0, manager);
|
Warehouse.Put(obj, id.ToString(), options.Store, null, null, 0, manager);
|
||||||
|
|
||||||
// obj.Instance.IntVal = id;//.Variables.Add("eid", id);
|
// obj.Instance.IntVal = id;//.Variables.Add("eid", id);
|
||||||
|
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
@ -89,7 +100,7 @@ namespace Esyur.Stores.EntityCore
|
|||||||
{
|
{
|
||||||
var proxyType = ResourceProxy.GetProxy(entityType.ClrType);
|
var proxyType = ResourceProxy.GetProxy(entityType.ClrType);
|
||||||
|
|
||||||
var ann = entityType.GetAnnotation(CoreAnnotationNames.ConstructorBinding);
|
// var ann = entityType.GetAnnotation(CoreAnnotationNames.ConstructorBinding);
|
||||||
|
|
||||||
var binding = (InstantiationBinding)entityType[CoreAnnotationNames.ConstructorBinding];
|
var binding = (InstantiationBinding)entityType[CoreAnnotationNames.ConstructorBinding];
|
||||||
if (binding == null)
|
if (binding == null)
|
||||||
@ -109,12 +120,18 @@ namespace Esyur.Stores.EntityCore
|
|||||||
{
|
{
|
||||||
new DependencyInjectionParameterBinding(typeof(IDbContextOptions), typeof(IDbContextOptions)),
|
new DependencyInjectionParameterBinding(typeof(IDbContextOptions), typeof(IDbContextOptions)),
|
||||||
new EntityTypeParameterBinding(),
|
new EntityTypeParameterBinding(),
|
||||||
//new DependencyInjectionParameterBinding(typeof(ILazyLoader), typeof(ILazyLoader)),
|
// constructor arguments
|
||||||
new ObjectArrayParameterBinding(binding.ParameterBindings),
|
//new ObjectArrayParameterBinding(binding.ParameterBindings),
|
||||||
new ContextParameterBinding(typeof(DbContext)),
|
//new ContextParameterBinding(typeof(DbContext)),
|
||||||
new PropertyParameterBinding(entityType.FindPrimaryKey().Properties.FirstOrDefault())
|
new ObjectArrayParameterBinding(new ParameterBinding[]{
|
||||||
|
new PropertyParameterBinding(entityType.FindPrimaryKey().Properties.FirstOrDefault())
|
||||||
|
})
|
||||||
|
// new Microsoft.EntityFrameworkCore.Metadata.ObjectArrayParameterBinding(),
|
||||||
|
//new ObjectArrayParameterBinding()
|
||||||
|
|
||||||
},
|
},
|
||||||
proxyType));
|
proxyType));
|
||||||
|
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
|
8
Esyur.Stores.EntityCore/Properties/launchSettings.json
Normal file
8
Esyur.Stores.EntityCore/Properties/launchSettings.json
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"profiles": {
|
||||||
|
"Esyur.Stores.EntityCore": {
|
||||||
|
"commandName": "Project",
|
||||||
|
"commandLineArgs": "--migrate"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -34,6 +34,7 @@ namespace Esyur.Core
|
|||||||
MethodNotFound,
|
MethodNotFound,
|
||||||
PropertyNotFound,
|
PropertyNotFound,
|
||||||
SetPropertyDenied,
|
SetPropertyDenied,
|
||||||
ReadOnlyProperty
|
ReadOnlyProperty,
|
||||||
|
GeneralFailure,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -659,9 +659,11 @@ namespace Esyur.Data
|
|||||||
|
|
||||||
public static Int16[] GetInt16Array(this byte[] data, uint offset, uint length)
|
public static Int16[] GetInt16Array(this byte[] data, uint offset, uint length)
|
||||||
{
|
{
|
||||||
|
var j = 0; var end = offset + length;
|
||||||
|
|
||||||
var rt = new Int16[length / 2];
|
var rt = new Int16[length / 2];
|
||||||
for (var i = 0; i < length; i += 2)
|
for (var i = offset; i < end; i += 2)
|
||||||
rt[i] = GetInt16(data, (uint)(offset + i));
|
rt[j++] = GetInt16(data, i);
|
||||||
|
|
||||||
return rt;
|
return rt;
|
||||||
}
|
}
|
||||||
@ -673,10 +675,14 @@ namespace Esyur.Data
|
|||||||
|
|
||||||
public static UInt16[] GetUInt16Array(this byte[] data, uint offset, uint length)
|
public static UInt16[] GetUInt16Array(this byte[] data, uint offset, uint length)
|
||||||
{
|
{
|
||||||
|
var j = 0; var end = offset + length;
|
||||||
var rt = new UInt16[length / 2];
|
var rt = new UInt16[length / 2];
|
||||||
for (var i = 0; i < length; i += 2)
|
|
||||||
rt[i] = GetUInt16(data, (uint)(offset + i));
|
for (var i = offset; i < end; i += 2)
|
||||||
|
rt[j++] = GetUInt16(data, i);
|
||||||
|
|
||||||
return rt;
|
return rt;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Int32 GetInt32(this byte[] data, uint offset)
|
public static Int32 GetInt32(this byte[] data, uint offset)
|
||||||
@ -686,9 +692,11 @@ namespace Esyur.Data
|
|||||||
|
|
||||||
public static Int32[] GetInt32Array(this byte[] data, uint offset, uint length)
|
public static Int32[] GetInt32Array(this byte[] data, uint offset, uint length)
|
||||||
{
|
{
|
||||||
|
var j = 0; var end = offset + length;
|
||||||
|
|
||||||
var rt = new Int32[length / 4];
|
var rt = new Int32[length / 4];
|
||||||
for (var i = 0; i < length; i += 4)
|
for (var i = offset; i < end; i += 4)
|
||||||
rt[i] = GetInt32(data, (uint)(offset + i));
|
rt[j++] = GetInt32(data, i);
|
||||||
|
|
||||||
return rt;
|
return rt;
|
||||||
}
|
}
|
||||||
@ -700,9 +708,11 @@ namespace Esyur.Data
|
|||||||
|
|
||||||
public static UInt32[] GetUInt32Array(this byte[] data, uint offset, uint length)
|
public static UInt32[] GetUInt32Array(this byte[] data, uint offset, uint length)
|
||||||
{
|
{
|
||||||
|
var j = 0; var end = offset + length;
|
||||||
var rt = new UInt32[length / 4];
|
var rt = new UInt32[length / 4];
|
||||||
for (var i = 0; i < length; i += 4)
|
|
||||||
rt[i] = GetUInt32(data, (uint)(offset + i));
|
for (var i = offset; i < end; i += 4)
|
||||||
|
rt[j++] = GetUInt16(data, i);
|
||||||
|
|
||||||
return rt;
|
return rt;
|
||||||
}
|
}
|
||||||
@ -728,9 +738,11 @@ namespace Esyur.Data
|
|||||||
|
|
||||||
public static Int64[] GetInt64Array(this byte[] data, uint offset, uint length)
|
public static Int64[] GetInt64Array(this byte[] data, uint offset, uint length)
|
||||||
{
|
{
|
||||||
|
var j = 0; var end = offset + length;
|
||||||
var rt = new Int64[length / 8];
|
var rt = new Int64[length / 8];
|
||||||
for (var i = 0; i < length; i += 8)
|
|
||||||
rt[i] = GetInt64(data, (uint)(offset + i));
|
for (var i = offset; i < end; i += 8)
|
||||||
|
rt[j++] = GetInt64(data, i);
|
||||||
|
|
||||||
return rt;
|
return rt;
|
||||||
}
|
}
|
||||||
@ -767,9 +779,11 @@ namespace Esyur.Data
|
|||||||
|
|
||||||
public static UInt64[] GetUInt64Array(this byte[] data, uint offset, uint length)
|
public static UInt64[] GetUInt64Array(this byte[] data, uint offset, uint length)
|
||||||
{
|
{
|
||||||
|
var j = 0; var end = offset + length;
|
||||||
var rt = new UInt64[length / 8];
|
var rt = new UInt64[length / 8];
|
||||||
for (var i = 0; i < length; i += 8)
|
|
||||||
rt[i] = GetUInt64(data, (uint)(offset + i));
|
for (var i = offset; i < end; i += 8)
|
||||||
|
rt[j++] = GetUInt64(data, i);
|
||||||
|
|
||||||
return rt;
|
return rt;
|
||||||
}
|
}
|
||||||
@ -787,9 +801,11 @@ namespace Esyur.Data
|
|||||||
|
|
||||||
public static float[] GetFloat32Array(this byte[] data, uint offset, uint length)
|
public static float[] GetFloat32Array(this byte[] data, uint offset, uint length)
|
||||||
{
|
{
|
||||||
|
var j = 0; var end = offset + length;
|
||||||
var rt = new float[length / 4];
|
var rt = new float[length / 4];
|
||||||
for (var i = 0; i < length; i += 4)
|
|
||||||
rt[i] = GetFloat32(data, (uint)(offset + i));
|
for (var i = offset; i < end; i += 4)
|
||||||
|
rt[j++] = GetFloat32(data, i);
|
||||||
|
|
||||||
return rt;
|
return rt;
|
||||||
}
|
}
|
||||||
@ -813,9 +829,11 @@ namespace Esyur.Data
|
|||||||
|
|
||||||
public static double[] GetFloat64Array(this byte[] data, uint offset, uint length)
|
public static double[] GetFloat64Array(this byte[] data, uint offset, uint length)
|
||||||
{
|
{
|
||||||
|
var j = 0; var end = offset + length;
|
||||||
var rt = new double[length / 8];
|
var rt = new double[length / 8];
|
||||||
for (var i = 0; i < length; i += 8)
|
|
||||||
rt[i] = GetFloat64(data, (uint)(offset + i));
|
for (var i = offset; i < end; i += 8)
|
||||||
|
rt[j++] = GetFloat64(data, i);
|
||||||
|
|
||||||
return rt;
|
return rt;
|
||||||
}
|
}
|
||||||
@ -840,9 +858,13 @@ namespace Esyur.Data
|
|||||||
|
|
||||||
public static char[] GetCharArray(this byte[] data, uint offset, uint length)
|
public static char[] GetCharArray(this byte[] data, uint offset, uint length)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
var j = 0; var end = offset + length;
|
||||||
var rt = new char[length / 2];
|
var rt = new char[length / 2];
|
||||||
for (var i = 0; i < length; i += 2)
|
|
||||||
rt[i] = GetChar(data, (uint)(offset + i));
|
for (var i = offset; i < end; i += 2)
|
||||||
|
rt[j++] = GetChar(data, i);
|
||||||
|
|
||||||
return rt;
|
return rt;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -875,9 +897,12 @@ namespace Esyur.Data
|
|||||||
|
|
||||||
public static Guid[] GetGuidArray(this byte[] data, uint offset, uint length)
|
public static Guid[] GetGuidArray(this byte[] data, uint offset, uint length)
|
||||||
{
|
{
|
||||||
|
var j = 0; var end = offset + length;
|
||||||
var rt = new Guid[length / 16];
|
var rt = new Guid[length / 16];
|
||||||
for (var i = 0; i < length; i += 16)
|
|
||||||
rt[i] = GetGuid(data, (uint)(offset + i));
|
for (var i = offset; i < end; i += 16)
|
||||||
|
rt[j++] = GetGuid(data, i);
|
||||||
|
|
||||||
return rt;
|
return rt;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -889,9 +914,12 @@ namespace Esyur.Data
|
|||||||
|
|
||||||
public static DateTime[] GetDateTimeArray(this byte[] data, uint offset, uint length)
|
public static DateTime[] GetDateTimeArray(this byte[] data, uint offset, uint length)
|
||||||
{
|
{
|
||||||
|
var j = 0; var end = offset + length;
|
||||||
var rt = new DateTime[length / 8];
|
var rt = new DateTime[length / 8];
|
||||||
for (var i = 0; i < length; i += 8)
|
|
||||||
rt[i] = GetDateTime(data, (uint)(offset + i));
|
for (var i = offset; i < end; i += 8)
|
||||||
|
rt[j++] = GetDateTime(data, i);
|
||||||
|
|
||||||
return rt;
|
return rt;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -902,9 +930,12 @@ namespace Esyur.Data
|
|||||||
|
|
||||||
public static IPAddress[] GetIPv4AddressArray(this byte[] data, uint offset, uint length)
|
public static IPAddress[] GetIPv4AddressArray(this byte[] data, uint offset, uint length)
|
||||||
{
|
{
|
||||||
|
var j = 0; var end = offset + length;
|
||||||
var rt = new IPAddress[length / 4];
|
var rt = new IPAddress[length / 4];
|
||||||
for (var i = 0; i < length; i += 4)
|
|
||||||
rt[i] = GetIPv4Address(data, (uint)(offset + i));
|
for (var i = offset; i < end; i += 4)
|
||||||
|
rt[j++] = GetIPv6Address(data, i);
|
||||||
|
|
||||||
return rt;
|
return rt;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -915,10 +946,14 @@ namespace Esyur.Data
|
|||||||
|
|
||||||
public static IPAddress[] GetIPv6AddressArray(this byte[] data, uint offset, uint length)
|
public static IPAddress[] GetIPv6AddressArray(this byte[] data, uint offset, uint length)
|
||||||
{
|
{
|
||||||
|
var j = 0; var end = offset + length;
|
||||||
var rt = new IPAddress[length / 16];
|
var rt = new IPAddress[length / 16];
|
||||||
for (var i = 0; i < length; i += 16)
|
|
||||||
rt[i] = GetIPv6Address(data, (uint)(offset + i));
|
for (var i = offset; i < end; i += 16)
|
||||||
|
rt[j++] = GetIPv6Address(data, i);
|
||||||
|
|
||||||
return rt;
|
return rt;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -43,13 +43,11 @@ namespace Esyur.Net.HTTP
|
|||||||
public class HTTPConnection : NetworkConnection
|
public class HTTPConnection : NetworkConnection
|
||||||
{
|
{
|
||||||
|
|
||||||
public void SetParent(HTTPServer Parent)
|
|
||||||
{
|
|
||||||
Server = Parent;
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool WSMode { get; internal set; }
|
public bool WSMode { get; internal set; }
|
||||||
private HTTPServer Server;
|
public HTTPServer Server { get; internal set; }
|
||||||
|
|
||||||
public WebsocketPacket WSRequest { get; set; }
|
public WebsocketPacket WSRequest { get; set; }
|
||||||
public HTTPRequestPacket Request { get; set; }
|
public HTTPRequestPacket Request { get; set; }
|
||||||
public HTTPResponsePacket Response { get; } = new HTTPResponsePacket();
|
public HTTPResponsePacket Response { get; } = new HTTPResponsePacket();
|
||||||
@ -100,7 +98,7 @@ namespace Esyur.Net.HTTP
|
|||||||
public void Flush()
|
public void Flush()
|
||||||
{
|
{
|
||||||
// close the connection
|
// close the connection
|
||||||
if (Request.Headers["connection"].ToLower() != "keep-alive" & Connected)
|
if (Request.Headers["connection"].ToLower() != "keep-alive" & IsConnected)
|
||||||
Close();
|
Close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -232,6 +230,102 @@ namespace Esyur.Net.HTTP
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected override void DataReceived(NetworkBuffer data)
|
||||||
|
{
|
||||||
|
|
||||||
|
byte[] msg = data.Read();
|
||||||
|
|
||||||
|
var BL = Parse(msg);
|
||||||
|
|
||||||
|
if (BL == 0)
|
||||||
|
{
|
||||||
|
if (Request.Method == HTTPRequestPacket.HTTPMethod.UNKNOWN)
|
||||||
|
{
|
||||||
|
Close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (Request.URL == "")
|
||||||
|
{
|
||||||
|
Close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (BL == -1)
|
||||||
|
{
|
||||||
|
data.HoldForNextWrite(msg);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else if (BL < 0)
|
||||||
|
{
|
||||||
|
data.HoldFor(msg, (uint)(msg.Length - BL));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else if (BL > 0)
|
||||||
|
{
|
||||||
|
if (BL > Server.MaxPost)
|
||||||
|
{
|
||||||
|
Send(
|
||||||
|
"<html><body>POST method content is larger than "
|
||||||
|
+ Server.MaxPost
|
||||||
|
+ " bytes.</body></html>");
|
||||||
|
|
||||||
|
Close();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
data.HoldFor(msg, (uint)(msg.Length + BL));
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else if (BL < 0) // for security
|
||||||
|
{
|
||||||
|
Close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if (IsWebsocketRequest() & !WSMode)
|
||||||
|
{
|
||||||
|
Upgrade();
|
||||||
|
//return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//return;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (!Server.Execute(this))
|
||||||
|
{
|
||||||
|
Response.Number = HTTPResponsePacket.ResponseCode.InternalServerError;
|
||||||
|
Send("Bad Request");
|
||||||
|
Close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
if (ex.Message != "Thread was being aborted.")
|
||||||
|
{
|
||||||
|
|
||||||
|
Global.Log("HTTPServer", LogType.Error, ex.ToString());
|
||||||
|
|
||||||
|
//Console.WriteLine(ex.ToString());
|
||||||
|
//EventLog.WriteEntry("HttpServer", ex.ToString(), EventLogEntryType.Error);
|
||||||
|
Send(Error500(ex.Message));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private string Error500(string msg)
|
||||||
|
{
|
||||||
|
return "<html><head><title>500 Internal Server Error</title></head><br>\r\n"
|
||||||
|
+ "<body><br>\r\n"
|
||||||
|
+ "<b>500</b> Internal Server Error<br>" + msg + "\r\n"
|
||||||
|
+ "</body><br>\r\n"
|
||||||
|
+ "</html><br>\r\n";
|
||||||
|
}
|
||||||
|
|
||||||
public async AsyncReply<bool> SendFile(string filename)
|
public async AsyncReply<bool> SendFile(string filename)
|
||||||
{
|
{
|
||||||
@ -329,5 +423,15 @@ namespace Esyur.Net.HTTP
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected override void Connected()
|
||||||
|
{
|
||||||
|
// do nothing
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void Disconencted()
|
||||||
|
{
|
||||||
|
// do nothing
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -62,7 +62,7 @@ namespace Esyur.Net.HTTP
|
|||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public abstract bool Execute(HTTPConnection sender);
|
public abstract AsyncReply<bool> Execute(HTTPConnection sender);
|
||||||
|
|
||||||
public virtual void ClientConnected(HTTPConnection HTTP)
|
public virtual void ClientConnected(HTTPConnection HTTP)
|
||||||
{
|
{
|
||||||
|
@ -140,113 +140,23 @@ namespace Esyur.Net.HTTP
|
|||||||
return Cookie;
|
return Cookie;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void ClientDisconnected(HTTPConnection sender)
|
protected override void ClientDisconnected(HTTPConnection connection)
|
||||||
{
|
{
|
||||||
//Console.WriteLine("OUT: " + this.Connections.Count);
|
|
||||||
|
|
||||||
foreach (var filter in filters)
|
foreach (var filter in filters)
|
||||||
filter.ClientDisconnected(sender);
|
filter.ClientDisconnected(connection);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
protected override void DataReceived(HTTPConnection sender, NetworkBuffer data)
|
internal bool Execute(HTTPConnection sender)
|
||||||
{
|
{
|
||||||
|
foreach (var resource in filters)
|
||||||
byte[] msg = data.Read();
|
if (resource.Execute(sender).Wait(30000))
|
||||||
|
return true;
|
||||||
var BL = sender.Parse(msg);
|
return false;
|
||||||
|
|
||||||
if (BL == 0)
|
|
||||||
{
|
|
||||||
if (sender.Request.Method == HTTPRequestPacket.HTTPMethod.UNKNOWN)
|
|
||||||
{
|
|
||||||
sender.Close();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (sender.Request.URL == "")
|
|
||||||
{
|
|
||||||
sender.Close();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (BL == -1)
|
|
||||||
{
|
|
||||||
data.HoldForNextWrite(msg);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
else if (BL < 0)
|
|
||||||
{
|
|
||||||
data.HoldFor(msg, (uint) (msg.Length - BL));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
else if (BL > 0)
|
|
||||||
{
|
|
||||||
if (BL > MaxPost)
|
|
||||||
{
|
|
||||||
sender.Send(
|
|
||||||
"<html><body>POST method content is larger than "
|
|
||||||
+ MaxPost
|
|
||||||
+ " bytes.</body></html>");
|
|
||||||
|
|
||||||
sender.Close();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
data.HoldFor(msg, (uint)(msg.Length + BL));
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
else if (BL < 0) // for security
|
|
||||||
{
|
|
||||||
sender.Close();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (sender.IsWebsocketRequest() & !sender.WSMode)
|
|
||||||
{
|
|
||||||
sender.Upgrade();
|
|
||||||
//return;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//return;
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
foreach (var resource in filters)
|
|
||||||
if (resource.Execute(sender))
|
|
||||||
return;
|
|
||||||
|
|
||||||
sender.Response.Number = HTTPResponsePacket.ResponseCode.InternalServerError;
|
|
||||||
sender.Send("Bad Request");
|
|
||||||
sender.Close();
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
if (ex.Message != "Thread was being aborted.")
|
|
||||||
{
|
|
||||||
|
|
||||||
Global.Log("HTTPServer", LogType.Error, ex.ToString());
|
|
||||||
|
|
||||||
//Console.WriteLine(ex.ToString());
|
|
||||||
//EventLog.WriteEntry("HttpServer", ex.ToString(), EventLogEntryType.Error);
|
|
||||||
sender.Send(Error500(ex.Message));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private string Error500(string msg)
|
|
||||||
{
|
|
||||||
return "<html><head><title>500 Internal Server Error</title></head><br>\r\n"
|
|
||||||
+ "<body><br>\r\n"
|
|
||||||
+ "<b>500</b> Internal Server Error<br>" + msg + "\r\n"
|
|
||||||
+ "</body><br>\r\n"
|
|
||||||
+ "</html><br>\r\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -298,7 +208,7 @@ namespace Esyur.Net.HTTP
|
|||||||
|
|
||||||
//if (ip == null) ip = IPAddress.Any;
|
//if (ip == null) ip = IPAddress.Any;
|
||||||
|
|
||||||
ISocket listener;
|
Sockets.ISocket listener;
|
||||||
IPAddress ipAdd;
|
IPAddress ipAdd;
|
||||||
|
|
||||||
if (IP == null)
|
if (IP == null)
|
||||||
@ -331,29 +241,33 @@ namespace Esyur.Net.HTTP
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void ClientConnected(HTTPConnection sender)
|
|
||||||
{
|
|
||||||
//sender.SessionModified += SessionModified;
|
|
||||||
//sender.SessionEnded += SessionExpired;
|
|
||||||
sender.SetParent(this);
|
|
||||||
|
|
||||||
//Console.WriteLine("IN: " + this.Connections.Count);
|
public override void Add(HTTPConnection connection)
|
||||||
|
{
|
||||||
|
connection.Server = this;
|
||||||
|
base.Add(connection);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Remove(HTTPConnection connection)
|
||||||
|
{
|
||||||
|
connection.Server = null;
|
||||||
|
base.Remove(connection);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void ClientConnected(HTTPConnection connection)
|
||||||
|
{
|
||||||
if (filters.Length == 0)
|
if (filters.Length == 0)
|
||||||
{
|
{
|
||||||
sender.Close();
|
connection.Close();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var resource in filters)
|
foreach (var resource in filters)
|
||||||
{
|
{
|
||||||
resource.ClientConnected(sender);
|
resource.ClientConnected(connection);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Destroy()
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
public int LocalPort
|
public int LocalPort
|
||||||
|
@ -13,7 +13,7 @@ namespace Esyur.Net.HTTP
|
|||||||
[Attribute]
|
[Attribute]
|
||||||
EntryPoint EntryPoint { get; set; }
|
EntryPoint EntryPoint { get; set; }
|
||||||
|
|
||||||
public override bool Execute(HTTPConnection sender)
|
public async override AsyncReply<bool> Execute(HTTPConnection sender)
|
||||||
{
|
{
|
||||||
if (sender.Request.URL != "iip")
|
if (sender.Request.URL != "iip")
|
||||||
return false;
|
return false;
|
||||||
|
@ -43,7 +43,7 @@ namespace Esyur.Net.HTTP
|
|||||||
set;
|
set;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override bool Execute(HTTPConnection sender)
|
public async override AsyncReply<bool> Execute(HTTPConnection sender)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (sender.IsWebsocketRequest())
|
if (sender.IsWebsocketRequest())
|
||||||
@ -58,11 +58,11 @@ namespace Esyur.Net.HTTP
|
|||||||
|
|
||||||
var httpServer = sender.Parent;
|
var httpServer = sender.Parent;
|
||||||
var wsSocket = new WSSocket(tcpSocket);
|
var wsSocket = new WSSocket(tcpSocket);
|
||||||
httpServer.RemoveConnection(sender);
|
httpServer.Remove(sender);
|
||||||
|
|
||||||
var iipConnection = new DistributedConnection();
|
var iipConnection = new DistributedConnection();
|
||||||
|
|
||||||
Server.AddConnection(iipConnection);
|
Server.Add(iipConnection);
|
||||||
iipConnection.Assign(wsSocket);
|
iipConnection.Assign(wsSocket);
|
||||||
wsSocket.Begin();
|
wsSocket.Begin();
|
||||||
|
|
||||||
|
@ -101,11 +101,7 @@ namespace Esyur.Net.IIP
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Distributed server responsible for this connection, usually for incoming connections.
|
/// Distributed server responsible for this connection, usually for incoming connections.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public DistributedServer Server
|
public DistributedServer Server { get; internal set; }
|
||||||
{
|
|
||||||
get;
|
|
||||||
set;
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool Remove(IResource resource)
|
public bool Remove(IResource resource)
|
||||||
{
|
{
|
||||||
@ -193,7 +189,7 @@ namespace Esyur.Net.IIP
|
|||||||
/// Assign a socket to the connection.
|
/// Assign a socket to the connection.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="socket">Any socket that implements ISocket.</param>
|
/// <param name="socket">Any socket that implements ISocket.</param>
|
||||||
public override void Assign(ISocket socket)
|
public override void Assign(Sockets.ISocket socket)
|
||||||
{
|
{
|
||||||
base.Assign(socket);
|
base.Assign(socket);
|
||||||
|
|
||||||
@ -202,39 +198,22 @@ namespace Esyur.Net.IIP
|
|||||||
session.LocalAuthentication.Source.Attributes[SourceAttributeType.IPv4] = socket.LocalEndPoint.Address;
|
session.LocalAuthentication.Source.Attributes[SourceAttributeType.IPv4] = socket.LocalEndPoint.Address;
|
||||||
session.LocalAuthentication.Source.Attributes[SourceAttributeType.Port] = socket.LocalEndPoint.Port;
|
session.LocalAuthentication.Source.Attributes[SourceAttributeType.Port] = socket.LocalEndPoint.Port;
|
||||||
|
|
||||||
if (session.LocalAuthentication.Type == AuthenticationType.Client)
|
if (socket.State == SocketState.Established &&
|
||||||
|
session.LocalAuthentication.Type == AuthenticationType.Client)
|
||||||
{
|
{
|
||||||
// declare (Credentials -> No Auth, No Enctypt)
|
// declare (Credentials -> No Auth, No Enctypt)
|
||||||
|
|
||||||
var un = DC.ToBytes(session.LocalAuthentication.Username);
|
var un = DC.ToBytes(session.LocalAuthentication.Username);
|
||||||
var dmn = DC.ToBytes(session.LocalAuthentication.Domain);// domain);
|
var dmn = DC.ToBytes(session.LocalAuthentication.Domain);// domain);
|
||||||
|
|
||||||
if (socket.State == SocketState.Established)
|
SendParams()
|
||||||
{
|
.AddUInt8(0x60)
|
||||||
SendParams()
|
.AddUInt8((byte)dmn.Length)
|
||||||
.AddUInt8(0x60)
|
.AddUInt8Array(dmn)
|
||||||
.AddUInt8((byte)dmn.Length)
|
.AddUInt8Array(localNonce)
|
||||||
.AddUInt8Array(dmn)
|
.AddUInt8((byte)un.Length)
|
||||||
.AddUInt8Array(localNonce)
|
.AddUInt8Array(un)
|
||||||
.AddUInt8((byte)un.Length)
|
.Done();//, dmn, localNonce, (byte)un.Length, un);
|
||||||
.AddUInt8Array(un)
|
|
||||||
.Done();//, dmn, localNonce, (byte)un.Length, un);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
socket.OnConnect += () =>
|
|
||||||
{ // declare (Credentials -> No Auth, No Enctypt)
|
|
||||||
//SendParams((byte)0x60, (byte)dmn.Length, dmn, localNonce, (byte)un.Length, un);
|
|
||||||
SendParams()
|
|
||||||
.AddUInt8(0x60)
|
|
||||||
.AddUInt8((byte)dmn.Length)
|
|
||||||
.AddUInt8Array(dmn)
|
|
||||||
.AddUInt8Array(localNonce)
|
|
||||||
.AddUInt8((byte)un.Length)
|
|
||||||
.AddUInt8Array(un)
|
|
||||||
.Done();
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -246,7 +225,7 @@ namespace Esyur.Net.IIP
|
|||||||
/// <param name="domain">Working domain.</param>
|
/// <param name="domain">Working domain.</param>
|
||||||
/// <param name="username">Username.</param>
|
/// <param name="username">Username.</param>
|
||||||
/// <param name="password">Password.</param>
|
/// <param name="password">Password.</param>
|
||||||
public DistributedConnection(ISocket socket, string domain, string username, string password)
|
public DistributedConnection(Sockets.ISocket socket, string domain, string username, string password)
|
||||||
{
|
{
|
||||||
this.session = new Session(new ClientAuthentication()
|
this.session = new Session(new ClientAuthentication()
|
||||||
, new HostAuthentication());
|
, new HostAuthentication());
|
||||||
@ -264,7 +243,7 @@ namespace Esyur.Net.IIP
|
|||||||
Assign(socket);
|
Assign(socket);
|
||||||
}
|
}
|
||||||
|
|
||||||
public DistributedConnection(ISocket socket, string domain, ulong tokenIndex, string token)
|
public DistributedConnection(Sockets.ISocket socket, string domain, ulong tokenIndex, string token)
|
||||||
{
|
{
|
||||||
this.session = new Session(new ClientAuthentication()
|
this.session = new Session(new ClientAuthentication()
|
||||||
, new HostAuthentication());
|
, new HostAuthentication());
|
||||||
@ -327,6 +306,12 @@ namespace Esyur.Net.IIP
|
|||||||
r.NextBytes(localNonce);
|
r.NextBytes(localNonce);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override void Destroy()
|
||||||
|
{
|
||||||
|
this.OnReady = null;
|
||||||
|
this.OnError = null;
|
||||||
|
base.Destroy();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private uint processPacket(byte[] msg, uint offset, uint ends, NetworkBuffer data, int chunkId)
|
private uint processPacket(byte[] msg, uint offset, uint ends, NetworkBuffer data, int chunkId)
|
||||||
@ -620,55 +605,78 @@ namespace Esyur.Net.IIP
|
|||||||
|
|
||||||
if (authPacket.RemoteMethod == AuthenticationMethod.Credentials && authPacket.LocalMethod == AuthenticationMethod.None)
|
if (authPacket.RemoteMethod == AuthenticationMethod.Credentials && authPacket.LocalMethod == AuthenticationMethod.None)
|
||||||
{
|
{
|
||||||
Server.Membership.UserExists(authPacket.RemoteUsername, authPacket.Domain).Then(x =>
|
try
|
||||||
{
|
{
|
||||||
if (x)
|
Server.Membership.UserExists(authPacket.RemoteUsername, authPacket.Domain).Then(x =>
|
||||||
{
|
{
|
||||||
session.RemoteAuthentication.Username = authPacket.RemoteUsername;
|
if (x)
|
||||||
remoteNonce = authPacket.RemoteNonce;
|
{
|
||||||
session.RemoteAuthentication.Domain = authPacket.Domain;
|
session.RemoteAuthentication.Username = authPacket.RemoteUsername;
|
||||||
SendParams()
|
remoteNonce = authPacket.RemoteNonce;
|
||||||
.AddUInt8(0xa0)
|
session.RemoteAuthentication.Domain = authPacket.Domain;
|
||||||
.AddUInt8Array(localNonce)
|
SendParams()
|
||||||
.Done();
|
.AddUInt8(0xa0)
|
||||||
//SendParams((byte)0xa0, localNonce);
|
.AddUInt8Array(localNonce)
|
||||||
}
|
.Done();
|
||||||
else
|
//SendParams((byte)0xa0, localNonce);
|
||||||
{
|
}
|
||||||
//Console.WriteLine("User not found");
|
else
|
||||||
SendParams().AddUInt8(0xc0)
|
{
|
||||||
.AddUInt8((byte)ExceptionCode.UserOrTokenNotFound)
|
//Console.WriteLine("User not found");
|
||||||
.AddUInt16(14)
|
SendParams().AddUInt8(0xc0)
|
||||||
.AddString("User not found").Done();
|
.AddUInt8((byte)ExceptionCode.UserOrTokenNotFound)
|
||||||
}
|
.AddUInt16(14)
|
||||||
});
|
.AddString("User not found").Done();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
var errMsg = DC.ToBytes(ex.Message);
|
||||||
|
|
||||||
|
SendParams().AddUInt8(0xc0)
|
||||||
|
.AddUInt8((byte)ExceptionCode.GeneralFailure)
|
||||||
|
.AddUInt16((ushort)errMsg.Length)
|
||||||
|
.AddUInt8Array(errMsg).Done();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else if (authPacket.RemoteMethod == AuthenticationMethod.Token && authPacket.LocalMethod == AuthenticationMethod.None)
|
else if (authPacket.RemoteMethod == AuthenticationMethod.Token && authPacket.LocalMethod == AuthenticationMethod.None)
|
||||||
{
|
{
|
||||||
// Check if user and token exists
|
try
|
||||||
Server.Membership.TokenExists(authPacket.RemoteTokenIndex, authPacket.Domain).Then(x =>
|
|
||||||
{
|
{
|
||||||
if (x != null)
|
// Check if user and token exists
|
||||||
|
Server.Membership.TokenExists(authPacket.RemoteTokenIndex, authPacket.Domain).Then(x =>
|
||||||
{
|
{
|
||||||
session.RemoteAuthentication.Username = x;
|
if (x != null)
|
||||||
session.RemoteAuthentication.TokenIndex = authPacket.RemoteTokenIndex;
|
{
|
||||||
remoteNonce = authPacket.RemoteNonce;
|
session.RemoteAuthentication.Username = x;
|
||||||
session.RemoteAuthentication.Domain = authPacket.Domain;
|
session.RemoteAuthentication.TokenIndex = authPacket.RemoteTokenIndex;
|
||||||
SendParams()
|
remoteNonce = authPacket.RemoteNonce;
|
||||||
.AddUInt8(0xa0)
|
session.RemoteAuthentication.Domain = authPacket.Domain;
|
||||||
.AddUInt8Array(localNonce)
|
SendParams()
|
||||||
.Done();
|
.AddUInt8(0xa0)
|
||||||
}
|
.AddUInt8Array(localNonce)
|
||||||
else
|
.Done();
|
||||||
{
|
}
|
||||||
//Console.WriteLine("User not found");
|
else
|
||||||
SendParams().AddUInt8(0xc0)
|
{
|
||||||
.AddUInt8((byte)ExceptionCode.UserOrTokenNotFound)
|
//Console.WriteLine("User not found");
|
||||||
.AddUInt16(15)
|
SendParams().AddUInt8(0xc0)
|
||||||
.AddString("Token not found").Done();
|
.AddUInt8((byte)ExceptionCode.UserOrTokenNotFound)
|
||||||
}
|
.AddUInt16(15)
|
||||||
});
|
.AddString("Token not found").Done();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
var errMsg = DC.ToBytes(ex.Message);
|
||||||
|
|
||||||
|
SendParams().AddUInt8(0xc0)
|
||||||
|
.AddUInt8((byte)ExceptionCode.GeneralFailure)
|
||||||
|
.AddUInt16((ushort)errMsg.Length)
|
||||||
|
.AddUInt8Array(errMsg).Done();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (authPacket.Command == IIPAuthPacket.IIPAuthPacketCommand.Action)
|
else if (authPacket.Command == IIPAuthPacket.IIPAuthPacketCommand.Action)
|
||||||
@ -678,54 +686,66 @@ namespace Esyur.Net.IIP
|
|||||||
var remoteHash = authPacket.Hash;
|
var remoteHash = authPacket.Hash;
|
||||||
AsyncReply<byte[]> reply = null;
|
AsyncReply<byte[]> reply = null;
|
||||||
|
|
||||||
if (session.RemoteAuthentication.Method == AuthenticationMethod.Credentials)
|
try
|
||||||
{
|
{
|
||||||
reply = Server.Membership.GetPassword(session.RemoteAuthentication.Username,
|
if (session.RemoteAuthentication.Method == AuthenticationMethod.Credentials)
|
||||||
session.RemoteAuthentication.Domain);
|
|
||||||
}
|
|
||||||
else if (session.RemoteAuthentication.Method == AuthenticationMethod.Token)
|
|
||||||
{
|
|
||||||
reply = Server.Membership.GetToken(session.RemoteAuthentication.TokenIndex,
|
|
||||||
session.RemoteAuthentication.Domain);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Error
|
|
||||||
}
|
|
||||||
|
|
||||||
reply.Then((pw) =>
|
|
||||||
{
|
|
||||||
if (pw != null)
|
|
||||||
{
|
{
|
||||||
var hashFunc = SHA256.Create();
|
reply = Server.Membership.GetPassword(session.RemoteAuthentication.Username,
|
||||||
//var hash = hashFunc.ComputeHash(BinaryList.ToBytes(pw, remoteNonce, localNonce));
|
session.RemoteAuthentication.Domain);
|
||||||
var hash = hashFunc.ComputeHash((new BinaryList())
|
|
||||||
.AddUInt8Array(pw)
|
|
||||||
.AddUInt8Array(remoteNonce)
|
|
||||||
.AddUInt8Array(localNonce)
|
|
||||||
.ToArray());
|
|
||||||
if (hash.SequenceEqual(remoteHash))
|
|
||||||
{
|
|
||||||
// send our hash
|
|
||||||
//var localHash = hashFunc.ComputeHash(BinaryList.ToBytes(localNonce, remoteNonce, pw));
|
|
||||||
//SendParams((byte)0, localHash);
|
|
||||||
|
|
||||||
var localHash = hashFunc.ComputeHash((new BinaryList()).AddUInt8Array(localNonce).AddUInt8Array(remoteNonce).AddUInt8Array(pw).ToArray());
|
|
||||||
SendParams().AddUInt8(0).AddUInt8Array(localHash).Done();
|
|
||||||
|
|
||||||
readyToEstablish = true;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
//Global.Log("auth", LogType.Warning, "U:" + RemoteUsername + " IP:" + Socket.RemoteEndPoint.Address.ToString() + " S:DENIED");
|
|
||||||
SendParams().AddUInt8(0xc0)
|
|
||||||
.AddUInt8((byte)ExceptionCode.AccessDenied)
|
|
||||||
.AddUInt16(13)
|
|
||||||
.AddString("Access Denied")
|
|
||||||
.Done();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
else if (session.RemoteAuthentication.Method == AuthenticationMethod.Token)
|
||||||
|
{
|
||||||
|
reply = Server.Membership.GetToken(session.RemoteAuthentication.TokenIndex,
|
||||||
|
session.RemoteAuthentication.Domain);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Error
|
||||||
|
}
|
||||||
|
|
||||||
|
reply.Then((pw) =>
|
||||||
|
{
|
||||||
|
if (pw != null)
|
||||||
|
{
|
||||||
|
var hashFunc = SHA256.Create();
|
||||||
|
//var hash = hashFunc.ComputeHash(BinaryList.ToBytes(pw, remoteNonce, localNonce));
|
||||||
|
var hash = hashFunc.ComputeHash((new BinaryList())
|
||||||
|
.AddUInt8Array(pw)
|
||||||
|
.AddUInt8Array(remoteNonce)
|
||||||
|
.AddUInt8Array(localNonce)
|
||||||
|
.ToArray());
|
||||||
|
if (hash.SequenceEqual(remoteHash))
|
||||||
|
{
|
||||||
|
// send our hash
|
||||||
|
//var localHash = hashFunc.ComputeHash(BinaryList.ToBytes(localNonce, remoteNonce, pw));
|
||||||
|
//SendParams((byte)0, localHash);
|
||||||
|
|
||||||
|
var localHash = hashFunc.ComputeHash((new BinaryList()).AddUInt8Array(localNonce).AddUInt8Array(remoteNonce).AddUInt8Array(pw).ToArray());
|
||||||
|
SendParams().AddUInt8(0).AddUInt8Array(localHash).Done();
|
||||||
|
|
||||||
|
readyToEstablish = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//Global.Log("auth", LogType.Warning, "U:" + RemoteUsername + " IP:" + Socket.RemoteEndPoint.Address.ToString() + " S:DENIED");
|
||||||
|
SendParams().AddUInt8(0xc0)
|
||||||
|
.AddUInt8((byte)ExceptionCode.AccessDenied)
|
||||||
|
.AddUInt16(13)
|
||||||
|
.AddString("Access Denied")
|
||||||
|
.Done();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
var errMsg = DC.ToBytes(ex.Message);
|
||||||
|
|
||||||
|
SendParams().AddUInt8(0xc0)
|
||||||
|
.AddUInt8((byte)ExceptionCode.GeneralFailure)
|
||||||
|
.AddUInt16((ushort)errMsg.Length)
|
||||||
|
.AddUInt8Array(errMsg).Done();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else if (authPacket.Action == IIPAuthPacket.IIPAuthPacketAction.NewConnection)
|
else if (authPacket.Action == IIPAuthPacket.IIPAuthPacketAction.NewConnection)
|
||||||
{
|
{
|
||||||
@ -741,9 +761,12 @@ namespace Esyur.Net.IIP
|
|||||||
.Done();
|
.Done();
|
||||||
|
|
||||||
ready = true;
|
ready = true;
|
||||||
|
Warehouse.Put(this, this.LocalUsername, null, Server);
|
||||||
|
|
||||||
openReply?.Trigger(true);
|
openReply?.Trigger(true);
|
||||||
OnReady?.Invoke(this);
|
OnReady?.Invoke(this);
|
||||||
Server.Membership.Login(session);
|
|
||||||
|
Server?.Membership.Login(session);
|
||||||
|
|
||||||
//Global.Log("auth", LogType.Warning, "U:" + RemoteUsername + " IP:" + Socket.RemoteEndPoint.Address.ToString() + " S:AUTH");
|
//Global.Log("auth", LogType.Warning, "U:" + RemoteUsername + " IP:" + Socket.RemoteEndPoint.Address.ToString() + " S:AUTH");
|
||||||
|
|
||||||
@ -813,6 +836,9 @@ namespace Esyur.Net.IIP
|
|||||||
session.Id = authPacket.SessionId;
|
session.Id = authPacket.SessionId;
|
||||||
|
|
||||||
ready = true;
|
ready = true;
|
||||||
|
// put it in the warehouse
|
||||||
|
Warehouse.Put(this, this.LocalUsername, null, Server);
|
||||||
|
|
||||||
openReply?.Trigger(true);
|
openReply?.Trigger(true);
|
||||||
OnReady?.Invoke(this);
|
OnReady?.Invoke(this);
|
||||||
|
|
||||||
@ -925,7 +951,7 @@ namespace Esyur.Net.IIP
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
protected override void ConnectionClosed()
|
protected void NetworkClose()
|
||||||
{
|
{
|
||||||
// clean up
|
// clean up
|
||||||
ready = false;
|
ready = false;
|
||||||
@ -948,7 +974,7 @@ namespace Esyur.Net.IIP
|
|||||||
x.Suspend();
|
x.Suspend();
|
||||||
}
|
}
|
||||||
|
|
||||||
public AsyncReply<bool> Connect(AuthenticationMethod method = AuthenticationMethod.Certificate, ISocket socket = null, string hostname = null, ushort port = 0, string username = null, ulong tokenIndex = 0, byte[] passwordOrToken = null, string domain = null)
|
public AsyncReply<bool> Connect(AuthenticationMethod method = AuthenticationMethod.Certificate, Sockets.ISocket socket = null, string hostname = null, ushort port = 0, string username = null, ulong tokenIndex = 0, byte[] passwordOrToken = null, string domain = null)
|
||||||
{
|
{
|
||||||
if (openReply != null)
|
if (openReply != null)
|
||||||
throw new AsyncException(ErrorType.Exception, 0, "Connection in progress");
|
throw new AsyncException(ErrorType.Exception, 0, "Connection in progress");
|
||||||
@ -1093,6 +1119,37 @@ namespace Esyur.Net.IIP
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected override void Connected()
|
||||||
|
{
|
||||||
|
if (session.LocalAuthentication.Type == AuthenticationType.Client)
|
||||||
|
{
|
||||||
|
// declare (Credentials -> No Auth, No Enctypt)
|
||||||
|
|
||||||
|
var un = DC.ToBytes(session.LocalAuthentication.Username);
|
||||||
|
var dmn = DC.ToBytes(session.LocalAuthentication.Domain);// domain);
|
||||||
|
|
||||||
|
SendParams()
|
||||||
|
.AddUInt8(0x60)
|
||||||
|
.AddUInt8((byte)dmn.Length)
|
||||||
|
.AddUInt8Array(dmn)
|
||||||
|
.AddUInt8Array(localNonce)
|
||||||
|
.AddUInt8((byte)un.Length)
|
||||||
|
.AddUInt8Array(un)
|
||||||
|
.Done();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void Disconencted()
|
||||||
|
{
|
||||||
|
if (ready)
|
||||||
|
{
|
||||||
|
Server?.Membership.Logout(session);
|
||||||
|
Warehouse.Remove(this);
|
||||||
|
ready = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
public AsyncBag<T> Children<T>(IResource resource)
|
public AsyncBag<T> Children<T>(IResource resource)
|
||||||
{
|
{
|
||||||
|
@ -103,44 +103,67 @@ namespace Esyur.Net.IIP
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
protected override void DataReceived(DistributedConnection sender, NetworkBuffer data)
|
//protected override void DataReceived(DistributedConnection sender, NetworkBuffer data)
|
||||||
{
|
//{
|
||||||
//throw new NotImplementedException();
|
// //throw new NotImplementedException();
|
||||||
|
|
||||||
|
//}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//protected override void ClientConnected(DistributedConnection sender)
|
||||||
|
//{
|
||||||
|
// //Console.WriteLine("DistributedConnection Client Connected");
|
||||||
|
//}
|
||||||
|
|
||||||
|
//private void ConnectionReadyEventReceiver(DistributedConnection sender)
|
||||||
|
//{
|
||||||
|
// sender.OnReady -= ConnectionReadyEventReceiver;
|
||||||
|
// Warehouse.Put(sender, sender.LocalUsername, null, this);
|
||||||
|
//}
|
||||||
|
|
||||||
|
|
||||||
|
//public override void RemoveConnection(DistributedConnection connection)
|
||||||
|
//{
|
||||||
|
// connection.OnReady -= Sender_OnReady;
|
||||||
|
// //connection.Server = null;
|
||||||
|
// base.RemoveConnection(connection);
|
||||||
|
//}
|
||||||
|
|
||||||
|
//public override void AddConnection(DistributedConnection connection)
|
||||||
|
//{
|
||||||
|
// connection.OnReady += Sender_OnReady;
|
||||||
|
// connection.Server = this;
|
||||||
|
// base.AddConnection(connection);
|
||||||
|
//}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
protected override void ClientConnected(DistributedConnection connection)
|
||||||
|
{
|
||||||
|
//connection.OnReady += ConnectionReadyEventReceiver;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SessionModified(DistributedConnection session, string key, object newValue)
|
public override void Add(DistributedConnection connection)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override void ClientConnected(DistributedConnection sender)
|
|
||||||
{
|
|
||||||
//Console.WriteLine("DistributedConnection Client Connected");
|
|
||||||
}
|
|
||||||
|
|
||||||
private void Sender_OnReady(DistributedConnection sender)
|
|
||||||
{
|
|
||||||
Warehouse.Put(sender, sender.LocalUsername, null, this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void RemoveConnection(DistributedConnection connection)
|
|
||||||
{
|
|
||||||
connection.OnReady -= Sender_OnReady;
|
|
||||||
//connection.Server = null;
|
|
||||||
base.RemoveConnection(connection);
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void AddConnection(DistributedConnection connection)
|
|
||||||
{
|
|
||||||
connection.OnReady += Sender_OnReady;
|
|
||||||
connection.Server = this;
|
connection.Server = this;
|
||||||
base.AddConnection(connection);
|
base.Add(connection);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void ClientDisconnected(DistributedConnection sender)
|
public override void Remove(DistributedConnection connection)
|
||||||
{
|
{
|
||||||
Warehouse.Remove(sender);
|
connection.Server = null;
|
||||||
|
base.Remove(connection);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected override void ClientDisconnected(DistributedConnection connection)
|
||||||
|
{
|
||||||
|
//connection.OnReady -= ConnectionReadyEventReceiver;
|
||||||
|
//Warehouse.Remove(connection);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
15
Esyur/Net/INetworkReceiver.cs
Normal file
15
Esyur/Net/INetworkReceiver.cs
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace Esyur.Net
|
||||||
|
{
|
||||||
|
public interface INetworkReceiver<T>
|
||||||
|
{
|
||||||
|
void NetworkClose(T sender);
|
||||||
|
void NetworkReceive(T sender, NetworkBuffer buffer);
|
||||||
|
//void NetworkError(T sender);
|
||||||
|
|
||||||
|
void NetworkConnect(T sender);
|
||||||
|
}
|
||||||
|
}
|
@ -38,20 +38,22 @@ using Esyur.Resource;
|
|||||||
|
|
||||||
namespace Esyur.Net
|
namespace Esyur.Net
|
||||||
{
|
{
|
||||||
public class NetworkConnection : IDestructible// <TS>: IResource where TS : NetworkSession
|
public abstract class NetworkConnection: IDestructible, INetworkReceiver<ISocket>// <TS>: IResource where TS : NetworkSession
|
||||||
{
|
{
|
||||||
private ISocket sock;
|
private Sockets.ISocket sock;
|
||||||
// private bool connected;
|
// private bool connected;
|
||||||
|
|
||||||
private DateTime lastAction;
|
private DateTime lastAction;
|
||||||
|
|
||||||
public delegate void DataReceivedEvent(NetworkConnection sender, NetworkBuffer data);
|
//public delegate void DataReceivedEvent(NetworkConnection sender, NetworkBuffer data);
|
||||||
public delegate void ConnectionClosedEvent(NetworkConnection sender);
|
//public delegate void ConnectionClosedEvent(NetworkConnection sender);
|
||||||
public delegate void ConnectionEstablishedEvent(NetworkConnection sender);
|
public delegate void NetworkConnectionEvent(NetworkConnection connection);
|
||||||
|
|
||||||
|
public event NetworkConnectionEvent OnConnect;
|
||||||
|
//public event DataReceivedEvent OnDataReceived;
|
||||||
|
public event NetworkConnectionEvent OnClose;
|
||||||
|
|
||||||
|
|
||||||
public event ConnectionEstablishedEvent OnConnect;
|
|
||||||
public event DataReceivedEvent OnDataReceived;
|
|
||||||
public event ConnectionClosedEvent OnClose;
|
|
||||||
public event DestroyedEvent OnDestroy;
|
public event DestroyedEvent OnDestroy;
|
||||||
//object receivingLock = new object();
|
//object receivingLock = new object();
|
||||||
|
|
||||||
@ -59,22 +61,31 @@ namespace Esyur.Net
|
|||||||
|
|
||||||
bool processing = false;
|
bool processing = false;
|
||||||
|
|
||||||
|
// public INetworkReceiver<NetworkConnection> Receiver { get; set; }
|
||||||
|
|
||||||
public void Destroy()
|
public virtual void Destroy()
|
||||||
{
|
{
|
||||||
// if (connected)
|
// remove references
|
||||||
|
//sock.OnClose -= Socket_OnClose;
|
||||||
|
//sock.OnConnect -= Socket_OnConnect;
|
||||||
|
//sock.OnReceive -= Socket_OnReceive;
|
||||||
|
sock.Destroy();
|
||||||
|
//Receiver = null;
|
||||||
Close();
|
Close();
|
||||||
|
sock = null;
|
||||||
|
|
||||||
|
OnClose = null;
|
||||||
|
OnConnect = null;
|
||||||
|
//OnDataReceived = null;
|
||||||
OnDestroy?.Invoke(this);
|
OnDestroy?.Invoke(this);
|
||||||
}
|
OnDestroy = null;
|
||||||
|
|
||||||
public NetworkConnection()
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public ISocket Socket
|
|
||||||
|
|
||||||
|
public Sockets.ISocket Socket
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
@ -82,81 +93,46 @@ namespace Esyur.Net
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual void Assign(ISocket socket)
|
public virtual void Assign(Sockets.ISocket socket)
|
||||||
{
|
{
|
||||||
lastAction = DateTime.Now;
|
lastAction = DateTime.Now;
|
||||||
sock = socket;
|
sock = socket;
|
||||||
//connected = true;
|
sock.Receiver = this;
|
||||||
socket.OnReceive += Socket_OnReceive;
|
|
||||||
socket.OnClose += Socket_OnClose;
|
//socket.OnReceive += Socket_OnReceive;
|
||||||
socket.OnConnect += Socket_OnConnect;
|
//socket.OnClose += Socket_OnClose;
|
||||||
//if (socket.State == SocketState.Established)
|
//socket.OnConnect += Socket_OnConnect;
|
||||||
// socket.Begin();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Socket_OnConnect()
|
//private void Socket_OnConnect()
|
||||||
{
|
//{
|
||||||
OnConnect?.Invoke(this);
|
// OnConnect?.Invoke(this);
|
||||||
}
|
//}
|
||||||
|
|
||||||
private void Socket_OnClose()
|
//private void Socket_OnClose()
|
||||||
{
|
//{
|
||||||
ConnectionClosed();
|
// ConnectionClosed();
|
||||||
OnClose?.Invoke(this);
|
// OnClose?.Invoke(this);
|
||||||
}
|
//}
|
||||||
|
|
||||||
protected virtual void ConnectionClosed()
|
//protected virtual void ConnectionClosed()
|
||||||
{
|
//{
|
||||||
|
|
||||||
}
|
//}
|
||||||
|
|
||||||
private void Socket_OnReceive(NetworkBuffer buffer)
|
//private void Socket_OnReceive(NetworkBuffer buffer)
|
||||||
{
|
//{
|
||||||
try
|
//}
|
||||||
{
|
|
||||||
// Unassigned ?
|
|
||||||
if (sock == null)
|
|
||||||
return;
|
|
||||||
|
|
||||||
// Closed ?
|
public Sockets.ISocket Unassign()
|
||||||
if (sock.State == SocketState.Closed || sock.State == SocketState.Terminated) // || !connected)
|
|
||||||
return;
|
|
||||||
|
|
||||||
lastAction = DateTime.Now;
|
|
||||||
|
|
||||||
if (!processing)
|
|
||||||
{
|
|
||||||
processing = true;
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
//lock(buffer.SyncLock)
|
|
||||||
while (buffer.Available > 0 && !buffer.Protected)
|
|
||||||
DataReceived(buffer);
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
processing = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
Global.Log("NetworkConnection", LogType.Warning, ex.ToString());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public ISocket Unassign()
|
|
||||||
{
|
{
|
||||||
if (sock != null)
|
if (sock != null)
|
||||||
{
|
{
|
||||||
// connected = false;
|
// connected = false;
|
||||||
sock.OnClose -= Socket_OnClose;
|
//sock.OnClose -= Socket_OnClose;
|
||||||
sock.OnConnect -= Socket_OnConnect;
|
//sock.OnConnect -= Socket_OnConnect;
|
||||||
sock.OnReceive -= Socket_OnReceive;
|
//sock.OnReceive -= Socket_OnReceive;
|
||||||
|
sock.Receiver = null;
|
||||||
|
|
||||||
var rt = sock;
|
var rt = sock;
|
||||||
sock = null;
|
sock = null;
|
||||||
@ -167,20 +143,20 @@ namespace Esyur.Net
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual void DataReceived(NetworkBuffer data)
|
//protected virtual void DataReceived(NetworkBuffer data)
|
||||||
{
|
//{
|
||||||
if (OnDataReceived != null)
|
// if (OnDataReceived != null)
|
||||||
{
|
// {
|
||||||
try
|
// try
|
||||||
{
|
// {
|
||||||
OnDataReceived?.Invoke(this, data);
|
// OnDataReceived?.Invoke(this, data);
|
||||||
}
|
// }
|
||||||
catch (Exception ex)
|
// catch (Exception ex)
|
||||||
{
|
// {
|
||||||
Global.Log("NetworkConenction:DataReceived", LogType.Error, ex.ToString());
|
// Global.Log("NetworkConenction:DataReceived", LogType.Error, ex.ToString());
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
//}
|
||||||
|
|
||||||
public void Close()
|
public void Close()
|
||||||
{
|
{
|
||||||
@ -234,11 +210,11 @@ namespace Esyur.Net
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public bool Connected
|
public bool IsConnected
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
return sock.State == SocketState.Established;// connected;
|
return sock.State == SocketState.Established;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -283,7 +259,7 @@ namespace Esyur.Net
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
sock.Send(msg);
|
sock?.Send(msg);
|
||||||
lastAction = DateTime.Now;
|
lastAction = DateTime.Now;
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
@ -309,5 +285,87 @@ namespace Esyur.Net
|
|||||||
{
|
{
|
||||||
Send(Encoding.UTF8.GetBytes(data));
|
Send(Encoding.UTF8.GetBytes(data));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void NetworkClose(ISocket socket)
|
||||||
|
{
|
||||||
|
Disconencted();
|
||||||
|
OnClose?.Invoke(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void NetworkConnect(ISocket socket)
|
||||||
|
{
|
||||||
|
Connected();
|
||||||
|
OnConnect?.Invoke(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
//{
|
||||||
|
//ConnectionClosed();
|
||||||
|
//OnClose?.Invoke(this);
|
||||||
|
|
||||||
|
//Receiver?.NetworkClose(this);
|
||||||
|
//}
|
||||||
|
|
||||||
|
//public void NetworkConenct(ISocket sender)
|
||||||
|
//{
|
||||||
|
// OnConnect?.Invoke(this);
|
||||||
|
//}
|
||||||
|
|
||||||
|
protected abstract void DataReceived(NetworkBuffer buffer);
|
||||||
|
protected abstract void Connected();
|
||||||
|
protected abstract void Disconencted();
|
||||||
|
|
||||||
|
public void NetworkReceive(ISocket sender, NetworkBuffer buffer)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Unassigned ?
|
||||||
|
if (sock == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
// Closed ?
|
||||||
|
if (sock.State == SocketState.Closed || sock.State == SocketState.Terminated) // || !connected)
|
||||||
|
return;
|
||||||
|
|
||||||
|
lastAction = DateTime.Now;
|
||||||
|
|
||||||
|
if (!processing)
|
||||||
|
{
|
||||||
|
processing = true;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
//lock(buffer.SyncLock)
|
||||||
|
while (buffer.Available > 0 && !buffer.Protected)
|
||||||
|
{
|
||||||
|
//Receiver?.NetworkReceive(this, buffer);
|
||||||
|
DataReceived(buffer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
processing = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Global.Log("NetworkConnection", LogType.Warning, ex.ToString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//{
|
||||||
|
// Receiver?.NetworkError(this);
|
||||||
|
//throw new NotImplementedException();
|
||||||
|
//}
|
||||||
|
|
||||||
|
//public void NetworkConnect(ISocket sender)
|
||||||
|
//{
|
||||||
|
// Receiver?.NetworkConnect(this);
|
||||||
|
//throw new NotImplementedException();
|
||||||
|
//}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -39,14 +39,14 @@ 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;
|
||||||
private ISocket listener;
|
private Sockets.ISocket listener;
|
||||||
private AutoList<TConnection, NetworkServer<TConnection>> connections;
|
public AutoList<TConnection, NetworkServer<TConnection>> Connections { get; private set; }
|
||||||
|
|
||||||
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);
|
||||||
protected abstract void ClientDisconnected(TConnection sender);
|
//protected abstract void ClientDisconnected(TConnection sender);
|
||||||
|
|
||||||
|
|
||||||
private Timer timer;
|
private Timer timer;
|
||||||
@ -54,74 +54,16 @@ namespace Esyur.Net
|
|||||||
|
|
||||||
public event DestroyedEvent OnDestroy;
|
public event DestroyedEvent OnDestroy;
|
||||||
|
|
||||||
public AutoList<TConnection, NetworkServer<TConnection>> Connections
|
//public AutoList<TConnection, NetworkServer<TConnection>> Connections => connections;
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
return connections;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
public void RemoveSession(string ID)
|
|
||||||
{
|
|
||||||
Sessions.Remove(ID);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void RemoveSession(TSession Session)
|
|
||||||
{
|
|
||||||
if (Session != null)
|
|
||||||
Sessions.Remove(Session.Id);
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
public TSession CreateSession(string ID, int Timeout)
|
|
||||||
{
|
|
||||||
TSession s = new TSession();
|
|
||||||
|
|
||||||
s.SetSession(ID, Timeout, new NetworkSession.SessionModifiedEvent(SessionModified)
|
|
||||||
, new NetworkSession.SessionEndedEvent(SessionEnded));
|
|
||||||
|
|
||||||
|
|
||||||
Sessions.Add(ID, s);
|
|
||||||
return s;
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
private void pSessionModified(TSession session, string key, object oldValue, object newValue)
|
|
||||||
{
|
|
||||||
SessionModified((TSession)session, key, oldValue, newValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void pSessionEnded(NetworkSession session)
|
|
||||||
{
|
|
||||||
SessionEnded((TSession)session);
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
protected virtual void SessionModified(NetworkSession session, string key, object oldValue, object newValue)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
protected virtual void SessionEnded(NetworkSession session)
|
|
||||||
{
|
|
||||||
Sessions.Remove(session.Id);
|
|
||||||
session.Destroy();
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
private void MinuteThread(object state)
|
private void MinuteThread(object state)
|
||||||
{
|
{
|
||||||
List<TConnection> ToBeClosed = null;
|
List<TConnection> ToBeClosed = null;
|
||||||
|
|
||||||
|
|
||||||
lock (connections.SyncRoot)
|
lock (Connections.SyncRoot)
|
||||||
{
|
{
|
||||||
foreach (TConnection c in connections)
|
foreach (TConnection c in Connections)
|
||||||
{
|
{
|
||||||
if (DateTime.Now.Subtract(c.LastAction).TotalSeconds >= Timeout)
|
if (DateTime.Now.Subtract(c.LastAction).TotalSeconds >= Timeout)
|
||||||
{
|
{
|
||||||
@ -132,8 +74,6 @@ namespace Esyur.Net
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Console.WriteLine("UnLock MinuteThread");
|
|
||||||
|
|
||||||
if (ToBeClosed != null)
|
if (ToBeClosed != null)
|
||||||
{
|
{
|
||||||
//Console.WriteLine("Term: " + ToBeClosed.Count + " " + this.listener.LocalEndPoint.ToString());
|
//Console.WriteLine("Term: " + ToBeClosed.Count + " " + this.listener.LocalEndPoint.ToString());
|
||||||
@ -145,18 +85,13 @@ namespace Esyur.Net
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Start(ISocket socket)//, uint timeout, uint clock)
|
public void Start(Sockets.ISocket socket)//, uint timeout, uint clock)
|
||||||
{
|
{
|
||||||
if (listener != null)
|
if (listener != null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
//if (socket.State == SocketState.Listening)
|
|
||||||
// return;
|
|
||||||
|
|
||||||
//if (isRunning)
|
Connections = new AutoList<TConnection, NetworkServer<TConnection>>(this);
|
||||||
// return;
|
|
||||||
|
|
||||||
connections = new AutoList<TConnection, NetworkServer<TConnection>>(this);
|
|
||||||
|
|
||||||
|
|
||||||
if (Timeout > 0 & Clock > 0)
|
if (Timeout > 0 & Clock > 0)
|
||||||
@ -165,11 +100,6 @@ namespace Esyur.Net
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// start a new thread for the server to live on
|
|
||||||
//isRunning = true;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
listener = socket;
|
listener = socket;
|
||||||
|
|
||||||
// Start accepting
|
// Start accepting
|
||||||
@ -182,8 +112,43 @@ namespace Esyur.Net
|
|||||||
{
|
{
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
var s = listener.Accept();
|
try
|
||||||
NewConnection(s);
|
{
|
||||||
|
var s = listener.Accept();
|
||||||
|
|
||||||
|
if (s == null)
|
||||||
|
{
|
||||||
|
Console.Write("sock == null");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var c = new TConnection();
|
||||||
|
//c.OnClose += ClientDisconnectedEventReceiver;
|
||||||
|
c.Assign(s);
|
||||||
|
Add(c);
|
||||||
|
//Connections.Add(c);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
//ClientConnected(c);
|
||||||
|
ClientConnected(c);
|
||||||
|
//NetworkConnect(c);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
// something wrong with the child.
|
||||||
|
}
|
||||||
|
|
||||||
|
s.Begin();
|
||||||
|
|
||||||
|
// Accept more
|
||||||
|
//listener.Accept().Then(NewConnection);
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@ -191,15 +156,6 @@ namespace Esyur.Net
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
public int LocalPort
|
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
return port;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
[Attribute]
|
[Attribute]
|
||||||
public uint Timeout
|
public uint Timeout
|
||||||
@ -237,7 +193,7 @@ namespace Esyur.Net
|
|||||||
|
|
||||||
//Console.WriteLine("Listener stopped");
|
//Console.WriteLine("Listener stopped");
|
||||||
|
|
||||||
var cons = connections.ToArray();
|
var cons = Connections.ToArray();
|
||||||
|
|
||||||
//lock (connections.SyncRoot)
|
//lock (connections.SyncRoot)
|
||||||
//{
|
//{
|
||||||
@ -261,67 +217,22 @@ namespace Esyur.Net
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public virtual void RemoveConnection(TConnection connection)
|
public virtual void Remove(TConnection connection)
|
||||||
{
|
{
|
||||||
connection.OnDataReceived -= OnDataReceived;
|
//connection.OnDataReceived -= OnDataReceived;
|
||||||
connection.OnConnect -= OnClientConnect;
|
//connection.OnConnect -= OnClientConnect;
|
||||||
connection.OnClose -= OnClientClose;
|
connection.OnClose -= ClientDisconnectedEventReceiver;
|
||||||
connections.Remove(connection);
|
Connections.Remove(connection);
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual void AddConnection(TConnection connection)
|
public virtual void Add(TConnection connection)
|
||||||
{
|
{
|
||||||
connection.OnDataReceived += OnDataReceived;
|
//connection.OnDataReceived += OnDataReceived;
|
||||||
connection.OnConnect += OnClientConnect;
|
//connection.OnConnect += OnClientConnect;
|
||||||
connection.OnClose += OnClientClose;
|
connection.OnClose += ClientDisconnectedEventReceiver;// OnClientClose;
|
||||||
connections.Add(connection);
|
Connections.Add(connection);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void NewConnection(ISocket sock)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
|
|
||||||
|
|
||||||
if (sock == null)
|
|
||||||
{
|
|
||||||
Console.Write("sock == null");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
TConnection c = new TConnection();
|
|
||||||
AddConnection(c);
|
|
||||||
|
|
||||||
c.Assign(sock);
|
|
||||||
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
ClientConnected(c);
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
// something wrong with the child.
|
|
||||||
}
|
|
||||||
|
|
||||||
sock.Begin();
|
|
||||||
|
|
||||||
// Accept more
|
|
||||||
//listener.Accept().Then(NewConnection);
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
//Console.WriteLine("TSERVER " + ex.ToString());
|
|
||||||
Global.Log("NetworkServer", LogType.Error, ex.ToString());
|
|
||||||
}
|
|
||||||
|
|
||||||
//isRunning = false;
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool IsRunning
|
public bool IsRunning
|
||||||
{
|
{
|
||||||
@ -332,44 +243,31 @@ namespace Esyur.Net
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnDataReceived(NetworkConnection sender, NetworkBuffer data)
|
//public void OnDataReceived(ISocket sender, NetworkBuffer data)
|
||||||
{
|
//{
|
||||||
DataReceived((TConnection)sender, data);
|
// DataReceived((TConnection)sender, data);
|
||||||
}
|
//}
|
||||||
|
|
||||||
public void OnClientConnect(NetworkConnection sender)
|
//public void OnClientConnect(ISocket sender)
|
||||||
{
|
//{
|
||||||
if (sender == null)
|
// if (sender == null)
|
||||||
return;
|
// return;
|
||||||
|
|
||||||
if (sender.RemoteEndPoint == null || sender.LocalEndPoint == null)
|
// if (sender.RemoteEndPoint == null || sender.LocalEndPoint == null)
|
||||||
{ }
|
// { }
|
||||||
//Console.WriteLine("NULL");
|
// //Console.WriteLine("NULL");
|
||||||
else
|
// else
|
||||||
Global.Log("Connections", LogType.Debug, sender.RemoteEndPoint.Address.ToString()
|
// Global.Log("Connections", LogType.Debug, sender.RemoteEndPoint.Address.ToString()
|
||||||
+ "->" + sender.LocalEndPoint.Port + " at " + DateTime.UtcNow.ToString("d")
|
// + "->" + sender.LocalEndPoint.Port + " at " + DateTime.UtcNow.ToString("d")
|
||||||
+ " " + DateTime.UtcNow.ToString("d"), false);
|
// + " " + DateTime.UtcNow.ToString("d"), false);
|
||||||
|
|
||||||
// Console.WriteLine("Connected " + sender.RemoteEndPoint.ToString());
|
// // Console.WriteLine("Connected " + sender.RemoteEndPoint.ToString());
|
||||||
ClientConnected((TConnection)sender);
|
// ClientConnected((TConnection)sender);
|
||||||
}
|
//}
|
||||||
|
|
||||||
public void OnClientClose(NetworkConnection sender)
|
//public void OnClientClose(ISocket sender)
|
||||||
{
|
//{
|
||||||
try
|
//}
|
||||||
{
|
|
||||||
sender.Destroy();
|
|
||||||
RemoveConnection((TConnection)sender);
|
|
||||||
ClientDisconnected((TConnection)sender);
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
Global.Log("NetworkServer:OnClientDisconnect", LogType.Error, ex.ToString());
|
|
||||||
}
|
|
||||||
|
|
||||||
sender = null;
|
|
||||||
GC.Collect();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public void Destroy()
|
public void Destroy()
|
||||||
@ -378,10 +276,34 @@ namespace Esyur.Net
|
|||||||
OnDestroy?.Invoke(this);
|
OnDestroy?.Invoke(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void ClientDisconnectedEventReceiver(NetworkConnection connection)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var con = connection as TConnection;
|
||||||
|
con.Destroy();
|
||||||
|
// con.OnClose -= ClientDisconnectedEventReceiver;
|
||||||
|
|
||||||
|
Remove(con);
|
||||||
|
|
||||||
|
//Connections.Remove(con);
|
||||||
|
ClientDisconnected(con);
|
||||||
|
//RemoveConnection((TConnection)sender);
|
||||||
|
//connections.Remove(sender)
|
||||||
|
//ClientDisconnected((TConnection)sender);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Global.Log("NetworkServer:OnClientDisconnect", LogType.Error, ex.ToString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract void ClientDisconnected(TConnection connection);
|
||||||
|
protected abstract void ClientConnected(TConnection connection);
|
||||||
|
|
||||||
~NetworkServer()
|
~NetworkServer()
|
||||||
{
|
{
|
||||||
Stop();
|
Stop();
|
||||||
//Connections = null;
|
|
||||||
listener = null;
|
listener = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -240,7 +240,7 @@ namespace Esyur.Net.Packets
|
|||||||
|
|
||||||
// check limit
|
// check limit
|
||||||
if (postSize > data.Length - headerSize)
|
if (postSize > data.Length - headerSize)
|
||||||
return postSize - (data.Length - headerSize);
|
return -(postSize - (data.Length - headerSize));
|
||||||
|
|
||||||
|
|
||||||
if (Headers["content-type"].StartsWith("application/x-www-form-urlencoded")
|
if (Headers["content-type"].StartsWith("application/x-www-form-urlencoded")
|
||||||
|
@ -38,17 +38,19 @@ using Esyur.Core;
|
|||||||
|
|
||||||
namespace Esyur.Net.Sockets
|
namespace Esyur.Net.Sockets
|
||||||
{
|
{
|
||||||
public delegate void ISocketReceiveEvent(NetworkBuffer buffer);
|
//public delegate void ISocketReceiveEvent(NetworkBuffer buffer);
|
||||||
public delegate void ISocketConnectEvent();
|
//public delegate void ISocketConnectEvent();
|
||||||
public delegate void ISocketCloseEvent();
|
//public delegate void ISocketCloseEvent();
|
||||||
|
|
||||||
public interface ISocket : IDestructible
|
public interface ISocket : IDestructible
|
||||||
{
|
{
|
||||||
SocketState State { get; }
|
SocketState State { get; }
|
||||||
|
|
||||||
event ISocketReceiveEvent OnReceive;
|
//event ISocketReceiveEvent OnReceive;
|
||||||
event ISocketConnectEvent OnConnect;
|
//event ISocketConnectEvent OnConnect;
|
||||||
event ISocketCloseEvent OnClose;
|
//event ISocketCloseEvent OnClose;
|
||||||
|
|
||||||
|
INetworkReceiver<ISocket> Receiver { get; set; }
|
||||||
|
|
||||||
AsyncReply<bool> SendAsync(byte[] message, int offset, int length);
|
AsyncReply<bool> SendAsync(byte[] message, int offset, int length);
|
||||||
|
|
||||||
|
@ -41,6 +41,9 @@ namespace Esyur.Net.Sockets
|
|||||||
{
|
{
|
||||||
public class SSLSocket : ISocket
|
public class SSLSocket : ISocket
|
||||||
{
|
{
|
||||||
|
|
||||||
|
public INetworkReceiver<ISocket> Receiver { get; set; }
|
||||||
|
|
||||||
Socket sock;
|
Socket sock;
|
||||||
byte[] receiveBuffer;
|
byte[] receiveBuffer;
|
||||||
|
|
||||||
@ -59,12 +62,11 @@ namespace Esyur.Net.Sockets
|
|||||||
|
|
||||||
SocketState state = SocketState.Initial;
|
SocketState state = SocketState.Initial;
|
||||||
|
|
||||||
public event ISocketReceiveEvent OnReceive;
|
//public event ISocketReceiveEvent OnReceive;
|
||||||
public event ISocketConnectEvent OnConnect;
|
//public event ISocketConnectEvent OnConnect;
|
||||||
public event ISocketCloseEvent OnClose;
|
//public event ISocketCloseEvent OnClose;
|
||||||
public event DestroyedEvent OnDestroy;
|
public event DestroyedEvent OnDestroy;
|
||||||
|
|
||||||
SocketAsyncEventArgs socketArgs = new SocketAsyncEventArgs();
|
|
||||||
|
|
||||||
SslStream ssl;
|
SslStream ssl;
|
||||||
X509Certificate2 cert;
|
X509Certificate2 cert;
|
||||||
@ -87,8 +89,8 @@ namespace Esyur.Net.Sockets
|
|||||||
{
|
{
|
||||||
await BeginAsync();
|
await BeginAsync();
|
||||||
state = SocketState.Established;
|
state = SocketState.Established;
|
||||||
OnConnect?.Invoke();
|
//OnConnect?.Invoke();
|
||||||
|
Receiver?.NetworkConnect(this);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@ -271,7 +273,8 @@ namespace Esyur.Net.Sockets
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
OnClose?.Invoke();
|
Receiver?.NetworkClose(this);
|
||||||
|
//OnClose?.Invoke();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -428,7 +431,9 @@ namespace Esyur.Net.Sockets
|
|||||||
|
|
||||||
receiveNetworkBuffer.Write(receiveBuffer, 0, (uint)bytesReceived);
|
receiveNetworkBuffer.Write(receiveBuffer, 0, (uint)bytesReceived);
|
||||||
|
|
||||||
OnReceive?.Invoke(receiveNetworkBuffer);
|
//OnReceive?.Invoke(receiveNetworkBuffer);
|
||||||
|
|
||||||
|
Receiver?.NetworkReceive(this, receiveNetworkBuffer);
|
||||||
|
|
||||||
ssl.BeginRead(receiveBuffer, 0, receiveBuffer.Length, ReceiveCallback, this);
|
ssl.BeginRead(receiveBuffer, 0, receiveBuffer.Length, ReceiveCallback, this);
|
||||||
|
|
||||||
@ -453,7 +458,10 @@ namespace Esyur.Net.Sockets
|
|||||||
public void Destroy()
|
public void Destroy()
|
||||||
{
|
{
|
||||||
Close();
|
Close();
|
||||||
|
Receiver = null;
|
||||||
|
receiveNetworkBuffer = null;
|
||||||
OnDestroy?.Invoke(this);
|
OnDestroy?.Invoke(this);
|
||||||
|
OnDestroy = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async AsyncReply<ISocket> AcceptAsync()
|
public async AsyncReply<ISocket> AcceptAsync()
|
||||||
|
@ -39,6 +39,8 @@ namespace Esyur.Net.Sockets
|
|||||||
{
|
{
|
||||||
public class TCPSocket : ISocket
|
public class TCPSocket : ISocket
|
||||||
{
|
{
|
||||||
|
public INetworkReceiver<ISocket> Receiver { get; set; }
|
||||||
|
|
||||||
Socket sock;
|
Socket sock;
|
||||||
byte[] receiveBuffer;
|
byte[] receiveBuffer;
|
||||||
|
|
||||||
@ -58,9 +60,9 @@ namespace Esyur.Net.Sockets
|
|||||||
|
|
||||||
SocketState state = SocketState.Initial;
|
SocketState state = SocketState.Initial;
|
||||||
|
|
||||||
public event ISocketReceiveEvent OnReceive;
|
//public event ISocketReceiveEvent OnReceive;
|
||||||
public event ISocketConnectEvent OnConnect;
|
//public event ISocketConnectEvent OnConnect;
|
||||||
public event ISocketCloseEvent OnClose;
|
//public event ISocketCloseEvent OnClose;
|
||||||
public event DestroyedEvent OnDestroy;
|
public event DestroyedEvent OnDestroy;
|
||||||
|
|
||||||
SocketAsyncEventArgs socketArgs = new SocketAsyncEventArgs();
|
SocketAsyncEventArgs socketArgs = new SocketAsyncEventArgs();
|
||||||
@ -103,7 +105,8 @@ namespace Esyur.Net.Sockets
|
|||||||
{
|
{
|
||||||
|
|
||||||
state = SocketState.Established;
|
state = SocketState.Established;
|
||||||
OnConnect?.Invoke();
|
//OnConnect?.Invoke();
|
||||||
|
Receiver?.NetworkConnect(this);
|
||||||
Begin();
|
Begin();
|
||||||
rt.Trigger(true);
|
rt.Trigger(true);
|
||||||
}
|
}
|
||||||
@ -134,7 +137,8 @@ namespace Esyur.Net.Sockets
|
|||||||
}
|
}
|
||||||
|
|
||||||
receiveNetworkBuffer.Write(receiveBuffer, 0, (uint)task.Result);
|
receiveNetworkBuffer.Write(receiveBuffer, 0, (uint)task.Result);
|
||||||
OnReceive?.Invoke(receiveNetworkBuffer);
|
//OnReceive?.Invoke(receiveNetworkBuffer);
|
||||||
|
Receiver?.NetworkReceive(this, receiveNetworkBuffer);
|
||||||
if (state == SocketState.Established)
|
if (state == SocketState.Established)
|
||||||
sock.ReceiveAsync(receiveBufferSegment, SocketFlags.None).ContinueWith(DataReceived);
|
sock.ReceiveAsync(receiveBufferSegment, SocketFlags.None).ContinueWith(DataReceived);
|
||||||
|
|
||||||
@ -163,11 +167,18 @@ namespace Esyur.Net.Sockets
|
|||||||
Close();
|
Close();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
else if (e.SocketError != SocketError.Success)
|
||||||
|
{
|
||||||
|
Close();
|
||||||
|
return;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
var recCount = e.BytesTransferred > e.Count ? e.Count : e.BytesTransferred;
|
var recCount = e.BytesTransferred > e.Count ? e.Count : e.BytesTransferred;
|
||||||
receiveNetworkBuffer.Write(receiveBuffer, 0, (uint)recCount);
|
receiveNetworkBuffer.Write(receiveBuffer, 0, (uint)recCount);
|
||||||
|
|
||||||
OnReceive?.Invoke(receiveNetworkBuffer);
|
//OnReceive?.Invoke(receiveNetworkBuffer);
|
||||||
|
Receiver?.NetworkReceive(this, receiveNetworkBuffer);
|
||||||
|
|
||||||
if (state == SocketState.Established)
|
if (state == SocketState.Established)
|
||||||
while (!sock.ReceiveAsync(e))
|
while (!sock.ReceiveAsync(e))
|
||||||
@ -189,6 +200,12 @@ namespace Esyur.Net.Sockets
|
|||||||
Close();
|
Close();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
else if (e.SocketError != SocketError.Success)
|
||||||
|
{
|
||||||
|
Close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
//if (e.BytesTransferred > 100000)
|
//if (e.BytesTransferred > 100000)
|
||||||
// Console.WriteLine("BytesTransferred is large " + e.BytesTransferred);
|
// Console.WriteLine("BytesTransferred is large " + e.BytesTransferred);
|
||||||
@ -197,7 +214,8 @@ namespace Esyur.Net.Sockets
|
|||||||
|
|
||||||
receiveNetworkBuffer.Write(receiveBuffer, 0, (uint)recCount);
|
receiveNetworkBuffer.Write(receiveBuffer, 0, (uint)recCount);
|
||||||
|
|
||||||
OnReceive?.Invoke(receiveNetworkBuffer);
|
//OnReceive?.Invoke(receiveNetworkBuffer);
|
||||||
|
Receiver?.NetworkReceive(this, receiveNetworkBuffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -341,7 +359,8 @@ namespace Esyur.Net.Sockets
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
OnClose?.Invoke();
|
//OnClose?.Invoke();
|
||||||
|
Receiver?.NetworkClose(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -454,7 +473,15 @@ namespace Esyur.Net.Sockets
|
|||||||
public void Destroy()
|
public void Destroy()
|
||||||
{
|
{
|
||||||
Close();
|
Close();
|
||||||
|
//OnClose = null;
|
||||||
|
//OnConnect = null;
|
||||||
|
//OnReceive = null;
|
||||||
|
Receiver = null;
|
||||||
|
receiveNetworkBuffer = null;
|
||||||
|
socketArgs.Completed -= SocketArgs_Completed;
|
||||||
|
socketArgs = null;
|
||||||
OnDestroy?.Invoke(this);
|
OnDestroy?.Invoke(this);
|
||||||
|
OnDestroy = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ISocket Accept()
|
public ISocket Accept()
|
||||||
|
@ -34,10 +34,11 @@ using System.IO;
|
|||||||
using Esyur.Core;
|
using Esyur.Core;
|
||||||
using Esyur.Resource;
|
using Esyur.Resource;
|
||||||
using Esyur.Data;
|
using Esyur.Data;
|
||||||
|
using System.Globalization;
|
||||||
|
|
||||||
namespace Esyur.Net.Sockets
|
namespace Esyur.Net.Sockets
|
||||||
{
|
{
|
||||||
public class WSSocket : ISocket
|
public class WSSocket : ISocket, INetworkReceiver<ISocket>
|
||||||
{
|
{
|
||||||
WebsocketPacket pkt_receive = new WebsocketPacket();
|
WebsocketPacket pkt_receive = new WebsocketPacket();
|
||||||
WebsocketPacket pkt_send = new WebsocketPacket();
|
WebsocketPacket pkt_send = new WebsocketPacket();
|
||||||
@ -49,9 +50,9 @@ namespace Esyur.Net.Sockets
|
|||||||
object sendLock = new object();
|
object sendLock = new object();
|
||||||
bool held;
|
bool held;
|
||||||
|
|
||||||
public event ISocketReceiveEvent OnReceive;
|
//public event ISocketReceiveEvent OnReceive;
|
||||||
public event ISocketConnectEvent OnConnect;
|
//public event ISocketConnectEvent OnConnect;
|
||||||
public event ISocketCloseEvent OnClose;
|
//public event ISocketCloseEvent OnClose;
|
||||||
public event DestroyedEvent OnDestroy;
|
public event DestroyedEvent OnDestroy;
|
||||||
|
|
||||||
long totalSent, totalReceived;
|
long totalSent, totalReceived;
|
||||||
@ -63,8 +64,6 @@ namespace Esyur.Net.Sockets
|
|||||||
get { return (IPEndPoint)sock.LocalEndPoint; }
|
get { return (IPEndPoint)sock.LocalEndPoint; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public IPEndPoint RemoteEndPoint
|
public IPEndPoint RemoteEndPoint
|
||||||
{
|
{
|
||||||
get { return sock.RemoteEndPoint; }
|
get { return sock.RemoteEndPoint; }
|
||||||
@ -79,6 +78,7 @@ namespace Esyur.Net.Sockets
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public INetworkReceiver<ISocket> Receiver { get; set; }
|
||||||
|
|
||||||
public WSSocket(ISocket socket)
|
public WSSocket(ISocket socket)
|
||||||
{
|
{
|
||||||
@ -86,12 +86,175 @@ namespace Esyur.Net.Sockets
|
|||||||
pkt_send.Mask = false;
|
pkt_send.Mask = false;
|
||||||
pkt_send.Opcode = WebsocketPacket.WSOpcode.BinaryFrame;
|
pkt_send.Opcode = WebsocketPacket.WSOpcode.BinaryFrame;
|
||||||
sock = socket;
|
sock = socket;
|
||||||
sock.OnClose += Sock_OnClose;
|
|
||||||
sock.OnConnect += Sock_OnConnect;
|
sock.Receiver = this;
|
||||||
sock.OnReceive += Sock_OnReceive;
|
|
||||||
|
//sock.OnClose += Sock_OnClose;
|
||||||
|
//sock.OnConnect += Sock_OnConnect;
|
||||||
|
//sock.OnReceive += Sock_OnReceive;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Sock_OnReceive(NetworkBuffer buffer)
|
//private void Sock_OnReceive(NetworkBuffer buffer)
|
||||||
|
//{
|
||||||
|
|
||||||
|
//}
|
||||||
|
|
||||||
|
//private void Sock_OnConnect()
|
||||||
|
//{
|
||||||
|
// OnConnect?.Invoke();
|
||||||
|
//}
|
||||||
|
|
||||||
|
//private void Sock_OnClose()
|
||||||
|
//{
|
||||||
|
// OnClose?.Invoke();
|
||||||
|
//}
|
||||||
|
|
||||||
|
public void Send(WebsocketPacket packet)
|
||||||
|
{
|
||||||
|
lock (sendLock)
|
||||||
|
if (packet.Compose())
|
||||||
|
sock.Send(packet.Data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Send(byte[] message)
|
||||||
|
{
|
||||||
|
lock (sendLock)
|
||||||
|
{
|
||||||
|
if (held)
|
||||||
|
{
|
||||||
|
sendNetworkBuffer.Write(message);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
totalSent += message.Length;
|
||||||
|
//Console.WriteLine("TX " + message.Length +"/"+totalSent);// + " " + DC.ToHex(message, 0, (uint)size));
|
||||||
|
|
||||||
|
pkt_send.Message = message;
|
||||||
|
|
||||||
|
|
||||||
|
if (pkt_send.Compose())
|
||||||
|
sock.Send(pkt_send.Data);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void Send(byte[] message, int offset, int size)
|
||||||
|
{
|
||||||
|
lock (sendLock)
|
||||||
|
{
|
||||||
|
if (held)
|
||||||
|
{
|
||||||
|
sendNetworkBuffer.Write(message, (uint)offset, (uint)size);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
totalSent += size;
|
||||||
|
//Console.WriteLine("TX " + size + "/"+totalSent);// + " " + DC.ToHex(message, 0, (uint)size));
|
||||||
|
|
||||||
|
pkt_send.Message = new byte[size];
|
||||||
|
Buffer.BlockCopy(message, offset, pkt_send.Message, 0, size);
|
||||||
|
if (pkt_send.Compose())
|
||||||
|
sock.Send(pkt_send.Data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void Close()
|
||||||
|
{
|
||||||
|
sock?.Close();
|
||||||
|
}
|
||||||
|
|
||||||
|
public AsyncReply<bool> Connect(string hostname, ushort port)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public bool Begin()
|
||||||
|
{
|
||||||
|
return sock.Begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Trigger(ResourceTrigger trigger)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Destroy()
|
||||||
|
{
|
||||||
|
Close();
|
||||||
|
//OnClose = null;
|
||||||
|
//OnConnect = null;
|
||||||
|
//OnReceive = null;
|
||||||
|
receiveNetworkBuffer = null;
|
||||||
|
//sock.OnReceive -= Sock_OnReceive;
|
||||||
|
//sock.OnClose -= Sock_OnClose;
|
||||||
|
//sock.OnConnect -= Sock_OnConnect;
|
||||||
|
sock.Receiver = null;
|
||||||
|
sock = null;
|
||||||
|
OnDestroy?.Invoke(this);
|
||||||
|
OnDestroy = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public AsyncReply<ISocket> AcceptAsync()
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Hold()
|
||||||
|
{
|
||||||
|
//Console.WriteLine("WS Hold ");
|
||||||
|
held = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Unhold()
|
||||||
|
{
|
||||||
|
lock (sendLock)
|
||||||
|
{
|
||||||
|
held = false;
|
||||||
|
|
||||||
|
var message = sendNetworkBuffer.Read();
|
||||||
|
|
||||||
|
//Console.WriteLine("WS Unhold {0}", message == null ? 0 : message.Length);
|
||||||
|
|
||||||
|
if (message == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
totalSent += message.Length;
|
||||||
|
|
||||||
|
pkt_send.Message = message;
|
||||||
|
if (pkt_send.Compose())
|
||||||
|
sock.Send(pkt_send.Data);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public AsyncReply<bool> SendAsync(byte[] message, int offset, int length)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ISocket Accept()
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public AsyncReply<bool> BeginAsync()
|
||||||
|
{
|
||||||
|
return sock.BeginAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void NetworkClose(ISocket sender)
|
||||||
|
{
|
||||||
|
Receiver?.NetworkClose(sender);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void NetworkReceive(ISocket sender, NetworkBuffer buffer)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (sock.State == SocketState.Closed || sock.State == SocketState.Terminated)
|
if (sock.State == SocketState.Closed || sock.State == SocketState.Terminated)
|
||||||
@ -161,8 +324,9 @@ namespace Esyur.Net.Sockets
|
|||||||
|
|
||||||
if (offset == msg.Length)
|
if (offset == msg.Length)
|
||||||
{
|
{
|
||||||
// Console.WriteLine("WS IN: " + receiveNetworkBuffer.Available);
|
|
||||||
OnReceive?.Invoke(receiveNetworkBuffer);
|
//OnReceive?.Invoke(receiveNetworkBuffer);
|
||||||
|
Receiver?.NetworkReceive(this, receiveNetworkBuffer);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -180,152 +344,20 @@ namespace Esyur.Net.Sockets
|
|||||||
|
|
||||||
//Console.WriteLine("WS IN: " + receiveNetworkBuffer.Available);
|
//Console.WriteLine("WS IN: " + receiveNetworkBuffer.Available);
|
||||||
|
|
||||||
OnReceive?.Invoke(receiveNetworkBuffer);
|
//OnReceive?.Invoke(receiveNetworkBuffer);
|
||||||
|
Receiver?.NetworkReceive(this, receiveNetworkBuffer);
|
||||||
|
|
||||||
processing = false;
|
processing = false;
|
||||||
|
|
||||||
if (buffer.Available > 0 && !buffer.Protected)
|
if (buffer.Available > 0 && !buffer.Protected)
|
||||||
Sock_OnReceive(buffer);
|
Receiver?.NetworkReceive(this, buffer);
|
||||||
}
|
//Sock_OnReceive(buffer);
|
||||||
|
|
||||||
private void Sock_OnConnect()
|
|
||||||
{
|
|
||||||
OnConnect?.Invoke();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void Sock_OnClose()
|
|
||||||
{
|
|
||||||
OnClose?.Invoke();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Send(WebsocketPacket packet)
|
|
||||||
{
|
|
||||||
lock (sendLock)
|
|
||||||
if (packet.Compose())
|
|
||||||
sock.Send(packet.Data);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Send(byte[] message)
|
|
||||||
{
|
|
||||||
lock (sendLock)
|
|
||||||
{
|
|
||||||
if (held)
|
|
||||||
{
|
|
||||||
sendNetworkBuffer.Write(message);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
totalSent += message.Length;
|
|
||||||
//Console.WriteLine("TX " + message.Length +"/"+totalSent);// + " " + DC.ToHex(message, 0, (uint)size));
|
|
||||||
|
|
||||||
pkt_send.Message = message;
|
|
||||||
|
|
||||||
|
|
||||||
if (pkt_send.Compose())
|
|
||||||
sock.Send(pkt_send.Data);
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void Send(byte[] message, int offset, int size)
|
public void NetworkConnect(ISocket sender)
|
||||||
{
|
{
|
||||||
lock (sendLock)
|
Receiver?.NetworkConnect(this);
|
||||||
{
|
|
||||||
if (held)
|
|
||||||
{
|
|
||||||
sendNetworkBuffer.Write(message, (uint)offset, (uint)size);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
totalSent += size;
|
|
||||||
//Console.WriteLine("TX " + size + "/"+totalSent);// + " " + DC.ToHex(message, 0, (uint)size));
|
|
||||||
|
|
||||||
pkt_send.Message = new byte[size];
|
|
||||||
Buffer.BlockCopy(message, offset, pkt_send.Message, 0, size);
|
|
||||||
if (pkt_send.Compose())
|
|
||||||
sock.Send(pkt_send.Data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public void Close()
|
|
||||||
{
|
|
||||||
sock.Close();
|
|
||||||
}
|
|
||||||
|
|
||||||
public AsyncReply<bool> Connect(string hostname, ushort port)
|
|
||||||
{
|
|
||||||
throw new NotImplementedException();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public bool Begin()
|
|
||||||
{
|
|
||||||
return sock.Begin();
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool Trigger(ResourceTrigger trigger)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Destroy()
|
|
||||||
{
|
|
||||||
Close();
|
|
||||||
OnDestroy?.Invoke(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public AsyncReply<ISocket> AcceptAsync()
|
|
||||||
{
|
|
||||||
throw new NotImplementedException();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Hold()
|
|
||||||
{
|
|
||||||
//Console.WriteLine("WS Hold ");
|
|
||||||
held = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Unhold()
|
|
||||||
{
|
|
||||||
lock (sendLock)
|
|
||||||
{
|
|
||||||
held = false;
|
|
||||||
|
|
||||||
var message = sendNetworkBuffer.Read();
|
|
||||||
|
|
||||||
//Console.WriteLine("WS Unhold {0}", message == null ? 0 : message.Length);
|
|
||||||
|
|
||||||
if (message == null)
|
|
||||||
return;
|
|
||||||
|
|
||||||
totalSent += message.Length;
|
|
||||||
|
|
||||||
pkt_send.Message = message;
|
|
||||||
if (pkt_send.Compose())
|
|
||||||
sock.Send(pkt_send.Data);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public AsyncReply<bool> SendAsync(byte[] message, int offset, int length)
|
|
||||||
{
|
|
||||||
throw new NotImplementedException();
|
|
||||||
}
|
|
||||||
|
|
||||||
public ISocket Accept()
|
|
||||||
{
|
|
||||||
throw new NotImplementedException();
|
|
||||||
}
|
|
||||||
|
|
||||||
public AsyncReply<bool> BeginAsync()
|
|
||||||
{
|
|
||||||
return sock.BeginAsync();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -34,11 +34,11 @@ using Esyur.Data;
|
|||||||
|
|
||||||
namespace Esyur.Net.TCP
|
namespace Esyur.Net.TCP
|
||||||
{
|
{
|
||||||
public class TCPConnection: NetworkConnection
|
public class TCPConnection:NetworkConnection {
|
||||||
{
|
|
||||||
|
|
||||||
private KeyList<string, object> variables = new KeyList<string, object>();
|
private KeyList<string, object> variables = new KeyList<string, object>();
|
||||||
|
|
||||||
|
public TCPServer Server { get; internal set; }
|
||||||
|
|
||||||
public KeyList<string, object> Variables
|
public KeyList<string, object> Variables
|
||||||
{
|
{
|
||||||
@ -47,5 +47,20 @@ namespace Esyur.Net.TCP
|
|||||||
return variables;
|
return variables;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected override void Connected()
|
||||||
|
{
|
||||||
|
// do nothing
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void DataReceived(NetworkBuffer buffer)
|
||||||
|
{
|
||||||
|
Server?.Execute(this, buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void Disconencted()
|
||||||
|
{
|
||||||
|
// do nothing
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -103,15 +103,19 @@ namespace Esyur.Net.TCP
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
protected override void DataReceived(TCPConnection sender, NetworkBuffer data)
|
|
||||||
|
|
||||||
|
internal bool Execute(TCPConnection sender, NetworkBuffer data)
|
||||||
{
|
{
|
||||||
var msg = data.Read();
|
var msg = data.Read();
|
||||||
|
|
||||||
foreach (var filter in filters)
|
foreach (var filter in filters)
|
||||||
{
|
{
|
||||||
if (filter.Execute(msg, data, sender))
|
if (filter.Execute(msg, data, sender))
|
||||||
return;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SessionModified(TCPConnection session, string key, object newValue)
|
private void SessionModified(TCPConnection session, string key, object newValue)
|
||||||
@ -119,22 +123,34 @@ namespace Esyur.Net.TCP
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void ClientConnected(TCPConnection sender)
|
protected override void ClientDisconnected(TCPConnection connection)
|
||||||
{
|
{
|
||||||
|
|
||||||
foreach (var filter in filters)
|
foreach (var filter in filters)
|
||||||
{
|
{
|
||||||
filter.Connected(sender);
|
filter.Connected(connection);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void ClientDisconnected(TCPConnection sender)
|
public override void Add(TCPConnection connection)
|
||||||
|
{
|
||||||
|
connection.Server = this;
|
||||||
|
base.Add(connection);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Remove(TCPConnection connection)
|
||||||
|
{
|
||||||
|
connection.Server = null;
|
||||||
|
base.Remove(connection);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void ClientConnected(TCPConnection connection)
|
||||||
{
|
{
|
||||||
foreach (var filter in filters)
|
foreach (var filter in filters)
|
||||||
{
|
{
|
||||||
filter.Disconnected(sender);
|
filter.Disconnected(connection);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -897,6 +897,8 @@ namespace Esyur.Resource
|
|||||||
//if (evt.EventHandlerType != typeof(ResourceEventHanlder))
|
//if (evt.EventHandlerType != typeof(ResourceEventHanlder))
|
||||||
// continue;
|
// continue;
|
||||||
|
|
||||||
|
if (evt.Info == null)
|
||||||
|
continue;
|
||||||
|
|
||||||
if (evt.Info.EventHandlerType == typeof(ResourceEventHanlder))
|
if (evt.Info.EventHandlerType == typeof(ResourceEventHanlder))
|
||||||
{
|
{
|
||||||
|
@ -36,6 +36,7 @@ using Esyur.Net.IIP;
|
|||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using Esyur.Misc;
|
using Esyur.Misc;
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
|
using System.Collections;
|
||||||
|
|
||||||
namespace Esyur.Resource
|
namespace Esyur.Resource
|
||||||
{
|
{
|
||||||
@ -61,16 +62,18 @@ namespace Esyur.Resource
|
|||||||
public static event StoreConnectedEvent StoreConnected;
|
public static event StoreConnectedEvent StoreConnected;
|
||||||
public static event StoreDisconnectedEvent StoreDisconnected;
|
public static event StoreDisconnectedEvent StoreDisconnected;
|
||||||
|
|
||||||
public static KeyList<string, Func<IStore>> Protocols { get; } = getSupportedProtocols();
|
public delegate IStore ProtocolInstance(string name, object properties);
|
||||||
|
|
||||||
|
public static KeyList<string, ProtocolInstance> Protocols { get; } = GetSupportedProtocols();
|
||||||
|
|
||||||
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, ProtocolInstance> GetSupportedProtocols()
|
||||||
{
|
{
|
||||||
var rt = new KeyList<string, Func<IStore>>();
|
var rt = new KeyList<string, ProtocolInstance>();
|
||||||
rt.Add("iip", () => new DistributedConnection());
|
rt.Add("iip", (name, props) => Warehouse.New<DistributedConnection>(name, null, null, null, props));
|
||||||
return rt;
|
return rt;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -396,26 +399,29 @@ namespace Esyur.Resource
|
|||||||
{
|
{
|
||||||
var handler = Protocols[url[1]];
|
var handler = Protocols[url[1]];
|
||||||
|
|
||||||
var store = handler();
|
var store = handler(url[2], attributes);
|
||||||
Put(store, url[2], null, parent, null, 0, manager, attributes);
|
|
||||||
|
|
||||||
|
|
||||||
store.Trigger(ResourceTrigger.Open).Then(x =>
|
store.Trigger(ResourceTrigger.Open).Then(x =>
|
||||||
{
|
{
|
||||||
|
|
||||||
warehouseIsOpen = true;
|
warehouseIsOpen = true;
|
||||||
|
Put(store, url[2], null, parent, null, 0, manager, attributes);
|
||||||
|
|
||||||
if (url[3].Length > 0 && url[3] != "")
|
if (url[3].Length > 0 && url[3] != "")
|
||||||
store.Get(url[3]).Then(r =>
|
store.Get(url[3]).Then(r =>
|
||||||
{
|
{
|
||||||
rt.Trigger(r);
|
rt.Trigger(r);
|
||||||
}).Error(e => rt.TriggerError(e));
|
}).Error(e =>
|
||||||
|
{
|
||||||
|
Warehouse.Remove(store);
|
||||||
|
rt.TriggerError(e);
|
||||||
|
});
|
||||||
else
|
else
|
||||||
rt.Trigger(store);
|
rt.Trigger(store);
|
||||||
}).Error(e =>
|
}).Error(e =>
|
||||||
{
|
{
|
||||||
rt.TriggerError(e);
|
rt.TriggerError(e);
|
||||||
Warehouse.Remove(store);
|
//Warehouse.Remove(store);
|
||||||
});
|
});
|
||||||
|
|
||||||
return rt;
|
return rt;
|
||||||
@ -457,13 +463,23 @@ namespace Esyur.Resource
|
|||||||
if (parent is IStore)
|
if (parent is IStore)
|
||||||
{
|
{
|
||||||
store = (IStore)parent;
|
store = (IStore)parent;
|
||||||
stores[store].Add(resourceReference);
|
List<WeakReference<IResource>> list;
|
||||||
|
if (stores.TryGetValue(store, out list))
|
||||||
|
lock (((ICollection)list).SyncRoot)
|
||||||
|
list.Add(resourceReference);
|
||||||
|
//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);
|
|
||||||
|
List<WeakReference<IResource>> list;
|
||||||
|
if (stores.TryGetValue(store, out list))
|
||||||
|
lock (((ICollection)list).SyncRoot)
|
||||||
|
list.Add(resourceReference);
|
||||||
|
|
||||||
|
//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)
|
||||||
@ -499,6 +515,7 @@ namespace Esyur.Resource
|
|||||||
|
|
||||||
if (resource is IStore)
|
if (resource is IStore)
|
||||||
{
|
{
|
||||||
|
|
||||||
stores.TryAdd(resource as IStore, new List<WeakReference<IResource>>());
|
stores.TryAdd(resource as IStore, new List<WeakReference<IResource>>());
|
||||||
StoreConnected?.Invoke(resource as IStore, name);
|
StoreConnected?.Invoke(resource as IStore, name);
|
||||||
}
|
}
|
||||||
@ -582,7 +599,7 @@ namespace Esyur.Resource
|
|||||||
{
|
{
|
||||||
pi.SetValue(res, p.Value);
|
pi.SetValue(res, p.Value);
|
||||||
}
|
}
|
||||||
catch(Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Global.Log(ex);
|
Global.Log(ex);
|
||||||
}
|
}
|
||||||
@ -679,8 +696,17 @@ namespace Esyur.Resource
|
|||||||
//}
|
//}
|
||||||
|
|
||||||
if (resource != resource.Instance.Store)
|
if (resource != resource.Instance.Store)
|
||||||
stores[resource.Instance.Store].Remove(resourceReference);
|
{
|
||||||
|
List<WeakReference<IResource>> list;
|
||||||
|
if (stores.TryGetValue(resource.Instance.Store, out list))
|
||||||
|
{
|
||||||
|
|
||||||
|
lock (((ICollection)list).SyncRoot)
|
||||||
|
list.Remove(resourceReference);
|
||||||
|
|
||||||
|
//list.TryTake(resourceReference);
|
||||||
|
}//.Remove(resourceReference);
|
||||||
|
}
|
||||||
if (resource is IStore)
|
if (resource is IStore)
|
||||||
{
|
{
|
||||||
var store = resource as IStore;
|
var store = resource as IStore;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user