增加模拟用户功能

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>
<add key="Host" value="http://192.168.0.97:5082/"/>
<add key="Top" value="0"/>
<add key="ShowVirtualUser" value="true"/>
<add key="Port" value="11000"/>
<add key="ShowVirtualUser" value="false"/>
<add key="Port" value="21000"/>
<add key="TcpPort" value="21001"/>
</appSettings>
</configuration>

View File

@ -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<MultiUserModel>($"/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);
}

View File

@ -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<MsgModel>(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<MsgModel>(item + "}");
var ipEndPoint = IPEndPoint.Parse(this.Socket.RemoteEndPoint.ToString());
_action(ipEndPoint, msg);
if(item == '}')
{
temp += item;
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)
{

View File

@ -23,7 +23,7 @@ namespace OnlineUserPool.Services
public void RunServer(Action<IPEndPoint, MsgModel, IService> 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;

View File

@ -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"]);
}
}
}

View File

@ -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<object>();
@ -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<HostModel> clients, List<MsgModel> msgModels)
{
if (!clients.Any())
{
return;
}
var clients1 = clients.ToList();
var list = CloneJson<List<MsgModel>>(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);
}
}

View File

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