RobotViewModel.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System;
  2. using System.ComponentModel.Composition;
  3. using System.Windows.Threading;
  4. using ELog;
  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 Host host;
  14. private string logText = "";
  15. private IEventAggregator eventAggregator = new EventAggregator();
  16. private readonly DispatcherTimer timer = new DispatcherTimer(DispatcherPriority.Normal)
  17. {
  18. Interval = new TimeSpan(0, 0, 0, 0, 50)
  19. };
  20. public string LogText
  21. {
  22. get
  23. {
  24. return this.logText;
  25. }
  26. set
  27. {
  28. if (this.logText == value)
  29. {
  30. return;
  31. }
  32. this.logText = value;
  33. this.RaisePropertyChanged("LogText");
  34. }
  35. }
  36. public RobotViewModel()
  37. {
  38. Library.Initialize();
  39. host = new Host(8888, Native.ENET_PROTOCOL_MAXIMUM_PEER_ID);
  40. timer.Tick += delegate { this.host.Run(); };
  41. timer.Start();
  42. }
  43. public async void StartClient()
  44. {
  45. try
  46. {
  47. Peer peer = await host.ConnectAsync(new Address { Host = "192.168.10.246", Port = 8901 }, 2, 0);
  48. Log.Debug("peer data: " + peer.Data);
  49. }
  50. catch (ENetException e)
  51. {
  52. Log.Debug(e.Message);
  53. }
  54. }
  55. public void Start()
  56. {
  57. for (int i = 0; i < 4095; ++i)
  58. {
  59. StartClient();
  60. }
  61. }
  62. }
  63. }