LJServerBase.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using System;
  2. using System.Collections.Generic;
  3. using LJLib.Net.SPI.Com;
  4. namespace LJLib.Net.SPI.Server
  5. {
  6. public abstract class LJServerBase : ILJServer
  7. {
  8. public delegate LJResponse DHandle(ILJRequest request, object state); // 委托声明
  9. protected Dictionary<string, DHandle> handlers = new Dictionary<string, DHandle>(); // 缓存所有处理
  10. protected Dictionary<string, Type> requestTypes = new Dictionary<string, Type>();
  11. public LJResponse DoExcute(ILJRequest request, object state)
  12. {
  13. if (!handlers.ContainsKey(request.GetApiName()))
  14. {
  15. throw new Exception("接口未定义:" + request.GetApiName().ToString());
  16. }
  17. DHandle handler = handlers[request.GetApiName()];// 必成从缓存读取
  18. if (handler == null)
  19. {
  20. throw new Exception("接口未定义:" + request.GetApiName().ToString());
  21. }
  22. return handler(request, state);
  23. }
  24. public Type GetRequestType(string apiName)
  25. {
  26. if (requestTypes.ContainsKey(apiName))
  27. {
  28. return requestTypes[apiName];
  29. }
  30. else
  31. {
  32. throw new Exception("接口未定义:" + apiName);
  33. }
  34. }
  35. public void AddMap(string apiName, Type requestType, DHandle handler)
  36. {
  37. requestTypes[apiName] = requestType;
  38. handlers[apiName] = handler;
  39. }
  40. }
  41. }