ISocket.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace TcpEventBus
  5. {
  6. public interface ISocket
  7. {
  8. public int ReceiveTimeout { get; }
  9. public Action OnConnected { get; set; }
  10. public Action<ArraySegment<byte>> OnData { get; set; }
  11. public Action OnDisconnected { get; set; }
  12. public bool IsConnect { get; }
  13. public void Disconnect();
  14. public bool IsService { get; }
  15. public void Start();
  16. /// <summary>
  17. /// 发生数据
  18. /// </summary>
  19. /// <param name="data"></param>
  20. public void Send(byte[] data);
  21. public static class SocketTool
  22. {
  23. public static ISocket CreateService(int port, int timeout = 1000)
  24. {
  25. return new Server("0.0.0.0", port)
  26. {
  27. };
  28. }
  29. public static ISocket CreateClient(string ip, int port, int timeout = 1000)
  30. {
  31. return new Client(ip, port)
  32. {
  33. };
  34. }
  35. }
  36. }
  37. }