Core发布

This commit is contained in:
若汝棋茗
2021-12-18 14:40:16 +08:00
parent 3093c7b083
commit caefbc79ef
27 changed files with 433 additions and 302 deletions

View File

@@ -25,8 +25,6 @@ namespace RRQMCore.ByteManager
{
internal bool @using;
internal BytePool _bytePool;
internal long length;
private static float ratio = 1.5f;
@@ -41,19 +39,19 @@ namespace RRQMCore.ByteManager
/// 构造函数
/// </summary>
/// <param name="capacity"></param>
public ByteBlock(int capacity = 1024 * 10) : this(new byte[capacity])
/// <param name="equalSize"></param>
public ByteBlock(int capacity = 1024 * 10, bool equalSize=false) : this(BytePool.GetByteCore(capacity, false))
{
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="buffer"></param>
public ByteBlock(byte[] buffer)
/// <param name="bytes"></param>
internal ByteBlock(byte[] bytes)
{
this.@using = true;
this._buffer = buffer;
this._buffer = bytes;
}
/// <summary>
@@ -61,8 +59,25 @@ namespace RRQMCore.ByteManager
/// </summary>
~ByteBlock()
{
this.AbsoluteDispose();
if (this.@using)
{
BytePool.Recycle(this._buffer);
this.AbsoluteDispose();
}
}
/// <summary>
/// 清空数据
/// </summary>
public void Clear()
{
if (!this.@using)
{
throw new RRQMException("内存块已释放");
}
Array.Clear(this._buffer, 0, this._buffer.Length);
}
/// <summary>
/// 扩容增长比默认为1.5
/// min1
@@ -88,26 +103,11 @@ namespace RRQMCore.ByteManager
get { return _buffer; }
}
/// <summary>
/// 该字节流块所属内存池若值为null则意味着该流块未被管理可能会被GC
/// </summary>
public BytePool BytePool
{
get { return _bytePool; }
}
/// <summary>
/// 可读取
/// </summary>
public override bool CanRead => this.@using;
/// <summary>
/// 能否回收
/// </summary>
public bool CanRecycle
{
get { return this._bytePool == null ? false : true; }
}
/// <summary>
/// 支持查找
/// </summary>
@@ -121,7 +121,7 @@ namespace RRQMCore.ByteManager
/// <summary>
/// 容量
/// </summary>
public int Capacity => this._buffer == null ? 0 : this._buffer.Length;
public int Capacity => this._buffer.Length;
/// <summary>
/// 表示持续性持有为True时Dispose将调用无效。
@@ -134,12 +134,14 @@ namespace RRQMCore.ByteManager
/// <summary>
/// Int真实长度
/// </summary>
public int Len { get { return (int)length; } }
public int Len
{ get { return (int)length; } }
/// <summary>
/// 真实长度
/// </summary>
public override long Length { get { return length; } }
public override long Length
{ get { return length; } }
/// <summary>
/// int型流位置
@@ -168,43 +170,15 @@ namespace RRQMCore.ByteManager
}
/// <summary>
/// 直接完全释放游离该对象然后GC
/// 直接完全释放,游离该对象,然后等待GC
/// </summary>
public void AbsoluteDispose()
{
this._bytePool = null;
this._buffer = null;
this.Disposed();
GC.Collect();
}
private void Disposed()
{
if (this.holding)
{
return;
}
if (this.@using)
{
this.@using = false;
this.position = 0;
this.length = 0;
GC.SuppressFinalize(this);
if (this.BytePool != null)
{
lock (this)
{
if (this._buffer != null)
{
this.BytePool.Recycle(this._buffer);
}
this._bytePool = null;
this._buffer = null;
}
}
}
this.holding = false;
this.@using = false;
this.position = 0;
this.length = 0;
this._buffer = default;
}
/// <summary>
@@ -212,8 +186,22 @@ namespace RRQMCore.ByteManager
/// </summary>
public new void Dispose()
{
GC.SuppressFinalize(this);
this.Disposed();
if (this.holding)
{
return;
}
if (this.@using)
{
lock (this)
{
if (this.@using)
{
GC.SuppressFinalize(this);
BytePool.Recycle(this._buffer);
this.AbsoluteDispose();
}
}
}
}
/// <summary>
@@ -282,18 +270,24 @@ namespace RRQMCore.ByteManager
}
/// <summary>
/// 重新指定Buffer
/// 重新设置容量
/// </summary>
public void SetBuffer(byte[] buffer)
/// <param name="size">新尺寸</param>
/// <param name="retainedData">是否保留元数据</param>
public void SetCapacity(int size, bool retainedData=false)
{
if (!this.@using)
{
throw new RRQMException("内存块已释放");
}
if (buffer != null)
byte[] bytes = new byte[size];
if (retainedData)
{
this._buffer = buffer;
Array.Copy(this._buffer, 0, bytes, 0, this.Len);
}
BytePool.Recycle(this._buffer);
this._buffer = bytes;
}
/// <summary>
@@ -337,13 +331,7 @@ namespace RRQMCore.ByteManager
/// <returns></returns>
public byte[] ToArray()
{
if (!this.@using)
{
throw new RRQMException("内存块已释放");
}
byte[] buffer = new byte[this.length];
Array.Copy(this._buffer, 0, buffer, 0, this.length);
return buffer;
return ToArray(0);
}
/// <summary>
@@ -376,9 +364,7 @@ namespace RRQMCore.ByteManager
}
if (this._buffer.Length - this.position < count)
{
byte[] newBuffer = new byte[this._buffer.Length + (int)((count + this.position - this._buffer.Length) * ratio)];
Array.Copy(this._buffer, newBuffer, this._buffer.Length);
this._buffer = newBuffer;
this.SetCapacity(this._buffer.Length + (int)((count + this.position - this._buffer.Length) * ratio),true);
}
Array.Copy(buffer, offset, _buffer, this.position, count);
this.position += count;
@@ -841,12 +827,6 @@ namespace RRQMCore.ByteManager
}
break;
case SerializationType.SystemBinary:
{
obj = SerializeConvert.BinaryDeserialize<T>(this._buffer, (int)this.position, length);
}
break;
case SerializationType.Json:
{
string jsonString = Encoding.UTF8.GetString(this._buffer, (int)this.position, length);
@@ -883,12 +863,6 @@ namespace RRQMCore.ByteManager
}
break;
case SerializationType.SystemBinary:
{
data = SerializeConvert.BinarySerialize(value);
}
break;
case SerializationType.Json:
{
data = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(value));

