RobotViewModel.cs 2.4 KB

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