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

1.优化 SQLiteHelper 删除多余

2.添加 WSCF , Reflect_Proxy 添加释放事件资源
parent a91821bb
......@@ -9,8 +9,6 @@ namespace SQLite
public class DBTable<T> : IDBTable
where T : new()
{
public List<SQLiteHelper.ArrayFieldTypeInfo> ArrayFieldTypeInfos = new List<SQLiteHelper.ArrayFieldTypeInfo>();
public string TableName { get; private set; }
private long freeID = 0;
......@@ -32,10 +30,7 @@ namespace SQLite
{
get
{
if (ArrayFieldTypeInfos.Count() == 0)
return ddl;
else
return SQLiteHelper.GetCreateTableCommandText(typeof(T), ArrayFieldTypeInfos.ToArray());
return ddl;
}
}
......@@ -81,7 +76,7 @@ namespace SQLite
sql += " " + condition;
DataTable dataTable = sqliteHelper.ExecuteReader(sql);
return SQLiteHelper.ToObjs<T>(dataTable, ArrayFieldTypeInfos.ToArray());
return SQLiteHelper.ToObjs<T>(dataTable);
}
/// <summary>
......
......@@ -11,15 +11,6 @@ namespace SQLite
}
}
public class BortherAttribute : Attribute
{
}
public class ChildAttribute : Attribute
{
}
public class PropertyIndexAttribute : Attribute
{
......
......@@ -109,24 +109,6 @@ namespace SQLite
return null;
}
}
public class ArrayFieldTypeInfo
{
/// <summary>
/// 属性名
/// </summary>
public string PropertyName { get; set; }
/// <summary>
/// 数组大小
/// </summary>
public int Length { get; set; }
public ArrayFieldTypeInfo(string propertyname, int length)
{
PropertyName = propertyname;
Length = length;
}
}
public static string GetTableName(Type type)
......@@ -142,7 +124,7 @@ namespace SQLite
}
}
static string GetCreateTableCommandText_fieldText(Type type, IEnumerable<ArrayFieldTypeInfo> arrayFieldTypeInfos)
static string GetCreateTableCommandText_fieldText(Type type)
{
string total_fieldtext = "";
......@@ -161,51 +143,15 @@ namespace SQLite
if (propertyIndex != null)
fieldText.index = propertyIndex.Index;//默认index=0
SQLiteFieldTypeInfo fieldTypeInfo = FieldTypeInfo.Find((fti) => fti.PropertyType == propertyInfo.PropertyType);
string text = "";
text += string.Format("{0} {1}", propertyInfo.Name, fieldTypeInfo.FieldType);
//这个属性 下面的全部属性,是同一个表
if (propertyInfo.GetCustomAttributes(typeof(BortherAttribute), false).Count() > 0)
{
//从arrayFieldTypeInfos 提取
string startswith = propertyInfo.Name + ".";
var aftis = from afti in arrayFieldTypeInfos
where afti.PropertyName.StartsWith(startswith)
select new ArrayFieldTypeInfo(afti.PropertyName.Substring(startswith.Length), afti.PropertyName.Length);
fieldText.fieldtext = GetCreateTableCommandText_fieldText(propertyInfo.PropertyType, aftis);
continue;
}
if (propertyInfo.PropertyType.IsArray)//是数组,需要明确大小
{
int length = arrayFieldTypeInfos.First((a) => a.PropertyName == propertyInfo.Name).Length;
Type elementType = propertyInfo.PropertyType.GetElementType();
SQLiteFieldTypeInfo fieldTypeInfo = FieldTypeInfo.Find((fti) => fti.PropertyType == elementType);
string text = "";
for (int i = 0; i < length; i++)
{
if (i != 0)
text += ",";
text += string.Format("{0}{1} {2}",
propertyInfo.Name,
i,
fieldTypeInfo.FieldType);
}
fieldText.fieldtext = text;
}
else
{
SQLiteFieldTypeInfo fieldTypeInfo = FieldTypeInfo.Find((fti) => fti.PropertyType == propertyInfo.PropertyType);
string text = "";
text += string.Format("{0} {1}", propertyInfo.Name, fieldTypeInfo.FieldType);
//主键
if (propertyInfo.GetCustomAttributes(typeof(KeyAttribute), false).Count() > 0)
text += " PRIMARY KEY";
fieldText.fieldtext = text;
}
//主键
if (propertyInfo.GetCustomAttributes(typeof(KeyAttribute), false).Count() > 0)
text += " PRIMARY KEY";
fieldText.fieldtext = text;
}
//从小到大排序
fieldTexts.Sort((fieldTextIndex0, fieldTextIndex1) =>
......@@ -227,11 +173,8 @@ namespace SQLite
}
return total_fieldtext;
}
public static string GetCreateTableCommandText(Type type, params ArrayFieldTypeInfo[] arrayFieldTypeInfos)
public static string GetCreateTableCommandText(Type type)
{
if (arrayFieldTypeInfos == null)
arrayFieldTypeInfos = new ArrayFieldTypeInfo[0];
//CREATE TABLE table_name(
//column1 datatype PRIMARY KEY,
//column2 datatype,
......@@ -240,7 +183,7 @@ namespace SQLite
// columnN datatype,
//)
string tablename = GetTableName(type);
string fieldtext = GetCreateTableCommandText_fieldText(type, arrayFieldTypeInfos);
string fieldtext = GetCreateTableCommandText_fieldText(type);
string commandText = string.Format("CREATE TABLE {0} ({1})", tablename, fieldtext);
return commandText;
}
......@@ -270,37 +213,10 @@ namespace SQLite
object o = propertyInfo.GetValue(cell, null);
SQLiteFieldTypeInfo fieldTypeInfo = FieldTypeInfo.Find((fti) => fti.PropertyType == propertyInfo.PropertyType);
//这个属性 下面的全部属性,是同一个表
if (propertyInfo.GetCustomAttributes(typeof(BortherAttribute), false).Count() > 0)
{
fieldText.fieldtext = GetInsertCommandText_fieldText(o);
continue;
}
if (propertyInfo.PropertyType.IsArray)//是数组,需要明确大小
{
Type elementType = propertyInfo.PropertyType.GetElementType();
SQLiteFieldTypeInfo fieldTypeInfo = FieldTypeInfo.Find((fti) => fti.PropertyType == elementType);
Array a = o as Array;
string text = "";
for (int i = 0; i < a.Length; i++)
{
if (i != 0)
text += ",";
text += fieldTypeInfo.PtoS(a.GetValue(i));
}
fieldText.fieldtext = text;
}
else
{
SQLiteFieldTypeInfo fieldTypeInfo = FieldTypeInfo.Find((fti) => fti.PropertyType == propertyInfo.PropertyType);
fieldText.fieldtext = fieldTypeInfo.PtoS(o);
}
fieldText.fieldtext = fieldTypeInfo.PtoS(o);
}
//从小到大排序
......@@ -364,36 +280,12 @@ namespace SQLite
object o = propertyInfo.GetValue(cell, null);
//这个属性 下面的全部属性,是同一个表
if (propertyInfo.GetCustomAttributes(typeof(BortherAttribute), false).Count() > 0)
{
fieldtext += GetUpdateCommandText_fieldText(o);
continue;
}
if (propertyInfo.PropertyType.IsArray)//是数组,需要明确大小
{
Type elementType = propertyInfo.PropertyType.GetElementType();
SQLiteFieldTypeInfo fieldTypeInfo = FieldTypeInfo.Find((fti) => fti.PropertyType == elementType);
Array a = o as Array;
for (int i = 0; i < a.Length; i++)
{
if (i != 0)
fieldtext += ",";
fieldtext += string.Format("{0}{1} = {2}", propertyInfo.Name, i, fieldTypeInfo.PtoS(a.GetValue(i)));
}
}
else
{
SQLiteFieldTypeInfo fieldTypeInfo = FieldTypeInfo.Find((fti) => fti.PropertyType == propertyInfo.PropertyType);
SQLiteFieldTypeInfo fieldTypeInfo = FieldTypeInfo.Find((fti) => fti.PropertyType == propertyInfo.PropertyType);
fieldtext += string.Format("{0} = {1}", propertyInfo.Name, fieldTypeInfo.PtoS(o));
}
fieldtext += string.Format("{0} = {1}", propertyInfo.Name, fieldTypeInfo.PtoS(o));
}
return fieldtext;
......@@ -421,19 +313,18 @@ namespace SQLite
return commandText;
}
public static List<T> ToObjs<T>(DataTable dataTable, params ArrayFieldTypeInfo[] arrayFieldTypeInfos)
public static List<T> ToObjs<T>(DataTable dataTable)
where T : new()
{
Type type = typeof(T);
List<T> list = new List<T>();
foreach (DataRow dataRow in dataTable.Rows)
{
list.Add(ToObj<T>(dataRow, arrayFieldTypeInfos));
list.Add(ToObj<T>(dataRow));
}
return list;
}
static void SetObj(object t, DataRow dataRow, IEnumerable<ArrayFieldTypeInfo> arrayFieldTypeInfos)
static void SetObj(object t, DataRow dataRow)
{
Type type = t.GetType();
......@@ -447,49 +338,17 @@ namespace SQLite
Type ptype = propertyInfo.PropertyType;
//这个属性 下面的全部属性,是同一个表
if (propertyInfo.GetCustomAttributes(typeof(BortherAttribute), false).Count() > 0)
{
//从arrayFieldTypeInfos 提取
string startswith = propertyInfo.Name + ".";
var aftis = from afti in arrayFieldTypeInfos
where afti.PropertyName.StartsWith(startswith)
select new ArrayFieldTypeInfo(afti.PropertyName.Substring(startswith.Length), afti.PropertyName.Length);
object obj = propertyInfo.GetValue(t, null);
SetObj(obj, dataRow, aftis);
continue;
}
if (ptype.IsArray)//是数组,需要明确大小
{
Type elementType = propertyInfo.PropertyType.GetElementType();
SQLiteFieldTypeInfo fieldTypeInfo = FieldTypeInfo.Find((fti) => fti.PropertyType == elementType);
int length = arrayFieldTypeInfos.First((a) => a.PropertyName == propertyInfo.Name).Length;
Array array = (Array)ptype.Assembly.CreateInstance(ptype.FullName, false, BindingFlags.CreateInstance, null, new object[] { length }, null, null);
for (int i = 0; i < length; i++)
{
object o = fieldTypeInfo.StoP(dataRow[propertyInfo.Name + i]);
array.SetValue(o, i);
}
propertyInfo.SetValue(t, array, null);
}
else
{
SQLiteFieldTypeInfo fieldTypeInfo = FieldTypeInfo.Find((fti) => fti.PropertyType == ptype);
object o = fieldTypeInfo.StoP(dataRow[propertyInfo.Name]);
propertyInfo.SetValue(t, o, null);
}
SQLiteFieldTypeInfo fieldTypeInfo = FieldTypeInfo.Find((fti) => fti.PropertyType == ptype);
object o = fieldTypeInfo.StoP(dataRow[propertyInfo.Name]);
propertyInfo.SetValue(t, o, null);
}
}
public static T ToObj<T>(DataRow dataRow, params ArrayFieldTypeInfo[] arrayFieldTypeInfos)
public static T ToObj<T>(DataRow dataRow)
where T : new()
{
Type type = typeof(T);
T t = new T();
SetObj(t, dataRow, arrayFieldTypeInfos);
SetObj(t, dataRow);
return t;
}
......
......@@ -7,7 +7,11 @@
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<Grid.RowDefinitions>
<RowDefinition Height="100"/>
<RowDefinition/>
</Grid.RowDefinitions>
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" >
<TextBlock>
Number=<Run Text="{Binding Number,Mode=OneWay}"/>
</TextBlock>
......@@ -15,5 +19,13 @@
AddFuncTime=<Run Text="{Binding AddFuncTime,Mode=OneWay}"/>
</TextBlock>
</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>
</Window>
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
......@@ -12,6 +13,7 @@ using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace WSCF.Test.Server
{
......@@ -22,6 +24,10 @@ namespace WSCF.Test.Server
{
WSCF.Test.Server.WsProxy.WsProxy wsProxy;
Foo foo;
DispatcherTimer timer;
ObservableCollection<DateTime> SessionStartTimes = new ObservableCollection<DateTime>();
public MainWindow()
{
InitializeComponent();
......@@ -30,6 +36,35 @@ namespace WSCF.Test.Server
wsProxy = new WsProxy.WsProxy("127.0.0.1:5540",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 @@
<Reference Include="System.Xaml">
<RequiredTargetFramework>4.0</RequiredTargetFramework>
</Reference>
<Reference Include="websocket-sharp, Version=1.0.1.0, Culture=neutral, PublicKeyToken=5660b08a1845a91e" />
<Reference Include="WindowsBase" />
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
......
......@@ -10,7 +10,7 @@ namespace WSCF.Test.Server.WsProxy
{
public class WsProxy
{
WebSocketServer wssv;
public WebSocketServer wssv;
public WsProxy(string addr, Foo foo)
{
wssv = new WebSocketServer($"ws://{addr}/");
......
......@@ -11,8 +11,9 @@ using WebSocketSharp.Server;
namespace WSCF
{
public class Reflect_Proxy : WebSocketBehavior
public class Reflect_Proxy : WebSocketBehavior,IDisposable
{
static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
//对象的接口类型
Type interfaceType;
//代理对象
......@@ -45,7 +46,20 @@ namespace WSCF
//处理[Push]
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
void InitPropertyChanged()
{
......@@ -55,6 +69,13 @@ namespace WSCF
//继承了INotifyPropertyChanged
((INotifyPropertyChanged)(this.obj)).PropertyChanged += Obj_PropertyChanged;
//资源释放时断开引用
DisposeActions.Add(new DisposeAction((_obj) =>
{
((INotifyPropertyChanged)(_obj)).PropertyChanged -= Obj_PropertyChanged;
}, this.obj));
var interfaceTypes = new List<Type>();
interfaceTypes.Add(this.interfaceType);
interfaceTypes.AddRange(this.interfaceType.GetInterfaces());
......@@ -112,7 +133,7 @@ namespace WSCF
JObject.FromObject(rd)
);
string json = JsonConvert.SerializeObject(pkgData);
Send(json);
SendEx(json);
}
};
//这个事件需要推送
......@@ -168,6 +189,12 @@ namespace WSCF
subProperties.Add(propertyValue, path);
((INotifyPropertyChanged)(propertyValue)).PropertyChanged += Sub_PropertyChanged;
//资源释放时断开引用
DisposeActions.Add(new DisposeAction((_obj) =>
{
((INotifyPropertyChanged)(_obj)).PropertyChanged -= Sub_PropertyChanged;
}, propertyValue));
//继续向下找
var subPropertyInfos = propertyInfo.PropertyType.GetProperties();
......@@ -203,9 +230,19 @@ namespace WSCF
jObject
);
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)
{
if (ignoreSet)//从服务器接收的数据,不用再推送给服务器
......@@ -249,7 +286,7 @@ namespace WSCF
jObject_parent
);
string json = JsonConvert.SerializeObject(pkgData);
Send(json);
SendEx(json);
}
#endregion
......@@ -264,6 +301,8 @@ namespace WSCF
{
base.OnClose(e);
isAlive = false;
Dispose();
}
protected override void OnMessage(MessageEventArgs e)
......@@ -299,7 +338,7 @@ namespace WSCF
pkgData.data = jObject;
string json = JsonConvert.SerializeObject(pkgData);
Send(json);
SendEx(json);
}
break;
case Reflect_OBJ_INTERFACE.PkgName.CALL_SetProperty:
......@@ -361,13 +400,24 @@ namespace WSCF
);
string json = JsonConvert.SerializeObject(pkgData);
Send(json);
SendEx(json);
}
public void Dispose()
{
foreach (var act in DisposeActions) {
act.Do();
}
DisposeActions.Clear();
}
class CC
{
public string methodName;
public string guid;
}
}
/// <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