RobotViewModel.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using System.ComponentModel.Composition;
  3. using System.Windows.Threading;
  4. using Log;
  5. using Microsoft.Practices.Prism.Events;
  6. using Microsoft.Practices.Prism.ViewModel;
  7. using ENet;
  8. namespace Modules.Robot
  9. {
  10. [Export(contractType: typeof (RobotViewModel)), PartCreationPolicy(creationPolicy: CreationPolicy.NonShared)]
  11. internal class RobotViewModel : NotificationObject
  12. {
  13. private readonly Host host;
  14. private string logText = "";
  15. private readonly DispatcherTimer timer = new DispatcherTimer(DispatcherPriority.Normal)
  16. {
  17. Interval = new TimeSpan(0, 0, 0, 0, 50)
  18. };
  19. public string LogText
  20. {
  21. get
  22. {
  23. return this.logText;
  24. }
  25. set
  26. {
  27. if (this.logText == value)
  28. {
  29. return;
  30. }
  31. this.logText = value;
  32. this.RaisePropertyChanged("LogText");
  33. }
  34. }
  35. public RobotViewModel()
  36. {
  37. Library.Initialize();
  38. host = new Host();
  39. timer.Tick += delegate { this.host.Run(); };
  40. timer.Start();
  41. }
  42. public async void StartClient()
  43. {
  44. try
  45. {
  46. Peer peer = await host.ConnectAsync(
  47. new Address { Host = "192.168.10.246", Port = 8901 });
  48. peer.Send(1, "aaaaaaaaaaa");
  49. Packet packet = await peer.ReceiveAsync();
  50. }
  51. catch (ENetException e)
  52. {
  53. Logger.Debug(e.Message);
  54. }
  55. }
  56. public void Start()
  57. {
  58. for (int i = 0; i < 4095; ++i)
  59. {
  60. StartClient();
  61. }
  62. }
  63. }
  64. }