forked from powerfun/udpservice
增加模拟用户功能
This commit is contained in:
1 parent
0e931a3ac9
commit
b9bb485a2d
7 files changed
+107
-28
No files matched your search
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user