using System; using System.Collections.Generic; namespace UniJSON { public class RpcDispatcher where T : IListTreeItem, IValue { delegate void Callback(int id, ListTreeNode args, IRpc f); Dictionary m_map = new Dictionary(); #region Action public void Register(string method, Action 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(string method, Action action) { throw new NotImplementedException(); } #endregion #region Func public void Register(string method, Func 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 args) { Callback callback; if (!m_map.TryGetValue(method, out callback)) { throw new KeyNotFoundException(); } callback(id, args, f); } } }