This commit is contained in:
CortexCore
2024-06-17 14:38:09 +08:00
parent 3d447499fe
commit 65f9e40105
8 changed files with 86 additions and 14 deletions

View File

@@ -11,6 +11,7 @@ using System.Numerics;
using System.Reflection;
using BITKit.Net.Examples;
using Newtonsoft.Json;
using Unity.Mathematics;
namespace BITKit.Net
{
@@ -21,7 +22,7 @@ namespace BITKit.Net
public event Action OnDisconnected;
public event Action OnConnectedFailed;
public bool IsConnected => client.connected;
public float2 Traffic { get; set; }
public bool ManualTick { get; set; }
public int Ping { get; private set; }
@@ -111,6 +112,7 @@ namespace BITKit.Net
await Task.Delay(100);
}
_timer.Start();
Traffic+=new float2(1,0);
client.Send(new[] { (byte)NetCommandType.Heartbeat }, KcpChannel.Reliable);
if (BITApp.SynchronizationContext is not null)
await UniTask.SwitchToSynchronizationContext(BITApp.SynchronizationContext);
@@ -144,6 +146,7 @@ namespace BITKit.Net
{
try
{
Traffic+=new float2(0,bytes.Count);
OnDataInternel(bytes, channel);
}
catch (Exception e)
@@ -187,6 +190,7 @@ namespace BITKit.Net
break;
case NetCommandType.Heartbeat:
Traffic+=new float2(1,0);
client.Send(new[] { (byte)NetCommandType.Heartbeat }, KcpChannel.Reliable);
_isConnected.AddElement(this);
break;
@@ -450,6 +454,7 @@ namespace BITKit.Net
.Write(((byte)NetCommandType.Message))
.Write(message)
.Build();
Traffic+=new float2(bytes.Length,0);
client.Send(bytes, KcpChannel.Reliable);
}
@@ -463,6 +468,7 @@ namespace BITKit.Net
{
while (commandQueue.TryDequeue(out var bytes))
{
Traffic+=new float2(bytes.Length,0);
client.Send(bytes, KcpChannel.Reliable);
}
@@ -486,6 +492,7 @@ namespace BITKit.Net
public void HandShake()
{
// send client to server
Traffic+=new float2(2,0);
client.Send(new byte[]{0x01, 0x02}, KcpChannel.Reliable);
}

View File

@@ -19,6 +19,34 @@ namespace BITKit
public sealed class NetRpcAttribute : Attribute
{
}
/// <summary>
/// 帮助类
/// </summary>
public static class NetUtils
{
/// <summary>
/// 计算文件大小函数(保留两位小数),Size为字节大小
/// </summary>
/// <param name="size">初始文件大小</param>
/// <returns></returns>
public static string GetFileSize(long size)
{
var num = 1024.00; //byte
if (size < num)
return size + "B";
if (size < Math.Pow(num, 2))
return (size / num).ToString("f2") + "K"; //kb
if (size < Math.Pow(num, 3))
return (size / Math.Pow(num, 2)).ToString("f2") + "M"; //M
if (size < Math.Pow(num, 4))
return (size / Math.Pow(num, 3)).ToString("f2") + "G"; //G
return (size / Math.Pow(num, 4)).ToString("f2") + "T"; //T
}
}
/// <summary>
/// 网络指令类型