View File

@@ -9,99 +9,118 @@
// 感谢您的下载和使用
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
using RRQMCore.Exceptions;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
namespace RRQMCore.ByteManager
{
/// <summary>
/// 字节池
/// </summary>
public class BytePool
public static class BytePool
{
private static BytePool bytePool = new BytePool(1024 * 1024 * 512, 1024 * 1024 * 5);
private static readonly ConcurrentDictionary<long, BytesQueue> bytesDictionary = new ConcurrentDictionary<long, BytesQueue>();
private ConcurrentDictionary<long, BytesQueue> bytesDictionary = new ConcurrentDictionary<long, BytesQueue>();
private static bool autoZero;
private static long fullSize;
private static int keyCapacity;
private static int maxBlockSize;
private static long maxSize;
private static int minBlockSize;
private long createdBlockSize;
private long fullSize;
private int maxBlockSize = 1024 * 1024;
private long maxSize = 1024 * 1024 * 100;
private int minBlockSize = 1024 * 64;
/// <summary>
/// 构造函数
/// </summary>
public BytePool()
static BytePool()
{
keyCapacity = 100;
autoZero = false;
maxSize = 1024 * 1024 * 512;
SetBlockSize(1024 * 64, 1024 * 1024 * 5);
AddSizeKey(10240);
}
/// <summary>
/// 构造函数
/// 回收内存时,自动归零
/// </summary>
/// <param name="maxSize">字节池最大值</param>
/// <param name="maxBlockSize">单个Block最大值</param>
public BytePool(long maxSize, int maxBlockSize)
public static bool AutoZero
{
this.maxSize = maxSize;
this.maxBlockSize = maxBlockSize;
get { return autoZero; }
set { autoZero = value; }
}
/// <summary>
/// 构造函数
/// 键容量
/// </summary>
/// <param name="maxSize">字节池最大值</param>
public BytePool(long maxSize) : this(maxSize, 1024 * 1024)
public static int KeyCapacity
{
this.maxSize = maxSize;
get { return keyCapacity; }
set { keyCapacity = value; }
}
/// <summary>
/// 默认内存池,
/// 内存池最大512Mb
/// 单体最大5Mb。
/// 单个块最大值
/// </summary>
public static BytePool Default { get { return bytePool; } }
/// <summary>
/// 已创建的块的最大值
/// </summary>
public long CreatedBlockSize
{
get { return createdBlockSize; }
}
/// <summary>
/// 单个块最大值默认为1024*1024字节
/// </summary>
public int MaxBlockSize
public static int MaxBlockSize
{
get { return maxBlockSize; }
set { maxBlockSize = value; }
}
/// <summary>
/// 允许的内存池最大值,默认为100M Byte
/// 允许的内存池最大值
/// </summary>
public long MaxSize
public static long MaxSize
{
get { return maxSize; }
set { maxSize = value; }
}
/// <summary>
/// 单个块最小值默认64*1024字节
/// 单个块最小值
/// </summary>
public int MinBlockSize
public static int MinBlockSize
{
get { return minBlockSize; }
set { minBlockSize = value; }
}
/// <summary>
/// 添加尺寸键
/// </summary>
/// <param name="byteSize"></param>
/// <returns></returns>
public static bool AddSizeKey(int byteSize)
{
if (bytesDictionary.TryAdd(byteSize, new BytesQueue(byteSize)))
{
return true;
}
return false;
}
/// <summary>
/// 清理
/// </summary>
public static void Clear()
{
bytesDictionary.Clear();
GC.Collect();
}
/// <summary>
/// 确定是否包含指定尺寸键
/// </summary>
/// <param name="byteSize"></param>
/// <returns></returns>
public static bool ContainsSizeKey(int byteSize)
{
return bytesDictionary.ContainsKey(byteSize);
}
/// <summary>
/// 获取所以内存键
/// </summary>
/// <returns></returns>
public static long[] GetAllSizeKeys()
{
return bytesDictionary.Keys.ToArray();
}
/// <summary>
@@ -110,13 +129,9 @@ namespace RRQMCore.ByteManager
/// <param name="byteSize">长度</param>
/// <param name="equalSize">要求长度相同</param>
/// <returns></returns>
public ByteBlock GetByteBlock(long byteSize, bool equalSize)
public static ByteBlock GetByteBlock(int byteSize, bool equalSize)
{
ByteBlock byteBlock = new ByteBlock(this.GetBytesCore(byteSize, equalSize));
if (byteSize < maxBlockSize)
{
byteBlock._bytePool = this;
}
ByteBlock byteBlock = new ByteBlock(GetByteCore(byteSize, equalSize));
return byteBlock;
}
@@ -125,102 +140,235 @@ namespace RRQMCore.ByteManager
/// </summary>
/// <param name="byteSize"></param>
/// <returns></returns>
public ByteBlock GetByteBlock(long byteSize)
public static ByteBlock GetByteBlock(int byteSize)
{
if (byteSize < this.minBlockSize)
if (byteSize < minBlockSize)
{
return this.GetByteBlock(this.minBlockSize, false);
byteSize = minBlockSize;
}
return this.GetByteBlock(byteSize, false);
return GetByteBlock(byteSize, false);
}
/// <summary>
/// 获取最大长度的ByteBlock
/// </summary>
/// <returns></returns>
public ByteBlock GetByteBlock()
public static ByteBlock GetByteBlock()
{
return this.GetByteBlock(this.MaxBlockSize, true);
return GetByteBlock(maxBlockSize, true);
}
internal void Recycle(byte[] bytes)
/// <summary>
/// 获取内存池容量
/// </summary>
/// <returns></returns>
public static long GetPoolSize()
{
this.createdBlockSize = Math.Max(CreatedBlockSize, bytes.Length);
if (maxSize > fullSize)
long size = 0;
foreach (var item in bytesDictionary.Values)
{
this.fullSize += bytes.Length;
BytesQueue bytesCollection = this.bytesDictionary.GetOrAdd(bytes.Length, (size) =>
size += item.FullSize;
}
return size;
}
/// <summary>
/// 移除尺寸键
/// </summary>
/// <param name="byteSize"></param>
/// <returns></returns>
public static bool RemoveSizeKey(int byteSize)
{
if (bytesDictionary.TryRemove(byteSize, out BytesQueue queue))
{
queue.Clear();
return true;
}
return false;
}
/// <summary>
/// 设置内存块参数
/// </summary>
/// <param name="minBlockSize"></param>
/// <param name="maxBlockSize"></param>
public static void SetBlockSize(int minBlockSize, int maxBlockSize)
{
BytePool.maxBlockSize = maxBlockSize;
BytePool.minBlockSize = minBlockSize;
bytesDictionary.Clear();
}
/// <summary>
/// 获取内存核心
/// </summary>
/// <param name="byteSize"></param>
/// <param name="equalSize"></param>
/// <returns></returns>
public static byte[] GetByteCore(int byteSize, bool equalSize)
{
BytesQueue bytesCollection;
if (equalSize)
{
//等长
if (bytesDictionary.TryGetValue(byteSize, out bytesCollection) && bytesCollection.TryGet(out byte[] bytes))
{
return new BytesQueue(size);
});
bytesCollection.Add(bytes);
fullSize -= byteSize;
return bytes;
}
CheckKeyCapacity(byteSize);
return new byte[byteSize];
}
else
{
long size = 0;
foreach (var collection in this.bytesDictionary.Values)
byteSize = HitSize(byteSize);
//搜索已创建集合
if (bytesDictionary.TryGetValue(byteSize, out bytesCollection) && bytesCollection.TryGet(out byte[] bytes))
{
size += collection.FullSize;
fullSize -= byteSize;
return bytes;
}
this.fullSize = size;
CheckKeyCapacity(byteSize);
return new byte[byteSize];
}
}
/// <summary>
/// 清理
/// 回收内存核心
/// </summary>
public void Clear()
/// <param name="bytes"></param>
public static void Recycle(byte[] bytes)
{
this.bytesDictionary.Clear();
}
private byte[] GetBytesCore(long byteSize, bool equalSize)
{
if (byteSize < 0)
if (maxSize > fullSize)
{
throw new RRQMException("申请内存的长度不能小于0");
}
if (byteSize > maxBlockSize)
{
return new byte[byteSize];
}
if (this.createdBlockSize < byteSize)
{
return new byte[byteSize];
if (bytesDictionary.TryGetValue(bytes.Length, out BytesQueue bytesQueue))
{
if (autoZero)
{
Array.Clear(bytes, 0, bytes.Length);
}
fullSize += bytes.Length;
bytesQueue.Add(bytes);
}
}
else
{
BytesQueue bytesCollection;
//搜索已创建集合
if (bytesDictionary.TryGetValue(byteSize, out bytesCollection))
long size = 0;
foreach (var collection in bytesDictionary.Values)
{
if (bytesCollection.TryGet(out byte[] bytes))
{
this.fullSize -= byteSize;
return bytes;
}
size += collection.FullSize;
}
fullSize = size;
}
}
if (!equalSize)
private static void CheckKeyCapacity(int byteSize)
{
if (byteSize < minBlockSize || byteSize > maxBlockSize)
{
return;
}
if (bytesDictionary.Count < keyCapacity)
{
bytesDictionary.TryAdd(byteSize, new BytesQueue(byteSize));
}
else
{
List<BytesQueue> bytesQueues = bytesDictionary.Values.ToList();
bytesQueues.Sort((x, y) => { return x.referenced > y.referenced ? -1 : 1; });
for (int i = (int)(bytesQueues.Count * 0.2); i < bytesQueues.Count; i++)
{
foreach (var size in bytesDictionary.Keys)
if (bytesDictionary.TryRemove(bytesQueues[i].size, out BytesQueue queue))
{
if (size > byteSize)
{
if (this.bytesDictionary.TryGetValue(size, out bytesCollection))
{
if (bytesCollection.TryGet(out byte[] bytes))
{
this.fullSize -= byteSize;
return bytes;
}
}
}
queue.Clear();
}
}
return new byte[byteSize];
}
}
private static int HitSize(int num)
{
switch (num)
{
case <= 1024:
{
return 1024;
}
case <= 2048:
{
return 2048;
}
case <= 4096:
{
return 4096;
}
case <= 8192:
{
return 8192;
}
case <= 10240:
{
return 10240;
}
case <= 16384:
{
return 16384;
}
case <= 32768:
{
return 32768;
}
case <= 65536:
{
return 65536;
}
case <= 131072:
{
return 131072;
}
case <= 262144:
{
return 262144;
}
case <= 524288:
{
return 524288;
}
case <= 1048576:
{
return 1048576;
}
case <= 2097152:
{
return 2097152;
}
case <= 4194304:
{
return 4194304;
}
case <= 8388608:
{
return 8388608;
}
case <= 16777216:
{
return 16777216;
}
case <= 33554432:
{
return 33554432;
}
case <= 67108864:
{
return 67108864;
}
case <= 134217728:
{
return 134217728;
}
default:
return num;
}
}
}

