Commit 0c4ca99f by liulongfei

1. 手动裁切

2.  TCP 监听器
parent f6e69ae5
...@@ -111,6 +111,7 @@ namespace VIZ.Framework.Common ...@@ -111,6 +111,7 @@ namespace VIZ.Framework.Common
return; return;
} }
// 处理数据
this.triggerEvent(video, data); this.triggerEvent(video, data);
this.VideoFrame = null; this.VideoFrame = null;
this.DataFrame = null; this.DataFrame = null;
......
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net.Sockets;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
...@@ -25,5 +26,10 @@ namespace VIZ.Framework.Connection ...@@ -25,5 +26,10 @@ namespace VIZ.Framework.Connection
/// 串口连接 /// 串口连接
/// </summary> /// </summary>
public static SerialPortConnection SerialPortConnection { get; set; } public static SerialPortConnection SerialPortConnection { get; set; }
/// <summary>
/// TCP连接监听器
/// </summary>
public static Dictionary<string, TcpConnectionListener> TcpConnectionListeners { get; private set; } = new Dictionary<string, TcpConnectionListener>();
} }
} }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace VIZ.Framework.Connection
{
/// <summary>
/// 连接监听基类
/// </summary>
public abstract class ConnListenerBase : IDisposable
{
/// <summary>
/// 键
/// </summary>
public string Key { get; protected set; }
/// <summary>
/// 销毁
/// </summary>
public abstract void Dispose();
}
}
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using log4net;
namespace VIZ.Framework.Connection
{
/// <summary>
/// TCP连接监听
/// </summary>
public abstract class TcpConnectionListener : ConnListenerBase
{
/// <summary>
/// 日志
/// </summary>
private static readonly ILog log = LogManager.GetLogger(typeof(TcpConnectionListener));
/// <summary>
/// TCP监听
/// </summary>
/// <param name="ip">IP地址</param>
/// <param name="port">端口</param>
public TcpConnectionListener(string ip, int port)
{
this.IP = ip;
this.Port = port;
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(ip), port);
this.Listener = new TcpListener(endPoint);
}
/// <summary>
/// IP地址
/// </summary>
public string IP { get; private set; }
/// <summary>
/// 端口
/// </summary>
public int Port { get; set; }
/// <summary>
/// TCP监听
/// </summary>
public TcpListener Listener { get; private set; }
/// <summary>
/// 连接的客户端终结点
/// </summary>
public List<TcpEndpointManager> EndpointManagers { get; private set; } = new List<TcpEndpointManager>();
/// <summary>
/// 开始监听
/// </summary>
public void Start()
{
this.Listener.Start();
this.Listener.BeginAcceptTcpClient(new AsyncCallback(this.acceptTcpclient), this.Listener);
}
/// <summary>
/// 销毁
/// </summary>
public override void Dispose()
{
this.EndpointManagers?.ForEach(p => p.Dispose());
this.Listener?.Stop();
}
/// <summary>
/// 创建终结点管理器
/// </summary>
/// <param name="tcpClient">TCP客户端</param>
/// <returns>TCP终结点管理器</returns>
protected abstract TcpEndpointManager CreateEndpointManager(TcpClient tcpClient);
/// <summary>
/// 接收到TCP连接请求
/// </summary>
private void acceptTcpclient(IAsyncResult result)
{
//获取监听事件,处理客户端请求
TcpListener listerer = (TcpListener)result.AsyncState;
TcpClient client = listerer.EndAcceptTcpClient(result);
TcpEndpointManager manager = this.CreateEndpointManager(client);
lock (this.EndpointManagers)
{
this.EndpointManagers.Add(manager);
}
manager.StartRecvMessage();
Debug.WriteLine($"接收到TCP客户端连接,当前连接数:{this.EndpointManagers.Count}");
this.Listener.BeginAcceptTcpClient(new AsyncCallback(this.acceptTcpclient), this.Listener);
}
}
}
...@@ -34,14 +34,25 @@ namespace VIZ.Framework.Connection ...@@ -34,14 +34,25 @@ namespace VIZ.Framework.Connection
/// TCP终结点管理器 /// TCP终结点管理器
/// </summary> /// </summary>
/// <param name="key">网络终结点键</param> /// <param name="key">网络终结点键</param>
/// <param name="ip">IP</param>
/// <param name="port">端口</param>
public TcpEndpointManager(string key) public TcpEndpointManager(string key)
{ {
this.Key = key; this.Key = key;
} }
/// <summary> /// <summary>
/// TCP终结点管理器
/// </summary>
/// <param name="key">网络终结点键</param>
/// <param name="tcpClient">TCP客户端</param>
public TcpEndpointManager(string key, TcpClient tcpClient)
{
this.Key = key;
this.TcpClient = tcpClient;
this.LocalIP = this.TcpClient.Client.LocalEndPoint.ToString();
this.NetworkStream = this.TcpClient.GetStream();
}
/// <summary>
/// 网络终结点键 /// 网络终结点键
/// </summary> /// </summary>
public string Key { get; private set; } public string Key { get; private set; }
...@@ -108,6 +119,9 @@ namespace VIZ.Framework.Connection ...@@ -108,6 +119,9 @@ namespace VIZ.Framework.Connection
/// <param name="remotePort">远程端口</param> /// <param name="remotePort">远程端口</param>
public void Connect(string remoteIP, int remotePort) public void Connect(string remoteIP, int remotePort)
{ {
if (this.TcpClient != null)
throw new Exception("tcp client is already exists");
this.RemoteIP = remoteIP; this.RemoteIP = remoteIP;
this.RemotePort = remotePort; this.RemotePort = remotePort;
......
...@@ -42,7 +42,7 @@ namespace VIZ.Framework.Connection ...@@ -42,7 +42,7 @@ namespace VIZ.Framework.Connection
str = this.lastCommandString + str; str = this.lastCommandString + str;
this.lastCommandString = null; this.lastCommandString = null;
string[] commands = str.Split(new char[] { '\r', '\n' }); string[] commands = str.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
// 最后一个命令是否完整 // 最后一个命令是否完整
bool isLastCommandComplete = str.EndsWith("\n") || str.EndsWith("\r"); bool isLastCommandComplete = str.EndsWith("\n") || str.EndsWith("\r");
......
...@@ -70,10 +70,12 @@ ...@@ -70,10 +70,12 @@
<Compile Include="ConnectionManager.cs" /> <Compile Include="ConnectionManager.cs" />
<Compile Include="Core\ConnBitLengthAttribute.cs" /> <Compile Include="Core\ConnBitLengthAttribute.cs" />
<Compile Include="Core\ConnInfoBase.cs" /> <Compile Include="Core\ConnInfoBase.cs" />
<Compile Include="Core\ConnListenerBase.cs" />
<Compile Include="Core\ConnMessageBase.cs" /> <Compile Include="Core\ConnMessageBase.cs" />
<Compile Include="Core\ConnPackageInfo.cs" /> <Compile Include="Core\ConnPackageInfo.cs" />
<Compile Include="Core\ConnPackageProviderAttribute.cs" /> <Compile Include="Core\ConnPackageProviderAttribute.cs" />
<Compile Include="Core\ConnSendMessage.cs" /> <Compile Include="Core\ConnSendMessage.cs" />
<Compile Include="Listener\TCP\TcpConnectionListener.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Protocol\SerialPort\SerialPortEndpointManagerExpand.cs" /> <Compile Include="Protocol\SerialPort\SerialPortEndpointManagerExpand.cs" />
<Compile Include="Protocol\SerialPort\SerialPortEndpointManager.cs" /> <Compile Include="Protocol\SerialPort\SerialPortEndpointManager.cs" />
......
...@@ -45,9 +45,10 @@ namespace VIZ.Framework.Core ...@@ -45,9 +45,10 @@ namespace VIZ.Framework.Core
/// </summary> /// </summary>
/// <param name="profileName">3D鼠标配置名称</param> /// <param name="profileName">3D鼠标配置名称</param>
/// <param name="maxX">X偏移量最大值</param> /// <param name="maxX">X偏移量最大值</param>
public static void Init(string profileName, double maxX) /// <param name="smoothCoefficient">平滑系数</param>
public static void Init(string profileName, double maxX, double smoothCoefficient)
{ {
Navigation3DModel = new Navigation3DModel(); Navigation3DModel = new Navigation3DModel(smoothCoefficient);
Navigation3DModel.MaxX = maxX; Navigation3DModel.MaxX = maxX;
Navigation3D = new Navigation3D(Navigation3DModel); Navigation3D = new Navigation3D(Navigation3DModel);
......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace VIZ.Framework.Core
{
/// <summary>
/// 3D鼠标映射
/// </summary>
public class Navigation3DMapping
{
/// <summary>
/// 编号
/// </summary>
public int ID { get; set; }
/// <summary>
/// 最小值
/// </summary>
public int MinValue { get; set; }
/// <summary>
/// 最大值
/// </summary>
public int MaxValue { get; set; }
/// <summary>
/// 映射值
/// </summary>
public int MappingValue { get; set; }
}
}
...@@ -16,9 +16,10 @@ namespace VIZ.Framework.Core ...@@ -16,9 +16,10 @@ namespace VIZ.Framework.Core
/// <summary> /// <summary>
/// 3D鼠标模型 /// 3D鼠标模型
/// </summary> /// </summary>
public Navigation3DModel() public Navigation3DModel(double smoothCoefficient)
{ {
this.Smooth.Init(0);
this.Smooth.Init(0, this.SmoothCoefficient);
} }
/// <summary> /// <summary>
...@@ -52,6 +53,11 @@ namespace VIZ.Framework.Core ...@@ -52,6 +53,11 @@ namespace VIZ.Framework.Core
public TimeSpan RenderTime { get; set; } = TimeSpan.Zero; public TimeSpan RenderTime { get; set; } = TimeSpan.Zero;
/// <summary> /// <summary>
/// 平滑系数
/// </summary>
public double SmoothCoefficient { get; set; } = 0.015;
/// <summary>
/// 平滑处理器 /// 平滑处理器
/// </summary> /// </summary>
private Navigation3DSmooth Smooth = new Navigation3DSmooth(); private Navigation3DSmooth Smooth = new Navigation3DSmooth();
...@@ -85,7 +91,7 @@ namespace VIZ.Framework.Core ...@@ -85,7 +91,7 @@ namespace VIZ.Framework.Core
0, 0, 1, 0, 0, 0, 1, 0,
-x, 0, 5550, 1); -x, 0, 5550, 1);
this.Smooth.Init(x); this.Smooth.Init(x, 0, this.SmoothCoefficient);
} }
} }
} }
...@@ -34,7 +34,7 @@ namespace VIZ.Framework.Core ...@@ -34,7 +34,7 @@ namespace VIZ.Framework.Core
/// <param name="min_cutoff">终止值</param> /// <param name="min_cutoff">终止值</param>
/// <param name="beta"></param> /// <param name="beta"></param>
/// <param name="d_cutoff"></param> /// <param name="d_cutoff"></param>
public void Init(double x0, double dx0 = 0, double min_cutoff = 0.02, double beta = 0, double d_cutoff = 1) public void Init(double x0, double dx0 = 0, double min_cutoff = 0.015, double beta = 0, double d_cutoff = 1)
{ {
this.min_cutoff = min_cutoff; this.min_cutoff = min_cutoff;
this.beta = beta; this.beta = beta;
......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace VIZ.Framework.Core
{
/// <summary>
/// 3D鼠标TCP管理器
/// </summary>
public static class Navigation3DTcpManager
{
/// <summary>
/// 命令
/// </summary>
public static string Command { get; set; }
/// <summary>
/// 值
/// </summary>
public static int Value { get; set; }
/// <summary>
/// 映射值
/// </summary>
public static int MappingValue { get; set; }
}
}
...@@ -131,7 +131,9 @@ ...@@ -131,7 +131,9 @@
<Compile Include="Expand\Monitor\System\Info\SystemMonitorInfo.cs" /> <Compile Include="Expand\Monitor\System\Info\SystemMonitorInfo.cs" />
<Compile Include="Expand\Monitor\System\SystemMonitorTask.cs" /> <Compile Include="Expand\Monitor\System\SystemMonitorTask.cs" />
<Compile Include="Core\Net\NetHelper.cs" /> <Compile Include="Core\Net\NetHelper.cs" />
<Compile Include="Expand\ThreeDMouse\Navigation3DMapping.cs" />
<Compile Include="Expand\ThreeDMouse\Navigation3DSmoothInner.cs" /> <Compile Include="Expand\ThreeDMouse\Navigation3DSmoothInner.cs" />
<Compile Include="Expand\ThreeDMouse\Navigation3DTcpManager.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Expand\SharpDx\RawRectangleFExpand.cs" /> <Compile Include="Expand\SharpDx\RawRectangleFExpand.cs" />
<Compile Include="Expand\SharpDx\SharpDxColorHelper.cs" /> <Compile Include="Expand\SharpDx\SharpDxColorHelper.cs" />
......
...@@ -28,6 +28,11 @@ namespace VIZ.Framework.Module ...@@ -28,6 +28,11 @@ namespace VIZ.Framework.Module
public override string Detail { get; } = "应用程序启动 -- 3D鼠标"; public override string Detail { get; } = "应用程序启动 -- 3D鼠标";
/// <summary> /// <summary>
/// 3D鼠标平滑系数
/// </summary>
private static readonly double APPLICATION_3D_MOUSE_SMOOTH_COEFFICIENT = ApplicationDomain.IniStorage.GetValue<ApplicationConfig, double>(p => p.APPLICATION_3D_MOUSE_SMOOTH_COEFFICIENT);
/// <summary>
/// 执行启动 /// 执行启动
/// </summary> /// </summary>
/// <param name="context">应用程序启动上下文</param> /// <param name="context">应用程序启动上下文</param>
...@@ -37,7 +42,7 @@ namespace VIZ.Framework.Module ...@@ -37,7 +42,7 @@ namespace VIZ.Framework.Module
string profileName = ApplicationDomain.IniStorage.GetValue<Navigation3DConfig, string>(p => p.NAVIGATION3D_PROFILE_NAME); string profileName = ApplicationDomain.IniStorage.GetValue<Navigation3DConfig, string>(p => p.NAVIGATION3D_PROFILE_NAME);
double maxX = ApplicationDomain.IniStorage.GetValue<Navigation3DConfig, double>(p => p.NAVIGATION3D_MAX_VALUE); double maxX = ApplicationDomain.IniStorage.GetValue<Navigation3DConfig, double>(p => p.NAVIGATION3D_MAX_VALUE);
Navigation3DManager.Init(profileName, maxX); Navigation3DManager.Init(profileName, maxX, APPLICATION_3D_MOUSE_SMOOTH_COEFFICIENT);
return true; return true;
} }
......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VIZ.Framework.Connection;
using log4net;
using System.Diagnostics;
using VIZ.Framework.Core;
namespace VIZ.Framework.Module
{
/// <summary>
/// 3D鼠标命令行解释器
/// </summary>
public class Navigation3DCommandProvider : ConnCommandLinePackageProvider
{
/// <summary>
/// 日志
/// </summary>
private readonly static ILog log = LogManager.GetLogger(typeof(Navigation3DCommandProvider));
/// <summary>
/// 3D鼠标命令行解释器
/// </summary>
/// <param name="mappings">映射集合</param>
public Navigation3DCommandProvider(List<Navigation3DMapping> mappings)
{
this.Mappings = mappings;
}
/// <summary>
/// 映射值
/// </summary>
public List<Navigation3DMapping> Mappings { get; private set; }
/// <summary>
/// 执行命令
/// </summary>
/// <param name="info">命令行信息</param>
protected override void Execute(ConnCommandLineInfo info)
{
if (this.Mappings == null || this.Mappings.Count == 0)
return;
string[] args = info.Command.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
string cmd = null;
int value1 = 0;
int value2 = 0;
if (args.Length >= 1)
{
cmd = args[0];
}
if (args.Length >= 2)
{
int.TryParse(args[1], out value1);
}
if (args.Length >= 3)
{
int.TryParse(args[2], out value2);
}
int temp = Math.Abs(value2);
Navigation3DMapping first = this.Mappings.FirstOrDefault();
Navigation3DMapping last = this.Mappings.LastOrDefault();
Navigation3DTcpManager.Command = cmd;
if (temp == 0 || temp < first.MinValue)
{
Navigation3DTcpManager.Value = 0;
Navigation3DTcpManager.MappingValue = 0;
return;
}
if (temp >= last.MaxValue)
{
Navigation3DTcpManager.Value = value2;
Navigation3DTcpManager.MappingValue = (value2 > 0 ? 1 : -1) * last.MappingValue;
return;
}
Navigation3DMapping mapping = this.Mappings.FirstOrDefault(p => temp >= p.MinValue && temp < p.MaxValue);
if (mapping == null)
return;
Navigation3DTcpManager.Value = value2;
Navigation3DTcpManager.MappingValue = (value2 > 0 ? 1 : -1) * mapping.MappingValue;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using VIZ.Framework.Connection;
using log4net;
using System.Runtime.Remoting.Messaging;
using VIZ.Framework.Core;
namespace VIZ.Framework.Module
{
/// <summary>
/// 3D鼠标TCP监听器
/// </summary>
public class Navigation3DTcpListener : TcpConnectionListener
{
/// <summary>
/// 日志
/// </summary>
private static readonly ILog log = LogManager.GetLogger(typeof(Navigation3DTcpListener));
/// <summary>
/// 监听器键
/// </summary>
public const string LISTENER_KEY = "Navigation3DTcpListener";
/// <summary>
/// 监听器客户端键
/// </summary>
public const string LISTENER_CLIENT_KEY = "Navigation3DTcpListener_CLIENT";
/// <summary>
/// 3D鼠标TCP监听器
/// </summary>
/// <param name="ip">IP地址</param>
/// <param name="port">端口</param>
/// <param name="mappings">映射集合</param>
public Navigation3DTcpListener(string ip, int port, List<Navigation3DMapping> mappings) : base(ip, port)
{
this.Key = LISTENER_KEY;
this.Mappings = mappings;
}
/// <summary>
/// 映射集合
/// </summary>
public List<Navigation3DMapping> Mappings { get; private set; }
/// <summary>
/// 创建终结点管理器
/// </summary>
/// <param name="tcpClient">TCP客户端</param>
/// <returns>TCP终结点管理器</returns>
protected override TcpEndpointManager CreateEndpointManager(TcpClient tcpClient)
{
TcpEndpointManager manager = new TcpEndpointManager(LISTENER_CLIENT_KEY, tcpClient);
manager.PackageProvider = new Navigation3DCommandProvider(this.Mappings);
return manager;
}
}
}
...@@ -107,6 +107,8 @@ ...@@ -107,6 +107,8 @@
<Compile Include="Setup\Provider\Setup\AppSetup_LogInit.cs" /> <Compile Include="Setup\Provider\Setup\AppSetup_LogInit.cs" />
<Compile Include="Setup\Provider\Load\AppSetup_Monitor.cs" /> <Compile Include="Setup\Provider\Load\AppSetup_Monitor.cs" />
<Compile Include="Setup\Provider\Setup\AppSetup_Single.cs" /> <Compile Include="Setup\Provider\Setup\AppSetup_Single.cs" />
<Compile Include="ThreeDMouse\TCP\Navigation3DCommandProvider.cs" />
<Compile Include="ThreeDMouse\TCP\Navigation3DTcpListener.cs" />
<EmbeddedResource Include="Properties\Resources.resx"> <EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator> <Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput> <LastGenOutput>Resources.Designer.cs</LastGenOutput>
......
...@@ -16,5 +16,23 @@ namespace VIZ.Framework.Storage ...@@ -16,5 +16,23 @@ namespace VIZ.Framework.Storage
/// </summary> /// </summary>
[Ini(Section = "Application", DefaultValue = "False", Type = typeof(bool))] [Ini(Section = "Application", DefaultValue = "False", Type = typeof(bool))]
public string APPLICATION_IS_DEBUG { get; set; } public string APPLICATION_IS_DEBUG { get; set; }
/// <summary>
/// 3D鼠标平滑系数
/// </summary>
[Ini(Section = "Application", DefaultValue = "0.015", Type = typeof(bool))]
public string APPLICATION_3D_MOUSE_SMOOTH_COEFFICIENT { get; set; }
/// <summary>
/// 3D鼠标TCP IP
/// </summary>
[Ini(Section = "Application", DefaultValue = "127.0.0.1", Type = typeof(string))]
public string APPLICATION_3D_MOUSE_TCP_IP { get; set; }
/// <summary>
/// 3D鼠标TCP 端口
/// </summary>
[Ini(Section = "Application", DefaultValue = "8201", Type = typeof(int))]
public string APPLICATION_3D_MOUSE_TCP_PORT { get; set; }
} }
} }
...@@ -89,5 +89,6 @@ ...@@ -89,5 +89,6 @@
<ItemGroup> <ItemGroup>
<None Include="packages.config" /> <None Include="packages.config" />
</ItemGroup> </ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project> </Project>
\ No newline at end of file
...@@ -44,8 +44,18 @@ namespace VIZ.Framework.UnitTest ...@@ -44,8 +44,18 @@ namespace VIZ.Framework.UnitTest
{ {
// 16612406883518854 // 16612406883518854
long begin = new DateTime(1970, 1, 1, 8, 0, 0).Ticks; long begin = new DateTime(1970, 1, 1, 8, 0, 0).Ticks;
DateTime time = new DateTime(16612406883518854 + begin); // 16613510251790000
// 16612406883518854
// 16615034160040000
// 16615032962820000
// 2022-08-26 16-43-36.0040000
// 2022-08-26 16:43:36,094
//
// 2022-08-26 16-41-36.2820000
// 2022-08-26 16:41:36,370
DateTime time = new DateTime(16615032962820000 + begin);
string str = time.ToString("yyyy-MM-dd HH-mm-ss.fffffff");
} }
} }
} }
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