IAsyncTcpClient.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using System.Net;
  3. using System.Text;
  4. using System.Threading.Tasks;
  5. using Team.Communicate.Data;
  6. using Team.Communicate.EventArg;
  7. namespace Team.Communicate.Interfaces
  8. {
  9. public interface IAsyncTcpClient : IDisposable
  10. {
  11. bool IsAutoConnect { get; set; }
  12. Encoding Encoding { get; set; }
  13. IPAddress RemoteIpAddress { get; set; }
  14. int Port { get; }
  15. bool IsConnected { get; }
  16. /// <summary>
  17. /// 连接成功为true
  18. /// </summary>
  19. /// 1.exception ArgumentOutOfRangeException
  20. /// 2.exception ArgumentNullException
  21. /// <returns></returns>
  22. bool Connect();
  23. /// <summary>
  24. /// 异步连接成功为true
  25. /// </summary>
  26. /// 1.exception ArgumentOutOfRangeException
  27. /// 2.exception ArgumentNullException
  28. /// <returns></returns>
  29. Task<bool> ConnectAsync();
  30. /// <summary>
  31. /// 异步发送
  32. ///1.Exception 未连接异常
  33. /// </summary>
  34. /// <param name="msg"></param>
  35. TransmissionResult SendString(string msg);
  36. /// <summary>
  37. /// 异步发送
  38. ///1.Exception 未连接异常
  39. /// </summary>
  40. /// <param name="msg"></param>
  41. Task<TransmissionResult> SendStringAsync(string msg);
  42. /// <summary>
  43. /// 发送并接收一个消息
  44. /// </summary>
  45. /// <param name="msg"></param>
  46. /// <returns></returns>
  47. string SendAndReceiveString(string msg);
  48. /// <summary>
  49. /// 异步发送并接收一个消息
  50. /// </summary>
  51. /// <param name="msg"></param>
  52. /// <returns></returns>
  53. Task<string> SendAndReceiveStringAsync(string msg);
  54. /// <summary>
  55. /// 设定RemoteIp和端口,如果已经连接并且端口和Ip发送变化将断开连接
  56. /// </summary>
  57. /// <param name="ip">ip地址</param>
  58. /// <param name="port">端口</param>
  59. /// <exception cref="ArgumentNullException"></exception>
  60. /// <exception cref="FormatException"></exception>
  61. void SetRemoteIpOrPort(string ip, int port);
  62. /// <summary>
  63. /// 异步发送
  64. /// </summary>
  65. /// <param name="data"></param>
  66. /// <returns></returns>
  67. TransmissionResult SendBytes(byte[] data);
  68. /// <summary>
  69. /// 异步发送
  70. /// </summary>
  71. /// <param name="data"></param>
  72. /// <returns></returns>
  73. Task<TransmissionResult> SendBytesAsync(byte[] data);
  74. /// <summary>
  75. /// 异步接收
  76. /// </summary>
  77. /// <returns></returns>
  78. Task<string> ReceiveStringAsync();
  79. /// <summary>
  80. /// 异步接收
  81. /// </summary>
  82. /// <returns></returns>
  83. string ReceiveString();
  84. /// <summary>
  85. /// 异步接收字节
  86. /// </summary>
  87. /// <returns></returns>
  88. Task<byte[]> ReceiveBytesAsync();
  89. /// <summary>
  90. /// 接收字节
  91. /// </summary>
  92. /// <returns></returns>
  93. byte[] ReceiveBytes();
  94. void DisConnect();
  95. /// <summary>
  96. /// tcp状态改变事件
  97. /// </summary>
  98. event EventHandler<TcpClientStatusEventArgs> StatusChanged;
  99. /// <summary>
  100. /// tcp获取数据事件
  101. /// </summary>
  102. event EventHandler<ReceivedEventArgs> Received;
  103. event EventHandler<SentEventArgs> Sent;
  104. void Reconnect();
  105. bool Connecting { get; }
  106. }
  107. }