View File

@@ -9,10 +9,10 @@
// 感谢您的下载和使用
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
using RRQMCore.Exceptions;
using RRQMCore.Helper;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace RRQMCore.ByteManager
{
@@ -20,11 +20,11 @@ namespace RRQMCore.ByteManager
/// 字节块集合
/// </summary>
[DebuggerDisplay("Count = {bytesQueue.Count}")]
public class BytesQueue
internal class BytesQueue
{
internal long size;
internal int size;
internal BytesQueue(long size)
internal BytesQueue(int size)
{
this.size = size;
}
@@ -32,17 +32,20 @@ namespace RRQMCore.ByteManager
/// <summary>
/// 占用空间
/// </summary>
public long FullSize { get { return this.size * this.bytesQueue.Count; } }
public long FullSize
{ get { return this.size * this.bytesQueue.Count; } }
private ConcurrentQueue<byte[]> bytesQueue = new ConcurrentQueue<byte[]>();
internal long referenced;
/// <summary>
/// 获取当前实例中的空闲的Block
/// </summary>
/// <returns></returns>
public bool TryGet(out byte[] bytes)
{
referenced++;
return this.bytesQueue.TryDequeue(out bytes);
}
@@ -55,9 +58,9 @@ namespace RRQMCore.ByteManager
this.bytesQueue.Enqueue(bytes);
}
internal List<byte[]> ToList()
internal void Clear()
{
return this.bytesQueue.ToList();
this.bytesQueue.Clear();
}
}
}

