Skip to content

Commit 7b2f00c

Browse files
committed
Add Notice handler feature.
1 parent ce80c77 commit 7b2f00c

File tree

8 files changed

+102
-13
lines changed

8 files changed

+102
-13
lines changed

Quick.Protocol/CommandExecuterManager.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@ public class CommandExecuterManager
1010
{
1111
private Dictionary<string, Delegate> commandExecuterDict = new Dictionary<string, Delegate>();
1212

13-
public CommandExecuterManager()
14-
{
15-
16-
}
17-
1813
/// <summary>
1914
/// 获取全部注册的命令请求类型名称
2015
/// </summary>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Quick.Protocol
7+
{
8+
public class NoticeHandlerManager
9+
{
10+
private Dictionary<string, Delegate> noticeHandlerDict = new Dictionary<string, Delegate>();
11+
12+
/// <summary>
13+
/// 获取全部注册的通知类型名称
14+
/// </summary>
15+
public string[] GetRegisterNoticeTypeNames() => noticeHandlerDict.Keys.ToArray();
16+
17+
public void Register(string noticeTypeName, Delegate noticeHandler)
18+
{
19+
noticeHandlerDict[noticeTypeName] = noticeHandler;
20+
}
21+
22+
public void Register<TNotice>(Action<QpChannel, TNotice> noticeHandler)
23+
where TNotice : class, new()
24+
{
25+
var noticeTypeName = typeof(TNotice).FullName;
26+
Register(noticeTypeName, noticeHandler);
27+
}
28+
29+
30+
/// <summary>
31+
/// 处理通知
32+
/// </summary>
33+
/// <param name="handler"></param>
34+
/// <param name="noticeTypeName"></param>
35+
/// <param name="noticeModel"></param>
36+
/// <returns></returns>
37+
public virtual void HandleNotice(QpChannel handler, string noticeTypeName, object noticeModel)
38+
{
39+
if (!CanHandleNoticed(noticeTypeName))
40+
return;
41+
Delegate noticeHandler = noticeHandlerDict[noticeTypeName];
42+
noticeHandler.DynamicInvoke(new object[] { handler, noticeModel });
43+
}
44+
45+
/// <summary>
46+
/// 能否处理指定类型的通知
47+
/// </summary>
48+
/// <param name="noticeTypeName"></param>
49+
/// <returns></returns>
50+
public virtual bool CanHandleNoticed(string noticeTypeName)
51+
{
52+
return noticeHandlerDict.ContainsKey(noticeTypeName);
53+
}
54+
}
55+
}

Quick.Protocol/QpChannel.cs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -707,18 +707,32 @@ protected void OnRawNoticePackageReceived(string typeName, string content)
707707
TypeName = typeName,
708708
Content = content
709709
});
710+
//如果在字典中未找到此类型名称,则直接返回
711+
if (!noticeTypeDict.ContainsKey(typeName))
712+
return;
713+
var contentModel = JsonConvert.DeserializeObject(content, noticeTypeDict[typeName]);
714+
715+
//处理通知
716+
var hasNoticeHandler = false;
717+
if (options.NoticeHandlerManagerList != null)
718+
foreach (var noticeHandlerManager in options.NoticeHandlerManagerList)
719+
{
720+
if (noticeHandlerManager.CanHandleNoticed(typeName))
721+
{
722+
hasNoticeHandler = true;
723+
noticeHandlerManager.HandleNotice(this, typeName, contentModel);
724+
break;
725+
}
726+
}
710727

711728
//如果配置了触发NoticePackageReceived事件
712729
if (options.RaiseNoticePackageReceivedEvent)
713-
{
714-
//如果在字典中未找到此类型名称,则直接返回
715-
if (!noticeTypeDict.ContainsKey(typeName))
716-
return;
717-
var contentModel = JsonConvert.DeserializeObject(content, noticeTypeDict[typeName]);
730+
{
718731
NoticePackageReceived?.Invoke(this, new NoticePackageReceivedEventArgs()
719732
{
720733
TypeName = typeName,
721-
ContentModel = contentModel
734+
ContentModel = contentModel,
735+
Handled = hasNoticeHandler
722736
});
723737
}
724738
}

Quick.Protocol/QpChannelOptions.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ public virtual void Check()
8585
[Browsable(false)]
8686
[JsonIgnore]
8787
public List<CommandExecuterManager> CommandExecuterManagerList { get; set; } = new List<CommandExecuterManager>();
88+
/// <summary>
89+
/// 通知处理器管理器列表
90+
/// </summary>
91+
[Browsable(false)]
92+
[JsonIgnore]
93+
public List<NoticeHandlerManager> NoticeHandlerManagerList { get; set; } = new List<NoticeHandlerManager>();
8894

