RpcDispatcher.cs
2.12 KB
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
using System;
using System.Collections.Generic;
namespace UniJSON
{
public class RpcDispatcher<T>
where T : IListTreeItem, IValue<T>
{
delegate void Callback(int id, ListTreeNode<T> args, IRpc f);
Dictionary<string, Callback> m_map = new Dictionary<string, Callback>();
#region Action
public void Register<A0>(string method, Action<A0> action)
{
m_map.Add(method, (id, args, f) =>
{
var it = args.ArrayItems().GetEnumerator();
var a0 = default(A0);
it.MoveNext();
it.Current.Deserialize(ref a0);
try
{
action(a0);
f.ResponseSuccess(id);
}
catch(Exception ex)
{
f.ResponseError(id, ex);
}
});
}
public void Register<A0, A1>(string method, Action<A0, A1> action)
{
throw new NotImplementedException();
}
#endregion
#region Func
public void Register<A0, A1, R>(string method, Func<A0, A1, R> action)
{
m_map.Add(method, (id, args, f) =>
{
var it = args.ArrayItems().GetEnumerator();
var a0 = default(A0);
it.MoveNext();
it.Current.Deserialize(ref a0);
var a1 = default(A1);
it.MoveNext();
it.Current.Deserialize(ref a1);
try
{
var r = action(a0, a1);
f.ResponseSuccess(id, r);
}
catch(Exception ex)
{
f.ResponseError(id, ex);
}
});
}
#endregion
public void Call(IRpc f, int id, string method, ListTreeNode<T> args)
{
Callback callback;
if (!m_map.TryGetValue(method, out callback))
{
throw new KeyNotFoundException();
}
callback(id, args, f);
}
}
}