diff --git a/App.config b/App.config index 1bcae48..b9492c7 100644 --- a/App.config +++ b/App.config @@ -3,7 +3,8 @@ - - + + + \ No newline at end of file diff --git a/Hander/MultiUserHandle.cs b/Hander/MultiUserHandle.cs index 60a84e1..1f17688 100644 --- a/Hander/MultiUserHandle.cs +++ b/Hander/MultiUserHandle.cs @@ -12,11 +12,12 @@ namespace OnlineUserPool.Hander { public class MultiUserHandle : IHandle { - private int _index = -1; + private int _index = 0; //3600*3; private MultiUserModel model; private TurfHelper _turfHelper; - private int _routeId = 4297; //1660; - private int _size = 200; + private int _routeId = 1255; //5593; //1660; + private int _size = 50; + private int _competitionid = 0;//35; public MultiUserHandle() { Init(); @@ -24,11 +25,23 @@ namespace OnlineUserPool.Hander public async void Init() { + #region 环台湾 + //_routeId = 5593; + //_competitionid = 35; + //_index = 3600 * 3; + #endregion + + + #region sis + //_routeId = 5710; + //_competitionid = 37; + #endregion + model = await BaseApi.GetAsync($"/Map/sss?routeId={ _routeId }&size={ _size }"); double i = 0; foreach (var item in model.users) { - i += 0.0005; + i += 0.0002; item.Distance = i; } _turfHelper = new TurfHelper(model.route.List.Select(d => d.Point)); @@ -68,7 +81,7 @@ namespace OnlineUserPool.Hander Speed = item.Speed, WeightKg = 0, PreDistance = preDistance, - Competitionid = 30 + Competitionid = _competitionid }; msgModels.Add(info); } diff --git a/Services/TcpService1.cs b/Services/TcpService1.cs index ee79564..3ec0e53 100644 --- a/Services/TcpService1.cs +++ b/Services/TcpService1.cs @@ -1,5 +1,6 @@ using NetCoreServer; using OnlineUserPool.Model; +using OnlineUserPool.Unility; using Serilog; using System; using System.Collections.Generic; @@ -24,7 +25,7 @@ namespace OnlineUserPool.Services { //throw new NotImplementedException(); //_action = action; - _server = new PfTcpServer(IPAddress.Any, 11001, (ip, model)=> { + _server = new PfTcpServer(IPAddress.Any, ConfigHelp.TcpPort, (ip, model)=> { action(ip, model, this); }); //_server.OptionNoDelay = true; @@ -40,6 +41,7 @@ namespace OnlineUserPool.Services return; } _server.Multicast(dgram); + } class PfTcpServer : TcpServer @@ -81,26 +83,44 @@ namespace OnlineUserPool.Services //base.OnDisconnected(); Debug.WriteLine("Tcp断开连接"); } - + private string temp = ""; protected override void OnReceived(byte[] buffer, long offset, long size) { //base.OnReceived(buffer, offset, size); - var returnData = Encoding.UTF8.GetString(buffer, (int)offset, (int)size); + //Debug.WriteLine(returnData); //Log.Information(returnData + "\r\n"); + string returnData = ""; try { + returnData = Encoding.UTF8.GetString(buffer, (int)offset, (int)size); //var msg = Newtonsoft.Json.JsonConvert.DeserializeObject(returnData); //var ipEndPoint = IPEndPoint.Parse(this.Socket.RemoteEndPoint.ToString()); //_action(ipEndPoint, msg); - - foreach (var item in returnData.Split(new string[] { "}" }, StringSplitOptions.RemoveEmptyEntries)) + foreach (var item in returnData) { - //if (string.IsNullOrWhiteSpace(item)) continue; - var msg = Newtonsoft.Json.JsonConvert.DeserializeObject(item + "}"); - var ipEndPoint = IPEndPoint.Parse(this.Socket.RemoteEndPoint.ToString()); - _action(ipEndPoint, msg); + if(item == '}') + { + temp += item; + var msg = Newtonsoft.Json.JsonConvert.DeserializeObject(temp); + var ipEndPoint = IPEndPoint.Parse(this.Socket.RemoteEndPoint.ToString()); + _action(ipEndPoint, msg); + + temp = ""; + } + else + { + temp += item; + } } + + //foreach (var item in returnData.Split(new string[] { "}" }, StringSplitOptions.RemoveEmptyEntries)) + //{ + // //if (string.IsNullOrWhiteSpace(item)) continue; + // var msg = Newtonsoft.Json.JsonConvert.DeserializeObject(item + "}"); + // var ipEndPoint = IPEndPoint.Parse(this.Socket.RemoteEndPoint.ToString()); + // _action(ipEndPoint, msg); + //} } catch (Exception ex) { diff --git a/Services/UdpService.cs b/Services/UdpService.cs index dccba6e..55eb033 100644 --- a/Services/UdpService.cs +++ b/Services/UdpService.cs @@ -23,7 +23,7 @@ namespace OnlineUserPool.Services public void RunServer(Action action) { - udpServer = new UdpClient(ConfigHelp.Port); + udpServer = new UdpClient(ConfigHelp.UdpPort); uint IOC_IN = 0x80000000; uint IOC_VENDOR = 0x18000000; uint SIO_UDP_CONNRESET = IOC_IN | IOC_VENDOR | 12; diff --git a/Unility/ConfigHelp.cs b/Unility/ConfigHelp.cs index a6010c7..f8aa254 100644 --- a/Unility/ConfigHelp.cs +++ b/Unility/ConfigHelp.cs @@ -18,7 +18,9 @@ namespace OnlineUserPool.Unility //public static string Ip { get; private set; } - public static int Port { get; private set; } + public static int UdpPort { get; private set; } + + public static int TcpPort { get; private set; } static ConfigHelp() @@ -27,7 +29,10 @@ namespace OnlineUserPool.Unility //Top = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["Top"]); ShowVirtualUser = bool.Parse(System.Configuration.ConfigurationManager.AppSettings["ShowVirtualUser"]); //Ip = System.Configuration.ConfigurationManager.AppSettings["Ip"]; - Port = int.Parse(System.Configuration.ConfigurationManager.AppSettings["Port"]); + UdpPort = int.Parse(System.Configuration.ConfigurationManager.AppSettings["Port"]); + + TcpPort = int.Parse(System.Configuration.ConfigurationManager.AppSettings["TcpPort"]); + } } } diff --git a/ViewModels/MainWindowViewModel.cs b/ViewModels/MainWindowViewModel.cs index 9468500..3ba82fb 100644 --- a/ViewModels/MainWindowViewModel.cs +++ b/ViewModels/MainWindowViewModel.cs @@ -54,15 +54,16 @@ namespace OnlineUserPool.ViewModels //private IService _udpService; public MainWindowViewModel() { - Title = $"{ IPAddress.Any }:{ ConfigHelp.Port }"; + Title = $"{ IPAddress.Any }:{ ConfigHelp.UdpPort }"; dispatcher = Dispatcher.CurrentDispatcher; //Customers.Add("suntao"); WriteLine(DateTime.Now.ToShortDateString()); LogHelper.Init(); - mapRecordRankingHander = new MapRecordRankingHander(); - Log.Information($"初始化连接,当前地址:{ IPAddress.Any }:{ConfigHelp.Port}"); + mapRecordRankingHander = new MultiUserHandle();//new MapRecordRankingHander(); + //mapRecordRankingHander = new MultiUserHandle(); + Log.Information($"初始化连接,当前地址:{ IPAddress.Any }:{ConfigHelp.UdpPort}"); new TcpService1().RunServer(ReceivedData); //var tet = new System.Collections.Concurrent.ConcurrentBag(); @@ -120,6 +121,25 @@ namespace OnlineUserPool.ViewModels { try { + if(!receiveMes.Any()) + { + dispatcher.Invoke(() => + { + Customers.Clear(); + + //删除已经发送的数据 + //receiveMes.Clear(); + //移除5钟内连接不上的客户端 + Clients.ToList().ForEach(item => + { + if (item.Expire) + { + Clients.Remove(item); + } + }); + }); + return; + } lock (locker) { var list = CloneJson(receiveMes).ToList(); @@ -185,7 +205,12 @@ namespace OnlineUserPool.ViewModels private void SendMessage(Collection clients, List msgModels) { + if (!clients.Any()) + { + return; + } var clients1 = clients.ToList(); + var list = CloneJson>(msgModels); foreach (var item in list) { @@ -208,14 +233,19 @@ namespace OnlineUserPool.ViewModels m.Saved, })); var data = Encoding.ASCII.GetBytes(jsonString); - SendDataSize = (data.Length/1000D).ToString() +"KB"; + //SendDataSize = (data.Length/1000D).ToString() +"KB"; var strV1 = string.Join("|", list.Select(m => m.ToString(1))); + if (!string.IsNullOrWhiteSpace(strV1)) + { + strV1 += "|"; + } + //System.IO.File.AppendAllText(System.Environment.CurrentDirectory + "要发送的数据.txt", strV1.Trim().Replace("\0", "") + "\r\n", Encoding.UTF8); WriteLine(strV1); WriteLine(strV1.Length.ToString()); var dataV1 = Encoding.ASCII.GetBytes(strV1); - - SendDataSize += $"\tV1:{ (strV1.Length/1000D) }KB"; + var dataV1ForUdp = strV1.Last() == '|' ? Encoding.ASCII.GetBytes(strV1.Substring(0, strV1.Length - 1)) : dataV1; + SendDataSize = $"\t单客户端数据包V1:{ (strV1.Length/1000D) }KB,一共占用带宽:{ strV1.Length / 1000D * clients1.Count }KB"; foreach (var item in clients1) { @@ -225,9 +255,14 @@ namespace OnlineUserPool.ViewModels { continue; } + if((item.Service is UdpService) ==false) + { + continue; + } if (item.V == 1) - { - item.Service.Send(dataV1, dataV1.Length, item.IPEndPoint); + { + + item.Service.Send(dataV1ForUdp, dataV1ForUdp.Length, item.IPEndPoint); } else { @@ -240,6 +275,11 @@ namespace OnlineUserPool.ViewModels Log.Error($"{ item.IPEndPoint.ToString() }:{ e.Message }\r\n{ e.StackTrace }"); } } + var tcpClient = clients1.Where(c => c.Service is TcpService1).FirstOrDefault(); + if(tcpClient != null) + { + tcpClient.Service.Send(dataV1, dataV1.Length, null); + } } diff --git a/Views/MainWindow.xaml b/Views/MainWindow.xaml index f26b8a0..2a1d2d5 100644 --- a/Views/MainWindow.xaml +++ b/Views/MainWindow.xaml @@ -32,7 +32,7 @@ - +