View File

@@ -58,12 +58,14 @@ namespace RRQMCore.Collections.Concurrent
/// <summary>
/// 获取集合中包含的元素数。
/// </summary>
public int Count { get { lock (locker) { return this.list.Count; } } }
public int Count
{ get { lock (locker) { return this.list.Count; } } }
/// <summary>
/// 是否只读
/// </summary>
public bool IsReadOnly { get { return false; } }
public bool IsReadOnly
{ get { return false; } }
/// <summary>
/// 将某项添加到 System.Collections.Generic.ICollection`1 中。

View File

@@ -26,4 +26,4 @@ namespace RRQMCore
/// </summary>
string Message { get; }
}
}
}

View File

@@ -35,4 +35,4 @@ namespace RRQMCore
}
}
}
}
}

View File

@@ -9,11 +9,6 @@
// 感谢您的下载和使用
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RRQMCore
{
@@ -22,6 +17,5 @@ namespace RRQMCore
/// </summary>
public class RRQMObject
{
}
}
}

View File

@@ -21,10 +21,15 @@ namespace RRQMCore
#pragma warning disable CS1591 // 缺少对公共可见类型或成员的 XML 注释
public static readonly Type stringType = typeof(string);
public static readonly Type byteType = typeof(byte);
public static readonly Type sbyteType = typeof(sbyte);
public static readonly Type shortType = typeof(short);
public static readonly Type ushortType = typeof(ushort);
public static readonly Type intType = typeof(int);
public static readonly Type uintType = typeof(uint);
public static readonly Type boolType = typeof(bool);
public static readonly Type charType = typeof(char);
public static readonly Type longType = typeof(long);
public static readonly Type ulongType = typeof(ulong);
public static readonly Type floatType = typeof(float);
public static readonly Type doubleType = typeof(double);
public static readonly Type decimalType = typeof(decimal);

