mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-07 16:45:10 +00:00
[+] TEngineServer
[+] TEngineServer
This commit is contained in:
47
Assets/GameScripts/DotNet/Core/Pool/ConcurrentPool.cs
Normal file
47
Assets/GameScripts/DotNet/Core/Pool/ConcurrentPool.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
namespace TEngine
|
||||
{
|
||||
/// <summary>
|
||||
/// 线程安全的静态通用对象池
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public static class ConcurrentPool<T>
|
||||
{
|
||||
private static readonly ConcurrentQueue<T> PoolQueue = new ConcurrentQueue<T>();
|
||||
|
||||
public static int Count => PoolQueue.Count;
|
||||
|
||||
public static T Rent()
|
||||
{
|
||||
return PoolQueue.TryDequeue(out var t) ? t : Activator.CreateInstance<T>();
|
||||
}
|
||||
|
||||
public static T Rent(Func<T> generator)
|
||||
{
|
||||
return PoolQueue.TryDequeue(out var t) ? t : generator();
|
||||
}
|
||||
|
||||
public static void Return(T t)
|
||||
{
|
||||
if (t == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
PoolQueue.Enqueue(t);
|
||||
}
|
||||
|
||||
public static void Return(T t, Action<T> reset)
|
||||
{
|
||||
if (t == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
reset(t);
|
||||
PoolQueue.Enqueue(t);
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/GameScripts/DotNet/Core/Pool/ConcurrentPool.cs.meta
Normal file
11
Assets/GameScripts/DotNet/Core/Pool/ConcurrentPool.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7af82d3e27c94b0498507beb740f96de
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
52
Assets/GameScripts/DotNet/Core/Pool/Pool.cs
Normal file
52
Assets/GameScripts/DotNet/Core/Pool/Pool.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace TEngine
|
||||
{
|
||||
/// <summary>
|
||||
/// 静态通用对象池
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public static class Pool<T>
|
||||
{
|
||||
private static readonly Queue<T> PoolQueue = new Queue<T>();
|
||||
|
||||
public static int Count => PoolQueue.Count;
|
||||
|
||||
public static T Rent()
|
||||
{
|
||||
return PoolQueue.Count == 0 ? Activator.CreateInstance<T>() : PoolQueue.Dequeue();
|
||||
}
|
||||
|
||||
public static T Rent(Func<T> generator)
|
||||
{
|
||||
return PoolQueue.Count == 0 ? generator() : PoolQueue.Dequeue();
|
||||
}
|
||||
|
||||
public static void Return(T t)
|
||||
{
|
||||
if (t == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
PoolQueue.Enqueue(t);
|
||||
}
|
||||
|
||||
public static void Return(T t, Action<T> reset)
|
||||
{
|
||||
if (t == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
reset(t);
|
||||
PoolQueue.Enqueue(t);
|
||||
}
|
||||
|
||||
public static void Clear()
|
||||
{
|
||||
PoolQueue.Clear();
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/GameScripts/DotNet/Core/Pool/Pool.cs.meta
Normal file
11
Assets/GameScripts/DotNet/Core/Pool/Pool.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5b3d07ffa12833d4a805acc09d939e7f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
46
Assets/GameScripts/DotNet/Core/Pool/PoolCore.cs
Normal file
46
Assets/GameScripts/DotNet/Core/Pool/PoolCore.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace TEngine
|
||||
{
|
||||
public abstract class PoolCore<T>
|
||||
{
|
||||
private readonly Action<T> _reset;
|
||||
private readonly Func<T> _generator;
|
||||
private readonly Stack<T> _objects = new Stack<T>();
|
||||
public int Count => _objects.Count;
|
||||
|
||||
/// <summary>
|
||||
/// 初始化一个池子
|
||||
/// </summary>
|
||||
/// <param name="generator">某些类型的构造函数中可能需要额外的参数,所以使用Func<T>生成器</param>
|
||||
/// <param name="reset">某些类型可能需要对返回的对象进行额外清理</param>
|
||||
/// <param name="initialCapacity">池子的初始大小、可以预先分配</param>
|
||||
protected PoolCore(Func<T> generator, Action<T> reset, int initialCapacity = 0)
|
||||
{
|
||||
_generator = generator;
|
||||
_reset = reset;
|
||||
|
||||
for (var i = 0; i < initialCapacity; ++i)
|
||||
{
|
||||
_objects.Push(generator());
|
||||
}
|
||||
}
|
||||
|
||||
public T Rent()
|
||||
{
|
||||
return _objects.Count > 0 ? _objects.Pop() : _generator();
|
||||
}
|
||||
|
||||
public void Return(T item)
|
||||
{
|
||||
_reset(item);
|
||||
_objects.Push(item);
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
_objects.Clear();
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/GameScripts/DotNet/Core/Pool/PoolCore.cs.meta
Normal file
11
Assets/GameScripts/DotNet/Core/Pool/PoolCore.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 89a4d3700191cee49b83574d93b0be08
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
54
Assets/GameScripts/DotNet/Core/Pool/PoolWithDisposable.cs
Normal file
54
Assets/GameScripts/DotNet/Core/Pool/PoolWithDisposable.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace TEngine
|
||||
{
|
||||
/// <summary>
|
||||
/// 静态通用对象池
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public static class PoolWithDisposable<T> where T : IDisposable
|
||||
{
|
||||
private static readonly Queue<T> PoolQueue = new Queue<T>();
|
||||
|
||||
public static int Count => PoolQueue.Count;
|
||||
|
||||
public static T Rent()
|
||||
{
|
||||
return PoolQueue.Count == 0 ? Activator.CreateInstance<T>() : PoolQueue.Dequeue();
|
||||
}
|
||||
|
||||
public static T Rent(Func<T> generator)
|
||||
{
|
||||
return PoolQueue.Count == 0 ? Activator.CreateInstance<T>() : PoolQueue.Dequeue();
|
||||
}
|
||||
|
||||
public static void Return(T t)
|
||||
{
|
||||
if (t == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
PoolQueue.Enqueue(t);
|
||||
t.Dispose();
|
||||
}
|
||||
|
||||
public static void Return(T t, Action<T> reset)
|
||||
{
|
||||
if (t == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
reset(t);
|
||||
PoolQueue.Enqueue(t);
|
||||
t.Dispose();
|
||||
}
|
||||
|
||||
public static void Clear()
|
||||
{
|
||||
PoolQueue.Clear();
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b6c920c7d1f7495459e82d52b6403332
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user