ITcpServer.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Team.Communicate.EventArg;
  7. using Team.Communicate.State;
  8. namespace Team.Communicate.Server
  9. {
  10. public interface ITeamTcpServer : IDisposable
  11. {
  12. /// <summary>
  13. /// 服务器是否正在运行
  14. /// </summary>
  15. bool IsRunning { get; }
  16. /// <summary>
  17. /// 监听的IP地址
  18. /// </summary>
  19. IPAddress Address { get; }
  20. /// <summary>
  21. /// 监听的端口
  22. /// </summary>
  23. int Port { get; }
  24. /// <summary>
  25. /// 通信使用的编码
  26. /// </summary>
  27. Encoding Encoding { get; set; }
  28. /// <summary>
  29. /// 启动服务器
  30. /// </summary>
  31. void Start();
  32. /// <summary>
  33. /// 启动服务器
  34. /// </summary>
  35. /// <param name="backlog">
  36. /// 服务器所允许的挂起连接序列的最大长度
  37. /// </param>
  38. void Start(int backlog);
  39. /// <summary>
  40. /// 停止服务器
  41. /// </summary>
  42. void Stop();
  43. /// <summary>
  44. /// 发送数据
  45. /// </summary>
  46. /// <param name="state">接收数据的客户端会话</param>
  47. /// <param name="data">数据报文</param>
  48. void Send(TcpClientState state, byte[] data);
  49. /// <summary>
  50. /// 发送数据
  51. /// </summary>
  52. /// <param name="state">接收数据的客户端会话</param>
  53. /// <param name="data">数据报文</param>
  54. Task SendAsync(TcpClientState state, byte[] data);
  55. /// <summary>
  56. /// 指定IP地址发送数据
  57. /// </summary>
  58. /// <param name="ip"></param>
  59. /// <param name="data"></param>
  60. void Send(string ip, byte[] data);
  61. /// <summary>
  62. /// 指定IP地址发送数据
  63. /// </summary>
  64. /// <param name="ip"></param>
  65. /// <param name="data"></param>
  66. Task SendAsync(string ip, byte[] data);
  67. /// <summary>
  68. /// 异步发送数据至指定的客户端
  69. /// </summary>
  70. /// <param name="client">客户端</param>
  71. /// <param name="data">报文</param>
  72. void Send(TcpClient client, byte[] data);
  73. /// <summary>
  74. /// 异步发送数据至指定的客户端
  75. /// </summary>
  76. /// <param name="client">客户端</param>
  77. /// <param name="data">报文</param>
  78. Task SendAsync(TcpClient client, byte[] data);
  79. void BroadToClient(string msg);
  80. Task BroadToClientAsync(string msg);
  81. /// <summary>
  82. /// 与客户端的连接已建立事件
  83. /// </summary>
  84. event EventHandler<ServerEventArgs> ClientConnected;
  85. /// <summary>
  86. /// 与客户端的连接已断开事件
  87. /// </summary>
  88. event EventHandler<ServerEventArgs> ClientDisconnected;
  89. /// <summary>
  90. /// 接收到数据事件
  91. /// </summary>
  92. event EventHandler<ServerEventArgs> DataReceived;
  93. /// <summary>
  94. /// 发送数据前的事件
  95. /// </summary>
  96. event EventHandler<ServerEventArgs> PrepareSend;
  97. /// <summary>
  98. /// 数据发送完毕事件
  99. /// </summary>
  100. event EventHandler<ServerEventArgs> CompletedSend;
  101. /// <summary>
  102. /// 网络错误事件
  103. /// </summary>
  104. event EventHandler<ServerEventArgs> NetError;
  105. /// <summary>
  106. /// 异常事件
  107. /// </summary>
  108. event EventHandler<ServerEventArgs> OtherException;
  109. /// <summary>
  110. /// 关闭一个与客户端之间的会话
  111. /// </summary>
  112. /// <param name="state">需要关闭的客户端会话对象</param>
  113. void Close(TcpClientState state);
  114. /// <summary>
  115. /// 关闭所有的客户端会话,与所有的客户端连接会断开
  116. /// </summary>
  117. void CloseAllClient();
  118. /// <summary>
  119. /// Performs application-defined tasks associated with freeing,
  120. /// releasing, or resetting unmanaged resources.
  121. /// </summary>
  122. }
  123. }