using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Ports;
using System.Threading;
using NewLife.Log;
using NewLife.Messaging;
using NewLife.Net;
using NewLife.Net.Common;
using NewLife.Net.Proxy;
using NewLife.Net.Sockets;
using NewLife.Reflection;
using NewLife.Serialization;
using NewLife.Threading;
using XCode.DataAccessLayer;
using XCode.Transform;
using System.Reflection;
using NewLife.CommonEntity;
#if NET4
using System.Linq;
#else
using NewLife.Linq;
using XCode;
using NewLife.Common;
using XCode.Sync;
#endif
namespace Test
{
public class Program
{
private static void Main(string[] args)
{
XTrace.UseConsole();
while (true)
{
Stopwatch sw = new Stopwatch();
sw.Start();
#if !DEBUG
try
{
#endif
Test9();
#if !DEBUG
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
#endif
sw.Stop();
Console.WriteLine("OK! 耗时 {0}", sw.Elapsed);
ConsoleKeyInfo key = Console.ReadKey(true);
if (key.Key != ConsoleKey.C) break;
}
}
static HttpProxy http = null;
private static void Test1()
{
//var server = new HttpReverseProxy();
//server.Port = 888;
//server.ServerHost = "www.cnblogs.com";
//server.ServerPort = 80;
//server.Start();
//var ns = Enum.GetNames(typeof(ConsoleColor));
//var vs = Enum.GetValues(typeof(ConsoleColor));
//for (int i = 0; i < ns.Length; i++)
//{
// Console.ForegroundColor = (ConsoleColor)vs.GetValue(i);
// Console.WriteLine(ns[i]);
//}
//NewLife.Net.Application.AppTest.Start();
http = new HttpProxy();
http.Port = 8080;
http.EnableCache = true;
//http.OnResponse += new EventHandler<HttpProxyEventArgs>(http_OnResponse);
http.Start();
var old = HttpProxy.GetIEProxy();
if (!old.IsNullOrWhiteSpace()) Console.WriteLine("旧代理:{0}", old);
HttpProxy.SetIEProxy("127.0.0.1:" + http.Port);
Console.WriteLine("已设置IE代理,任意键结束测试,关闭IE代理!");
ThreadPoolX.QueueUserWorkItem(ShowStatus);
Console.ReadKey(true);
HttpProxy.SetIEProxy(old);
//server.Dispose();
http.Dispose();
//var ds = new DNSServer();
//ds.Start();
//for (int i = 5; i < 6; i++)
//{
// var buffer = File.ReadAllBytes("dns" + i + ".bin");
// var entity2 = DNSEntity.Read(buffer, false);
// Console.WriteLine(entity2);
// var buffer2 = entity2.GetStream().ReadBytes();
// var p = buffer.CompareTo(buffer2);
// if (p != 0)
// {
// Console.WriteLine("{0:X2} {1:X2} {2:X2}", p, buffer[p], buffer2[p]);
// }
//}
}
static void ShowStatus()
{
//var pool = PropertyInfoX.GetValue<SocketBase, ObjectPool<NetEventArgs>>("Pool");
var pool = NetEventArgs.Pool;
while (true)
{
var asyncCount = 0; try
{
foreach (var item in http.Servers)
{
asyncCount += item.AsyncCount;
}
foreach (var item in http.Sessions.Values.ToArray())
{
var remote = (item as IProxySession).Remote;
if (remote != null) asyncCount += remote.Host.AsyncCount;
}
}
catch (Exception ex) { Console.WriteLine(ex.ToString()); }
Int32 wt = 0;
Int32 cpt = 0;
ThreadPool.GetAvailableThreads(out wt, out cpt);
Int32 threads = Process.GetCurrentProcess().Threads.Count;
var color = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("异步:{0} 会话:{1} Thread:{2}/{3}/{4} Pool:{5}/{6}/{7}", asyncCount, http.Sessions.Count, threads, wt, cpt, pool.StockCount, pool.FreeCount, pool.CreateCount);
Console.ForegroundColor = color;
Thread.Sleep(3000);
//GC.Collect();
}
}
static void Test2()
{
HttpClientMessageProvider client = new HttpClientMessageProvider();
client.Uri = new Uri("http://localhost:8/Web/MessageHandler.ashx");
var rm = MethodMessage.Create("Admin.Login", "admin", "admin");
rm.Header.Channel = 88;
//Message.Debug = true;
//var ms = rm.GetStream();
//var m2 = Message.Read(ms);
Message msg = client.SendAndReceive(rm, 0);
var rs = msg as EntityMessage;
Console.WriteLine("返回:" + rs.Value);
msg = client.SendAndReceive(rm, 0);
rs = msg as EntityMessage;
Console.WriteLine("返回:" + rs.Value);
}
static void Test3()
{
using (var sp = new SerialPort("COM2"))
{
sp.Open();
var b = 0;
while (true)
{
Console.WriteLine(b);
var bs = new Byte[] { (Byte)b };
sp.Write(bs, 0, bs.Length);
b = b == 0 ? 0xFF : 0;
Thread.Sleep(1000);
}
}
}
static NetServer server = null;
static IMessageProvider smp = null;
static IMessageProvider cmp = null;
static void Test4()
{
Console.Clear();
if (server == null)
{
server = new NetServer();
server.Port = 1234;
//server.Received += new EventHandler<NetEventArgs>(server_Received);
var mp = new ServerMessageProvider(server);
mp.OnReceived += new EventHandler<MessageEventArgs>(smp_OnReceived);
//mp.MaxMessageSize = 1460;
mp.AutoJoinGroup = true;
smp = mp;
server.Start();
}
if (cmp == null)
{
var client = NetService.CreateSession(new NetUri("udp://::1:1234"));
client.ReceiveAsync();
cmp = new ClientMessageProvider() { Session = client };
cmp.OnReceived += new EventHandler<MessageEventArgs>(cmp_OnReceived);
}
//Message.Debug = true;
var msg = new EntityMessage();
var rnd = new Random((Int32)DateTime.Now.Ticks);
var bts = new Byte[rnd.Next(1000000, 5000000)];
//var bts = new Byte[1460 * 1 - rnd.Next(0, 20)];
rnd.NextBytes(bts);
msg.Value = bts;
//var rs = cmp.SendAndReceive(msg, 5000);
cmp.Send(msg);
}
static void smp_OnReceived(object sender, MessageEventArgs e)
{
var msg = e.Message;
Console.WriteLine("服务端收到:{0}", msg);
var rs = new EntityMessage();
rs.Value = "收到" + msg;
(sender as IMessageProvider).Send(rs);
}
static void cmp_OnReceived(object sender, MessageEventArgs e)
{
Console.WriteLine("客户端收到:{0}", e.Message);
}
static void Test5()
{
DAL.AddConnStr("xxgk", "Data Source=192.168.1.21;Initial Catalog=信息公开;user id=sa;password=Pass@word", null, "mssql");
var dal = DAL.Create("xxgk");
DAL.AddConnStr("xxgk2", "Data Source=XXGK.db;Version=3;", null, "sqlite");
File.Delete("XXGK.db");
//DAL.ShowSQL = false;
var etf = new EntityTransform();
etf.SrcConn = "xxgk";
etf.DesConn = "xxgk2";
etf.AllowInsertIdentity = true;
//etf.TableNames.Remove("PubInfoLog");
//etf.TableNames.Remove("PublicInformation");
//etf.TableNames.Remove("SystemUserLog");
etf.PartialTableNames.Add("PubInfoLog");
etf.PartialTableNames.Add("PublicInformation");
etf.PartialTableNames.Add("SystemUserLog");
etf.PartialCount = 25;
etf.OnTransformTable += (s, e) => { if (e.Arg.Name == "")e.Arg = null; };
var rs = etf.Transform();
Console.WriteLine("共转移:{0}", rs);
}
static void Test6()
{
Message.DumpStreamWhenError = true;
//var msg = new EntityMessage();
//msg.Value = Guid.NewGuid();
var msg = new MethodMessage();
msg.TypeName = "Admin";
msg.Name = "Login";
msg.Parameters = new Object[] { "admin", "password" };
var kind = RWKinds.Json;
var ms = msg.GetStream(kind);
//ms = new MemoryStream(ms.ReadBytes(ms.Length - 1));
//Console.WriteLine(ms.ReadBytes().ToHex());
Console.WriteLine(ms.ToStr());
//ms = msg.GetStream(RWKinds.Xml);
//Console.WriteLine(ms.ToStr());
Message.Debug = true;
ms.Position = 0;
var rs = Message.Read(ms, kind);
Console.WriteLine(rs);
}
static void Test7()
{
//Console.Write("请输入表达式:");
//var code = Console.ReadLine();
//var rs = ScriptEngine.Execute(code, new Dictionary<String, Object> { { "a", 222 }, { "b", 333 } });
////Console.WriteLine(rs);
//var se = ScriptEngine.Create(code);
//var fm = code.Replace("a", "{0}").Replace("b", "{1}");
//for (int i = 1; i <= 9; i++)
//{
// for (int j = 1; j <= i; j++)
// {
// Console.Write(fm + "={2}\t", j, i, se.Invoke(i, j));
// }
// Console.WriteLine();
//}
var se = ScriptEngine.Create("Test.Program.TestMath(k)");
if (se.Method == null)
{
se.Parameters.Add("k", typeof(Double));
se.Compile();
}
var fun = (DM)(Object)Delegate.CreateDelegate(typeof(DM), se.Method as MethodInfo);
var timer = 1000000;
var k = 123;
CodeTimer.ShowHeader();
CodeTimer.TimeLine("原生", timer, n => TestMath(k));
CodeTimer.TimeLine("动态", timer, n => se.Invoke(k));
CodeTimer.TimeLine("动态2", timer, n => fun(k));
}
public static Double TestMath(Double k)
{
//var bts = File.ReadAllBytes(Assembly.GetExecutingAssembly().Location);
return Math.Sin(k) * Math.Log10(k) * Math.Exp(k);
}
delegate Object DM(Double k);
static void Test8()
{
}
class FMTest
{
static Int32 k1 = 1000;
static Int32 k2 = 1024;
static Int32 k3 = 230942;
private Double _Value;
/// <summary>属性说明</summary>
public Double Value
{
get
{
//Thread.Sleep(10);
return _Value * k1 * k1 / k2 / k2 * Math.Log10(k3);
}
set { _Value = value; }
}
private static Double _Value2;
/// <summary>属性说明</summary>
public static Double Value2
{
get
{
//Thread.Sleep(10);
return _Value2 * k1 * k1 / k2 / k2 * Math.Log10(k3);
}
set { _Value2 = value; }
}
}
static void Test9()
{
var tb = Menu.Meta.Table.DataTable;
var table = tb.Clone() as IDataTable;
table = table.CopyAllFrom(tb);
// 添加两个字段
var fi = table.CreateColumn();
fi.Name = "LastUpdate";
fi.DataType = typeof(DateTime);
table.Columns.Add(fi);
fi = table.CreateColumn();
fi.Name = "LastAsync";
fi.DataType = typeof(DateTime);
table.Columns.Add(fi);
var dal = DAL.Create("Common99");
// 检查架构
dal.SetTables(table);
var sl = new SyncSlave();
dal.CreateOperate(table.Name);
var mt = new SyncMaster();
mt.Facotry = Menu.Meta.Factory;
var sm = new SyncManager();
sm.Slave = sl;
sm.Master = mt;
sm.Start();
}
}
}
|