RobotViewModel.cs 1.4 KB

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