针对unity版本比赛功能的调整

This commit is contained in:
suntao 2021-07-07 09:49:36 +08:00
parent b9bb485a2d
commit c380654041
10 changed files with 393 additions and 74 deletions

View File

@ -16,7 +16,7 @@ namespace OnlineUserPool.Hander
private MultiUserModel model;
private TurfHelper _turfHelper;
private int _routeId = 1255; //5593; //1660;
private int _size = 50;
private int _size = 150;
private int _competitionid = 0;//35;
public MultiUserHandle()
{
@ -33,8 +33,8 @@ namespace OnlineUserPool.Hander
#region sis
//_routeId = 5710;
//_competitionid = 37;
_routeId = 3386;
_competitionid = 1048;
#endregion
model = await BaseApi.GetAsync<MultiUserModel>($"/Map/sss?routeId={ _routeId }&size={ _size }");

View File

@ -20,14 +20,31 @@ namespace OnlineUserPool.Model
SetProperty(ref _LastActiveTime, value);
}
}
private int _RouteId = 0;
/// <summary>
/// 地图编号
/// </summary>
public int RouteId { get; set; }
public int RouteId {
get {
return _RouteId;
}
set {
SetProperty(ref _RouteId, value);
}
}
private int _MemberId;
/// <summary>
/// 用户Id
/// </summary>
public int MemberId { get; set; }
public int MemberId {
get {
return _MemberId;
}
set {
SetProperty(ref _MemberId, value);
}
}
/// <summary>
/// 客户端过期
@ -51,15 +68,37 @@ namespace OnlineUserPool.Model
/// </summary>
public List<int> ShowRouteId { get; set; }
private int _Competitionid = 0;
public int Competitionid {
get {
return _Competitionid;
} set {
SetProperty(ref _Competitionid, value);
}
}
///// <summary>
///// 是否展示在线的人
///// </summary>
//public bool ShowVirtual { get; set; }
public string Encoding { get; set; }
public string Client { get; set; }
private int _V = 0;
/// <summary>
/// 客户端支持的消息格式的版本号
/// </summary>
public int V { get; set; }
public int V {
get
{
return _V;
}
set
{
SetProperty(ref _V, value);
}
}
[Newtonsoft.Json.JsonIgnore]
public IService Service { get; set; }
@ -72,5 +111,15 @@ namespace OnlineUserPool.Model
return Service.ToString();
}
}
private bool _IsWatch;
public bool IsWatch
{
get { return _IsWatch; }
set
{
SetProperty(ref _IsWatch, value);
}
}
}
}

View File

