This repository has been archived by the owner on Aug 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathEasyTcpActionServer.cs
86 lines (77 loc) · 3.37 KB
/
EasyTcpActionServer.cs
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
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;
using EasyTcp4.Actions.ActionsCore;
using EasyTcp4.Actions.Utils;
using EasyTcp4.Protocols;
namespace EasyTcp4.Actions
{
public class EasyTcpActionServer : EasyTcpServer
{
/// <summary>
/// Dictionary with all loaded actions of server [action code, action method]
/// </summary>
public Dictionary<int, LoadedAction> Actions = new Dictionary<int, LoadedAction>();
/// <summary>
/// Function that determines whether an action should be executed
/// </summary>
public Func<Message, bool> Interceptor;
/// <summary>
/// Event that is fired when an unknown action is received
/// </summary>
public event EventHandler<Message> OnUnknownAction;
/// <summary>
/// Fire the OnUnknownAction event
/// </summary>
public void FireOnUnknownAction(Message e) => OnUnknownAction?.Invoke(this, e);
/// <summary>
/// Load new actions from an assembly
/// </summary>
/// <param name="assembly">assembly with actions</param>
/// <param name="nameSpace">only load actions from a specific namespace</param>
public void LoadActionsFromAssembly(Assembly assembly, string nameSpace = null)
{
foreach (var action in ReflectionCore.GetActionsFromAssembly(assembly, nameSpace))
Actions.Add(action.Key, action.Value);
}
/// <summary>
/// Create new EasyTcpActionServer and load actions from (calling) assembly
/// </summary>
/// <param name="protocol"></param>
/// <param name="assembly">assembly with actions, calling assembly if null</param>
/// <param name="nameSpace">only load actions from a specific namespace</param>
public EasyTcpActionServer(IEasyProtocol protocol = null, Assembly assembly = null, string nameSpace = null) : base(protocol)
{
LoadActionsFromAssembly(assembly ?? Assembly.GetCallingAssembly(), nameSpace);
OnDataReceiveAsync += async (sender, message) =>
{
try { await ExecuteAction(message.ConvertToActionMessage()); }
catch (Exception ex) { FireOnError(ex); }
};
}
/// <summary>
/// Execute action
/// </summary>
/// <param name="message">message with an action code</param>
public async Task ExecuteAction(Message message)
{
if (Actions.TryGetValue(message.GetActionCode(), out var action)) await action.TryExecute(this, message, Interceptor);
else OnUnknownAction?.Invoke(this, message);
}
/// <summary>
/// Execute action
/// </summary>
/// <param name="actionCode"></param>
/// <param name="message">message without action code</param>
public Task ExecuteAction(int actionCode, Message message = null)
=> ExecuteAction((message ?? new Message()).SetActionCode(actionCode));
/// <summary>
/// Execute action
/// </summary>
/// <param name="actionCode"></param>
/// <param name="message">message without action code</param>
public Task ExecuteAction(string actionCode, Message message = null)
=> ExecuteAction(actionCode.ToActionCode(), message);
}
}