创建C#工程

SimpleServer

打开Visual Studio,使用.NET 6创建一个控制台项目:

项目名称为:SimpleServer。注意指定好项目文件夹。

项目创建好后,在解决方案资源管理器中添加一些新文件夹:Business(业务相关)、Net(网络相关)、Proto(协议相关)

ServerBase

选择“文件——新建——项目”,再次使用.NET 6创建一个控制台项目:

项目名称为:ServerBase,解决方案:添加到解决方案。

在这个项目中添加一些工具类,比如说AES加密,Protobuff,SingletonPattern等。由于是工具类,不设命名空间。

我们将SingletonPattern修改为多线程单例,instance == null判断两次,第一次是防止重复加锁,提高性能,第二次是防止重复实例化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class SingletonPattern<T> where T : class,new()
{
private static object _lock = new object();
private static T? instance;
public static T Instance
{
get
{
if (instance == null)
{
lock (_lock)
{
if (instance == null)
{
instance = new T();
}
}
}
return instance;
}
}
}

AES

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
using System.Security.Cryptography;
using System.Text;

public class AES
{
private static string AESHead = "AESEncrypt";

/// <summary>
/// 文件加密,传入文件路径
/// </summary>
public static void AESFileEncrypt(string path, string EncryptKey)
{
if (!File.Exists(path))
return;

try
{
using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
if (fs != null)
{
byte[] headBuff = new byte[10];
fs.Read(headBuff, 0, 10);
string headTag = Encoding.UTF8.GetString(headBuff);
if (headTag == AESHead)
{
#if UNITY_EDITOR
Debug.Log(path + "已经加密过了!");
#endif
return;
}
fs.Seek(0, SeekOrigin.Begin);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, Convert.ToInt32(fs.Length));
fs.Seek(0, SeekOrigin.Begin);
fs.SetLength(0);
byte[] headBuffer = Encoding.UTF8.GetBytes(AESHead);
fs.Write(headBuffer, 0, 10);
byte[] EncBuffer = AESEncrypt(buffer, EncryptKey);
fs.Write(EncBuffer, 0, EncBuffer.Length);
}
}
}
catch (Exception e)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(e);
}
}

/// <summary>
/// 文件解密,传入文件路径 (改动了加密文件,不合适运行时)
/// </summary>
public static void AESFileDecrypt(string path, string EncryptKey)
{
if (!File.Exists(path))
return;

try
{
using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
if (fs != null)
{
byte[] headBuff = new byte[10];
fs.Read(headBuff, 0, headBuff.Length);
string headTag = Encoding.UTF8.GetString(headBuff);
if (headTag == AESHead)
{
byte[] buffer = new byte[fs.Length - headBuff.Length];
fs.Read(buffer, 0, Convert.ToInt32(fs.Length - headBuff.Length));
fs.Seek(0, SeekOrigin.Begin);
fs.SetLength(0);
byte[] EncBuffer = AESDecrypt(buffer, EncryptKey);
fs.Write(EncBuffer, 0, EncBuffer.Length);
}
}
}
}
catch (Exception e)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(e);
}
}

/// <summary>
/// 文件解密,传入文件路径,返回字节
/// </summary>
public static byte[] AESFileByteDecrypt(string path, string EncryptKey)
{
if (!File.Exists(path))
return null;

byte[] EncBuffer = null;
try
{
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
{
if (fs != null)
{
byte[] headBuff = new byte[10];
fs.Read(headBuff, 0, headBuff.Length);
string headTag = Encoding.UTF8.GetString(headBuff);
if (headTag == AESHead)
{
byte[] buffer = new byte[fs.Length - headBuff.Length];
fs.Read(buffer, 0, Convert.ToInt32(fs.Length - headBuff.Length));
EncBuffer = AESDecrypt(buffer, EncryptKey);
}
}
}
}
catch (Exception e)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(e);
}

return EncBuffer;
}

/// <summary>
/// AES 加密(高级加密标准,是下一代的加密算法标准,速度快,安全级别高,目前 AES 标准的一个实现是 Rijndael 算法)
/// </summary>
/// <param name="EncryptString">待加密密文</param>
/// <param name="EncryptKey">加密密钥</param>
public static string AESEncrypt(string EncryptString, string EncryptKey)
{
return Convert.ToBase64String(AESEncrypt(Encoding.Default.GetBytes(EncryptString), EncryptKey));
}

