RobotViewModel.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System;
  2. using System.ComponentModel.Composition;
  3. using System.Windows.Threading;
  4. using ENet;
  5. using Microsoft.Practices.Prism.ViewModel;
  6. using Robot;
  7. namespace Modules.Robot
  8. {
  9. [Export(contractType: typeof (RobotViewModel)), PartCreationPolicy(creationPolicy: CreationPolicy.NonShared)]
  10. internal sealed class RobotViewModel: NotificationObject, IDisposable
  11. {
  12. private readonly ClientHost clientHost;
  13. private string loginIp;
  14. private ushort loginPort;
  15. private readonly DispatcherTimer timer = new DispatcherTimer(DispatcherPriority.Normal)
  16. { Interval = new TimeSpan(0, 0, 0, 0, 50) };
  17. public string LoginIp
  18. {
  19. get
  20. {
  21. return this.loginIp;
  22. }
  23. set
  24. {
  25. if (this.loginIp == value)
  26. {
  27. return;
  28. }
  29. this.loginIp = value;
  30. this.RaisePropertyChanged("LoginIp");
  31. }
  32. }
  33. public ushort LoginPort
  34. {
  35. get
  36. {
  37. return this.loginPort;
  38. }
  39. set
  40. {
  41. if (this.loginPort == value)
  42. {
  43. return;
  44. }
  45. this.loginPort = value;
  46. this.RaisePropertyChanged("LoginPort");
  47. }
  48. }
  49. public RobotViewModel()
  50. {
  51. this.clientHost = new ClientHost();
  52. this.timer.Tick += delegate { this.clientHost.RunOnce(); };
  53. this.timer.Start();
  54. }
  55. ~RobotViewModel()
  56. {
  57. this.Disposing(false);
  58. }
  59. public void Dispose()
  60. {
  61. this.Disposing(true);
  62. GC.SuppressFinalize(this);
  63. }
  64. private void Disposing(bool disposing)
  65. {
  66. this.clientHost.Dispose();
  67. }
  68. public void Login(string account, string password)
  69. {
  70. //try
  71. //{
  72. // var address = new Address { HostName = this.LoginIp, Port = this.LoginPort };
  73. // Peer peer = await this.clientHost.ConnectAsync(address);
  74. // using (Packet packet = await peer.ReceiveAsync())
  75. // {
  76. // var bytes = packet.Bytes;
  77. // var packetStream = new MemoryStream(bytes, 4, bytes.Length - 4);
  78. // var smsg = Serializer.Deserialize<SMSG_Auth_Challenge>(packetStream);
  79. // Logger.Debug(string.Format("opcode: {0}\n{1}",
  80. // BitConverter.ToUInt16(bytes, 0), XmlHelper.XmlSerialize(smsg)));
  81. // await peer.DisconnectLaterAsync();
  82. // }
  83. //}
  84. //catch (Exception e)
  85. //{
  86. // Logger.Debug(e.Message);
  87. //}
  88. var session = new RealmSession("192.168.11.95", 8888);
  89. session.Login(account, password);
  90. }
  91. }
  92. }