Session.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Collections;
  5. using CommonLang.Protocol;
  6. using CommonLang.Net;
  7. namespace CommonServer.Server
  8. {
  9. /// <summary>
  10. /// 描述一个链接
  11. /// </summary>
  12. public interface ISession : AbstractSession
  13. {
  14. /// <summary>
  15. /// Session ID
  16. /// </summary>
  17. string ID { get; }
  18. bool IsConnected { get; }
  19. /// <summary>
  20. /// 总共发送字节数
  21. /// </summary>
  22. long TotalSentBytes { get; }
  23. /// <summary>
  24. /// 总共接收字节数
  25. /// </summary>
  26. long TotalRecvBytes { get; }
  27. ISessionListener Listener { get; }
  28. /// <summary>
  29. /// 关闭此链接
  30. /// </summary>
  31. /// <param name="force"></param>
  32. /// <returns></returns>
  33. bool Disconnect(bool force);
  34. /// <summary>
  35. /// 发送消息【通知】
  36. /// </summary>
  37. /// <param name="message"></param>
  38. /// <returns></returns>
  39. bool Send(IMessage message);
  40. /// <summary>
  41. /// 发送消息【回馈】
  42. /// </summary>
  43. /// <param name="request"></param>
  44. /// <param name="response"></param>
  45. /// <returns></returns>
  46. bool SendResponse(IMessage request, IMessage response);
  47. /// <summary>
  48. /// 获取远端地址
  49. /// </summary>
  50. /// <returns></returns>
  51. string GetRemoteAddress();
  52. }
  53. /// <summary>
  54. /// 服务端监听器
  55. /// </summary>
  56. public interface ISessionListener
  57. {
  58. /// <summary>
  59. /// Session建立回调
  60. /// </summary>
  61. /// <param name="session"></param>
  62. void OnConnected(ISession session);
  63. /// <summary>
  64. /// Session关闭回调
  65. /// </summary>
  66. /// <param name="session"></param>
  67. /// <param name="force"></param>
  68. /// <param name="reason"></param>
  69. void OnDisconnected(ISession session, bool force, String reason);
  70. /// <summary>
  71. /// 错误【编解码或者网络底层】
  72. /// </summary>
  73. /// <param name="session"></param>
  74. /// <param name="err"></param>
  75. void OnError(ISession session, Exception err);
  76. /// <summary>
  77. /// 消息发送成功
  78. /// </summary>
  79. /// <param name="session"></param>
  80. /// <param name="message"></param>
  81. void OnSentMessage(ISession session, object message);
  82. /// <summary>
  83. /// 消息接收到
  84. /// </summary>
  85. /// <param name="session"></param>
  86. /// <param name="message"></param>
  87. void OnReceivedMessage(ISession session, object message);
  88. }
  89. }