// See https://aka.ms/new-console-template for more information using System.Diagnostics; using System.Globalization; using System.Text; using BITKit; using BITKit.Mod; using BITKit.Net; using MemoryPack; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Net.BITKit.Teleport; await BITAppForNet.InitializeAsync("Net.BITKit.Teleport"); var isClient = Environment.GetCommandLineArgs().Contains("client", StringComparer.CurrentCultureIgnoreCase); var serviceCollection = new ServiceCollection(); serviceCollection.AddLogging(x => { x.AddConsole(); Console.OutputEncoding=Encoding.UTF8; }); serviceCollection.AddTransient(); serviceCollection.AddSingleton(); serviceCollection.AddSingleton(); if (isClient) { serviceCollection.AddSingleton(x => x.GetRequiredService()); serviceCollection.AddRemoteInterface(); } else { serviceCollection.AddSingleton(x => x.GetRequiredService()); serviceCollection.AddSingleton(); } var serviceProvider = serviceCollection.BuildServiceProvider(); var logger = serviceProvider.GetRequiredService>(); if (isClient) { var client = serviceProvider.GetRequiredService(); Tick(client); if (await client.Connect() is false) { throw new Exception("Failed to connect to server."); } var rpc = serviceProvider.GetRequiredService(); for (int i = 1; i <= 3; i++) { //rpc.Func(i.ToString()); logger.LogInformation("Rpc:\t" + await rpc.FuncAsync(i.ToString())); } logger.LogInformation("客户端链接完成,按任意键开始Rpc测试"); var isPressed=false; Console.CancelKeyPress += (sender, args) => { isPressed = true; args.Cancel = true; // Prevent the process from terminating }; while (true) { if (isPressed) { using var stopWatcher = new StopWatcher(logger,"Rpc测试"); logger.LogInformation("Rpc:\t" + await rpc.FuncAsync(DateTime.Now.ToString(CultureInfo.InvariantCulture))); isPressed = false; } await Task.Delay(100); } } else { var server = serviceProvider.GetRequiredService(); Tick(server); server.StartServer(); var count = 0; while (true) { await Task.Delay(1000); foreach (var id in server.Connections.Keys) { server.SendMessageToClient(id,$"Tick:{++count} from server!"); } } } while (true) { await Task.Delay(300); } async void Tick(INetProvider netProvider) { while (true) { netProvider.Tick(); await Task.Delay(30); } }