View File

@@ -9,11 +9,6 @@
// 感谢您的下载和使用
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RRQMCore
{
@@ -56,6 +51,7 @@ namespace RRQMCore
/// 通道设置失败
/// </summary>
SetChannelFail,
/// <summary>
/// 路径无效
/// </summary>
@@ -105,6 +101,5 @@ namespace RRQMCore
/// 长时间没有响应。
/// </summary>
NoResponse,
}
}
}

View File

@@ -9,27 +9,20 @@
// 感谢您的下载和使用
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
using RRQMCore.Helper;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RRQMCore
{
/// <summary>
/// 结果返回
/// </summary>
public struct Result:IResult
public struct Result : IResult
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name="resultCode"></param>
/// <param name="message"></param>
public Result(ResultCode resultCode, string message )
public Result(ResultCode resultCode, string message)
{
this.ResultCode = resultCode;
this.Message = message;
@@ -42,7 +35,7 @@ namespace RRQMCore
public Result(ResultCode resultCode)
{
this.ResultCode = resultCode;
this.Message =resultCode.GetResString();
this.Message = resultCode.GetResString();
}
/// <summary>
@@ -50,7 +43,6 @@ namespace RRQMCore
/// </summary>
public ResultCode ResultCode { get; private set; }
/// <summary>
/// <inheritdoc/>
/// </summary>
@@ -65,4 +57,4 @@ namespace RRQMCore
return $"类型:{ResultCode},信息:{Message}";
}
}
}
}

