增加模拟用户功能

This commit is contained in:
suntao 2021-06-24 09:20:01 +08:00
parent 0e931a3ac9
commit b9bb485a2d
7 changed files with 107 additions and 28 deletions

View File

@ -3,7 +3,8 @@
<appSettings> <appSettings>
<add key="Host" value="http://192.168.0.97:5082/"/> <add key="Host" value="http://192.168.0.97:5082/"/>
<add key="Top" value="0"/> <add key="Top" value="0"/>
<add key="ShowVirtualUser" value="true"/> <add key="ShowVirtualUser" value="false"/>
<add key="Port" value="11000"/> <add key="Port" value="21000"/>
<add key="TcpPort" value="21001"/>
</appSettings> </appSettings>
</configuration> </configuration>

View File

@ -12,11 +12,12 @@ namespace OnlineUserPool.Hander
{ {
public class MultiUserHandle : IHandle public class MultiUserHandle : IHandle
{ {
private int _index = -1; private int _index = 0; //3600*3;
private MultiUserModel model; private MultiUserModel model;
private TurfHelper _turfHelper; private TurfHelper _turfHelper;
private int _routeId = 4297; //1660; private int _routeId = 1255; //5593; //1660;
private int _size = 200; private int _size = 50;
private int _competitionid = 0;//35;
public MultiUserHandle() public MultiUserHandle()
{ {
Init(); Init();
@ -24,11 +25,23 @@ namespace OnlineUserPool.Hander
public async void Init() public async void Init()
{ {
#region
//_routeId = 5593;
//_competitionid = 35;
//_index = 3600 * 3;
#endregion
#region sis
//_routeId = 5710;
//_competitionid = 37;
#endregion
model = await BaseApi.GetAsync<MultiUserModel>($"/Map/sss?routeId={ _routeId }&size={ _size }"); model = await BaseApi.GetAsync<MultiUserModel>($"/Map/sss?routeId={ _routeId }&size={ _size }");
double i = 0; double i = 0;
foreach (var item in model.users) foreach (var item in model.users)
{ {
i += 0.0005; i += 0.0002;
item.Distance = i; item.Distance = i;
} }
_turfHelper = new TurfHelper(model.route.List.Select(d => d.Point)); _turfHelper = new TurfHelper(model.route.List.Select(d => d.Point));
@ -68,7 +81,7 @@ namespace OnlineUserPool.Hander
Speed = item.Speed, Speed = item.Speed,
WeightKg = 0, WeightKg = 0,
PreDistance = preDistance, PreDistance = preDistance,
Competitionid = 30 Competitionid = _competitionid
}; };
msgModels.Add(info); msgModels.Add(info);
} }

View File

@ -1,5 +1,6 @@
using NetCoreServer; using NetCoreServer;
using OnlineUserPool.Model; using OnlineUserPool.Model;
using OnlineUserPool.Unility;
using Serilog; using Serilog;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -24,7 +25,7 @@ namespace OnlineUserPool.Services
{ {
//throw new NotImplementedException(); //throw new NotImplementedException();
//_action = action; //_action = action;
_server = new PfTcpServer(IPAddress.Any, 11001, (ip, model)=> { _server = new PfTcpServer(IPAddress.Any, ConfigHelp.TcpPort, (ip, model)=> {
action(ip, model, this); action(ip, model, this);
}); });
//_server.OptionNoDelay = true; //_server.OptionNoDelay = true;
@ -40,6 +41,7 @@ namespace OnlineUserPool.Services
return; return;
} }
_server.Multicast(dgram); _server.Multicast(dgram);
} }
class PfTcpServer : TcpServer class PfTcpServer : TcpServer
@ -81,26 +83,44 @@ namespace OnlineUserPool.Services
//base.OnDisconnected(); //base.OnDisconnected();
Debug.WriteLine("Tcp断开连接"); Debug.WriteLine("Tcp断开连接");
} }
private string temp = "";
protected override void OnReceived(byte[] buffer, long offset, long size) protected override void OnReceived(byte[] buffer, long offset, long size)
{ {
//base.OnReceived(buffer, offset, size); //base.OnReceived(buffer, offset, size);
var returnData = Encoding.UTF8.GetString(buffer, (int)offset, (int)size);
//Debug.WriteLine(returnData); //Debug.WriteLine(returnData);
//Log.Information(returnData + "\r\n"); //Log.Information(returnData + "\r\n");
string returnData = "";
try try
{ {
returnData = Encoding.UTF8.GetString(buffer, (int)offset, (int)size);
//var msg = Newtonsoft.Json.JsonConvert.DeserializeObject<MsgModel>(returnData); //var msg = Newtonsoft.Json.JsonConvert.DeserializeObject<MsgModel>(returnData);
//var ipEndPoint = IPEndPoint.Parse(this.Socket.RemoteEndPoint.ToString()); //var ipEndPoint = IPEndPoint.Parse(this.Socket.RemoteEndPoint.ToString());
//_action(ipEndPoint, msg); //_action(ipEndPoint, msg);
foreach (var item in returnData)
foreach (var item in returnData.Split(new string[] { "}" }, StringSplitOptions.RemoveEmptyEntries))
{ {
//if (string.IsNullOrWhiteSpace(item)) continue; if(item == '}')
var msg = Newtonsoft.Json.JsonConvert.DeserializeObject<MsgModel>(item + "}"); {
var ipEndPoint = IPEndPoint.Parse(this.Socket.RemoteEndPoint.ToString()); temp += item;
_action(ipEndPoint, msg); var msg = Newtonsoft.Json.JsonConvert.DeserializeObject<MsgModel>(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<MsgModel>(item + "}");
// var ipEndPoint = IPEndPoint.Parse(this.Socket.RemoteEndPoint.ToString());
// _action(ipEndPoint, msg);
//}
} }
catch (Exception ex) catch (Exception ex)
{ {

View File

@ -23,7 +23,7 @@ namespace OnlineUserPool.Services
public void RunServer(Action<IPEndPoint, MsgModel, IService> action) public void RunServer(Action<IPEndPoint, MsgModel, IService> action)
{ {
udpServer = new UdpClient(ConfigHelp.Port); udpServer = new UdpClient(ConfigHelp.UdpPort);
uint IOC_IN = 0x80000000; uint IOC_IN = 0x80000000;
uint IOC_VENDOR = 0x18000000; uint IOC_VENDOR = 0x18000000;
uint SIO_UDP_CONNRESET = IOC_IN | IOC_VENDOR | 12; uint SIO_UDP_CONNRESET = IOC_IN | IOC_VENDOR | 12;

View File

@ -18,7 +18,9 @@ namespace OnlineUserPool.Unility
//public static string Ip { get; private set; } //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() static ConfigHelp()
@ -27,7 +29,10 @@ namespace OnlineUserPool.Unility
//Top = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["Top"]); //Top = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["Top"]);
ShowVirtualUser = bool.Parse(System.Configuration.ConfigurationManager.AppSettings["ShowVirtualUser"]); ShowVirtualUser = bool.Parse(System.Configuration.ConfigurationManager.AppSettings["ShowVirtualUser"]);
//Ip = System.Configuration.ConfigurationManager.AppSettings["Ip"]; //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"]);
} }
} }
} }

View File

@ -54,15 +54,16 @@ namespace OnlineUserPool.ViewModels
//private IService _udpService; //private IService _udpService;
public MainWindowViewModel() public MainWindowViewModel()
{ {
Title = $"{ IPAddress.Any }:{ ConfigHelp.Port }"; Title = $"{ IPAddress.Any }:{ ConfigHelp.UdpPort }";
dispatcher = Dispatcher.CurrentDispatcher; dispatcher = Dispatcher.CurrentDispatcher;
//Customers.Add("suntao"); //Customers.Add("suntao");
WriteLine(DateTime.Now.ToShortDateString()); WriteLine(DateTime.Now.ToShortDateString());
LogHelper.Init(); LogHelper.Init();
mapRecordRankingHander = new MapRecordRankingHander(); mapRecordRankingHander = new MultiUserHandle();//new MapRecordRankingHander();
Log.Information($"初始化连接,当前地址:{ IPAddress.Any }:{ConfigHelp.Port}"); //mapRecordRankingHander = new MultiUserHandle();
Log.Information($"初始化连接,当前地址:{ IPAddress.Any }:{ConfigHelp.UdpPort}");
new TcpService1().RunServer(ReceivedData); new TcpService1().RunServer(ReceivedData);
//var tet = new System.Collections.Concurrent.ConcurrentBag<object>(); //var tet = new System.Collections.Concurrent.ConcurrentBag<object>();
@ -120,6 +121,25 @@ namespace OnlineUserPool.ViewModels
{ {
try try
{ {
if(!receiveMes.Any())
{
dispatcher.Invoke(() =>
{
Customers.Clear();
//删除已经发送的数据
//receiveMes.Clear();
//移除5钟内连接不上的客户端
Clients.ToList().ForEach(item =>
{
if (item.Expire)
{
Clients.Remove(item);
}
});
});
return;
}
lock (locker) lock (locker)
{ {
var list = CloneJson(receiveMes).ToList(); var list = CloneJson(receiveMes).ToList();
@ -185,7 +205,12 @@ namespace OnlineUserPool.ViewModels
private void SendMessage(Collection<HostModel> clients, List<MsgModel> msgModels) private void SendMessage(Collection<HostModel> clients, List<MsgModel> msgModels)
{ {
if (!clients.Any())
{
return;
}
var clients1 = clients.ToList(); var clients1 = clients.ToList();
var list = CloneJson<List<MsgModel>>(msgModels); var list = CloneJson<List<MsgModel>>(msgModels);
foreach (var item in list) foreach (var item in list)
{ {
@ -208,14 +233,19 @@ namespace OnlineUserPool.ViewModels
m.Saved, m.Saved,
})); }));
var data = Encoding.ASCII.GetBytes(jsonString); 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))); 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);
WriteLine(strV1.Length.ToString()); WriteLine(strV1.Length.ToString());
var dataV1 = Encoding.ASCII.GetBytes(strV1); var dataV1 = Encoding.ASCII.GetBytes(strV1);
var dataV1ForUdp = strV1.Last() == '|' ? Encoding.ASCII.GetBytes(strV1.Substring(0, strV1.Length - 1)) : dataV1;
SendDataSize += $"\tV1{ (strV1.Length/1000D) }KB"; SendDataSize = $"\t单客户端数据包V1{ (strV1.Length/1000D) }KB一共占用带宽{ strV1.Length / 1000D * clients1.Count }KB";
foreach (var item in clients1) foreach (var item in clients1)
{ {
@ -225,9 +255,14 @@ namespace OnlineUserPool.ViewModels
{ {
continue; continue;
} }
if((item.Service is UdpService) ==false)
{
continue;
}
if (item.V == 1) if (item.V == 1)
{ {
item.Service.Send(dataV1, dataV1.Length, item.IPEndPoint);
item.Service.Send(dataV1ForUdp, dataV1ForUdp.Length, item.IPEndPoint);
} }
else else
{ {
@ -240,6 +275,11 @@ namespace OnlineUserPool.ViewModels
Log.Error($"{ item.IPEndPoint.ToString() }:{ e.Message }\r\n{ e.StackTrace }"); 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);
}
} }

View File

@ -32,7 +32,7 @@
<TextBlock> <TextBlock>
<Run Text="在线客户端:"></Run> <Run Text="在线客户端:"></Run>
<Run Text="{ Binding Clients.Count, Mode=OneWay}"></Run> <Run Text="{ Binding Clients.Count, Mode=OneWay}"></Run>
<Run Text="发送单个客户端数据包大小:"></Run> <!--<Run Text="发送单个客户端数据包大小:"></Run>-->
<Run Text="{Binding SendDataSize,Mode=OneWay}"></Run> <Run Text="{Binding SendDataSize,Mode=OneWay}"></Run>
</TextBlock> </TextBlock>
<DataGrid ItemsSource="{ Binding Clients }" Grid.Row="1" IsReadOnly="True"></DataGrid> <DataGrid ItemsSource="{ Binding Clients }" Grid.Row="1" IsReadOnly="True"></DataGrid>