IProtocol.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using DeepCore;
  2. using DeepCore.IO;
  3. using DeepCore.Reflection;
  4. using DeepMMO.Attributes;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Reflection;
  9. using System.Text;
  10. namespace DeepMMO.Protocol
  11. {
  12. /// <summary>
  13. /// 网络序列化标识接口
  14. /// </summary>
  15. public interface INetProtocol : ISerializable { }
  16. public interface INetProtocolS2C { }
  17. public interface INetProtocolC2S { }
  18. public interface INetProtocolBotIgnore { }
  19. //---------------------------------------------------------------------------------------------
  20. /// <summary>
  21. /// 请求
  22. /// </summary>
  23. public abstract class Request : INetProtocol, INetProtocolC2S
  24. {
  25. public override string ToString()
  26. {
  27. return string.Format("{0}", GetType().Name);
  28. }
  29. }
  30. /// <summary>
  31. /// 回馈
  32. /// </summary>
  33. public abstract class Response : INetProtocol, INetProtocolS2C
  34. {
  35. [MessageCodeAttribute("成功")]
  36. public const int CODE_OK = 200;
  37. [MessageCodeAttribute("未知错误")]
  38. public const int CODE_ERROR = 500;
  39. /// <summary>
  40. /// 返回码
  41. /// </summary>
  42. public int s2c_code = CODE_OK;
  43. /// <summary>
  44. /// 返回信息(优先网络消息,如果网络消息为空,则从MessageCode中找)
  45. /// </summary>
  46. public string s2c_msg;
  47. /// <summary>
  48. /// 内部消息,用于一个系统的反馈,传递给原始请求
  49. /// </summary>
  50. public Response InnerResponse;
  51. /// <summary>
  52. /// 请求是否成功
  53. /// </summary>
  54. public bool IsSuccess
  55. {
  56. get { return s2c_code >= 200 && s2c_code <= 299; }
  57. }
  58. public override string ToString()
  59. {
  60. return string.Format("{0}: {1} : {2}", GetType().Name, s2c_code, s2c_msg);
  61. }
  62. public static bool CheckSuccess(Response rsp)
  63. {
  64. return (rsp != null && rsp.IsSuccess);
  65. }
  66. public virtual void EndRead()
  67. {
  68. if (s2c_msg == null)
  69. {
  70. if (s2c_code == CODE_OK || s2c_code == CODE_ERROR)
  71. {
  72. if (InnerResponse != null)
  73. {
  74. InnerResponse.EndRead();
  75. s2c_msg = InnerResponse.s2c_msg;
  76. }
  77. }
  78. if (s2c_msg == null)
  79. {
  80. s2c_msg = MessageCodeManager.Instance.GetCodeMessage(this);
  81. }
  82. if (s2c_msg == null && InnerResponse != null)
  83. {
  84. s2c_msg = InnerResponse.s2c_msg;
  85. }
  86. }
  87. }
  88. }
  89. /// <summary>
  90. /// 单向通知
  91. /// </summary>
  92. public abstract class Notify : INetProtocol
  93. {
  94. }
  95. //---------------------------------------------------------------------------------------------
  96. //---------------------------------------------------------------------------------------------
  97. }