View File

@@ -9,12 +9,6 @@
// 感谢您的下载和使用
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RRQMCore
{
@@ -48,4 +42,4 @@ namespace RRQMCore
/// </summary>
Canceled
}
}
}

View File

@@ -11,10 +11,6 @@
//------------------------------------------------------------------------------
using RRQMCore.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RRQMCore
{
@@ -44,4 +40,4 @@ namespace RRQMCore
return Resource.ResourceManager.GetString(@enum.ToString()).Format(objs);
}
}
}
}

View File

@@ -10,11 +10,7 @@
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
using RRQMCore.ByteManager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RRQMCore.Helper
{
@@ -30,9 +26,9 @@ namespace RRQMCore.Helper
/// <param name="offset"></param>
/// <param name="length"></param>
/// <returns></returns>
public static string ToUtf8String(this ByteBlock byteBlock,int offset,int length)
public static string ToUtf8String(this ByteBlock byteBlock, int offset, int length)
{
return Encoding.UTF8.GetString(byteBlock.Buffer,offset,length);
return Encoding.UTF8.GetString(byteBlock.Buffer, offset, length);
}
}
}
}

View File

@@ -10,11 +10,7 @@
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace RRQMCore.Helper
{
@@ -24,7 +20,7 @@ namespace RRQMCore.Helper
public static class EnumHelper
{
/// <summary>
/// 获取自定义attribute
/// 获取自定义attribute
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="enumObj"></param>
@@ -39,4 +35,4 @@ namespace RRQMCore.Helper
return (T)attr;
}
}
}
}

View File

