forked from connamara/quickfixn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMessageCracker.cs
66 lines (56 loc) · 2 KB
/
MessageCracker.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QuickFix.Fields;
using System.Reflection;
namespace QuickFix
{
/// <summary>
/// Helper class for delegating message types for various FIX versions to
/// type-safe OnMessage methods.
/// </summary>
public abstract class MessageCracker
{
private Dictionary<Type, MethodInfo> _handlerMethods = new Dictionary<Type, MethodInfo>();
public MessageCracker()
{
initialize(this);
}
private void initialize(Object messageHandler)
{
Type handlerType = messageHandler.GetType();
MethodInfo[] methods = handlerType.GetMethods();
foreach (MethodInfo m in methods)
{
if (IsHandlerMethod(m))
{
_handlerMethods[m.GetParameters()[0].ParameterType] = m;
}
}
}
static public bool IsHandlerMethod(MethodInfo m)
{
return (m.IsPublic == true
&& m.Name.Equals("OnMessage")
&& m.GetParameters().Length == 2
&& m.GetParameters()[0].ParameterType.IsSubclassOf(typeof(QuickFix.Message))
&& typeof(QuickFix.SessionID).IsAssignableFrom(m.GetParameters()[1].ParameterType)
&& m.ReturnType == typeof(void));
}
/// <summary>
/// Process ("crack") a FIX message and call the registered handlers for that type, if any
/// </summary>
/// <param name="message"></param>
/// <param name="sessionID"></param>
public void Crack(Message message, SessionID sessionID)
{
Type messageType = message.GetType();
MethodInfo handler = null;
if (_handlerMethods.TryGetValue(messageType, out handler))
handler.Invoke(this, new object[] { message, sessionID });
else
throw new UnsupportedMessageType();
}
}
}