8995
// <summary>
9096
// 注册指令执行器管理器
@@ -93,5 +99,14 @@ public void RegisterCommandExecuterManager(CommandExecuterManager commandExecute
9399
{
94100
CommandExecuterManagerList.Add(commandExecuterManager);
95101
}
102+
103+
/// <summary>
104+
/// 注册通知处理器管理器
105+
/// </summary>
106+
/// <param name="noticeHandlerManager"></param>
107+
public void RegisterNoticeHandlerManager(NoticeHandlerManager noticeHandlerManager)
108+
{
109+
NoticeHandlerManagerList.Add(noticeHandlerManager);
110+
}
96111
}
97112
}

Quick.Protocol/QpEventArgs.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ public class NoticePackageReceivedEventArgs : QpEventArgs
3636
/// 内容模型
3737
/// </summary>
3838
public object ContentModel { get; set; }
39+
/// <summary>
40+
/// 是否已处理
41+
/// </summary>
42+
public bool Handled { get; set; }
3943
}
4044

4145
/// <summary>

Quick.Protocol/QpServerChannel.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ public class QpServerChannel : QpChannel
1919
private string question;
2020
//通过认证后,才允许使用的命令执行管理器列表
2121
private List<CommandExecuterManager> authedCommandExecuterManagerList = null;
22-
22+
//通过认证后,才允许使用的通知处理器管理器列表
23+
private List<NoticeHandlerManager> authedNoticeHandlerManagerList = null;
2324
public string ChannelName { get; private set; }
2425

2526
/// <summary>
@@ -38,6 +39,8 @@ public QpServerChannel(QpServer server, Stream stream, string channelName, Cance
3839
this.ChannelName = channelName;
3940
this.options = options;
4041
this.authedCommandExecuterManagerList = options.CommandExecuterManagerList;
42+
this.authedNoticeHandlerManagerList = options.NoticeHandlerManagerList;
43+
4144
cts = new CancellationTokenSource();
4245
cancellationToken.Register(() => Stop());
4346
//修改缓存大小
@@ -50,6 +53,7 @@ public QpServerChannel(QpServer server, Stream stream, string channelName, Cance
5053
connectAndAuthCommandExecuterManager.Register(new Commands.HandShake.Request(), handShake);
5154
connectAndAuthCommandExecuterManager.Register(new Commands.GetQpInstructions.Request(), getQpInstructions);
5255
options.CommandExecuterManagerList = new List<CommandExecuterManager>() { connectAndAuthCommandExecuterManager };
56+
options.NoticeHandlerManagerList = null;
5357

5458
InitQpPackageHandler_Stream(stream);
5559
//开始读取其他数据包
@@ -117,6 +121,7 @@ private Commands.Authenticate.Response authenticate(QpChannel handler, Commands.
117121
private Commands.HandShake.Response handShake(QpChannel handler, Commands.HandShake.Request request)
118122
{
119123
options.CommandExecuterManagerList.AddRange(authedCommandExecuterManagerList);
124+
options.NoticeHandlerManagerList = authedNoticeHandlerManagerList;
120125
options.InternalCompress = request.EnableCompress;
121126
options.InternalEncrypt = request.EnableEncrypt;
122127
options.InternalTransportTimeout = request.TransportTimeout;

Quick.Protocol/QpServerOptions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public virtual QpServerOptions Clone()
3232
var ret = JsonConvert.DeserializeObject<QpServerOptions>(JsonConvert.SerializeObject(this));
3333
ret.InstructionSet = InstructionSet;
3434
ret.CommandExecuterManagerList = CommandExecuterManagerList;
35+
ret.NoticeHandlerManagerList = NoticeHandlerManagerList;
3536
ret.ProtocolErrorHandler = ProtocolErrorHandler;
3637
return ret;
3738
}

Test/Tcp/TcpServer/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ static void Main(string[] args)
3434
</head>
3535
<body>
3636
<p>Welcome to use <b>Quick.Protocol</b>.</p>
37-
<p>Source Code:<a href=""http://github.com/aaasoft/Quick.Protocol"">http://github.com/aaasoft/Quick.Protocol</a></p>
37+
<p>Source Code:<a href=""https://github.com/QuickProtocol"">https://github.com/QuickProtocol</a></p>
3838
<p>Time:{DateTime.Now}</p>
3939
</body>
4040
</html>";

0 commit comments

Comments
 (0)