@@ -11,7 +11,7 @@
//------------------------------------------------------------------------------
using System;
namespace RRQMCore.ByteManager
namespace RRQMCore.Pool
{
/// <summary>
/// 对象池接口

View File

@@ -10,7 +10,7 @@
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
namespace RRQMCore.ByteManager
namespace RRQMCore.Pool
{
/// <summary>
/// 对象池单位接口

View File

@@ -14,7 +14,7 @@ using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;
namespace RRQMCore.ByteManager
namespace RRQMCore.Pool
{
/// <summary>
/// 对象池
@@ -59,7 +59,8 @@ namespace RRQMCore.ByteManager
/// <summary>
/// 可使用(创建)数量
/// </summary>
public int FreeSize { get { return this.freeSize; } }
public int FreeSize
{ get { return this.freeSize; } }
private int freeSize;

View File

@@ -5,15 +5,15 @@
<Authors>若汝棋茗</Authors>
<PackageIcon>RRQM.png</PackageIcon>
<Copyright>Copyright © 2021 若汝棋茗</Copyright>
<Version>6.0.1</Version>
<Version>6.1.0</Version>
<LangVersion>9.0</LangVersion>
<SignAssembly>true</SignAssembly>
<Description>此程序集是RRQM的核心开源库其中包含了内存池、高性能序列化、日志接口在内的很多基本内容。
更新内容:
添加IResult
添加:资源
修复RRQM序列化Bug
修复RRQM序列化字典bug
修改重构BytePool改为静态类相关操作优化其运行逻辑
优化ByteBlock用法可以直接new新对象其实质效果和BytePool.GetByteBlock一致。
特别说明本程序集在源码里内嵌了Newtonsoft.Json 11.0.3,但为防止冲突,已修改其命名空间。
特此感谢其作者!!!

View File

@@ -49,9 +49,9 @@ namespace RRQMCore.Run
/// <typeparam name="T"></typeparam>
/// <param name="statu"></param>
/// <param name="action"></param>
public static void TaskRun<T>(T statu,Action<T> action)
public static void TaskRun<T>(T statu, Action<T> action)
{
Task.Run(()=>
Task.Run(() =>
{
action.Invoke(statu);
});

View File

@@ -225,9 +225,9 @@ namespace RRQMCore.Run
public Task RunAsync()
{
return Task.Run(() =>
{
this.Run();
});
{
this.Run();
});
}
/// <summary>

View File

@@ -20,4 +20,4 @@ namespace RRQMCore.Serialization
public class RRQMNonSerializedAttribute : Attribute
{
}
}
}

View File

@@ -81,6 +81,10 @@ namespace RRQMCore.Serialization
{
data = new byte[] { by };
}
else if (graph is sbyte sby)
{
data = BitConverter.GetBytes((short)sby);
}
else if (graph is bool b)
{
data = BitConverter.GetBytes(b);
@@ -89,14 +93,26 @@ namespace RRQMCore.Serialization
{
data = BitConverter.GetBytes(s);
}
else if (graph is int)
else if (graph is ushort us)
{
data = BitConverter.GetBytes((int)graph);
data = BitConverter.GetBytes(us);
}
else if (graph is int i)
{
data = BitConverter.GetBytes(i);
}
else if (graph is uint ui)
{
data = BitConverter.GetBytes(ui);
}
else if (graph is long l)
{
data = BitConverter.GetBytes(l);
}
else if (graph is ulong ul)
{
data = BitConverter.GetBytes(ul);
}
else if (graph is float f)
{
data = BitConverter.GetBytes(f);
@@ -109,6 +125,10 @@ namespace RRQMCore.Serialization
{
data = Encoding.UTF8.GetBytes(time.Ticks.ToString());
}
else if (graph is char c)
{
data = BitConverter.GetBytes(c);
}
else if (graph is Enum)
{
var enumValType = Enum.GetUnderlyingType(graph.GetType());
@@ -184,7 +204,7 @@ namespace RRQMCore.Serialization
PropertyInfo[] propertyInfos = this.GetProperties(type);
foreach (PropertyInfo property in propertyInfos)
{
if (property.SetMethod == null || property.GetCustomAttribute<RRQMNonSerializedAttribute>() != null)
if (property.GetCustomAttribute<RRQMNonSerializedAttribute>() != null)
{
continue;
}
@@ -274,6 +294,10 @@ namespace RRQMCore.Serialization
{
obj = datas[offset];
}
else if (type == RRQMReadonly.sbyteType)
{
obj = (sbyte)(BitConverter.ToInt16(datas, offset));
}
else if (type == RRQMReadonly.boolType)
{
obj = (BitConverter.ToBoolean(datas, offset));
@@ -281,15 +305,27 @@ namespace RRQMCore.Serialization
else if (type == RRQMReadonly.shortType)
{
obj = (BitConverter.ToInt16(datas, offset));
}
else if (type == RRQMReadonly.ushortType)
{
obj = (BitConverter.ToUInt16(datas, offset));
}
else if (type == RRQMReadonly.intType)
{
obj = (BitConverter.ToInt32(datas, offset));
}
else if (type == RRQMReadonly.uintType)
{
obj = (BitConverter.ToUInt32(datas, offset));
}
else if (type == RRQMReadonly.longType)
{
obj = (BitConverter.ToInt64(datas, offset));
}
else if (type == RRQMReadonly.ulongType)
{
obj = (BitConverter.ToUInt64(datas, offset));
}
else if (type == RRQMReadonly.floatType)
{
obj = (BitConverter.ToSingle(datas, offset));
@@ -302,6 +338,10 @@ namespace RRQMCore.Serialization
{
obj = (BitConverter.ToDouble(datas, offset));
}
else if (type == RRQMReadonly.charType)
{
obj = (BitConverter.ToChar(datas, offset));
}
else if (type == RRQMReadonly.dateTimeType)
{
obj = (new DateTime(long.Parse(Encoding.UTF8.GetString(datas, offset, len))));

View File

@@ -22,11 +22,6 @@ namespace RRQMCore.Serialization
/// </summary>
RRQMBinary,
/// <summary>
/// 系统二进制
/// </summary>
SystemBinary,
/// <summary>
/// Json
/// </summary>

View File

@@ -24,6 +24,8 @@ namespace RRQMCore.Serialization
/// </summary>
public static class SerializeConvert
{
#if NET45_OR_GREATER
#region
/// <summary>
@@ -173,6 +175,8 @@ namespace RRQMCore.Serialization
#endregion
#endif
#region RRQM二进制序列化
/// <summary>
@@ -198,7 +202,6 @@ namespace RRQMCore.Serialization
{
using (ByteBlock byteBlock = new ByteBlock() { @using = true })
{
byteBlock.SetBuffer(new byte[1024 * 10]);
RRQMBinarySerialize(byteBlock, obj, reserveAttributeName);
return byteBlock.ToArray();
}

View File

@@ -100,7 +100,7 @@ namespace RRQMCore.XREF.Newtonsoft.Json
/// <param name="writer">The <see cref="JsonWriter"/> to write to.</param>
/// <param name="value">The value.</param>
/// <param name="serializer">The calling serializer.</param>
public sealed override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
public override sealed void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
if (!(value != null ? value is T : ReflectionUtils.IsNullable(typeof(T))))
{
@@ -125,7 +125,7 @@ namespace RRQMCore.XREF.Newtonsoft.Json
/// <param name="existingValue">The existing value of object being read.</param>
/// <param name="serializer">The calling serializer.</param>
/// <returns>The object value.</returns>
public sealed override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
public override sealed object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
bool existingIsNull = existingValue == null;
if (!(existingIsNull || existingValue is T))
@@ -153,7 +153,7 @@ namespace RRQMCore.XREF.Newtonsoft.Json
/// <returns>
/// <c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>.
/// </returns>
public sealed override bool CanConvert(Type objectType)
public override sealed bool CanConvert(Type objectType)
{
return typeof(T).IsAssignableFrom(objectType);
}

View File

@@ -212,11 +212,7 @@ namespace RRQMCore.XREF.Newtonsoft.Json.Serialization
/// <param name="serializedType">The type of the object the formatter creates a new instance of.</param>
/// <param name="assemblyName">Specifies the <see cref="Assembly"/> name of the serialized object.</param>
/// <param name="typeName">Specifies the <see cref="System.Type"/> name of the serialized object.</param>
public
#if HAVE_SERIALIZATION_BINDER_BIND_TO_NAME
override
#endif
new void BindToName(Type serializedType, out string assemblyName, out string typeName)
public new void BindToName(Type serializedType, out string assemblyName, out string typeName)
{
#if !HAVE_FULL_REFLECTION
assemblyName = serializedType.GetTypeInfo().Assembly.FullName;

View File

@@ -3043,5 +3043,6 @@ namespace System.Runtime.CompilerServices
/// </remarks>
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Assembly)]
internal sealed class ExtensionAttribute : Attribute { }
internal sealed class ExtensionAttribute : Attribute
{ }
}