NetTool.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using IFramework.Net.Tcp;
  2. using IFramework.Net.Udp;
  3. using IFramework.Net.Http;
  4. using IFramework.Net.WebSocket;
  5. using System.Net;
  6. using System.Linq;
  7. using System.Net.Sockets;
  8. namespace IFramework.Net
  9. {
  10. public static class NetTool
  11. {
  12. public static INetPacketProvider CreatePacketsProvider(int capacity=4096)
  13. {
  14. return NetPacketProvider.CreateProvider(capacity);
  15. }
  16. public static INetProtocolProvider CreateProtocolProvider()
  17. {
  18. return NetProtocolProvider.CreateProvider();
  19. }
  20. public static INetTokenPoolProvider CreateTokenPoolProvider(int taskExecutePeriod=60)
  21. {
  22. return NetTokenPoolProvider.CreateProvider(taskExecutePeriod);
  23. }
  24. public static ITcpClientProvider CreateTcpClient(int chunkBufferSize = 4096, int sendConcurrentSize = 8)
  25. {
  26. return new TcpClientProvider(chunkBufferSize, sendConcurrentSize);
  27. }
  28. public static ITcpServerProvider CreateTcpSever(int chunkBufferSize = 4096, int maxNumberOfConnections = 32)
  29. {
  30. return new TcpServerProvider(chunkBufferSize, maxNumberOfConnections);
  31. }
  32. public static IUdpClientProvider CreateUdpClient(int chunkBufferSize = 4096, int sendConcurrentSize = 8)
  33. {
  34. return new UdpClientProvider(chunkBufferSize, sendConcurrentSize);
  35. }
  36. public static IUdpServerProvider CreateUdpSever(int chunkBufferSize = 4096, int maxNumberOfConnections = 32,bool broadcast=false)
  37. {
  38. return new UdpServerProvider(chunkBufferSize, maxNumberOfConnections,broadcast);
  39. }
  40. public static IWSClientProvider CreateWSClient(int chunkBufferSize = 4096, int sendConcurrentSize = 8)
  41. {
  42. return new WSClientProvider(chunkBufferSize, sendConcurrentSize);
  43. }
  44. public static IWSServerProvider CreateWSSever(int chunkBufferSize = 4096, int maxNumberOfConnections = 32)
  45. {
  46. return new WSServerProvider(maxNumberOfConnections, chunkBufferSize);
  47. }
  48. public static IHttpServerProvider CreateHttpSever(int maxPoolCount = 64, int blockSize = 4096)
  49. {
  50. return new HttpServer( maxPoolCount ,blockSize);
  51. }
  52. public static IPAddress[] GetLoacalIpv4()
  53. {
  54. IPAddress[] addresses = Dns.GetHostAddresses("localhost");
  55. return (from x in addresses where x.AddressFamily == AddressFamily.InterNetwork select x).ToArray();
  56. }
  57. public static IPAddress[] GetLoacalIpv6()
  58. {
  59. IPAddress[] addresses = Dns.GetHostAddresses("localhost");
  60. return (from x in addresses where x.AddressFamily == AddressFamily.InterNetworkV6 select x).ToArray();
  61. }
  62. public static string GetOutSideIP()
  63. {
  64. using (WebClient wc = new WebClient())
  65. {
  66. return wc.DownloadString(@"http://icanhazip.com/").Replace("\n", "");
  67. }
  68. }
  69. }
  70. }