/// <summary> /// 连接服务器 /// </summary> private static async void ConnectAsync() { IRemote remote = new TcpRemote(); var ex = await remote.ConnectAsync(new IPEndPoint(IPAddress.IPv6Loopback, 54321)); if (ex == null) { ///没有异常,连接成功 Console.WriteLine("连接成功"); ///创建一个登陆消息 var login = new Login2Gate { Account = $"TestClient", Password = "******" }; ///有返回值,这个是一个RPC过程,Exception在网络中传递 var resp = await remote.SendAsyncSafeAwait <Login2GateResult>(login); if (resp.IsSuccess) { Console.WriteLine("登陆成功"); } ///没有返回值,不是RPC过程 } else { ///连接失败 Console.WriteLine(ex.ToString()); } }
public async Task TestAsync4() { TcpRemote remote = new TcpRemote(); var res = await remote.SendAsync <TestPacket1>(null); res.ToString(); await Task.Delay(10); res.ToString(); }
public async void TestAsync2() { TcpRemote remote = new TcpRemote(); var res = await remote.SendAsyncSafeAwait <TestPacket1>(null); res.ToString(); await Task.Delay(10); res.ToString(); }
public void TestLazyTcpSend() { const int Port = 54324; CancellationTokenSource cancellation = new CancellationTokenSource(); PrepareEnvironment(cancellation); StartTcpListen(Port, cancellation); TcpRemote remote = new TcpRemote(); remote.RpcCallbackPool.RpcTimeOutMilliseconds = 2000; remote.ConnectAsync(new IPEndPoint(IPAddress.Loopback, Port)).Wait(); TestLazySendAsync(remote).Wait(); cancellation.Cancel(); }
private static async void Connect(int index) { IRemote remote = new TcpRemote(); var res = await remote.ConnectAsync(new IPEndPoint(IPAddress.Loopback, 54321)); if (res == null) { Console.WriteLine($"Remote{index}:Success"); } else { Console.WriteLine($"Remote:{res}"); } //remote.SendAsync(new Packet1()); }
private static async void NewRemote(int clientIndex) { IRemote remote = new TcpRemote() { }; remote.OnReceiveCallback += receiver.TestReceive; var res = await remote.ConnectAsync(new IPEndPoint(IPAddress.Loopback, 54321)); if (res == null) { Console.WriteLine($"Remote{clientIndex}:Success"); } else { throw res; } await TestRpc(clientIndex, remote); Stopwatch look1 = new Stopwatch(); var msg = new TestPacket1 { Value = 0 }; look1.Start(); await Task.Run(() => { for (int i = 0; i < MessageCount; i++) { //Console.WriteLine($"Remote{clientIndex}:发送{nameof(Packet1)}=={i}"); msg.Value = i; remote.SendAsync(msg); } }); look1.Stop(); Console.WriteLine($"Remote{clientIndex}: SendAsync{MessageCount}包 ------ 发送总时间: {look1.ElapsedMilliseconds}----- 平均每秒发送:{MessageCount * 1000 / (look1.ElapsedMilliseconds + 1)}"); //Remote.BroadCastAsync(new Packet1 { Value = -99999 },remote); //var (Result, Excption) = await remote.SendAsync<Packet2>(new Packet1 { Value = 100 }); //Console.WriteLine($"RPC接收消息{nameof(Packet2)}--{Result.Value}"); }
private void Awake() { _remote = GameObject.Find("RaiSimUnity").GetComponent <TcpRemote>(); _camera = GameObject.Find("Main Camera").GetComponent <CameraController>(); if (_remote == null) { // TODO exception } if (_camera == null) { // TODO exception } // modal view { var modal = GameObject.Find(_ErrorModalViewName).GetComponent <Canvas>(); var okButton = modal.GetComponentInChildren <Button>(); okButton.onClick.AddListener(() => { modal.enabled = false; }); } // visualize section { var toggleVisual = GameObject.Find(_ToggleVisualBodiesName).GetComponent <Toggle>(); toggleVisual.onValueChanged.AddListener((isSelected) => { _remote.ShowVisualBody = isSelected; _remote.ShowOrHideObjects(); }); var toggleCollision = GameObject.Find(_ToggleCollisionBodiesName).GetComponent <Toggle>(); toggleCollision.onValueChanged.AddListener((isSelected) => { _remote.ShowCollisionBody = isSelected; _remote.ShowOrHideObjects(); }); var toggleContactPoints = GameObject.Find(_ToggleContactPointsName).GetComponent <Toggle>(); toggleContactPoints.onValueChanged.AddListener((isSelected) => { _remote.ShowContactPoints = isSelected; _remote.ShowOrHideObjects(); }); var toggleContactForces = GameObject.Find(_ToggleContactForcesName).GetComponent <Toggle>(); toggleContactForces.onValueChanged.AddListener((isSelected) => { _remote.ShowContactForces = isSelected; _remote.ShowOrHideObjects(); }); } // connection section { var ipInputField = GameObject.Find(_InputFieldTcpName).GetComponent <InputField>(); ipInputField.text = _remote.TcpAddress; var portInputField = GameObject.Find(_InputFieldPortName).GetComponent <InputField>(); portInputField.text = _remote.TcpPort.ToString(); var connectButton = GameObject.Find(_ButtonConnectName).GetComponent <Button>(); connectButton.onClick.AddListener(() => { _remote.TcpAddress = ipInputField.text; _remote.TcpPort = Int32.Parse(portInputField.text); // connect / disconnect if (!_remote.TcpConnected) { try { _remote.EstablishConnection(); } catch (Exception e) { var modal = GameObject.Find(_ErrorModalViewName).GetComponent <ErrorViewController>(); modal.Show(true); modal.SetMessage(e.Message); } } else { try { _remote.CloseConnection(); } catch (Exception) { } } }); } // recording section { var screenshotButton = GameObject.Find(_ButtonScreenshotName).GetComponent <Button>(); screenshotButton.onClick.AddListener(() => { string dirName = Path.Combine(Application.dataPath, "../Screenshot"); if (!File.Exists(dirName)) { Directory.CreateDirectory(dirName); } var filename = Path.Combine( dirName, "Screenshot " + DateTime.Now.ToString("yyyy-MM-d hh-mm-ss") + ".png"); ScreenCapture.CaptureScreenshot(filename); }); var recordButton = GameObject.Find(_ButtonRecordName).GetComponent <Button>(); if (!_camera.videoAvailable) { recordButton.interactable = false; } else { recordButton.interactable = true; } recordButton.onClick.AddListener(() => { if (_camera.IsRecording) { _camera.FinishRecording(); } else { _camera.StartRecording(); } }); } // background section { _daySky = Resources.Load <Material>("backgrounds/Wispy Sky/Materials/WispySkyboxMat2"); _sunriseSky = Resources.Load <Material>("backgrounds/Wispy Sky/Materials/WispySkyboxMat"); _sunsetSky = Resources.Load <Material>("backgrounds/Skybox/Materials/Skybox_Sunset"); _nightSky = Resources.Load <Material>("backgrounds/FreeNightSky/Materials/nightsky1"); _milkywaySky = Resources.Load <Material>("backgrounds/MilkyWay/Material/MilkyWay"); var backgroundDropdown = GameObject.Find(_DropdownBackgroundName).GetComponent <Dropdown>(); backgroundDropdown.onValueChanged.AddListener(delegate { ChangeBackground(backgroundDropdown); DynamicGI.UpdateEnvironment(); }); } // resource section { _remote.ResourceLoader.LoadFromPref(); RefereshScrollResources(); var addButton = GameObject.Find(_ButtonAddResourceName).GetComponent <Button>(); addButton.onClick.AddListener(() => { SimpleFileBrowser.FileBrowser.ShowLoadDialog((path) => { _remote.ResourceLoader.AddResourceDirectory(path); RefereshScrollResources(); }, null, true); }); var removeButton = GameObject.Find(_ButtonDeleteResourceName).GetComponent <Button>(); removeButton.onClick.AddListener(() => { _remote.ResourceLoader.RemoveResourceDirectory(); RefereshScrollResources(); }); } }