1. 修改了Serein.Library中的ObjectPool工具类,提供了对象归还的默认处理方法(通过构造方法指定Action<T>委托)

2. 不再显式调用流程上下文的Reset()方法,而改为通过对象池调用
3. 同样的,对于同样使用了ObjectPool管理上下文的Serein.Proto.WebSocket项目而言,也进行了同2.一样的修改
This commit is contained in:
fengjiayi
2025-08-23 16:19:30 +08:00
parent 5656619a3b
commit 819320f355
8 changed files with 47 additions and 49 deletions

View File

@@ -344,7 +344,7 @@ namespace Serein.Proto.WebSocket.Handle
return;
}
// 返回结果
var responseData = context.OnReplyMakeData(context, data);
var responseData = context.OnReplyMakeData?.Invoke(context, data);
if (responseData is null)
{
context.TriggerExceptionTracking(new ArgumentNullException($"处理回调函数 OnReplyMakeData 返回 null"));

View File

@@ -325,14 +325,22 @@ namespace Serein.Proto.WebSocket
{
await SocketExtension.SendAsync(webSocket, text); // 回复客户端,处理方法中入参如果需要发送消息委托,则将该回调方法作为委托参数传入
}
/*
ObjectPool<WebSocketMsgContext> contextPool = new ObjectPool<WebSocketMsgContext>(() =>
{
return new WebSocketMsgContext(sendasync);
}, 20);
var context = contextPool.Allocate();
contextPool.Free(context);
*/
ObjectPool<WebSocketHandleContext> contextPool = new ObjectPool<WebSocketHandleContext>(() =>
{
var context = new WebSocketHandleContext(sendasync);
context.OnExceptionTracking = _onExceptionTracking;
context.OnReplyMakeData = _onReplyMakeData;
context.OnReply = _onReply;
return context;
}, context =>
{
context.MsgRequest = null;
context.MsgData = null;
context.ErrorMessage = null;
context.Model = null;
});
while (webSocket.State == WebSocketState.Open)
{
var message = await tranTool.WaitMsgAsync(); // 有消息时通知
@@ -341,12 +349,10 @@ namespace Serein.Proto.WebSocket
Console.WriteLine($"WebSocket 消息解析失败: {message}");
continue;
}
var context = new WebSocketHandleContext(sendasync);
var context = contextPool.Allocate();
context.MsgRequest = jsonReques;
context.OnExceptionTracking = _onExceptionTracking;
context.OnReplyMakeData = _onReplyMakeData;
context.OnReply = _onReply;
await HandleAsync(context); // 处理消息
contextPool.Free(context);
}
}