RobotViewModel.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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(8888, Native.ENET_PROTOCOL_MAXIMUM_PEER_ID);
  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(new Address { Host = "192.168.10.246", Port = 8901 }, 2);
  47. LogText += string.Format("peer data: {0}\r\n", peer.Data);
  48. }
  49. catch (ENetException e)
  50. {
  51. Logger.Debug(e.Message);
  52. }
  53. }
  54. public void Start()
  55. {
  56. for (int i = 0; i < 4095; ++i)
  57. {
  58. StartClient();
  59. }
  60. }
  61. }
  62. }