/// <summary>
/// AES 加密(高级加密标准,是下一代的加密算法标准,速度快,安全级别高,目前 AES 标准的一个实现是 Rijndael 算法)
/// </summary>
/// <param name="EncryptString">待加密密文</param>
/// <param name="EncryptKey">加密密钥</param>
public static byte[] AESEncrypt(byte[] EncryptByte, string EncryptKey)
{
if (EncryptByte.Length == 0) { throw (new Exception("明文不得为空")); }
if (string.IsNullOrEmpty(EncryptKey)) { throw (new Exception("密钥不得为空")); }
byte[] m_strEncrypt;
byte[] m_btIV = Convert.FromBase64String("Rkb4jvUy/ye7Cd7k89QQgQ==");
byte[] m_salt = Convert.FromBase64String("gsf4jvkyhye5/d7k8OrLgM==");
Rijndael m_AESProvider = Rijndael.Create();
try
{
MemoryStream m_stream = new MemoryStream();
PasswordDeriveBytes pdb = new PasswordDeriveBytes(EncryptKey, m_salt);
ICryptoTransform transform = m_AESProvider.CreateEncryptor(pdb.GetBytes(32), m_btIV);
CryptoStream m_csstream = new CryptoStream(m_stream, transform, CryptoStreamMode.Write);
m_csstream.Write(EncryptByte, 0, EncryptByte.Length);
m_csstream.FlushFinalBlock();
m_strEncrypt = m_stream.ToArray();
m_stream.Close(); m_stream.Dispose();
m_csstream.Close(); m_csstream.Dispose();
}
catch (IOException ex) { throw ex; }
catch (CryptographicException ex) { throw ex; }
catch (ArgumentException ex) { throw ex; }
catch (Exception ex) { throw ex; }
finally { m_AESProvider.Clear(); }
return m_strEncrypt;
}


/// <summary>
/// AES 解密(高级加密标准,是下一代的加密算法标准,速度快,安全级别高,目前 AES 标准的一个实现是 Rijndael 算法)
/// </summary>
/// <param name="DecryptString">待解密密文</param>
/// <param name="DecryptKey">解密密钥</param>
public static string AESDecrypt(string DecryptString, string DecryptKey)
{
return Convert.ToBase64String(AESDecrypt(Encoding.Default.GetBytes(DecryptString), DecryptKey));
}

/// <summary>
/// AES 解密(高级加密标准,是下一代的加密算法标准,速度快,安全级别高,目前 AES 标准的一个实现是 Rijndael 算法)
/// </summary>
/// <param name="DecryptString">待解密密文</param>
/// <param name="DecryptKey">解密密钥</param>
public static byte[] AESDecrypt(byte[] DecryptByte, string DecryptKey)
{
if (DecryptByte.Length == 0) { throw (new Exception("密文不得为空")); }
if (string.IsNullOrEmpty(DecryptKey)) { throw (new Exception("密钥不得为空")); }
byte[] m_strDecrypt;
byte[] m_btIV = Convert.FromBase64String("Rkb4jvUy/ye7Cd7k89QQgQ==");
byte[] m_salt = Convert.FromBase64String("gsf4jvkyhye5/d7k8OrLgM==");
Rijndael m_AESProvider = Rijndael.Create();
try
{
MemoryStream m_stream = new MemoryStream();
PasswordDeriveBytes pdb = new PasswordDeriveBytes(DecryptKey, m_salt);
ICryptoTransform transform = m_AESProvider.CreateDecryptor(pdb.GetBytes(32), m_btIV);
CryptoStream m_csstream = new CryptoStream(m_stream, transform, CryptoStreamMode.Write);
m_csstream.Write(DecryptByte, 0, DecryptByte.Length);
m_csstream.FlushFinalBlock();
m_strDecrypt = m_stream.ToArray();
m_stream.Close(); m_stream.Dispose();
m_csstream.Close(); m_csstream.Dispose();
}
catch (IOException ex) { throw ex; }
catch (CryptographicException ex) { throw ex; }
catch (ArgumentException ex) { throw ex; }
catch (Exception ex) { throw ex; }
finally { m_AESProvider.Clear(); }
return m_strDecrypt;
}
}

SingletonPattern(非多线程版,弃用)

1
2
3
4
5
public class SingletonPattern<T> where T : class,new()
{
public static T Instance => instance ??= new T();
private static T? instance;
}

ObjectManager

这个对象池管理器是直接移过来的,里面有一些代码和之前学过的不一样。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
    //类对象池
