LJServerBase.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using LJLib.Net.SPI.Com;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. namespace LJProxy.LJLib.Net.SPI.Server
  7. {
  8. public abstract class LJServerBase : ILJServer
  9. {
  10. public delegate LJResponse DHandle(ILJRequest request, object state);
  11. protected Dictionary<string, DHandle> handlers = new Dictionary<string, DHandle>(StringComparer.OrdinalIgnoreCase);
  12. protected Dictionary<string, Type> requestTypes = new Dictionary<string, Type>(StringComparer.OrdinalIgnoreCase);
  13. public LJResponse DoExcute(ILJRequest request, object state)
  14. {
  15. if (!handlers.ContainsKey(request.GetApiName()))
  16. {
  17. throw new Exception("接口未定义:" + request.GetApiName().ToString());
  18. }
  19. var handler = handlers[request.GetApiName()];
  20. if (handler == null)
  21. {
  22. throw new Exception("接口未定义:" + request.GetApiName().ToString());
  23. }
  24. return handler(request, state);
  25. }
  26. public Type GetRequestType(string apiName)
  27. {
  28. if (requestTypes.ContainsKey(apiName))
  29. {
  30. return requestTypes[apiName];
  31. }
  32. else
  33. {
  34. return null;
  35. }
  36. }
  37. public void AddMap(string apiName,Type requestType,DHandle handler)
  38. {
  39. requestTypes[apiName] = requestType;
  40. handlers[apiName] = handler;
  41. }
  42. }
  43. }