@ -5,7 +5,7 @@ using System.Text;
namespace OnlineUserPool.Model
{
public class MsgModel
public class MsgModel : ReceiveModel
{
public int RouteId { get; set; }
public int MemberId { get; set; }
@ -22,14 +22,6 @@ namespace OnlineUserPool.Model
public double PreDistance { get; set; }
public double EndDistance { get; set; }
/// <summary>
/// 是否展示在线的人
/// </summary>
//public bool ShowVirtual { get; set; }
/// <summary>
/// 命令类型,0命令1消息
/// </summary>
public byte CommandType { get; set; }
//public bool IsVirtual { get; set; }
@ -43,10 +35,9 @@ namespace OnlineUserPool.Model
public int Competitionid { get; set; }
public bool Saved { get; set; }
/// <summary>
/// 能处理的消息格式的版本号
/// </summary>
public int V { get; set; }
//public bool ShowVirtual { get; set; }
public string ToString(int v)

38
Model/ReceiveModel.cs Normal file
View File

@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace OnlineUserPool.Model
{
public class ReceiveModel
{
/// <summary>
/// 命令类型,0ping命令1消息, 2设置客户端信息, 3观察者模式
/// </summary>
public byte CommandType { get; set; }
/// <summary>
/// 能处理的消息格式的版本号
/// </summary>
public int V { get; set; }
}
public class SetClientCommand : ReceiveModel
{
public string Encoding { get; set; }
public string Client { get; set; }
public int Competitionid { get; set; }
public bool IsWatch { get; set; }
public int MemberId { get; set; }
}
public class SetWatchCommand :ReceiveModel
{
public int Competitionid { get; set; }
}
}

View File

@ -7,7 +7,10 @@ namespace OnlineUserPool.Model
public class TargetData
{
public double _Power = 0d;
public double _Speed = 0;
/// <summary>
/// km/h
/// </summary>
public double _Speed { get; set; } = 0;
public double _Distance = 0D;
public double? _Cadence = 0;
public int? _HeartRate = 0;

View File

@ -8,7 +8,7 @@ namespace OnlineUserPool.Services
{
public interface IService
{
public void RunServer(Action<IPEndPoint, MsgModel, IService> action);
public void RunServer(Action<IPEndPoint, ReceiveModel, IService> received, Action<EndPoint> disconnected = null);
public void Send(byte[] dgram, int bytes, IPEndPoint endPoint);

View File

@ -1,4 +1,5 @@
using NetCoreServer;
using Newtonsoft.Json;
using OnlineUserPool.Model;
using OnlineUserPool.Unility;
using Serilog;
@ -21,35 +22,40 @@ namespace OnlineUserPool.Services
//throw new NotImplementedException();
}
public void RunServer(Action<IPEndPoint, MsgModel, IService> action)
public void RunServer(Action<IPEndPoint, ReceiveModel, IService> action, Action<EndPoint> disconnected = null)
{
//throw new NotImplementedException();
//_action = action;
_server = new PfTcpServer(IPAddress.Any, ConfigHelp.TcpPort, (ip, model)=> {
action(ip, model, this);
});
}, disconnected);
//_server.OptionNoDelay = true;
_server.Start();
}
public void Send(byte[] dgram, int bytes, IPEndPoint endPoint)
{
if (dgram == null || !dgram.Any())
{
return;
}
_server.Multicast(dgram);
//_server.Multicast(dgram);
var session = _server.FindSession(endPoint);
if(session != null && session.IsConnected)
{
session.Send(dgram);
}
}
class PfTcpServer : TcpServer
{
Action<IPEndPoint, MsgModel> _action;
public PfTcpServer(IPAddress address, int port, Action<IPEndPoint, MsgModel> action) :base(address, port)
private Action<IPEndPoint, ReceiveModel> _action;
private Action<EndPoint> _disconnected;
//private System.Collections.Generic.Dictionary<Guid, PfTcpSession> clients = new Dictionary<Guid, PfTcpSession>();
public PfTcpServer(IPAddress address, int port, Action<IPEndPoint, ReceiveModel> action, Action<EndPoint> disconnected = null) :base(address, port)
{
_action = action;
_disconnected = disconnected;
}
protected override TcpSession CreateSession()
@ -63,24 +69,46 @@ namespace OnlineUserPool.Services
//base.OnError(error);
Debug.WriteLine(error);
}
public TcpSession FindSession(IPEndPoint ip)
{
var item = this.Sessions.Values.FirstOrDefault(t => t.Socket.RemoteEndPoint.ToString() == ip.ToString());
return item;
}
protected override void OnDisconnected(TcpSession session)
{
//var bbb = this.Sessions.GetValueOrDefault(session.Id);
var bbb = (session as PfTcpSession);
if(bbb.IPEndPoint != null)
{
_disconnected?.Invoke(bbb.IPEndPoint);
}
base.OnDisconnected(session);
}
}
class PfTcpSession : TcpSession
{
Action<IPEndPoint, MsgModel> _action;
public PfTcpSession(TcpServer server, Action<IPEndPoint, MsgModel> action) : base(server) {
Action<IPEndPoint, ReceiveModel> _action;
public IPEndPoint IPEndPoint { get; private set; }
public PfTcpSession(TcpServer server, Action<IPEndPoint, ReceiveModel> action) : base(server) {
_action = action;
}
protected override void OnConnected()
{
//base.OnConnected();
base.OnConnected();
Debug.WriteLine("有新的Tcp连接");
IPEndPoint = IPEndPoint.Parse(this.Socket.RemoteEndPoint.ToString());
}
protected override void OnDisconnected()
{
//base.OnDisconnected();
base.OnDisconnected();
Debug.WriteLine("Tcp断开连接");
}
private string temp = "";
@ -102,10 +130,25 @@ namespace OnlineUserPool.Services
if(item == '}')
{
temp += item;
var msg = Newtonsoft.Json.JsonConvert.DeserializeObject<MsgModel>(temp);
Debug.WriteLine("收到消息:"+temp);
var msg = JsonConvert.DeserializeObject<ReceiveModel>(temp);
var ipEndPoint = IPEndPoint.Parse(this.Socket.RemoteEndPoint.ToString());
if (msg.CommandType == 1)
{
_action(ipEndPoint, JsonConvert.DeserializeObject<MsgModel>(temp));
}
else if (msg.CommandType == 2)
{
_action(ipEndPoint, JsonConvert.DeserializeObject<SetClientCommand>(temp));
}
//else if(msg.CommandType == 3)
//{
// _action(ipEndPoint, JsonConvert.DeserializeObject<SetWatchCommand>(temp));
//}
else
{
_action(ipEndPoint, msg);
}
temp = "";
}
else

View File

@ -1,4 +1,5 @@
using OnlineUserPool.Model;
using Newtonsoft.Json;
using OnlineUserPool.Model;
using OnlineUserPool.Unility;
using System;
using System.Collections.Generic;
@ -21,7 +22,7 @@ namespace OnlineUserPool.Services
//serverIpEndPoint = new IPEndPoint(IPAddress.Parse(ConfigHelp.Ip), ConfigHelp.Port);
}
public void RunServer(Action<IPEndPoint, MsgModel, IService> action)
public void RunServer(Action<IPEndPoint, ReceiveModel, IService> action, Action<EndPoint> disconnected = null)
{
udpServer = new UdpClient(ConfigHelp.UdpPort);
uint IOC_IN = 0x80000000;
@ -37,14 +38,25 @@ namespace OnlineUserPool.Services
{
byte[] receiveBytes = udpServer.Receive(ref remoteIpEndPoint);
var returnData = Encoding.ASCII.GetString(receiveBytes);
var msg = Newtonsoft.Json.JsonConvert.DeserializeObject<MsgModel>(returnData);
var msg = JsonConvert.DeserializeObject<ReceiveModel>(returnData);
//lock (locker)
{
#if DEBUG
WriteLine($"本次接收:{ remoteIpEndPoint.Address.ToString() }:{ remoteIpEndPoint.Port }收到消息:{ returnData }");
#endif
if (msg.CommandType == 1)
{
action(remoteIpEndPoint, JsonConvert.DeserializeObject<MsgModel>(returnData), this);
}
else if (msg.CommandType == 2)
{
action(remoteIpEndPoint, JsonConvert.DeserializeObject<SetClientCommand>(returnData), this);
}
else
{
action(remoteIpEndPoint, msg, this);
}
//if (timer.AutoReset == false)
//{

View File

@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Security.Cryptography;
using System.Text;
using TurfCS;
@ -23,5 +25,42 @@ namespace OnlineUserPool.Unility
new RNGCryptoServiceProvider().GetBytes(randomNumberBuffer);
return new Random(BitConverter.ToInt32(randomNumberBuffer, 0)).Next(min, max);
}
public static byte[] Compress(string text)
{
var bytes = Encoding.UTF8.GetBytes(text);
return Compress(bytes);
}
public static byte[] Compress(byte[] bytes)
{
//var bytes = Encoding.Unicode.GetBytes(text);
using (var mso = new MemoryStream())
{
using (var gs = new GZipStream(mso, CompressionMode.Compress))
{
gs.Write(bytes, 0, bytes.Length);
}
return mso.ToArray();
}
}
public static string Decompress(byte[] data)
{
// Read the last 4 bytes to get the length
byte[] lengthBuffer = new byte[4];
Array.Copy(data, data.Length - 4, lengthBuffer, 0, 4);
int uncompressedSize = BitConverter.ToInt32(lengthBuffer, 0);
var buffer = new byte[uncompressedSize];
using (var ms = new MemoryStream(data))
{
using (var gzip = new GZipStream(ms, CompressionMode.Decompress))
{
gzip.Read(buffer, 0, uncompressedSize);
}
}
return Encoding.UTF8.GetString(buffer);
}
}
}

View File

@ -16,6 +16,8 @@ using System.Diagnostics;
using OnlineUserPool.Services;
using Newtonsoft.Json;
using System.Collections.Concurrent;
using System.IO.Compression;
using System.IO;
namespace OnlineUserPool.ViewModels
{
@ -61,11 +63,11 @@ namespace OnlineUserPool.ViewModels
//Customers.Add("suntao");
WriteLine(DateTime.Now.ToShortDateString());
LogHelper.Init();
mapRecordRankingHander = new MultiUserHandle();//new MapRecordRankingHander();
mapRecordRankingHander = new MapRecordRankingHander();//new MultiUserHandle();//
//mapRecordRankingHander = new MultiUserHandle();
Log.Information($"初始化连接,当前地址:{ IPAddress.Any }:{ConfigHelp.UdpPort}");
new TcpService1().RunServer(ReceivedData);
new TcpService1().RunServer(ReceivedData, CientDisconnected);
//var tet = new System.Collections.Concurrent.ConcurrentBag<object>();
var _udpService = new UdpService();
@ -81,7 +83,7 @@ namespace OnlineUserPool.ViewModels
//Console.ReadKey();
}
private void ReceivedData(IPEndPoint remoteIpEndPoint, MsgModel msg, IService service)
private void ReceivedData(IPEndPoint remoteIpEndPoint, ReceiveModel msg, IService service)
{
dispatcher.Invoke(() =>
{
@ -91,24 +93,76 @@ namespace OnlineUserPool.ViewModels
{
IPEndPoint = remoteIpEndPoint,
LastActiveTime = DateTime.Now,
RouteId = msg.RouteId,
MemberId = msg.MemberId,
//ShowVirtual = msg.ShowVirtual
V = msg.V,
//RouteId = msg.RouteId,
//MemberId = msg.MemberId,
//V = msg.V,
//Encoding = msg.Encoding,
//Client = msg.Client,
Service = service
});
}
if (msg.CommandType == 0)
switch (msg.CommandType)
{
//ping命令
case 0:
{
var client = Clients.FirstOrDefault(n => n.Equals(remoteIpEndPoint));
client.LastActiveTime = DateTime.Now;
if(msg.V > 0)
{
client.V = msg.V;
}
}
break;
//消息
case 1:
{
var client = Clients.FirstOrDefault(n => n.Equals(remoteIpEndPoint));
var msg1 = (msg as MsgModel);
client.Competitionid = msg1.Competitionid;
if (msg1.V > 0)
{
client.V = msg1.V;
}
receiveMes.Add(msg1);
}
break;
//设置客户端
case 2:
{
var client = Clients.FirstOrDefault(n => n.Equals(remoteIpEndPoint));
var msg1 = (msg as SetClientCommand);
client.Encoding = msg1.Encoding;
client.Client = msg1.Client;
client.V = msg1.V;
client.MemberId = msg1.MemberId;
client.Competitionid = msg1.Competitionid;
client.IsWatch = msg1.IsWatch;
}
break;
//case 3:
// {
// var client = Clients.FirstOrDefault(n => n.Equals(remoteIpEndPoint));
// var msg1 = (msg as SetWatchCommand);
// client.Competitionid = msg1.Competitionid;
// client.IsWatch = true;
// }
// break;
default:
break;
}
if (msg.CommandType == 0)
{
//client.ShowVirtual = msg.ShowVirtual;
}
else if (msg.CommandType == 1)
{
receiveMes.Add(msg);
}
});
}
@ -125,7 +179,7 @@ namespace OnlineUserPool.ViewModels
{
dispatcher.Invoke(() =>
{
Customers.Clear();
//Customers.Clear();
//删除已经发送的数据
//receiveMes.Clear();
@ -142,7 +196,7 @@ namespace OnlineUserPool.ViewModels
}
lock (locker)
{
var list = CloneJson(receiveMes).ToList();
var msgs = CloneJson(receiveMes).ToList();
receiveMes.Clear();
//加入虚拟人物消息
@ -150,17 +204,17 @@ namespace OnlineUserPool.ViewModels
WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "-当前在线人数:" + Clients.Count + "-当前虚拟人数:" + virtualData.Count);
#if DEBUG
WriteLine($"在线人:{Newtonsoft.Json.JsonConvert.SerializeObject(list)}");
WriteLine($"在线人:{Newtonsoft.Json.JsonConvert.SerializeObject(msgs)}");
//\r\n虚拟人{Newtonsoft.Json.JsonConvert.SerializeObject(virtualData)}
#endif
list.AddRange(virtualData);
msgs.AddRange(virtualData);
SendMessage(Clients, list);
SendMessage(Clients, msgs);
dispatcher.Invoke(() =>
{
Customers.Clear();
foreach (var item in list)
foreach (var item in msgs)
{
if (Customers.Any(c => c.MemberId == item.MemberId))
{
@ -170,12 +224,12 @@ namespace OnlineUserPool.ViewModels
}
//移除下线的客户端
for (int i = 0; i < list.Count; i++)
for (int i = 0; i < msgs.Count; i++)
{
if (list[i].exit && list[i].MemberId >0)//客户端退出,并且不是虚拟的人物
if (msgs[i].exit && msgs[i].MemberId >0)//客户端退出,并且不是虚拟的人物
{
//这个地方有严重的逻辑错误(虚拟的人物不能和真实的人用同一个名字)
var info = Clients.FirstOrDefault(n => n.MemberId == list[i].MemberId);
var info = Clients.FirstOrDefault(n => n.MemberId == msgs[i].MemberId);
if (info != null)
{
Clients.Remove(info);
@ -240,13 +294,29 @@ namespace OnlineUserPool.ViewModels
{
strV1 += "|";
}
//strV1 = "abcdefghijklmnopqrstuvwxyz";
//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);
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";
//var dataV1 = Encoding.UTF8.GetBytes(strV1);
//var dataV1ForUdp = strV1.Last() == '|' ? Encoding.ASCII.GetBytes(strV1.Substring(0, strV1.Length - 1)) : dataV1;
//var dataCompressV1 = Encoding.UTF8.GetBytes($"*{ Convert.ToBase64String(CommonHelper.Compress(strV1)) }#");
var data1 = new Data1(strV1);
var data1ForUdp = new Data1(strV1.Last() == '|' ? strV1.Substring(0, strV1.Length - 1) : strV1);
//var watchList = string.Join('|', clients1.Where(c => c.IsWatch).Select(c => c.MemberId));
//var strV2 = $"*l{{{ strV1 }}};w{{{ watchList }}}#";
////var dataCompressV2 = Encoding.UTF8.GetBytes($"{ Convert.ToBase64String(CommonHelper.Compress(strV2)) }");
//var data2 = new Data1(strV2);
SendDataSize = $"\t单客户端数据包V1{ data1.GetBytes().Length/1000D }KB一共占用带宽{ data1.GetBytes().Length / 1000D * clients1.Count }KB压缩以后{ data1.GetBytes(true).Length/1000D * clients1.Count }KB";
#region udp发送数据
foreach (var item in clients1)
{
try
@ -255,18 +325,43 @@ namespace OnlineUserPool.ViewModels
{
continue;
}
if((item.Service is UdpService) ==false)
//if((item.Service is UdpService) ==false)
//{
// continue;
//}
if (item.V != 1 && item.V != 2)
{
item.Service.Send(data, data.Length, item.IPEndPoint);
continue;
}
if (item.V == 1)
if (item.V == 1 && item.Service is UdpService)
{
item.Service.Send(dataV1ForUdp, dataV1ForUdp.Length, item.IPEndPoint);
item.Service.Send(data1ForUdp.GetBytes(), data1ForUdp.GetBytes().Length, item.IPEndPoint);
}
else
{
item.Service.Send(data, data.Length, item.IPEndPoint);
Data1 ddd = null;// = item.V == 1 ? ;
if (item.V == 1)
{
ddd = data1;
}
else if (item.V == 2)
{
//ddd = data2;
var temp = string.Join("|", list.Where(m => m.Competitionid == item.Competitionid).Select(m => m.ToString(1))) + "|";
var watchList1 = string.Join('|', clients1.Where(c => c.IsWatch && c.Competitionid == item.Competitionid).Select(c => c.MemberId));
var strV21 = $"*l{{{ temp }}};w{{{ watchList1 }}}#";
ddd = new Data1(strV21);
}
else
{
continue;
}
bool isZip = item.Encoding == "gzip";
item.Service.Send(ddd.GetBytes(isZip), ddd.GetBytes(isZip).Length, item.IPEndPoint);
}
}
catch (Exception e)
@ -275,11 +370,15 @@ 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);
}
#endregion
//#region tcp发送数据
//var tcpClient = clients1.Where(c => c.Service is TcpService1).FirstOrDefault();
//if(tcpClient != null)
//{
// tcpClient.Service.Send(dataV1, dataV1.Length, null);
//}
//#endregion
}
@ -304,5 +403,50 @@ namespace OnlineUserPool.ViewModels
return JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(source), deserializeSettings);
}
private void CientDisconnected(EndPoint point)
{
var client = Clients.FirstOrDefault(f => f.IPEndPoint.ToString() == point.ToString());
if (client != null)
{
dispatcher.Invoke(() =>
{
Clients.Remove(client);
});
}
}
}
public class Data1
{
private string _txt = "";
public Data1(string txt)
{
this._txt = txt;
}
public byte[] GetBytes(bool compress = false)
{
if (compress)
{
return GetCompressBytes();
}
return Encoding.UTF8.GetBytes(this._txt);
}
private byte[] GetCompressBytes()
{
var temp = this._txt;
if(temp[0] == '*')
{
temp = temp.Substring(1);
}
if(temp.Last() == '#')
{
temp = temp.Substring(0, temp.Length - 1);
}
return Encoding.UTF8.GetBytes($"*{ Convert.ToBase64String(CommonHelper.Compress(temp)) }#");
}
}
}