BaseCommunication.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*----------------------------------------------------------------
  2. // Copyright (C) 2007 liu523@QQ.COM
  3. // 版权所有。
  4. // 开发者:liu523@QQ.COM团队
  5. // 文件名:BaseCommunication.cs
  6. // 文件功能描述:基本通讯类,本身不能直接当作网络通讯使用,被BaseServer和BaseControler继承。
  7. //----------------------------------------------------------------*/
  8. using System;
  9. using System.Text;
  10. using System.Net.Sockets;
  11. using System.IO;
  12. using System.Runtime.Serialization.Formatters.Binary;
  13. using System.Runtime.Serialization;
  14. using System.Windows.Forms;
  15. using RemoteControlLib.Common;
  16. using RemoteControlLib.Codes;
  17. namespace RemoteControlLib.Bases
  18. {
  19. /// <summary>
  20. /// 执行指令的委托
  21. /// </summary>
  22. /// <param name="code">指令</param>
  23. public delegate void ExecuteCodeEvent(BaseCommunication sender, Code code);
  24. /// <summary>
  25. /// 基本通讯类
  26. /// </summary>
  27. public class BaseCommunication
  28. {
  29. /// <summary>
  30. /// 序列化网络流读者
  31. /// </summary>
  32. private BinaryFormatter formatterReader;
  33. /// <summary>
  34. /// 序列化网络流写者
  35. /// </summary>
  36. private BinaryFormatter formatterWriter;
  37. /// <summary>
  38. /// 网络流
  39. /// </summary>
  40. protected NetworkStream stream;
  41. /// <summary>
  42. /// 端口
  43. /// </summary>
  44. protected int port;
  45. /// <summary>
  46. ///获取等待重试时间
  47. /// </summary>
  48. protected virtual int sleepTime
  49. {
  50. get { return Constant.SleepTime; }//默认值
  51. }
  52. /// <summary>
  53. ///是否断开连接
  54. /// </summary>
  55. public bool Disconnected;
  56. /// <summary>
  57. /// 是否退出(不再连接)
  58. /// </summary>
  59. public bool Exit;
  60. /// <summary>
  61. /// 委托(执行指令)
  62. /// </summary>
  63. public ExecuteCodeEvent Execute;
  64. /// <summary>
  65. /// 客户端基类构造函数
  66. /// </summary>
  67. public BaseCommunication()
  68. {
  69. Disconnected =true;
  70. Exit = false;
  71. formatterReader = new BinaryFormatter();
  72. formatterWriter = new BinaryFormatter();
  73. }
  74. /// <summary>
  75. /// 传输信息
  76. /// </summary>
  77. protected void communication()
  78. {
  79. try
  80. {
  81. Disconnected = false;
  82. do
  83. {
  84. //读取网络流
  85. Code code = (Code)formatterReader.Deserialize(stream);
  86. if (code != null)
  87. {
  88. if (code.Head == CodeHead.CONNECT_CLOSE)
  89. Disconnected = true;
  90. else if (code.Head == CodeHead.EXIT)
  91. {
  92. Disconnected = true;
  93. Exit = true;
  94. }
  95. else
  96. Execute(this, code);
  97. }
  98. } while (!Disconnected);
  99. }
  100. catch// (Exception e)
  101. {
  102. // MessageBox.Show(e.ToString());
  103. }
  104. Disconnected = true;
  105. }
  106. /// <summary>
  107. /// 告诉对方连接成功
  108. /// </summary>
  109. protected void SayHello()
  110. {
  111. try
  112. {
  113. Disconnected = false;
  114. BaseCode ConnectOK = new BaseCode();
  115. ConnectOK.Head = CodeHead.CONNECT_OK;
  116. SendCode(ConnectOK);
  117. }
  118. catch
  119. {
  120. Disconnected = true;
  121. }
  122. }
  123. /// <summary>
  124. /// 关闭所有连接
  125. /// </summary>
  126. /// <returns></returns>
  127. public void CloseStream()
  128. {
  129. try
  130. {
  131. Disconnected = true;
  132. if (stream != null)
  133. stream.Close();
  134. }
  135. catch
  136. {
  137. }
  138. }
  139. /// <summary>
  140. /// 发送指令
  141. /// </summary>
  142. /// <param name="code">指令</param>
  143. public void SendCode(Code code)
  144. {
  145. try
  146. {
  147. formatterWriter.Serialize(stream, code);
  148. }
  149. catch// (Exception exp)
  150. {
  151. // MessageBox.Show("Error:" + exp.ToString());
  152. }
  153. }
  154. }
  155. }