Program.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using DotNetty.Transport.Bootstrapping;
  7. using DotNetty.Transport.Channels;
  8. using DotNetty.Buffers;
  9. using System.Threading;
  10. namespace nettyCommunication
  11. {
  12. class Program
  13. {
  14. private static Communication comm = null;
  15. private static Thread receiveMsg = null;
  16. private static bool connected { get; set; }
  17. static void Main(string[] args)
  18. {
  19. //持续进行连接尝试
  20. Task.Factory.StartNew(() =>
  21. {
  22. //初始化后与web持续连接
  23. int count = 0;
  24. while (true)
  25. {
  26. try
  27. {
  28. if (count != 0)
  29. {
  30. Connections.close();
  31. Thread.Sleep(100);
  32. }
  33. //Thread.Sleep(100);
  34. Connections.Connection();
  35. connected = true;
  36. comm = new Communication();
  37. count++;
  38. break;
  39. }
  40. catch (Exception)
  41. {
  42. connected = false;
  43. Console.WriteLine("服务没有开启,请检查服务器");
  44. }
  45. Thread.Sleep(200);
  46. }
  47. //持续判断连接状态并重连
  48. while (true)
  49. {
  50. Console.WriteLine(Connections.isAlive() + ", " + receiveMsg.ThreadState.ToString());
  51. if (Connections.isAlive())
  52. {
  53. if (!connected)
  54. {
  55. comm = new Communication();
  56. }
  57. connected = true;
  58. if (receiveMsg.ThreadState == ThreadState.Aborted)
  59. {
  60. try
  61. {
  62. receiveMsg.Start();
  63. }
  64. catch (Exception ex) { Console.WriteLine(ex.Message); }
  65. }
  66. }
  67. else
  68. {
  69. connected = false;
  70. try
  71. {
  72. if (receiveMsg.ThreadState == ThreadState.WaitSleepJoin)
  73. {
  74. receiveMsg.Interrupt();
  75. }
  76. }
  77. catch (Exception ex)
  78. {
  79. Console.WriteLine("连接断开,终止消息接收线程");
  80. }
  81. Console.WriteLine(" 连接关闭,需要重新连接注册");
  82. try
  83. {
  84. Connections.close();
  85. Thread.Sleep(100);
  86. Connections.Connection();
  87. }
  88. catch (Exception)
  89. {
  90. Console.WriteLine("服务没有开启,请检查服务器");
  91. }
  92. }
  93. Thread.Sleep(10000);
  94. }
  95. });
  96. //持续接收消息
  97. receiveMsg = new Thread(() =>
  98. {
  99. byte[] bytes = new byte[256];
  100. while (true)
  101. {
  102. try
  103. {
  104. if (connected && comm != null)
  105. {
  106. MessageUTF8 msg = ((MessageUTF8)comm.ReceiveMessage(out bytes));
  107. }
  108. }
  109. catch { Console.WriteLine("线程已中断"); }
  110. }
  111. });
  112. receiveMsg.Start();
  113. Console.ReadLine();
  114. }
  115. //public static async Task sendMessage(IChannelHandlerContext ctx,object message)
  116. //{
  117. // AbstractMessage ms = (AbstractMessage)message;
  118. // IByteBuffer initialMessage = Unpooled.Buffer(ClientSettings.Size);
  119. // //byte[] messageByte = Encoding.UTF8.GetBytes(msg);
  120. // byte[] byteMessage = JsonByByteToObjectTools.ObjToJsonByte(ms);
  121. // initialMessage.WriteBytes(byteMessage);
  122. // await ctx.WriteAndFlushAsync(initialMessage);
  123. // ctx.Flush();
  124. //}
  125. }
  126. }