#if UNITY_EDITOR&&OBJ_POOL_TRACE_STACK
public class ClassObjectPool<T>:ClassPoolBase where T : class, new()
#else
public class ClassObjectPool<T> where T : class, new()
#endif
{
//池
protected Stack<T> mPool = new Stack<T>();
//最大对象个数,<=0表示不限
protected int miMaxCount;
//没有回收的对象个数
protected int m_nNoRecycleCount = 0;

#if UNITY_EDITOR && OBJ_POOL_TRACE_STACK
protected bool m_bHavePromptPoolIsEmpty = false;//是否提示过池已经空了,只提示一次
protected Dictionary<T, string> m_NoRecycleItem = new Dictionary<T, string>();
//打印日志
protected override void PrintLog()
{
if (m_nNoRecycleCount > 0)
{
string msg = "从类对象池申请的对象没有全部回收,有泄漏可能性,请检查:" + GetType().ToString() + ",未回收个数" + m_nNoRecycleCount + ",申请调用堆栈如下:\r\n";
Dictionary<T, string>.Enumerator it = m_NoRecycleItem.GetEnumerator();
int i = 1;
while (it.MoveNext())
{
msg += "第" + (i++) + "次:\r\n" + it.Current.Value;
if (i > 10)
{
break;
}
}

GDebugger.LogError(msg);
}
}
#endif

//iNewCountPercent,开始初始化iMaxCount的百分之多少个
public ClassObjectPool(int iMaxCount)
{
miMaxCount = iMaxCount;
for (int i = 0; i < iMaxCount; i++)
{
mPool.Push(new T());
}

#if UNITY_EDITOR && OBJ_POOL_TRACE_STACK
m_PoolList.Add(this);
#endif
}

//扩大池
public void Enlarge(int iExpandCount)
{
if (miMaxCount >= 0)
{
miMaxCount = m_nNoRecycleCount = mPool.Count + iExpandCount;
}

for (int i = 0; i < iExpandCount; i++)
{
mPool.Push(new T());
}
}

//产生一个新对象
public T? Spawn(bool bCreateIfPoolEmpty = true)//bCreateIfPoolEmpty如果pool为空时是否new新的出来
{
if (mPool.Count > 0)
{
T rtn = mPool.Pop();
if (rtn == null)
{
#if UNITY_EDITOR && OBJ_POOL_TRACE_STACK
GDebugger.LogError("Spawn is null");
#endif
if (bCreateIfPoolEmpty)
{
rtn = new T();
}
}
m_nNoRecycleCount++;
#if UNITY_EDITOR && OBJ_POOL_TRACE_STACK
m_NoRecycleItem.Add(rtn, StackTraceUtility.ExtractStackTrace());
#endif
return rtn;
}

#if UNITY_EDITOR && OBJ_POOL_TRACE_STACK
if (miMaxCount > 0 && !m_bHavePromptPoolIsEmpty)
{
m_bHavePromptPoolIsEmpty = true;
GDebugger.LogError("对象池已经空了,是否初始池太小或者没有完全回收?当前设定大小为:" + miMaxCount + "," + GetType().ToString());
}
#endif

if (bCreateIfPoolEmpty)
{
T rtn = new T();
m_nNoRecycleCount++;
#if UNITY_EDITOR && OBJ_POOL_TRACE_STACK
m_NoRecycleItem.Add(rtn, StackTraceUtility.ExtractStackTrace());
#endif
return rtn;
}

return null;
}
//回收一个对象到对象池
public bool Recycle(T? obj)
{
if (obj == null)
{
return false;
}

m_nNoRecycleCount--;
#if UNITY_EDITOR && OBJ_POOL_TRACE_STACK
m_NoRecycleItem.Remove(obj);
#endif

if (mPool.Count >= miMaxCount && miMaxCount > 0)
{
obj = null;
return false;
}

#if UNITY_EDITOR && OBJ_POOL_TRACE_STACK
if (mPool.Contains(obj))
{
GDebugger.LogError("同一个对象被回收了多次,请检查");
}
else
#endif
{
mPool.Push(obj);
}
return true;
}
}

#if UNITY_EDITOR && OBJ_POOL_TRACE_STACK
public class ClassPoolBase
{
protected virtual void PrintLog() { }
protected static List<ClassPoolBase> m_PoolList = new List<ClassPoolBase>();
//析构函数
public static void CheckRecycle()
{
int iCount = m_PoolList.Count;
for (int i = 0; i < iCount; i++)
{
ClassPoolBase item = m_PoolList[i];
item.PrintLog();
}
}
}
#endif

class ObjectManager :SingletonPattern<ObjectManager>
{
//类对象池列表
protected static Dictionary<System.Type, object> m_dClassPool = new Dictionary<System.Type, object>();

/// <summary>
/// 创建类对象池,创建完以后可以外面保存WooolObjectPool<T>,然后调用Spawn和Recycle来创建和回收类对象
/// </summary>
/// <typeparam name="T">只要是类</typeparam>
/// <param name="iMaxCount">最大个数</param>
/// <returns></returns>
public static ClassObjectPool<T>? GetOrCreateClassPool<T>(int iMaxCount) where T : class, new()
{
System.Type theType = typeof(T);
object? outObj = null;
if (!m_dClassPool.TryGetValue(theType, out outObj) || outObj == null)
{
ClassObjectPool<T> newPool = new ClassObjectPool<T>(iMaxCount);
m_dClassPool.Add(theType, newPool);
return newPool;
}

return outObj as ClassObjectPool<T>;
}
}

ServerBase准备好后,在Simple Server的“依赖项”中右键——“添加项目引用”,将ServerBase这个项目依赖过来