RobotViewModel.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel.Composition;
  5. using System.Threading.Tasks;
  6. using System.Windows.Threading;
  7. using BossClient;
  8. using Helper;
  9. using Log;
  10. using Microsoft.Practices.Prism.ViewModel;
  11. namespace Modules.Robot
  12. {
  13. [Export(contractType: typeof (RobotViewModel)),
  14. PartCreationPolicy(creationPolicy: CreationPolicy.NonShared)]
  15. internal sealed class RobotViewModel: NotificationObject, IDisposable
  16. {
  17. private string loginIP = "192.168.11.95";
  18. private ushort loginPort = 8888;
  19. private string account = "egametang@163.com";
  20. private string password = "163bio1";
  21. private string command = "";
  22. private bool isButtonEnable;
  23. private readonly BossClient.BossClient bossClient = new BossClient.BossClient();
  24. private readonly ObservableCollection<ServerViewModel> serverInfos =
  25. new ObservableCollection<ServerViewModel>();
  26. public readonly Dictionary<ushort, Action<byte[]>> messageHandlers =
  27. new Dictionary<ushort, Action<byte[]>>();
  28. private readonly DispatcherTimer timer = new DispatcherTimer(DispatcherPriority.Normal)
  29. { Interval = new TimeSpan(0, 0, 0, 0, 50) };
  30. public string LoginIP
  31. {
  32. get
  33. {
  34. return this.loginIP;
  35. }
  36. set
  37. {
  38. if (this.loginIP == value)
  39. {
  40. return;
  41. }
  42. this.loginIP = value;
  43. this.RaisePropertyChanged("LoginIP");
  44. }
  45. }
  46. public ushort LoginPort
  47. {
  48. get
  49. {
  50. return this.loginPort;
  51. }
  52. set
  53. {
  54. if (this.loginPort == value)
  55. {
  56. return;
  57. }
  58. this.loginPort = value;
  59. this.RaisePropertyChanged("LoginPort");
  60. }
  61. }
  62. public string Account
  63. {
  64. get
  65. {
  66. return this.account;
  67. }
  68. set
  69. {
  70. if (this.account == value)
  71. {
  72. return;
  73. }
  74. this.account = value;
  75. this.RaisePropertyChanged("Account");
  76. }
  77. }
  78. public string Password
  79. {
  80. get
  81. {
  82. return this.password;
  83. }
  84. set
  85. {
  86. if (this.password == value)
  87. {
  88. return;
  89. }
  90. this.password = value;
  91. this.RaisePropertyChanged("Password");
  92. }
  93. }
  94. public string Command
  95. {
  96. get
  97. {
  98. return this.command;
  99. }
  100. set
  101. {
  102. if (this.command == value)
  103. {
  104. return;
  105. }
  106. this.command = value;
  107. this.RaisePropertyChanged("Command");
  108. }
  109. }
  110. public bool IsButtonEnable
  111. {
  112. get
  113. {
  114. return this.isButtonEnable;
  115. }
  116. set
  117. {
  118. if (this.isButtonEnable == value)
  119. {
  120. return;
  121. }
  122. this.isButtonEnable = value;
  123. this.RaisePropertyChanged("IsButtonEnable");
  124. }
  125. }
  126. public ObservableCollection<ServerViewModel> ServerInfos
  127. {
  128. get
  129. {
  130. return this.serverInfos;
  131. }
  132. }
  133. public RobotViewModel()
  134. {
  135. this.messageHandlers.Add(
  136. MessageOpcode.SMSG_BOSS_SERVERSINFO, Handle_SMSG_Boss_ServersInfo);
  137. this.timer.Tick += delegate { this.bossClient.RunOnce(); };
  138. this.timer.Start();
  139. }
  140. ~RobotViewModel()
  141. {
  142. this.Disposing(false);
  143. }
  144. public void Dispose()
  145. {
  146. this.Disposing(true);
  147. GC.SuppressFinalize(this);
  148. }
  149. private void Disposing(bool disposing)
  150. {
  151. this.bossClient.Dispose();
  152. }
  153. public async Task Login()
  154. {
  155. await this.bossClient.Login(
  156. this.LoginIP, this.LoginPort, this.Account, this.Password);
  157. this.IsButtonEnable = true;
  158. this.HandleMessages();
  159. }
  160. public async void HandleMessages()
  161. {
  162. try
  163. {
  164. while (true)
  165. {
  166. var result = await this.bossClient.GateSession.IMessageChannel.RecvMessage();
  167. ushort opcode = result.Item1;
  168. byte[] message = result.Item2;
  169. if (!messageHandlers.ContainsKey(opcode))
  170. {
  171. Logger.Debug("not found opcode: {0}", opcode);
  172. continue;
  173. }
  174. messageHandlers[opcode](message);
  175. }
  176. }
  177. catch (Exception e)
  178. {
  179. this.IsButtonEnable = false;
  180. Logger.Trace(e.ToString());
  181. }
  182. }
  183. public void SendCommand()
  184. {
  185. this.bossClient.SendCommand(this.Command);
  186. }
  187. public void Servers()
  188. {
  189. this.bossClient.SendCommand("servers");
  190. }
  191. public void Reload()
  192. {
  193. this.bossClient.SendCommand("reload");
  194. }
  195. public void Handle_SMSG_Boss_ServersInfo(byte[] message)
  196. {
  197. var smsgBossServersInfo = ProtobufHelper.FromBytes<SMSG_Boss_ServersInfo>(message);
  198. this.ServerInfos.Clear();
  199. foreach (var name in smsgBossServersInfo.Name)
  200. {
  201. this.ServerInfos.Add(new ServerViewModel {Name = name});
  202. }
  203. }
  204. }
  205. }