Commit e7d9bf62 authored by 潘栩锋's avatar 潘栩锋 🚴

1.优化 SQLiteHelper 删除多余

2.添加 WSCF , Reflect_Proxy 添加释放事件资源
parent a91821bb
...@@ -9,8 +9,6 @@ namespace SQLite ...@@ -9,8 +9,6 @@ namespace SQLite
public class DBTable<T> : IDBTable public class DBTable<T> : IDBTable
where T : new() where T : new()
{ {
public List<SQLiteHelper.ArrayFieldTypeInfo> ArrayFieldTypeInfos = new List<SQLiteHelper.ArrayFieldTypeInfo>();
public string TableName { get; private set; } public string TableName { get; private set; }
private long freeID = 0; private long freeID = 0;
...@@ -32,10 +30,7 @@ namespace SQLite ...@@ -32,10 +30,7 @@ namespace SQLite
{ {
get get
{ {
if (ArrayFieldTypeInfos.Count() == 0) return ddl;
return ddl;
else
return SQLiteHelper.GetCreateTableCommandText(typeof(T), ArrayFieldTypeInfos.ToArray());
} }
} }
...@@ -81,7 +76,7 @@ namespace SQLite ...@@ -81,7 +76,7 @@ namespace SQLite
sql += " " + condition; sql += " " + condition;
DataTable dataTable = sqliteHelper.ExecuteReader(sql); DataTable dataTable = sqliteHelper.ExecuteReader(sql);
return SQLiteHelper.ToObjs<T>(dataTable, ArrayFieldTypeInfos.ToArray()); return SQLiteHelper.ToObjs<T>(dataTable);
} }
/// <summary> /// <summary>
......
...@@ -11,15 +11,6 @@ namespace SQLite ...@@ -11,15 +11,6 @@ namespace SQLite
} }
} }
public class BortherAttribute : Attribute
{
}
public class ChildAttribute : Attribute
{
}
public class PropertyIndexAttribute : Attribute public class PropertyIndexAttribute : Attribute
{ {
......
...@@ -7,7 +7,11 @@ ...@@ -7,7 +7,11 @@
mc:Ignorable="d" mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800"> Title="MainWindow" Height="450" Width="800">
<Grid> <Grid>
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center"> <Grid.RowDefinitions>
<RowDefinition Height="100"/>
<RowDefinition/>
</Grid.RowDefinitions>
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" >
<TextBlock> <TextBlock>
Number=<Run Text="{Binding Number,Mode=OneWay}"/> Number=<Run Text="{Binding Number,Mode=OneWay}"/>
</TextBlock> </TextBlock>
...@@ -15,5 +19,13 @@ ...@@ -15,5 +19,13 @@
AddFuncTime=<Run Text="{Binding AddFuncTime,Mode=OneWay}"/> AddFuncTime=<Run Text="{Binding AddFuncTime,Mode=OneWay}"/>
</TextBlock> </TextBlock>
</StackPanel> </StackPanel>
<Grid Grid.Row="1" Margin="5">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Text="Session 开始时间:"/>
<ListBox x:Name="lst" Grid.Row="1" />
</Grid>
</Grid> </Grid>
</Window> </Window>
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
...@@ -12,6 +13,7 @@ using System.Windows.Media; ...@@ -12,6 +13,7 @@ using System.Windows.Media;
using System.Windows.Media.Imaging; using System.Windows.Media.Imaging;
using System.Windows.Navigation; using System.Windows.Navigation;
using System.Windows.Shapes; using System.Windows.Shapes;
using System.Windows.Threading;
namespace WSCF.Test.Server namespace WSCF.Test.Server
{ {
...@@ -22,6 +24,10 @@ namespace WSCF.Test.Server ...@@ -22,6 +24,10 @@ namespace WSCF.Test.Server
{ {
WSCF.Test.Server.WsProxy.WsProxy wsProxy; WSCF.Test.Server.WsProxy.WsProxy wsProxy;
Foo foo; Foo foo;
DispatcherTimer timer;
ObservableCollection<DateTime> SessionStartTimes = new ObservableCollection<DateTime>();
public MainWindow() public MainWindow()
{ {
InitializeComponent(); InitializeComponent();
...@@ -30,6 +36,35 @@ namespace WSCF.Test.Server ...@@ -30,6 +36,35 @@ namespace WSCF.Test.Server
wsProxy = new WsProxy.WsProxy("127.0.0.1:5540",foo); wsProxy = new WsProxy.WsProxy("127.0.0.1:5540",foo);
this.DataContext = foo; this.DataContext = foo;
timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += Timer_Tick;
timer.Start();
lst.ItemsSource = SessionStartTimes;
}
private void Timer_Tick(object sender, EventArgs e)
{
if (wsProxy.wssv.WebSocketServices.Hosts.Count() > 0)
{
var startimes = wsProxy.wssv.WebSocketServices.Hosts.First().Sessions.Sessions.Select(s => s.StartTime);
for (int i = 0; i < startimes.Count(); i++)
{
var startime = startimes.ElementAt(i);
if (i < SessionStartTimes.Count)
{
SessionStartTimes[i] = startime;
}
else
{
SessionStartTimes.Add(startime);
}
}
while (SessionStartTimes.Count() > startimes.Count())
SessionStartTimes.RemoveAt(SessionStartTimes.Count() - 1);
}
} }
} }
} }
...@@ -46,6 +46,7 @@ ...@@ -46,6 +46,7 @@
<Reference Include="System.Xaml"> <Reference Include="System.Xaml">
<RequiredTargetFramework>4.0</RequiredTargetFramework> <RequiredTargetFramework>4.0</RequiredTargetFramework>
</Reference> </Reference>
<Reference Include="websocket-sharp, Version=1.0.1.0, Culture=neutral, PublicKeyToken=5660b08a1845a91e" />
<Reference Include="WindowsBase" /> <Reference Include="WindowsBase" />
<Reference Include="PresentationCore" /> <Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" /> <Reference Include="PresentationFramework" />
......
...@@ -10,7 +10,7 @@ namespace WSCF.Test.Server.WsProxy ...@@ -10,7 +10,7 @@ namespace WSCF.Test.Server.WsProxy
{ {
public class WsProxy public class WsProxy
{ {
WebSocketServer wssv; public WebSocketServer wssv;
public WsProxy(string addr, Foo foo) public WsProxy(string addr, Foo foo)
{ {
wssv = new WebSocketServer($"ws://{addr}/"); wssv = new WebSocketServer($"ws://{addr}/");
......
...@@ -11,8 +11,9 @@ using WebSocketSharp.Server; ...@@ -11,8 +11,9 @@ using WebSocketSharp.Server;
namespace WSCF namespace WSCF
{ {
public class Reflect_Proxy : WebSocketBehavior public class Reflect_Proxy : WebSocketBehavior,IDisposable
{ {
static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
//对象的接口类型 //对象的接口类型
Type interfaceType; Type interfaceType;
//代理对象 //代理对象
...@@ -45,7 +46,20 @@ namespace WSCF ...@@ -45,7 +46,20 @@ namespace WSCF
//处理[Push] //处理[Push]
InitEventPush(); InitEventPush();
} }
List<DisposeAction> DisposeActions = new List<DisposeAction>();
class DisposeAction
{
public Action<object> Dispose;
public object Obj;
public DisposeAction(Action<object> dispose, object obj)
{
this.Dispose = dispose;
this.Obj = obj;
}
public void Do() {
Dispose(Obj);
}
}
#region Init #region Init
void InitPropertyChanged() void InitPropertyChanged()
{ {
...@@ -55,6 +69,13 @@ namespace WSCF ...@@ -55,6 +69,13 @@ namespace WSCF
//继承了INotifyPropertyChanged //继承了INotifyPropertyChanged
((INotifyPropertyChanged)(this.obj)).PropertyChanged += Obj_PropertyChanged; ((INotifyPropertyChanged)(this.obj)).PropertyChanged += Obj_PropertyChanged;
//资源释放时断开引用
DisposeActions.Add(new DisposeAction((_obj) =>
{
((INotifyPropertyChanged)(_obj)).PropertyChanged -= Obj_PropertyChanged;
}, this.obj));
var interfaceTypes = new List<Type>(); var interfaceTypes = new List<Type>();
interfaceTypes.Add(this.interfaceType); interfaceTypes.Add(this.interfaceType);
interfaceTypes.AddRange(this.interfaceType.GetInterfaces()); interfaceTypes.AddRange(this.interfaceType.GetInterfaces());
...@@ -112,7 +133,7 @@ namespace WSCF ...@@ -112,7 +133,7 @@ namespace WSCF
JObject.FromObject(rd) JObject.FromObject(rd)
); );
string json = JsonConvert.SerializeObject(pkgData); string json = JsonConvert.SerializeObject(pkgData);
Send(json); SendEx(json);
} }
}; };
//这个事件需要推送 //这个事件需要推送
...@@ -168,6 +189,12 @@ namespace WSCF ...@@ -168,6 +189,12 @@ namespace WSCF
subProperties.Add(propertyValue, path); subProperties.Add(propertyValue, path);
((INotifyPropertyChanged)(propertyValue)).PropertyChanged += Sub_PropertyChanged; ((INotifyPropertyChanged)(propertyValue)).PropertyChanged += Sub_PropertyChanged;
//资源释放时断开引用
DisposeActions.Add(new DisposeAction((_obj) =>
{
((INotifyPropertyChanged)(_obj)).PropertyChanged -= Sub_PropertyChanged;
}, propertyValue));
//继续向下找 //继续向下找
var subPropertyInfos = propertyInfo.PropertyType.GetProperties(); var subPropertyInfos = propertyInfo.PropertyType.GetProperties();
...@@ -203,9 +230,19 @@ namespace WSCF ...@@ -203,9 +230,19 @@ namespace WSCF
jObject jObject
); );
string json = JsonConvert.SerializeObject(pkgData); string json = JsonConvert.SerializeObject(pkgData);
Send(json); SendEx(json);
}
void SendEx(string msg) {
try
{
Send(msg);
}
catch (Exception e)
{
//异常,通常是断开了!!
logger.Error(e);
}
} }
private void Sub_PropertyChanged(object sender, PropertyChangedEventArgs e) private void Sub_PropertyChanged(object sender, PropertyChangedEventArgs e)
{ {
if (ignoreSet)//从服务器接收的数据,不用再推送给服务器 if (ignoreSet)//从服务器接收的数据,不用再推送给服务器
...@@ -249,7 +286,7 @@ namespace WSCF ...@@ -249,7 +286,7 @@ namespace WSCF
jObject_parent jObject_parent
); );
string json = JsonConvert.SerializeObject(pkgData); string json = JsonConvert.SerializeObject(pkgData);
Send(json); SendEx(json);
} }
#endregion #endregion
...@@ -264,6 +301,8 @@ namespace WSCF ...@@ -264,6 +301,8 @@ namespace WSCF
{ {
base.OnClose(e); base.OnClose(e);
isAlive = false; isAlive = false;
Dispose();
} }
protected override void OnMessage(MessageEventArgs e) protected override void OnMessage(MessageEventArgs e)
...@@ -299,7 +338,7 @@ namespace WSCF ...@@ -299,7 +338,7 @@ namespace WSCF
pkgData.data = jObject; pkgData.data = jObject;
string json = JsonConvert.SerializeObject(pkgData); string json = JsonConvert.SerializeObject(pkgData);
Send(json); SendEx(json);
} }
break; break;
case Reflect_OBJ_INTERFACE.PkgName.CALL_SetProperty: case Reflect_OBJ_INTERFACE.PkgName.CALL_SetProperty:
...@@ -361,13 +400,24 @@ namespace WSCF ...@@ -361,13 +400,24 @@ namespace WSCF
); );
string json = JsonConvert.SerializeObject(pkgData); string json = JsonConvert.SerializeObject(pkgData);
Send(json); SendEx(json);
} }
public void Dispose()
{
foreach (var act in DisposeActions) {
act.Do();
}
DisposeActions.Clear();
}
class CC class CC
{ {
public string methodName; public string methodName;
public string guid; public string guid;
} }
} }
/// <summary> /// <summary>
/// 事件推送包装 /// 事件推送包装
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment