AMHandler.cs 696 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. using System;
  2. namespace ETModel
  3. {
  4. public abstract class AMHandler<Message> : IMHandler where Message: class
  5. {
  6. protected abstract ETTask Run(Session session, Message message);
  7. public async ETVoid Handle(Session session, object msg)
  8. {
  9. Message message = msg as Message;
  10. if (message == null)
  11. {
  12. Log.Error($"消息类型转换错误: {msg.GetType().Name} to {typeof(Message).Name}");
  13. return;
  14. }
  15. if (session.IsDisposed)
  16. {
  17. Log.Error($"session disconnect {msg}");
  18. return;
  19. }
  20. try
  21. {
  22. await this.Run(session, message);
  23. }
  24. catch (Exception e)
  25. {
  26. Log.Error(e);
  27. }
  28. }
  29. public Type GetMessageType()
  30. {
  31. return typeof(Message);
  32. }
  33. }
  34. }