RobotViewModel.cs 2.4 KB

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