static async Task Main(string[] args) { var encoder = new ProxyPackageEncoder(); var decoder = new ProxyPackageDecoder(); var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); var endpoint = new IPEndPoint(IPAddress.Any, 9091); socket.Bind(endpoint); socket.Listen(10); Console.WriteLine($"开始监听:{endpoint}"); var factory = new LoggerFactory(); var logger = factory.CreateLogger(""); while (true) { var nscoket = await socket.AcceptAsync(); Console.WriteLine($"连接成功:{nscoket.RemoteEndPoint}"); var channel = new ProxyChannel <ProxyPackage>(nscoket, encoder, decoder, logger, new ChannelOptions()); channel.PackageReceived += async(channl, package) => { Console.WriteLine($"接收到一个数据包:{package},内容:{Encoding.UTF8.GetString(package.Data)}"); var reply = new ProxyPackage { Data = Encoding.UTF8.GetBytes("你好呀") }; await channel.SendAsync(package); }; await channel.StartAsync(); } }
public ProxyServer(IOptions <ServerOptions> options , ILogger <ProxyServer> logger , ProxyPackageDecoder decoder , ProxyPackageEncoder encoder , IIdGenerator idGenerator , EasyHttpServer httpServer , ConfigHelper configHelper) { this.logger = logger; this.options = options?.Value; Checker.NotNull(this.options); this.decoder = decoder; this.encoder = encoder; this.configHelper = configHelper; this.idGenerator = idGenerator; this.httpServer = httpServer; }
static async Task Main(string[] args) { var encoder = new ProxyPackageEncoder(); var decoder = new ProxyPackageDecoder(); var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); var endpoint = new IPEndPoint(IPAddress.Loopback, 9091); await socket.ConnectAsync(endpoint); var factory = new LoggerFactory(); var logger = factory.CreateLogger(""); var options = new ChannelOptions(); var channel = new ProxyChannel <ProxyPackage>(socket, encoder, decoder, logger, options); channel.PackageReceived += async(channl, package) => { Console.WriteLine($"接收到一个数据包:{package},内容:{Encoding.UTF8.GetString(package.Data)}"); await Task.CompletedTask; }; _ = channel.StartAsync(); var c = 0; while (c < 100000) { var package = new ProxyPackage { ChannelId = 1, ConnectionId = 100, Type = PackageType.Transfer, Data = Encoding.UTF8.GetBytes(c.ToString()) }; await channel.SendAsync(package); c++; } Console.ReadKey(); }