Server.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using CommonLang.Protocol;
  5. using System.Collections.Specialized;
  6. using CommonLang.Net;
  7. namespace CommonServer.Server
  8. {
  9. /// <summary>
  10. /// 服务器监听器
  11. /// </summary>
  12. public interface IServerListener
  13. {
  14. /// <summary>
  15. /// 服务器初始化回调
  16. /// </summary>
  17. /// <param name="server"></param>
  18. void OnInit(IServer server);
  19. /// <summary>
  20. /// 服务器关闭回调
  21. /// </summary>
  22. void OnDestory();
  23. /// <summary>
  24. /// 一个链接建立成功
  25. /// </summary>
  26. /// <param name="session"></param>
  27. /// <returns></returns>
  28. ISessionListener OnSessionConnected(ISession session);
  29. }
  30. /// <summary>
  31. ///
  32. /// </summary>
  33. public interface IServerFactory
  34. {
  35. IServer CreateServer(INetPackageCodec codec);
  36. }
  37. public interface IServer
  38. {
  39. /// <summary>
  40. /// 客户端连接套接字
  41. /// </summary>
  42. string ClientConnectString { get; }
  43. /// <summary>
  44. /// 获取编解码器
  45. /// </summary>
  46. INetPackageCodec PackageCodec { get; }
  47. /// <summary>
  48. /// 获取当前已连接数
  49. /// </summary>
  50. int SessionCount { get; }
  51. /// <summary>
  52. /// 启动服务器
  53. /// </summary>
  54. /// <param name="port"></param>
  55. /// <param name="listener"></param>
  56. void Open(IDictionary<string, string> config, IServerListener listener);
  57. /// <summary>
  58. /// 关闭服务器
  59. /// </summary>
  60. void Dispose();
  61. /// <summary>
  62. /// 广播消息
  63. /// </summary>
  64. /// <param name="message"></param>
  65. void Broadcast(IMessage message);
  66. /// <summary>
  67. /// 服务器是否有此链接
  68. /// </summary>
  69. /// <param name="session"></param>
  70. /// <returns></returns>
  71. bool HasSession(ISession session);
  72. /// <summary>
  73. /// 根据 Session ID 获取链接
  74. /// </summary>
  75. /// <param name="sessionID"></param>
  76. /// <returns></returns>
  77. ISession GetSessionByID(string sessionID);
  78. /// <summary>
  79. /// 获取所有链接
  80. /// </summary>
  81. /// <returns></returns>
  82. IEnumerable<ISession> GetSessions();
  83. long TotalSentBytes { get; }
  84. long TotalRecvBytes { get; }
  85. void SetEmulateLaggingMS(int min, int max);
  86. void GetEmulateLaggingMS(out int min, out int max);
  87. }
  88. }